# Error when running stan() in r

**URL:** <https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080>\
**Category:** RStan\
**Created:** [May 13, 2020, 6:36am UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080 "2020-05-13T06:36:49Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![DrP](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@DrP](https://discourse.mc-stan.org/u/DrP)\
**Post date:** [May 13, 2020, 6:36am UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/1 "2020-05-13T06:36:49Z")

</div>

Hello!  
Very new to RStan; so new that I haven’t been able to get it to work. Below is some code from a tutorial I found on line produced by Rasmus Baath:  
library(rstan)

# The Stan model as a string

```
model_string<- "
data {
// Number of trials
 int nA;
 int nB;
 // Number of successes
 int sA;
 int sB;
}

parameters {
 real<lower=0, upper=1> rateA;
 real<lower=0, upper=1> rateB;
}

model {
 rateA ~ uniform(0,1);
 rateB ~ uniform(0,1);
 sA ~ binomial(nA,rateA);
 sB ~ binomial(nB,rateB);
}

generated quantities {
 real rate_diff;
 rate_diff= rateB-rateA;
}
"
data_list <- list(nA=16, nB=16, sA=6, sB=10)

```

# Compiling and producing posterior samples from the model

stan\_samples \<-stan(model\_code=model\_string, data=data\_list)  
###################################################  
When I run the above code, I get the following error messages:

```
Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(cmd, intern = !verbose) :
  running command CMD SHLIB file340c3f4110d1.cpp 2> file340c3f4110d1.cpp.err.txt' had status 1
2: In file(con, "r") :
  cannot open file 'file340c3f4110d1.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection

```

I assum this means I need file340c3f4110d1.cpp.err.txt, but I can’t find it and I don’t know where I got it or if I can get it again if somehow I had it and now I don’t. At this point, I’m wondering if I can get this file somewhere, or if this can only be fixed by removing all r and rstudio and start from scratch. Any recommendations would be very much appreciate it.

Thank you.  
P

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [May 13, 2020, 1:50pm UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/2 "2020-05-13T13:50:53Z")

</div>

Start with

```
example(stan_model, package = "rstan", run.dontrun = TRUE)

```

What is the line that includes `error:` (with the colon).

---

<div class="post-metadata">

**Author:** ![DrP](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@DrP](https://discourse.mc-stan.org/u/DrP)\
**Post date:** [May 13, 2020, 2:50pm UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/3 "2020-05-13T14:50:37Z")

</div>

I’m not quite sure I understand your question, but here goes.

This line:  
stan\_samples \<-stan(model\_code=model\_string, data=data\_list)

resulted in this error message:

Error in file(con, “r”) : cannot open the connection  
In addition: Warning messages:  
1: In system(cmd, intern = !verbose) :  
running command ‘[]\*\*\*\*\*/bin/x64/R) CMD SHLIB file340c3f4110d1.cpp 2\> file340c3f4110d1.cpp.err.txt’ had status 1  
2: In file(con, “r”) :  
cannot open file ‘file340c3f4110d1.cpp.err.txt’: No such file or directory  
Error in sink(type = “output”) : invalid connection

---

<div class="post-metadata">

**Author:** ![DrP](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@DrP](https://discourse.mc-stan.org/u/DrP)\
**Post date:** [May 13, 2020, 3:44pm UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/4 "2020-05-13T15:44:00Z")

</div>

I think I understand now. I fan your code (i.e., example(stan\_model, package = “rstan”, run.dontrun = TRUE))  
Here is the line that includes error:  
Error in file(con, “r”) : cannot open the connection  
In addition: Warning message:  
In file(con, “r”) :  
cannot open file ‘file4e4021452a4b.cpp.err.txt’: No such file or directory

So I guess i need file4e4021452a4b.cpp.err.txt but I can’t find it.

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [May 13, 2020, 4:05pm UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/5 "2020-05-13T16:05:36Z")

</div>

There should be about 1000 more lines of output. We need the one with `error:` in it.

---

<div class="post-metadata">

**Author:** ![DrP](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@DrP](https://discourse.mc-stan.org/u/DrP)\
**Post date:** [May 13, 2020, 4:10pm UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/6 "2020-05-13T16:10:22Z")

</div>

Oh. My bad. Sorry about that  
How about this:

fatal error: stan/math/prim/mat/fun/Eigen.hpp: No such file or directory  
compilation terminated.

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [May 13, 2020, 6:35pm UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/7 "2020-05-13T18:35:37Z")

</div>

You need to do

```
install.packages("StanHeaders")

```

---

<div class="post-metadata">

**Author:** ![DrP](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@DrP](https://discourse.mc-stan.org/u/DrP)\
**Post date:** [May 13, 2020, 7:01pm UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/8 "2020-05-13T19:01:51Z")

</div>

I just installed StanHeaders and ran the code you gave me again (i.e., example(stan\_model, package = “rstan”, run.dontrun = TRUE))

I got the same error message as before:

fatal error: stan/math/prim/mat/fun/Eigen.hpp: No such file or directory  
compilation terminated.

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [May 13, 2020, 7:03pm UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/9 "2020-05-13T19:03:56Z")

</div>

Is there more on or before that line?

---

<div class="post-metadata">

**Author:** ![DrP](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@DrP](https://discourse.mc-stan.org/u/DrP)\
**Post date:** [May 13, 2020, 7:06pm UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/10 "2020-05-13T19:06:56Z")

</div>

cc1plus.exe: fatal error: stan/math/prim/mat/fun/Eigen.hpp: No such file or directory  
compilation terminated.

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [May 13, 2020, 7:08pm UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/11 "2020-05-13T19:08:44Z")

</div>

What is

```
system.file("include", "stan", "math", "prim", "mat", "fun", "Eigen.hpp", 
            package = "StanHeaders")

```

?

---

<div class="post-metadata">

**Author:** ![DrP](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@DrP](https://discourse.mc-stan.org/u/DrP)\
**Post date:** [May 13, 2020, 7:10pm UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/12 "2020-05-13T19:10:50Z")

</div>

“\\oya.ad/Users/Central/gettyp/R/R-4.0.0/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp”

![image001.jpg](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/f/f464dd1c7c382c015b3fc53eebe1cd1aadffee61.jpg)

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [May 13, 2020, 7:12pm UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/13 "2020-05-13T19:12:11Z")

</div>

There are a lot of difficulties when you install packages in network-mounted home directories on Windows. It is better to install them where R is installed.

---

<div class="post-metadata">

**Author:** ![DrP](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@DrP](https://discourse.mc-stan.org/u/DrP)\
**Post date:** [May 13, 2020, 7:16pm UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/14 "2020-05-13T19:16:56Z")

</div>

Ok. So, it sounds like I need to uninstall and reinstall and have install packages directly on the machine? Or can I simply move the R folder to the computer?

Thank you very much, by the way, for coaching me through this. I appreciate it.

![image001.jpg](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/f/f464dd1c7c382c015b3fc53eebe1cd1aadffee61.jpg)

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [May 13, 2020, 7:17pm UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/15 "2020-05-13T19:17:24Z")

</div>

Best to reinstall

---

<div class="post-metadata">

**Author:** ![DrP](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@DrP](https://discourse.mc-stan.org/u/DrP)\
**Post date:** [May 13, 2020, 7:18pm UTC](https://discourse.mc-stan.org/t/error-when-running-stan-in-r/15080/16 "2020-05-13T19:18:19Z")

</div>

Ok. That’s what I figured. Thank again for you help.

![image001.jpg](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/f/f464dd1c7c382c015b3fc53eebe1cd1aadffee61.jpg)
