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?