# Cannot find function "stan" in R

**URL:** <https://discourse.mc-stan.org/t/cannot-find-function-stan-in-r/25162>\
**Category:** RStan\
**Created:** [November 8, 2021, 11:55am UTC](https://discourse.mc-stan.org/t/cannot-find-function-stan-in-r/25162 "2021-11-08T11:55:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maths\_21](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Maths\_21](https://discourse.mc-stan.org/u/Maths_21)\
**Post date:** [November 8, 2021, 11:55am UTC](https://discourse.mc-stan.org/t/cannot-find-function-stan-in-r/25162/1 "2021-11-08T11:55:50Z")

</div>

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](https://cran.r-project.org/web/packages/rstan/vignettes/rstan.html#sample-from-the-posterior-distribution)

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!

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [November 8, 2021, 12:00pm UTC](https://discourse.mc-stan.org/t/cannot-find-function-stan-in-r/25162/2 "2021-11-08T12:00:40Z")

</div>

Can you run:

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

```

and then try again?

---

<div class="post-metadata">

**Author:** ![Maths\_21](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Maths\_21](https://discourse.mc-stan.org/u/Maths_21)\
**Post date:** [November 8, 2021, 12:06pm UTC](https://discourse.mc-stan.org/t/cannot-find-function-stan-in-r/25162/3 "2021-11-08T12:06:29Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [November 8, 2021, 12:07pm UTC](https://discourse.mc-stan.org/t/cannot-find-function-stan-in-r/25162/4 "2021-11-08T12:07:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Maths\_21](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Maths\_21](https://discourse.mc-stan.org/u/Maths_21)\
**Post date:** [November 8, 2021, 12:09pm UTC](https://discourse.mc-stan.org/t/cannot-find-function-stan-in-r/25162/5 "2021-11-08T12:09:52Z")

</div>

Yes I did :)

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [November 8, 2021, 12:11pm UTC](https://discourse.mc-stan.org/t/cannot-find-function-stan-in-r/25162/6 "2021-11-08T12:11:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Maths\_21](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Maths\_21](https://discourse.mc-stan.org/u/Maths_21)\
**Post date:** [November 8, 2021, 12:18pm UTC](https://discourse.mc-stan.org/t/cannot-find-function-stan-in-r/25162/7 "2021-11-08T12:18:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [November 8, 2021, 12:20pm UTC](https://discourse.mc-stan.org/t/cannot-find-function-stan-in-r/25162/8 "2021-11-08T12:20:07Z")

</div>

Can you try reinstalling ggplot2 and rstan?

---

<div class="post-metadata">

**Author:** ![Maths\_21](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Maths\_21](https://discourse.mc-stan.org/u/Maths_21)\
**Post date:** [November 8, 2021, 12:25pm UTC](https://discourse.mc-stan.org/t/cannot-find-function-stan-in-r/25162/9 "2021-11-08T12:25:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [November 8, 2021, 12:27pm UTC](https://discourse.mc-stan.org/t/cannot-find-function-stan-in-r/25162/10 "2021-11-08T12:27:30Z")

</div>

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](https://github.com/tidyverse/ggplot2)

---

<div class="post-metadata">

**Author:** ![Jiamo\_Li](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jiamo_li/32/16849_2.png) [@Jiamo\_Li](https://discourse.mc-stan.org/u/Jiamo_Li)\
**Post date:** [June 8, 2024, 7:35pm UTC](https://discourse.mc-stan.org/t/cannot-find-function-stan-in-r/25162/11 "2024-06-08T19:35:45Z")

</div>

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.
