Stan() function does not work after updating to R 4.0.3

Start with a short summary of your inquiry.

I recently updated R 3.6.3 to R 4.0.3. I updated all packages including rstan. When I tried to fit a model with stan() function, error messages appear. R tools 4.0 was also automatically updated.

You can see the error message, below.

Error in compileCode(f, code, language = language, verbose = verbose) : sh: c:/rtools40/mingw64/bin/g++: No such file or directorymake: *** [C:/PROGRA~1/R/R-40~1.3/etc/x64/Makeconf:229: file131481e681912.o] Error 127 In addition: Warning message: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) : '-E' not found Error in sink(type = "output") : invalid connection

My session info is below.

sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
system code page: 949

How may I solve this error? Could you help me to solve this?
Thank you for your time and consideration!

@bgoodri Could you maybe help me solve this issue, please?

Hi Ihnwhi,

Can you try following the instructions here for installing RStan under R4.0: https://github.com/stan-dev/rstan/wiki/Installing-RStan-from-source-on-Windows#r40

1 Like

Hi @andrjohns, thank you for your quick reply! I will try right away and tell you the results!

Hi @andrjohns,
I have a quick question.
Should Sys.which("make") give me "C:\\rtools40\\usr\\bin\\make.exe as an output? Since it gives me an empty "" output.

Yep, it should. Did you restart R after running the writeLines command? Otherwise R won’t pick up the new RTools location

Hmm, then I do not understand why it gives me an empty output. I did restart R after running writeLines command.

Can you post the outputs from:

Sys.getenv("PATH")
Sys.getenv("BINPREF")
Sys.getenv("RTOOLS40_HOME")
readLines("~/.R/Makevars.win")
readLines("~/.Rprofile")
readLines("~/.Renviron")
devtools::session_info("rstan")

It gives me the below output.

Sys.getenv(“PATH”)
[1] “\usr\bin;C:\Program Files\R\R-4.0.3\bin\x64;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\;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(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\Mplus Demo\;C:\Program Files\Git\cmd;C:\Users\Ihn-Whi Heo\AppData\Roaming\TinyTeX\bin\win32;C:\Users\Ihn-Whi Heo\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64\;C:\Users\Ihn-Whi Heo\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Users\Ihn-Whi Heo\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Ihn-Whi Heo\AppData\Local\GitHubDesktop\bin;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;”
Sys.getenv(“BINPREF”)
[1] “”
Sys.getenv(“RTOOLS40_HOME”)
[1] “”
readLines(“~/.R/Makevars.win”)
[1] “CXX14FLAGS += -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2”
readLines(“~/.Rprofile”)
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘C:/Users/Ihn-Whi Heo/Documents/.Rprofile’: No such file or directory
readLines(“~/.Renviron”)
[1] "PATH=\"${RTOOLS40_HOME}\\usr\\bin;${PATH}\""
devtools::session_info(“rstan”)
Error in loadNamespace(name) : there is no package called ‘devtools’

It looks like the issue is that R can’t find the RTools4 installation, what directory is it installed in?

I downloaded directly at C: (with the folder name rtools40)!

What output do you get from:

list.dirs(path="C:/rtools40")

It gives a very long output (1000 lines), I cannot even see the first row. Still, the output looks like:

[2] “C:/rtools40/clang32”
[3] “C:/rtools40/clang32/bin”
[4] “C:/rtools40/clang32/etc”
[5] “C:/rtools40/clang32/include”
[6] “C:/rtools40/clang32/lib”
[7] “C:/rtools40/clang32/share”
[8] “C:/rtools40/clang64”
[9] “C:/rtools40/clang64/bin”
[10] “C:/rtools40/clang64/etc”
[11] “C:/rtools40/clang64/include”
[12] “C:/rtools40/clang64/lib”
[13] “C:/rtools40/clang64/share”
[14] “C:/rtools40/dev”
[15] “C:/rtools40/dev/mqueue”
[16] “C:/rtools40/dev/shm”
[17] “C:/rtools40/etc”

Can you try reinstalling RTools4, and then restart R and run:

Sys.getenv("RTOOLS40_HOME")

I removed the existing RTools4, reinstalled it, restarted R, and ran the code. The output is like:

Sys.getenv(“RTOOLS40_HOME”)
[1] “C:\rtools40”

What can I do, next?
Thanks, Andrew!

Thats a great sign! What happens when you run:

library(rstan)
example(stan_model,run.dontrun=T)
1 Like

When I ran the code, it gives:

library(rstan)
Error in library(rstan) : there is no package called ‘rstan’
example(stan_model,run.dontrun=T)
Warning message:
In example(stan_model, run.dontrun = T) : no help found for ‘stan_model’

Is it right? Since, I think I installed rstan…?

It might not have installed correctly, just try installing it again and see how it goes

1 Like

It took quite a long time to download rstan. When tried your code, the results are:

library(rstan)
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.21.2, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
Do not specify ‘-march=native’ in ‘LOCAL_CPPFLAGS’ or a Makevars file
example(stan_model,run.dontrun=T)

stn_md> stancode ← ‘data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}’

stn_md> mod ← stan_model(model_code = stancode, verbose = TRUE)

TRANSLATING MODEL ‘73fc79f8b1915e8208c736914c86d1a1’ FROM Stan CODE TO C++ CODE NOW.
successful in parsing the Stan model ‘73fc79f8b1915e8208c736914c86d1a1’.
COMPILING THE C++ CODE FOR MODEL ‘73fc79f8b1915e8208c736914c86d1a1’ NOW.
OS: x86_64, mingw32; rstan: 2.21.2; Rcpp: 1.0.5; inline: 0.3.16
Error in cleanup_makevar(old) :
argument “RMU” is missing, with no default
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/rtools40/usr/mingw_/bin/g++’ not found

Rstan is currently having some issues with Makevars files, try running:

file.rename("~/.R/Makevars.win", "~/.R/Makevars.win.bak")

And then restart R and try again