Cannot find function "stan" in R

Hi,

I have installed the “rstan” package in R. However when trying to use the stan function, I get an error that there is no package called ‘callr’ and the function ‘stan’ cannot be found.

I am following the steps from: RStan: the R interface to Stan

I get the error after I enter:
library(rstan)
fit1 ← stan(
file = “schools.stan”, # Stan program
data = schools_data, # named list of data
chains = 4, # number of Markov chains
warmup = 1000, # number of warmup iterations per chain
iter = 2000, # total number of iterations per chain
cores = 1, # number of cores (could use one per chain)
refresh = 0 # no progress shown
)

I am using the most updated R.

Thanks!

Can you run:

install.packages("rstan", repos = "https://cloud.r-project.org/", dependencies = TRUE)

and then try again?

Thanks for replying.

I’m still getting the same error message:
Error in stan(file = “schools.stan”, data = schools_data, chains = 4, :
could not find function “stan”

Have you called library(rstan) before running that model?

Yes I did :)

Can you post the output you get when you run library(rstan)

This is the output I get:

Loading required package: ggplot2
Error: package or namespace load failed for ‘ggplot2’ in inDL(x, as.logical(local), as.logical(now), …):
unable to load shared object ‘C:/Program Files/R/R-3.4.3/library/stats/libs/x64/stats.dll’:
LoadLibrary failure: The specified module could not be found.

Error: package ‘ggplot2’ could not be loaded
In addition: Warning messages:
1: package ‘rstan’ was built under R version 3.4.4
2: package ‘ggplot2’ was built under R version 3.4.4

I don’t normally have a problem with running ggplot2 though

Can you try reinstalling ggplot2 and rstan?

I have tried to reinstall both ggplot2 and rstan.

When I then run library(ggplot2) or library(rstan) I get this error:
Error: package or namespace load failed for ‘ggplot2’ in inDL(x, as.logical(local), as.logical(now), …):
unable to load shared object ‘C:/Program Files/R/R-3.4.3/library/stats/libs/x64/stats.dll’:
LoadLibrary failure: The specified module could not be found.
In addition: Warning message:
package ‘ggplot2’ was built under R version 3.4.4

So this is a ggplot2 error, rather than rstan. Can you try removing ggplot2, restarting R, and then installing it again? If that fails, try to install from source.

If the install from source fails, then you’ll need to open an issue on the ggplot2 github to get the developers help in resolving this: GitHub - tidyverse/ggplot2: An implementation of the Grammar of Graphics in R

1 Like

Hi
I am currently facing the same issue. I wrote a fit_exponential_model function using rStan in R studio . However, when I run a unit test for this function, I keep encountering the following error:
-----------------------Error: fit_exponential_model checks for length column -----------------------
Error in stan(model_code = stan_model_code, data = stan_data, chains = 4, warmup = 1000, iter = 2000, cores = 2, refresh = 100): could not find function “stan”
I have already restarted R, removed the old rStan, reinstalled it, and loaded rStan, but the issue persists. Could you kindly provide some suggestions?

Thank you very much in advance.