Error when Configuring RStan C++ toolchain

Hello! I am a new user to RStan and I ran into an issue when configuring the C++ toolchain and subsequent loading of the RStan package.

I am using Windows 10, R x64 v.4.0.2 and RStudio v.1.3.1073. I followed these instructions to the letter in R and RStudio: https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started. Below are the commands I used an abbreviated version of the output highlighting the error.

> install.packages("rstan", repos = "https://cloud.r-project.org/", dependencies = TRUE)
ERROR: compilation failed for package 'jsonlite'
* removing 'C:/Users/Allison/Documents/R/win-library/4.0/jsonlite'
Warning in install.packages :
  installation of package ‘jsonlite’ had non-zero exit status
The downloaded source packages are in
‘C:\Users\Allison\AppData\Local\Temp\RtmpAnUOQm\downloaded_packages’
> pkgbuild::has_build_tools(debug = TRUE)
>library("rstan") 
Loading required package: StanHeaders
Loading required package: ggplot2
Error: package or namespace load failed for ‘rstan’ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
 there is no package called ‘jsonlite’

Any insights on would be helpful.
Thank you!
Allison

Can you try running:

writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron")

and restart the R ression and then try installing rstan again.

Still the same error.
Error: package or namespace load failed for ‘rstan’ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
there is no package called ‘jsonlite’

Can you run

install.packages(“jsonlite”)

This is what I get when I try installing just the jsonlite package.

ERROR: compilation failed for package ‘jsonlite’

  • removing ‘C:/Users/Allison/Documents/R/win-library/4.0/jsonlite’

The downloaded source packages are in
‘C:\Users\Allison\AppData\Local\Temp\Rtmpu2NfgF\downloaded_packages’
Warning message:
In install.packages(“jsonlite”) :
installation of package ‘jsonlite’ had non-zero exit status

Here is the complete output in case it helps.

install.packages(“jsonlite”)
Installing package into ‘C:/Users/Allison/Documents/R/win-library/4.0’
(as ‘lib’ is unspecified)
— Please select a CRAN mirror for use in this session —

There is a binary version available but the source version is later:
binary source needs_compilation
jsonlite 1.7.0 1.7.1 TRUE

installing the source package ‘jsonlite’

trying URL ‘https://cran.case.edu/src/contrib/jsonlite_1.7.1.tar.gz
Content type ‘application/x-gzip’ length 1057786 bytes (1.0 MB)
downloaded 1.0 MB

  • installing source package ‘jsonlite’ …
    ** package ‘jsonlite’ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** libs

*** arch - i386
C:/Rtools/mingw_32/bin/gcc -I"C:/PROGRA~1/R/R-40~1.2/include" -DNDEBUG -Iyajl/api -D__USE_MINGW_ANSI_STDIO -O2 -Wall -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c base64.c -o base64.o
C:/Rtools/mingw_32/bin/gcc: not found
make: *** [base64.o] Error 127
ERROR: compilation failed for package ‘jsonlite’

  • removing ‘C:/Users/Allison/Documents/R/win-library/4.0/jsonlite’

The downloaded source packages are in
‘C:\Users\Allison\AppData\Local\Temp\Rtmpu2NfgF\downloaded_packages’
Warning message:
In install.packages(“jsonlite”) :
installation of package ‘jsonlite’ had non-zero exit status

What does Sys.getenv(“RTOOLS40_HOME”) return?

Sys.getenv(“RTOOLS40_HOME”)
[1] “”

Oh ok, in this case you do not have RTools 4.0 installed.

See: https://cran.r-project.org/bin/windows/Rtools/

I followed the instructions to the link you sent and I get a compilation error for the jsonlite package.

Sys.which(“make”)
make
“C:\rtools40\usr\bin\make.exe”
install.packages(“jsonlite”, type = “source”)
Installing package into ‘C:/Users/Allison/Documents/R/win-library/4.0’
(as ‘lib’ is unspecified)
trying URL ‘https://cran.rstudio.com/src/contrib/jsonlite_1.7.1.tar.gz
Content type ‘application/x-gzip’ length 1057786 bytes (1.0 MB)
downloaded 1.0 MB

  • installing source package ‘jsonlite’ …
    ** package ‘jsonlite’ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** libs

*** arch - i386
C:/Rtools/mingw_32/bin/gcc -I"C:/PROGRA~1/R/R-40~1.2/include" -DNDEBUG -Iyajl/api -D__USE_MINGW_ANSI_STDIO -O2 -Wall -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c base64.c -o base64.o
sh: C:/Rtools/mingw_32/bin/gcc: No such file or directory
make: *** [C:/PROGRA~1/R/R-40~1.2/etc/i386/Makeconf:222: base64.o] Error 127
ERROR: compilation failed for package ‘jsonlite’

  • removing ‘C:/Users/Allison/Documents/R/win-library/4.0/jsonlite’
    Warning in install.packages :
    installation of package ‘jsonlite’ had non-zero exit status

The downloaded source packages are in
‘C:\Users\Allison\AppData\Local\Temp\RtmpyI6z2r\downloaded_packages’

Hi Allison,

That means that R is still configured to look for the old RTools installation. To help figure out what needs to be changed, can you post the outputs from:

Sys.getenv("BINPREF")
Sys.getenv("PATH")
readLines("~/.Renviron")
readLines("~/.Rprofile")
readLines("~/.R/Makevars.win")

Sys.getenv(“BINPREF”)
[1] “C:/Rtools/mingw_$(WIN)/bin/”

