Error installing rstan

I have R 4.0.2 installed in RStudio on Windows Server 2012 R2
I followed the rtools40 and rstan website installation procedure and run into an error when running the following code

 install.packages(c("StanHeaders", "rstan"), type = "source")

The ‘StanHeaders’ package installs properly via

 install.packages("StanHeaders", type = "source")

but for

 install.packages("rstan", type = "source")

I get the following error:

* installing *source* package 'rstan' ...
** package 'rstan' successfully unpacked and MD5 sums checked
** using staged installation
** libs
"C:/rtools40/mingw64/bin/"g++ -m64 -std=gnu++14 -I"C:/PROGRA~1/MIE74D~1/ROPEN~1/R-40~1.2/include" -DNDEBUG -I"../inst/include" -I"../inst/include/boost_not_in_BH" -I"." -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -DBOOST_NO_AUTO_PTR -DSTAN_THREADS Microsoft R Open 4.0.2 The enhanced R distribution from Microsoft Microsoft packages Copyright (C) 2020 Microsoft Corporation  Using the Intel MKL for parallel mathematical computing (using 40 cores).  Default CRAN mirror snapshot taken on 2020-07-16. See: https://mran.microsoft.com/.  -DRCPP_PARALLEL_USE_TBB=1 -I'C:/Users/kenneth.smart/Documents/R/win-library/4.0/Rcpp/include' -I'C:/Users/kenneth.smart/Documents/R/win-library/4.0/RcppEigen/include' -I'C:/Users/kenneth.smart/Documents/R/win-library/4.0/BH/include' -I'C:/Users/kenneth.smart/Documents/R/win-library/4.0/StanHeaders/include' -I'C:/Users/kenneth.smart/Documents/R/win-library/4.0/RcppParallel/include'   -I"C:/a/w/1/s/vendor/extsoft/include"  -DRCPP_PARALLEL_USE_TBB=1   -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -c sparse_extractors.cpp -o sparse_extractors.o
sh: -c: line 1: syntax error near unexpected token `('
sh: -c: line 1: `"C:/rtools40/mingw64/bin/"g++ -m64 -std=gnu++14 -I"C:/PROGRA~1/MIE74D~1/ROPEN~1/R-40~1.2/include" -DNDEBUG -I"../inst/include" -I"../inst/include/boost_not_in_BH" -I"." -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -DBOOST_NO_AUTO_PTR -DSTAN_THREADS Microsoft R Open 4.0.2 The enhanced R distribution from Microsoft Microsoft packages Copyright (C) 2020 Microsoft Corporation  Using the Intel MKL for parallel mathematical computing (using 40 cores).  Default CRAN mirror snapshot taken on 2020-07-16. See: https://mran.microsoft.com/.  -DRCPP_PARALLEL_USE_TBB=1 -I'C:/Users/kenneth.smart/Documents/R/win-library/4.0/Rcpp/include' -I'C:/Users/kenneth.smart/Documents/R/win-library/4.0/RcppEigen/include' -I'C:/Users/kenneth.smart/Documents/R/win-library/4.0/BH/include' -I'C:/Users/kenneth.smart/Documents/R/win-library/4.0/StanHeaders/include' -I'C:/Users/kenneth.smart/Documents/R/win-library/4.0/RcppParallel/include'   -I"C:/a/w/1/s/vendor/extsoft/include"  -DRCPP_PARALLEL_USE_TBB=1   -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -c sparse_extractors.cpp -o sparse_extractors.o'
make: *** [C:/PROGRA~1/MIE74D~1/ROPEN~1/R-40~1.2/etc/x64/Makeconf:229: sparse_extractors.o] Error 2
ERROR: compilation failed for package 'rstan'
* removing 'C:/Users/kenneth.smart/Documents/R/win-library/4.0/rstan'
Warning in install.packages :
  installation of package ‘rstan’ had non-zero exit status

These are the following settings

Sys.which('make')
                              make 
