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")))

This seemed to have worked - thanks!!