Rstan on Windows

Hi everyone,
thanks for putting in all the work.

I am trying to install Rstan for the first time. I ran the code of the original post, that is

file.edit("~/.R/Makevars.win")
file.edit("~/.R/Makevars")
install.packages(
"https://artifacts.r-hub.io/StanHeaders_2.21.0-6.tar.gz-6fc9f644607800c88f50865e72d8a81a/StanHeaders_2.21.0-6.zip",
repos = NULL, type = "win.binary")
install.packages(
"https://win-builder.r-project.org/EwLg0orTrr82/rstan_2.21.2.zip",
repos = NULL, type = "win.binary")

in RStudio 3.5.1. It does not give any error, however if I try to run
library(rstan) in tells me that ‘StanHeaders’ could not be loaded and that RcppParallel 5.0.0. is loaded but 5.0.1. is needed.

Are there any additional steps not mentioned in this thread I have to take first in order to install Rstan for the first time?

Thank you

Based on that error message I would start by upgrading RcppParallel (and any other packages), but I don’t specifically recognize the error.

Could you post the error message itself?

I updated PcppParallel with update.packages("RcppParallel"), then I ran

> file.edit("~/.R/Makevars.win")

> file.edit("~/.R/Makevars")

> install.packages(
+     "https://artifacts.r-hub.io/StanHeaders_2.21.0-6.tar.gz-6fc9f644607800c88f50865e72d8a81a/StanHeaders_2.21.0-6.zip",
+     repos = NULL, type = "win.binary")
trying URL 'https://artifacts.r-hub.io/StanHeaders_2.21.0-6.tar.gz-6fc9f644607800c88f50865e72d8a81a/StanHeaders_2.21.0-6.zip'
Content type 'application/zip' length 2340489 bytes (2.2 MB)
downloaded 2.2 MB

package ‘StanHeaders’ successfully unpacked and MD5 sums checked
> install.packages(
+     "https://win-builder.r-project.org/EwLg0orTrr82/rstan_2.21.2.zip",
+     repos = NULL, type = "win.binary")
trying URL 'https://win-builder.r-project.org/EwLg0orTrr82/rstan_2.21.2.zip'
Content type 'application/x-zip-compressed' length 5874605 bytes (5.6 MB)
downloaded 5.6 MB

package ‘rstan’ successfully unpacked and MD5 sums checked
 > library(rstan)
    Lade nötiges Paket: StanHeaders *[Loading StanHeader]*
    Error: package or namespace load failed for ‘StanHeaders’ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
     Namensraum ‘RcppParallel’ 5.0.0 ist geladen, aber >= 5.0.1 wird benötigt *[Namespace ‘RcppParallel’ 5.0.0 is loaded, but >= 5.0.1 is needed ]*
    Error: Paket ‘StanHeaders’ konnte nicht geladen werden *[Package 'StanHeader' could not be loaded]*
    In addition: Warning messages:
    1: Paket ‘rstan’ wurde unter R Version 3.6.3 erstellt 
    2: Paket ‘StanHeaders’ wurde unter R Version 3.6.3 erstellt

Restarting R after installing the packages (if this is what you meant) gives the same error.
I also made a mistake in my earlier post: I am using R 3.5.1 in Rstudio 1.0.143.

Thank you!

Maybe restart R in between?

Perhaps the order of paths where libraries are searched for on your system is different than the default one? Using .libPaths() usually one sees something like this:

> .libPaths()
[1] "/home/user/R/library"       "/usr/local/lib/R/site-library"
[3] "/usr/lib/R/site-library"    "/usr/lib/R/library"

But if your home library appears after one of the system library, even in you install version 5.0.1 there, you’ll get version 5.0.0 loaded from one of the system paths.

Thanks for your comment, @mcol, it directed me to parts of the problem.

Forcing RcppParallel to install v5.0.1. by install.packages("RcppParallel", version = "5.0.1.") gave the following warning

Created ../build/lib_release directory
../../build/Makefile.tbb:32: CONFIG: cfg=release arch=ia32 compiler=gcc target=windows runtime=mingw
sh: C:/Rtools/mingw_32/bin/g++: No such file or directory
make[2]: *** [../../build/common_rules.inc:119: concurrent_hash_map.o] Error 127
make[1]: *** [Makefile:104: tbb_release] Error 2
cp: cannot stat 'tbb/build/lib_release/tbb*.dll': No such file or directory
make: *** [Makevars:89: tbb] Error 1
ERROR: compilation failed for package 'RcppParallel'
* removing 'C:/Users/Lucia/R-3.5.1/library/RcppParallel'
* restoring previous 'C:/Users/Lucia/R-3.5.1/library/RcppParallel'
In R CMD INSTALL
Warning in install.packages :
  installation of package ‘RcppParallel’ had non-zero exit status

making me think RTools is part of the problem. Indeed I had installed the latest (April 2020) version of RTools which needs >= R 4.0.0, so I reinstalled RTools version 3.5.

However, I still had the problem that RcppParallel installed version 5.0.0. even when explicitly telling to install >=5.0.1., so I downloaded in manually from CRAN. [1]

Now loading RcppParallel seems to work.

> library(RcppParallel)
Warning message:
Paket ‘RcppParallel’ wurde unter R Version 4.0.2 erstellt

Now deleting rstan, restarting R, and running same as before I get:

Error: package or namespace load failed for ‘rstan’ in rbind(info, getNamespaceInfo(env, "S3methods")):
 number of columns of matrices must match (see arg 2)

So old error is gone, but now I get a new error I do not understand.

[1] Downloading r-release from https://cran.r-project.org/web/packages/RcppParallel/index.html, unzipping and copying it into the folder where my r packages lie

@bgoodri the URLs aren’t working anymore

You can try the Windows binaries from CRAN now.

The temporary URL must have expired, as I can not access (404).

There’s a new version on rstan on CRAN now, so I don’t think these links are necessary anymore. Does it work for you to just do

install.packages("rstan", dependencies = TRUE)

(That said, @bgoodri it looks like the StanHeaders on CRAN is behind the version number in your r-hub link. Does that matter?})

Hi Jonah,

I just started learning stan today so this may be a simple fix, but when I try and fit a model I get the following error message:

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 file23083eda7975.cpp 2> file23083eda7975.cpp.err.txt’ had status 1
3: In file(con, “r”) :
cannot open file ‘file23083eda7975.cpp.err.txt’: No such file or directory
Error in sink(type = “output”) : invalid connection

I just installed R and stan today, so pretty much have no clue what this means. Any help would be greatly appreciated!

Thanks very much … I loaded it am good to go.

@mguzqui Great, glad it’s working for you!

@graham Welcome to Stan and sorry you’re running into issues on your first day! Which version of Windows do you have? And do you know which version of Rtools you have?

@bgoodri @bbbales2 or anyone else who knows windows, since @graham just installed R and Stan today they presumably have the latest versions. So any ideas for what’s up here?

Here’s my system and environment info:

R version 4.0.2 (2020-06-22)
RStan 2.21.2
Rtools 4.0.0.28
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