"C:\\rtools40\\usr\\bin\\make.exe" 
> Sys.getenv('BINPREF')
[1] ""
> Sys.getenv('PATH')
[1] "C:\\rtools40\\usr\\bin;\\usr\\bin;C:\\Program Files\\Microsoft\\R Open\\R-4.0.2\\bin\\x64;C:\\Program Files\\Microsoft MPI\\Bin\\;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Microsoft SQL Server\\110\\Tools\\Binn\\ManagementStudio\\;C:\\Program Files (x86)\\Microsoft SQL Server\\110\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\110\\Tools\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\110\\DTS\\Binn\\;C:\\Program Files (x86)\\Enterprise Vault\\EVClient\\;%JAVA_HOME%\\bin;C:\\progs\\scala\\bin;C:\\progs2\\had\\winutils-master\\hadoop-2.6.4\\bin;C:\\progs2\\spark\\spark-2.0.0-bin-hadoop2.6\\bin;C:\\Program Files (x86)\\Microsoft SQL Server\\130\\Tools\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\130\\DTS\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\Client SDK\\ODBC\\130\\Tools\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\130\\Tools\\Binn\\ManagementStudio\\;C:\\progs\\scala\\bin;C:\\oracle\\instantclient_19_3;C:\\Program Files\\\\SUT\\bin;C:\\Program Files (x86)\\Microsoft SQL Server\\150\\DTS\\Binn\\;C:\\Program Files\\Azure Data Studio\\bin;C:\\Program Files\\Microsoft VS Code\\bin;C:\\Program Files\\Git\\cmd"
> Sys.getenv('RTOOLS40_HOME')
[1] "C:/rtools40"

Any help would be greatly appreciated.

Hi Kenneth,

This is an issue we see when using Microsoft R Open, it seems that they insert a build message which causes a compilation failure. You can see this a bit further along the build arguments:

-DSTAN_THREADS Microsoft R Open 4.0.2 The enhanced R distribution from Microsoft Microsoft packages Copyright (C) 2020 Microsoft Corporation  Using the Intel MKL for parallel mathematical computing (using 40 cores).  Default CRAN mirror snapshot taken on 2020-07-16. See: https://mran.microsoft.com/.  -DRCPP_PARALLEL_USE_TBB=1 -I'C:/Users/kenneth.smart/Documents/R/win-library/4.0/Rcpp/include'

You can either use the regular CRAN R, or another user posted instructions on how to remove the message over in this thread. Unfortunately even after those messages are fixed, there were still issues with linking to the RcppParallel package.

While not tested on MRO, if you fix the build message you might also want to try the preview of the next RStan version, to see if the linking issues remain:

remove.packages(c("StanHeaders", "rstan"))
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
1 Like

This seemed to have worked - thanks!!

I have as similar problem. Since upgrading to R 4.3, brms did not run either on my Windows or Mac. Whenever I solve the problem for one, the other one crashes (probably because of some files being on Onedrive that is shared across two computers).

It used to work until yesterday for my Windows. While trying to solve it on my Mac (which I couldn’t), now I get the following error on my Windows:

I run this code:

remove.packages(c("StanHeaders", "rstan"))
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
example(stan_model, package = "rstan", run.dontrun = TRUE)

I get this error:

The downloaded source packages are in
	‘C:\Users\Kadir\AppData\Local\Temp\RtmpQFggUN\downloaded_packages’
Warning message:
In utils::install.packages("rstan", repos = c("https://mc-stan.org/r-packages/",  :
  installation of package ‘rstan’ had non-zero exit status

Settings:

Sys.which('make')
                              make 
"C:\\rtools43\\usr\\bin\\make.exe" 

> sessionInfo()
R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: Asia/Seoul
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.3.0  tools_4.3.0     rstudioapi_0.14 renv_0.17.3

If I run this command and then run the brm model the R session is aborted.
library(brms)
mod ← brm(Anzahl_Pflanzen ~ Mischung * Saattermin, data=dat, family=negbinomial)

I work with rtools42 and R version 4.3.1