Rstan on Windows

Here’s my system and environment info:

R version 4.0.2 (2020-06-22)
RStan 2.21.2
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Additionally, I now get the following error message when trying to fit one of the Stan example models:

Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  '-E' not found
2: In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/R-40~1.2/bin/x64/R CMD SHLIB file44f8185f33.cpp 2> file44f8185f33.cpp.err.txt' had status 1
3: In file(con, "r") :
  cannot open file 'file44f8185f33.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection

Thanks for responding so quickly! I’m guessing this is a relatively easy fix that a newbie like me wouldn’t think of

So I did some digging, looks like the R CMD SHLIB file mentioned in the above error message is used by R to compile source code in R packages (such as Stan) and to link object files into a specific object. When I follow the path from the error message

C:/PROGRA~1/R/R-40~1.2/bin/x64/R CMD SHLIB file44f8185f33.cpp 2

I do not have a filed named Rcmdshlib. I suspect this is from installation but tried a fresh install of R and the file is still missing. I hope this helps narrow down the issue

Same problem but I only get the first warning message :

Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘-E’ not found

for the error, installing r from the source helped : install.packages(c(“StanHeaders”, “rstan”), type = “source”)

Thanks for the suggestion! Tried to download from source, and the installation took 30 minutes then got stuck cycling through the following two messages in the R console:

C:/Program Files/R/R-4.0.2/library/BH/include/boost/spirit/home/support/char_encoding/standard_wide.hpp:86:42: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
             return ch >= WCHAR_MIN && ch <= WCHAR_MAX;
C:/Program Files/R/R-4.0.2/library/BH/include/boost/mpl/assert.hpp:199:21: warning: unnecessary parentheses in declaration of 'assert_not_arg' [-Wparentheses]
 failed ************ (boost::mpl::not_<Pred>::************

Not sure what’s going on but it’s beginning to become frustrating

Sorry for the hassle! I’m not sure what’s going on here either. @bgoodri @bbbales2 or @wds15 any ideas?

I read through the errors but it’s not ringing any bells.

Searching for the string I saw this post which is dealing with a similar error: Installation issue

Thanks, I’ll take a look and see what I can do!

Wow that did the trick! Thanks to all of you for helping me get up and running, I’m very appreciative of it!


@graham Glad it’s working! @bbbales2 thanks for tracking that down.

@stevebronder Any progress on the patch for this error?

It was just reported again on the rstan issue tracker:

I just wanted to jump in an confirm that this is still an issue for me. The weird thing is, sometimes R studio simply crashes with the “C stack trace error” even I simply subset my data. I also notice that when stan is running in a loop (to fit multiple datasets to the same model), once it works, it will work and will not crash. My solution so far is to simply keep rerunning Rstudio hoping it will make it to the stan part.

(PS: not sure if it is coincidental, but my code hasn’t crashed (yet) since I removed my generated quantities block, where I compute the log likelihood used to compute the LOOIC and WAIC later in the R code…)

1 Like

@avdminne Thanks for letting us know that’s still an issue. A few comments/questions:

Can you share what’s in your file? You can check with

file.edit(file.path(Sys.getenv("HOME"), ".R", ""))

If there’s anything in there I would try deleting it all and just using
CXX14FLAGS=-O3 -mtune=native.

Are you using repeated calls to the stan() function or are you compiling first with stan_model() and then calling sampling() repeatedly?

Finally, if we can’t figure out how to get it running for you without you having to always worry it’s going to crash, then I recommend tryinng the new CmdStanR interface, which shouldn’t have that issue. (If you try that and run into any trouble installing on Windows let us know and we can definitely fix that issue)

1 Like

the URLs are actually not valid. When trying to run these commands (for R4.0) both yield a 404

Hi @etaifour, I don’t think the links are needed anymore. Can you try just using install.packages("rstan", dependencies = TRUE)?

Thanks, you are right, it seems they are not needed anymore. I have just ran the command you have shared, and did not receive any errors.
I then loaded rstan, and ran the schools.stan “hello world” program (from the documentation). I was able to print/plot the fitted models.

Thanks for your help
rEgards, Eyas

Great, glad it’s working!

I couldn’t replicate the issue on my linux machine. I can spin up a windows instance tmrw and try this again

1 Like

BTW… we need to include in our instructions to more frequently restart R. This comes up really often that people forget that.

1 Like

Hi Jonah,

I make a stan_model() first, and subsequently use sampling(). (I learned that the hard way as well.) As for my Makevars, I deleted everything that was in the file. It was a recommendation I got earlier… So I guess I don’t have to show the actual file then, right?

It hasn’t crashed for a full 24h now, so that is reassuring, but I will definitely try out CmdStanR. Many thanks.


Yeah emptying the Makevars file is a good idea when troubleshooting, but now that it’s working I suggest adding just this line

to Makevars which should make Stan a decent bit faster.

Anyway, glad it’s not crashing at the moment. And if you give CmdStanR a try let us know if you run into any trouble.