Error in compiling rstan, rstudio keeps asking to install Rtools

I would like to use brms package for Bayesian mixed model in R. I installed, brms, rstan, rstanarm and StanHeaders packages in Rstudio. If I run the model, window pops up suggesting to install Rtools although it is already installed. I put Rtools on the first order in Path in Environmental Variables. If I press No button for Rtools installation pop -up window, following error message comes out.
Compiling the C++ model
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! sh: c:/Rtools/mingw_64/bin/g++: No such file or directory
make: *** [C:/PROGRA~1/R/R-36~1.2/etc/x64/Makeconf:215: file69286a911ec5.o] Error 127
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-36~1.2/bin/x64/R CMD SHLIB file69286a911ec5.cpp 2> file69286a911ec5.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

Thanks in advance for help.

  • Session info
    setting value
    version R version 3.6.2 (2019-12-12)
    os Windows >= 8 x64
    system x86_64, mingw32
    ui RStudio

Can you do

library(rstan)
example(stan_model, run.dontrun = TRUE)

and tell us the part that has the string error: (with the colon)?

Thank you bgoodri, there is no error: (error with colon), but the last message was this:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! sh: c:/Rtools/mingw_64/bin/g++: No such file or directory
make: *** [C:/PROGRA~1/R/R-36~1.2/etc/x64/Makeconf:215: file6928439b4ad2.o] Error 127

Is there a Rtools directory under c:/ that has a mingw_64 subfolder?

There is RBuildTools folder and mingw_64 subfolder. Thank you

What is the content of your ~/.R/Makevars.win file?

It is
CXX14FLAGS=-O3 -march=corei7 -mtune=corei7
CXX14 = (BINPREF)g++ -m(WIN) -std=c++1y
CXX11FLAGS=-O3 -march=corei7 -mtune=corei7

There should be dollar signs before the left parentheses, like

CXX14 = $(BINPREF)g++ -m$(WIN) -std=c++1y

Do you also have BINPREF set in ~/.Rprofile ? If so, you can probably delete it.

Thank you, changed this but getting the same error. Where is ~/.Rprofile located?

I’m having a similar issue. Here’s what I’ve done.

File .Rprofile in C:\Users\MyUser:

Sys.setenv(BINPREF="C:/RBuildTools/3.5/mingw_64/bin")
Sys.setenv(CXX14FLAGS="-O3")
Sys.setenv(CXX="($BINPREF)g++ -m$(WIN) -std=c++14")
Sys.setenv(CXX14="($BINPREF)g++ -m$(WIN) -std=c++14")
Sys.setenv(CXX11FLAGS="-O3")

Launch R.

> Sys.getenv("CXX")
[1] "($BINPREF)g++ -m$(WIN) -std=c++14"
> Sys.getenv("BINPREF")
[1] "C:/RBuildTools/3.5/mingw_64/bin"
> 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
Warning messages:
1: package ‘rstan’ was built under R version 3.6.3 
2: package ‘StanHeaders’ was built under R version 3.6.3 
3: package ‘ggplot2’ was built under R version 3.6.3 
> example(stan_model, run.dontrun = TRUE)

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'.
Error in open.connection(con, open = mode) : 
  Timeout was reached: [github.com] Connection timed out after 10003 milliseconds
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/cygwin64/mingw_/bin/g++' not found
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

Never mind the github.com access error (probably proxy related). But it keeps using C:/cygwin64/mingw_/bin/g++. Why?

Seems to be discussed here.