When I was running the following code, R told me ’ C++14 standard requested but CXX14 is not defined’.
Sys.setenv(USE_Cxx14 = 1)
require(“rstan”) # observe startup messages
mstring = "data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
real mu;
real<lower=0> tau;
real eta[J];
}
transformed parameters {
real theta[J];
for (j in 1:J)
theta[j] = mu + tau * eta[j];
}
model {
target += normal_lpdf(eta | 0, 1);
target += normal_lpdf(y | theta, sigma);
}
"
smodel <- stan_model(model_code = mstring)
You need to put
CXX14 = g++ # or clang++ if you have that
CXX14FLAGS = -O3
into your ~/.R/Makevars file.
2 Likes
Just Now, I encountered this issue when I try to install rstan package. I cannot understand your suggestion sorry I am beginner.
Please let me ask what the following means;
" into your ~/.R/Makevars file".
In the following directory, there is not a file named Makevars.file:
C:\Program Files\R
This is quick reply to thank you. I will try.
By execute the following code, I did install the rstan package.
Thank you!!
I’m trying to troubleshoot this same error but the link you’ve posted is taking me to a blank page at the moment.
Thank you!
Just in case the link breaks in the future, here’s the relevant section of code to be run from R after having installed Rtools but before installing rstan:
dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
M <- file.path(dotR, "Makevars.win")
if (!file.exists(M)) file.create(M)
cat("\nCXX14FLAGS=-O3 -march=native",
"CXX14 = g++ -m$(WIN) -std=c++1y",
"CXX11FLAGS=-O3 -march=native",
file = M, sep = "\n", append = TRUE)
In my particular case, I also had to edit this line
"CXX14 = g++ -m$(WIN) -std=c++1y"
and replace g++
with the full path C:/Rtools/mingw_64/g++.exe
2 Likes
I uninstalled rstan, ran the code as instructed, reinstalled rstan, and the error has now changed to the following:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! file17e86c9243ea.cpp:1:0: sorry, unimplemented: 64-bit mode not compiled in
^
make: *** [C:/PROGRA~1/R/R-36~1.0/etc/x64/Makeconf:215: file17e86c9243ea.o] Error 1
In addition: Warning message:
In system(cmd, intern = !verbose) :
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! file17e86c9243ea.cpp:1:0: sorry, unimplemented: 64-bit mode not compiled in
^
make: *** [C:/PROGRA~1/R/R-36~1.0/etc/x64/Makeconf:215: file17e86c9243ea.o] Error 1
Error in sink(type = “output”) : invalid connection
Many thanks, sir. That fixed it.
Hi there!
Thanks for your suggestion. However, I still cannot make it work. Changing the code does nothing for me.
I still got this message: Error in sink(type = “output”) : invalid connection.
The Rstan 2.19.1.zip link is no longer awailable and it reports an error when you install it from the Tools bar.
This is my current Makevars.win file:
CXX14 = g++ -m$(WIN) -std=c++1y
CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=native -mtune=native
Rstudio 1.2.13
Rstan 2.18
I’ve even tried installing a different Rstudio version… No luck with that either.
Thanks in advance!
Try this link now
install.packages("https://win-builder.r-project.org/rTkumr03G8UM/rstan_2.19.1.zip", repos = NULL)
1 Like
Thank you very much for your help!!! You saved me!!
Hello. I am having difficulty completing the steps laid out in RStan Getting Started · stan-dev/rstan Wiki · GitHub (retried everything several times). I am able to verify the toolchain by successfully installing jsonlite from the source, but get the error message seen in the example when I try to install rstan. When I attempt to install rstan from the source, here is the final portion of the output…
testing if installed package keeps a record of temporary installation path
- DONE (StanHeaders)
- installing source package ‘rstan’ …
** package ‘rstan’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
*** arch - i386
Error in .shlib_internal(args) :
C++14 standard requested but CXX14 is not defined
- removing ‘C:/Users/Ann/Documents/R/win-library/3.6/rstan’
Warning in install.packages :
installation of package ‘rstan’ had non-zero exit status
I have followed the code for configuring c++ for R 3.6 from the github page (I have version 3.6.1) and I have Rstudio 1.41106. I have downloaded Rtools 3.5. My Makevars file reads:
STANHEADERS_SRC = (shell "(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e “message()” -e “cat(system.file(‘include’, ‘src’, package = ‘StanHeaders’, mustWork = TRUE))” -e “message()” | grep “StanHeaders”)
PKG_CPPFLAGS = -I"…/inst/include" -I"(STANHEADERS_SRC)" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DRCPP_PARALLEL_USE_TBB=1
PKG_CXXFLAGS = (shell “(R_HOME)/bin(R_ARCH_BIN)/Rscript” -e “RcppParallel::CxxFlags()”) (shell "(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e “StanHeaders:::CxxFlags()”)
PKG_LIBS = (shell "(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e “RcppParallel::RcppParallelLibs()”) (shell "(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e “StanHeaders:::LdFlags()”)
CXX_STD = CXX14
Please how can I define CXX14? Thanks in advance.
I just need a little more info about your system, can you post the outputs from:
Sys.getenv("BINPREF")
readLines("~/.R/Makevars.win")
readLines("~/.Rprofile")
Sys.getenv(“BINPREF”)
[1] “C:/Rtools35/mingw_$(WIN)/bin/”
readLines(“~/.R/Makevars.win”)
[1] “”
[2] " CXX14FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2"
readLines(“~/.Rprofile”)
[1] “Sys.setenv(BINPREF = "C:/Rtools35/mingw_$(WIN)/bin/")”
Thanks.
Great, we just need to edit your Makevars.win
to tell R where to find the compiler. Can you run:
cat("\n CXX14 = $(BINPREF)g++",
file = "~/.R/Makevars.win", sep = "\n", append = TRUE)
Then restart R and try the install from source again?
No dice. I uninstalled all packages (Rtools, pkgbuild, coda, mvtnorm, devtools–the last three I know are after the fact), shut down my computer reinstalled Rtools 35, reconfigured R for C++, was able to install jsonlite from the source, followed the steps for Rstan getting started, tried to install Rstan from the source, had the same error as before about the Makevars not being defined. The edits to Makevars are being registered in R but the actual Makevars file remains unchanged (unsure if it should change):
Sys.getenv(“BINPREF”)
[1] “C:/Rtools/mingw_$(WIN)/bin/”
readLines(“~/.R/Makevars.win”)
[1] “”
[2] " CXX14FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2"
[3] “”
[4] " CXX14 = (BINPREF)g++"
[5] ""
[6] " CXX14 = (BINPREF)g++"
[7] “”
[8] " CXX14 = (BINPREF)g++"
readLines("~/.Rprofile")
[1] "Sys.setenv(BINPREF = \"C:/Rtools/mingw_(WIN)/bin/")"
Happy to entertain other ideas. Thanks.