Have trouble using RStan

I had been able to use RStan normally until I upgraded the R version from 4.0.4 to 4.1.1.

The example code example(stan_model, package = "rstan", run.dontrun = TRUE) worked; however, the following code didn’t work:

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)

It gave me the following error message:

Error in dyn.load(libLFile) : 
  unable to load shared object '/var/folders/g1/hm0813d126767lp1cc4h6vxc0000gp/T//RtmpGRxjig/file9974158d9550.so':
  dlopen(/var/folders/g1/hm0813d126767lp1cc4h6vxc0000gp/T//RtmpGRxjig/file9974158d9550.so, 6): Library not loaded: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libR.dylib
  Referenced from: /private/var/folders/g1/hm0813d126767lp1cc4h6vxc0000gp/T/RtmpGRxjig/file9974158d9550.so
  Reason: image not found

Looks like somehow stan is still looking for something in the version 4.0 library.
The result for .libPaths() seems correct:


I also tried the following:

  • uninstalling all versions of R on my mac through terminal using
    csudo rm -rf /Library/Frameworks/R.framework
    sudo rm -rf /Library/Frameworks/R.framework

  • uninstalling RStudio

  • removing packages “rstan”, “StanHeaders”, “Rcpp”, “rstantools” using commands similar to remove.packages("rstan")

  • reinstalling R 4.1.1 and Rstudio

  • reinstall the packages from source (e.g. install.packages(c("StanHeaders","rstan"),type="source"))

but none of them worked.

When using other packages such as ggplot2, everything was normal, no such error was prompted.

Operating System: MacOS 10.15.7

R 4.1.1 ( R-4.1.1.pkg) was installed from R for macOS

Could someone help me with this please?

Many thanks in advance.

1 Like

I am not a Mac user myself, but tagging @andrjohns who might know more. The only practical solution I can offer is to try cmdstanr instead of rstan, as cmdstanr tends to be easier to install/maintain.

Hope you can get this resolved soon.

Thank you!

I also found this post and I’m putting it here for reference: Able to load rstan, but unable to run stan() funciton The author of that post encountered a similar problem over a year ago and no suggested solution worked.

1 Like