[Beginner] Error: Can't find model file when running Stan example

Hi,

I just downloaded R 4.0.2 and rstan 2.21.2. I followed along with the Getting Started guide. I ran the following code in a .stan file

data {
  int<lower=0> J;         // number of schools 
  real y[J];              // estimated treatment effects
  real<lower=0> sigma[J]; // standard error of effect estimates 
}
parameters {
  real mu;                // population treatment effect
  real<lower=0> tau;      // standard deviation in treatment effects
  vector[J] eta;          // unscaled deviation from mu by school
}
transformed parameters {
  vector[J] theta = mu + tau * eta;        // school treatment effects
}
model {
  target += normal_lpdf(eta | 0, 1);       // prior log-density
  target += normal_lpdf(y | theta, sigma); // log-likelihood
}

and the following code in a .R file

library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)

schools_dat <- list(J = 8, 
                    y = c(28,  8, -3,  7, -1,  1, 18, 12),
                    sigma = c(15, 10, 16, 11,  9, 11, 10, 18))

fit <- stan(file = 'schools.stan', data = schools_dat)

The R console gives the following error after executing the fit line:

Error in file(fname, "rt") : cannot open the connection
In addition: Warning messages:
1: In normalizePath(path.expand(path), winslash, mustWork) :
  path[1]="C:/users/graha/Desktop/schools.stan": The system cannot find the file specified
2: In file(fname, "rt") :
  cannot open file 'C:\users\graha\Desktop\schools.stan': No such file or directory
Error in get_model_strcode(file, model_code) : 
  cannot open model file "C:\users\graha\Desktop\schools.stan"

Both the .stan and .R files are saved in the current working directory (desktop).

Here is my system environment

R version 4.0.2 (2020-06-22)
RStan 2.21.2
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Any help would be much appreciated!

Sorry to doubt you, but could you double-check you have the R session pointing at the right working directory by checking with getwd()?

No worries! With getwd() I see “C:/users/graha/Desktop” where both files are saved. Now I’m getting the following error code when I execute the fit command:

Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  '-E' not found
2: In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/R-40~1.2/bin/x64/R CMD SHLIB file44f8185f33.cpp 2> file44f8185f33.cpp.err.txt' had status 1
3: In file(con, "r") :
  cannot open file 'file44f8185f33.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection

I haven’t been able to find any related posts on this forum, so hopefully this helps

Just updating this in case anyone has any ideas

I did a complete reinstall of R, Rtools, RStudion, and rstan. I’m now getting a new error message when trying to run the schools model:

Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/RBUILD~1/4.0/usr/mingw_/bin/g++' not found
2: In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/bin/x64/R CMD SHLIB file56e044a05c9d.cpp 2> file56e044a05c9d.cpp.err.txt' had status 1
3: In file(con, "r") :
  cannot open file 'file56e044a05c9d.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection

Any help would be much appreciated!