The console on Rstudio didn't find a data file when loading rstan library

When I loaded rstan library with necessary dependencies,
it was given an error as follows.

①fit <- stan(file=scr, model_name=scr, data=data, pars=par, verbose=F,seed=see, chains=cha, warmup=war,iter=ite)
Error in stan(file = scr, model_name = scr, data = data, pars = par, verbose = F, :
‘data’ must be a list, environment, or character vector

②Error in file(filename, “r”, encoding = encoding) :
cannot open the connection
In addition: Warning message:
In file(filename, “r”, encoding = encoding) :
cannot open file ‘data622.R’: No such file or directory

The code shows as follows

library(rstan) 
options(mc.cores = parallel::detectCores())
scr <- "model622.stan" 
source("data622.R") 
data <- list(N=N,x=x) 
par <-c("mu", "Sigma", "rho","delta","delta_over","delta_over2", "rho_over","rho_over05")
war <- 1000 
ite <- 110000 
see <- 1234 
dig <- 3 
cha <- 1 
fit <- stan(file=scr, model_name=scr, data=data, pars=par, verbose=F,seed=see, chains=cha, warmup=war,iter=ite)
print(fit,pars=par,digits_summary=dig) 
plot(fit, pars=par)
traceplot(fit,inc_warmup = T, pars=par)

Mac OS/X 10.13.6
R Version: 3.6.1
Rstan:2.19.2

The above seems to be a problem. You need to give the function the path to the file, if you’re not using the directory where the file is as a working directory. This is not a Stan problem. :)

Thank you very much for answering my question. I tried to run my file on a working directory, but I had the same error. When I tried to confirm path using Sys.getenv(), I got several environment variables including PATH /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin. Please tell me how to read my file on Rstudio by setting the path to the file with Sys.setenv().

Hey!

No, you literally just have to give the path to the file as a character string, like

scr <- "C:/My Files/Stan Models/model622.stan

Or

source("C:/My Files/Data/data622.R")

You can also run

scr <- file.choose() in an interactive session and find the file via the menu.

All these are R specific questions and this is a Stan forum. I don’t mean to sound harsh, but it seems to me like you need to go back to learning the basics of R (you should find plenty of resources online). For R questions you can usually just Google, like “how to source file in R” or something like that.

Cheers!

2 Likes

I also encountered such an error.
The following site is very helpful for me, and I think for you.

Thank for your help.
Of course, I have already investigated the site related to R such as Stack Overflow, and Google. In fact, this sample script, which read in the Bayes’ textbook, was run on this summer, but I haven’t recently run it. When I try to change the code on Mac as you said,I also have errors as follows. I could have a problem to set up on Rstudio or Stan. Could you tell me an explanation for the error?

Changed code:
scr <- "/Users/lina/Downloads/model622.stan"
source("/Users/lina/Downloads/data622.R")

Error:
in stan_model(file, model_name = model_name, model_code = model_code, :
model name must match (^[[:alnum:]]{2,}.*)|(^[A-E,G-S,U-Z,a-z].*)|(^[F,T].+).
In addition: Warning message:
In readLines(file, warn = TRUE) :
incomplete final line found on ‘/Users/lina/Downloads/model622.stan’.

Thank for your advise. Of course, I am going to use Stack Overflow from now, but I think the Stan discourse site is more active in discussion than Stack Overflow.