Sys.getenv(“PATH”)
[1] “C:\rtools40\usr\bin;C:\Program Files\R\R-4.0.2\bin\x64;c:\Rtools\bin;c:\Rtools\mingw_64\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\ia32\mpirt;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\ia32\compiler;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel® Management Engine Components\DAL;C:\Program Files\Intel\Intel® Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel® Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel® Management Engine Components\IPT;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;C:\Program Files (x86)\Toshiba\Bluetooth Toshiba Stack\sys\;C:\Program Files (x86)\Toshiba\Bluetooth Toshiba Stack\sys\x64\;C:\MATLAB701\bin\win32;C:\Program Files\MATLAB\R2017b\runtime\win64;C:\Program Files\MATLAB\R2017b\bin;C:\Program Files (x86)\Kensington\TrackballWorks;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;”

readLines("~/.Renviron")
[1] “PATH=${RTOOLS40_HOME}\usr\bin;${PATH}”

readLines("~/.Rprofile")
[1] “Sys.setenv(BINPREF = “C:/Rtools/mingw_$(WIN)/bin/”)”

readLines("~/.R/Makevars.win")
[1] “”
[2] “CXX14FLAGS=-O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2”
[3] “”
[4] “CXX14FLAGS=-O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2”
[5] “”
[6] “CXX14FLAGS=-O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2”
[7] “”
[8] “CXX14FLAGS=-O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2”

Great! Three steps to take here:

  • In your Documents folder there will a file called .Rprofile, delete that
  • In your Documents folder there will be a .R folder, in that will be a Makevars.win file. Open that in a text editor and delete all but one of the CXX14FLAGS lines
  • Restart R and try your model again
1 Like

Hi There!

I have a similar problem. I have R version 4.0.4 with Rstudio version 1.4.1106 (both updated today). I have been trying to get the installation from source to work, but I keep getting the same error messages. I have already done all the steps from this thread, including numerous steps from other threads to get jsonlite to install. I get the following error message:

install.packages(“jsonlite”, type = “source”)
Installing package into ‘C:/Users/laura/OneDrive/Documenten/R/win-library/4.0’
(as ‘lib’ is unspecified)
trying URL ‘http://cran.rstudio.com/src/contrib/jsonlite_1.7.2.tar.gz
Content type ‘application/x-gzip’ length 421716 bytes (411 KB)
downloaded 411 KB

  • installing source package ‘jsonlite’ …
    ** package ‘jsonlite’ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** libs

*** arch - i386
C:/rtools40/usr/mingw_32/bin/gcc -I"C:/PROGRA~1/R/R-40~1.4/include" -DNDEBUG -Iyajl/api -D__USE_MINGW_ANSI_STDIO -O2 -Wall -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c base64.c -o base64.o
sh: C:/rtools40/usr/mingw_32/bin/gcc: No such file or directory
make: *** [C:/PROGRA~1/R/R-40~1.4/etc/i386/Makeconf:222: base64.o] Error 127
ERROR: compilation failed for package ‘jsonlite’

  • removing ‘C:/Users/laura/OneDrive/Documenten/R/win-library/4.0/jsonlite’
  • restoring previous ‘C:/Users/laura/OneDrive/Documenten/R/win-library/4.0/jsonlite’
    Warning in install.packages :
    installation of package ‘jsonlite’ had non-zero exit status

The downloaded source packages are in
‘C:\Users\laura\AppData\Local\Temp\Rtmpctpahf\downloaded_packages’

Btw, my output to the code metioned above is as following:

Sys.getenv(“BINPREF”)
[1] “C:/rtools40/usr/mingw_$(WIN)/bin/”

Sys.getenv(“PATH”)
[1] “C:\rtools40\usr\bin;C:\Program Files\R\R-4.0.4\bin\x64;C:\Python27\Lib\site-packages\PyQt4;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files (x86)\Intel\Intel® Management Engine Components\DAL;C:\Program Files\Intel\Intel® Management Engine Components\DAL;C:\Program Files\MATLAB\R2019a\bin;C:\Python27;C:\Python27\DLLs;C:\Python27\Scripts;C:\Python27\gnuplot\binary;C:\Program Files (x86)\pythonxy\SciTE-3.5.1-4;C:\Program Files (x86)\pythonxy\console;C:\Users\laura\AppData\Roaming\TinyTeX\bin\win32;C:\Users\laura\AppData\Local\Microsoft\WindowsApps;C:\Users\laura\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64”
readLines("~/.Renviron")
[1] “PATH=”{RTOOLS40_HOME}\\usr\\bin;{PATH}""
readLines("~/.Rprofile")
[1] “Sys.setenv(BINPREF = “C:/rtools40/usr/mingw_$(WIN)/bin/”)”
readLines("~/.R/Makevars.win")
[1] “”
[2] " CXX14FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2"

Hi Laura,

The issue is caused by your setting of BINPREF in your .Rprofile file. See the RStan Getting Started Instructions for information on how to fix this.

Hi there,

Thank you for your quick reply! I had already deleted my Rprofile file before, but there was a new one now. I again deleted it, as was in the instructions. The problem is still there though. Do you have any idea how I can fix it?

What do you get from:

Sys.getenv("BINPREF")
readLines("~/.Rprofile")

The first one gives:

Sys.getenv(“BINPREF”)
[1] “”

And the second one:

readLines("~/.Rprofile")
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘C:/Users/laura/OneDrive/Documenten/.Rprofile’: No such file or directory

If you restart your R session and run the example model:

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

What error do you get? If you’re still getting an error now that the .Rprofile file has been deleted then there is a different issue to be fixed