Running rstan with latest R (3.6.0) fails with Error in compileCode

I had the same problem today, after switching to R 3.6.0

The output of Sys.which(“g++”) was (and still is):
g++
“”

So I changed the contents of Makevars.win from

CXX14FLAGS=-O3 -march=native
CXX14 = g++ -m$(WIN) -std=c++1y
CXX11FLAGS=-O3 -march=native

to

CXX14FLAGS=-O3 -march=native
CXX14 = “C:\Rtools\mingw_64\bin\g++.exe” -m$(WIN) -std=c++1y
CXX11FLAGS=-O3 -march=native

and then it worked as before. Does that make sense?

hello, I encontered the same problem. and I have done what you recommeded in your first reply and then I still had the same problem decribed as:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! sh: g++: command not found
make: *** [C:/PROGRA~1/R/R-36~1.0/etc/x64/Makeconf:215: file4108395342f8.o] Error 127
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-36~1.0/bin/x64/R CMD SHLIB file4108395342f8.cpp 2> file4108395342f8.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

and my Sys.which(“g++”) says:
g++
“”
And then I add "CXX14=(BINPREF)g++ -O2 -march=native -mtune=native" to the "Makevars.win". Now the file contains the following statements: CXX14FLAGS=-O3 -march=native CXX14 = g++ -m(WIN) -std=c++1y
CXX11FLAGS=-O3 -march=native
CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=native -mtune=native
CXX14=$(BINPREF)g++ -O2 -march=native -mtune=native

I am not sure whether the file is right now. luckily, “fit<-stan(……)” works now.

Same obstacle: Mac 10.14.5, R 3.6.0, Rstudio 1.2.1335, and Stan 1.18.1 produces that compilation error (below). I updated Xtools, followed instructions carefully: remove.packages(“rstan”), restart R, pkgbuild::has_build_tools(debug = TRUE) returns TRUE.
Alas.
BUT because I am an domain-specific analyst instead of an IT guru, please allow me to politely refuse to make incomprehensible command line alterations to OS variables. My solution is to try Stan via Rstudio in the Cloud and Stan via Nextjournal.com. As opposed to just waiting in sure and certain hope of the Resurrection of Stan to eternal life.

Error in compileCode(f, code, language = language, verbose = verbose) : Compilation ERROR, function(s)/method(s) not created! clang: warning: no such sysroot directory: ‘/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk’ [-Wmissing-sysroot] In file included from file81626ee3f5d9.cpp:8: In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4: In file included from …

Is there a current intro to what goes into a successful Makevars file for brms and Stan?

I seem to have a similar issue on Windows 10. I’m pretty sure I last fitted a model a week or so ago using brms in R 3.5.3. I upgraded to R 3.6.0, and now I have problems:

R version 3.6.0 (2019-04-26) -- "Planting of a Tree"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> setwd('<localpathname>')
> library(brms)
Loading required package: Rcpp
Registered S3 method overwritten by 'xts':
  method     from
  as.zoo.xts zoo 
Loading 'brms' package (version 2.9.0). Useful instructions
can be found by typing help('brms'). A more detailed introduction
to the package is available through vignette('brms_overview').
> example(brm,run.dontrun = TRUE)

brm> # Poisson regression for the number of seizures in epileptic patients
brm> # using student_t priors for population-level effects
brm> # and half cauchy priors for standard deviations of group-level effects
brm> bprior1 <- prior(student_t(5,0,10), class = b) +
brm+   prior(cauchy(0,2), class = sd)

brm> fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
brm+             data = epilepsy, family = poisson(), prior = bprior1)
Compiling the C++ model
Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! In file included from c:/R/R-3.6.0/library/BH/include/boost/random/detail/integer_log2.hpp:19:0,
                 from c:/R/R-3.6.0/library/BH/include/boost/random/detail/int_float_pair.hpp:26,
                 from c:/R/R-3.6.0/library/BH/include/boost/random/exponential_distribution.hpp:27,
                 from c:/R/R-3.6.0/library/BH/include/boost/random/gamma_distribution.hpp:25,
                 from c:/R/R-3.6.0/library/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_rng.hpp:5,
                 from c:/R/R-3.6.0/library/StanHeaders/include/stan/math/prim/mat.hpp:292,
                 from c:/R/R-3.6.0/library/StanHeaders/include/stan/math/rev/mat.hpp:12,
                 from c:/R/R-3.6.0/library/StanHeaders/include/stan/math.hpp:4,
                 from c:/R/R-3.6.0/library/StanHeaders/include/src/stan/model/model_header.hpp:4,
                 from file55ec5a816343.cpp:8:
c:/R/R-3.6.0/library/BH/include/boos
Error in sink(type = "output") : invalid connection
> 

I’ve searched for Makevar.win help, and I’ve tried several alternatives, starting with the one created programatically by the snippet on Stan’s R interface page. Here’s the version that created the above trace:

CXX14 = g++ -std=c++1y

CXX14FLAGS=-O3 -Wno-unused-variable -Wno-unused-function
CXX11FLAGS=-O3

Here is a traceback:

> traceback()
21: sink(type = "output")
20: cxxfunctionplus(signature(), body = paste(" return Rcpp::wrap(\"", 
        model_name, "\");", sep = ""), includes = inc, plugin = "rstan", 
        save_dso = save_dso | auto_write, module_name = paste("stan_fit4", 
            model_cppname, "_mod", sep = ""), verbose = verbose)
19: .fun(model_code = .x1) at <text>#1
18: eval(expr, envir, ...)
17: eval(expr, envir, ...)
16: eval2(call, envir = args, enclos = parent.frame())
15: do_call(rstan::stan_model, stan_model_args)
14: eval(expr, envir)
13: eval(expr, envir)
12: eval(expr, pf)
11: eval(expr, pf)
10: withVisible(eval(expr, pf))
9: evalVis(expr)
8: utils::capture.output(out <- eval(expr, envir), type = type, 
       ...)
7: eval_silent(do_call(rstan::stan_model, stan_model_args), silent = silence_stan_model, 
       type = "message")
6: brm(count ~ zAge + zBase * Trt + (1 | patient), data = epilepsy, 
       family = poisson(), prior = bprior1) at Rex3c0c7e5b6545#14
5: eval(ei, envir)
4: eval(ei, envir)
3: withVisible(eval(ei, envir))
2: source(tf, local, echo = echo, prompt.echo = paste0(prompt.prefix, 
       getOption("prompt")), continue.echo = paste0(prompt.prefix, 
       getOption("continue")), verbose = verbose, max.deparse.length = Inf, 
       encoding = "UTF-8", skip.echo = skips, keep.source = TRUE)
1: example(brm, run.dontrun = TRUE)
> 

Here’s my sessionInfo():

> sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

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    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] brms_2.9.0 Rcpp_1.0.1

loaded via a namespace (and not attached):
 [1] mvtnorm_1.0-11        lattice_0.20-38       prettyunits_1.0.2    
 [4] ps_1.3.0              zoo_1.8-6             gtools_3.8.1         
 [7] assertthat_0.2.1      digest_0.6.19         mime_0.7             
[10] R6_2.4.0              plyr_1.8.4            backports_1.1.4      
[13] ggridges_0.5.1        stats4_3.6.0          coda_0.19-2          
[16] ggplot2_3.2.0         colourpicker_1.0      pillar_1.4.2         
[19] rlang_0.4.0           lazyeval_0.2.2        miniUI_0.1.1.1       
[22] callr_3.2.0           Matrix_1.2-17         DT_0.7               
[25] shinythemes_1.1.2     shinyjs_1.0           stringr_1.4.0        
[28] htmlwidgets_1.3       loo_2.1.0             igraph_1.2.4.1       
[31] munsell_0.5.0         shiny_1.3.2           compiler_3.6.0       
[34] httpuv_1.5.1          rstan_2.18.2          pkgconfig_2.0.2      
[37] base64enc_0.1-3       pkgbuild_1.0.3        rstantools_1.5.1     
[40] htmltools_0.3.6       tidyselect_0.2.5      tibble_2.1.3         
[43] gridExtra_2.3         threejs_0.3.1         matrixStats_0.54.0   
[46] withr_2.1.2           crayon_1.3.4          dplyr_0.8.2          
[49] later_0.8.0           grid_3.6.0            nlme_3.1-140         
[52] xtable_1.8-4          gtable_0.3.0          magrittr_1.5         
[55] StanHeaders_2.18.1-10 scales_1.0.0          cli_1.1.0            
[58] stringi_1.4.3         reshape2_1.4.3        promises_1.0.1       
[61] dygraphs_1.1.1.6      xts_0.11-2            tools_3.6.0          
[64] glue_1.3.1            shinystan_2.5.0       markdown_1.0         
[67] purrr_0.3.2           crosstalk_1.0.0       processx_3.3.1       
[70] rsconnect_0.8.13      abind_1.4-5           parallel_3.6.0       
[73] inline_0.3.15         colorspace_1.4-1      bridgesampling_0.6-0 
[76] bayesplot_1.7.0       Brobdingnag_1.2-6    
> 

I’m running in ESS version 18.10.2 on GNU Emacs version 26.1.

UPDATE: I am running Rtools version 3.4.0.1964. Should I consider 3.5.0?

I don’t know much about C++ or what should go into a Makevars file.

Is there anything I can look at that might help you help me get brms working again?

More information:

> Sys.getenv("R_MAKEVARS_USER")
[1] ""
> 

I suspect that means R is not picking up my Makevars.win. It’s sitting in a directory .R in a directory which has a UNC with “Home$” as the Resource entry (as in \ComputerName\SharedFolder\Resource). Emacs thinks Home (~/) is c:/Users/myname/AppData/Roaming. Could that be important?

Also,

pkgbuild::has_build_tools(debug = TRUE)
Scanning R CMD config CC...
cc_path: c:/Rtools/mingw_64/bin/gcc 
install_path: c:/Rtools 
VERSION.txt
Rtools version 3.4.0.1964 
Found compatible gcc from R CMD config CC
[1] TRUE
>

Does that make it sound like Rtools 3.4.0 is perfectly fine?

I did find Failing to install rstan 2.18 on Win10 x64, which offers some advice that worked for some but seems to contradict https://cran.r-project.org/doc/manuals/R-admin.html#The-Windows-toolset as to what Rtools info you put in Windows Path.

A colleague, Meichun Lan, got it to work on her system yesterday, and her solution worked for me, too.

I changed my Makevars.win file to

CXX14=$(BINPREF)g++ -O2 -march=native -mtune=native
CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=native -mtune=native

and that was all it took! Sys.getenv("R_MAKEVARS_USER") still returns an empty string.

I’m having trouble following these fixes - where do I find the makevars.win file and how do I modify it? I’m running R 3.60 on 64 bit Win 10, and getting similar errors

I found makevars.win in the nimble package and all of the Rcpp packages and inserted the suggested text, but this does not help.

Paul, I was afraid someone would ask that question, and I don’t have a great answer.

If I do C-x d ~/ in Emacs, which I think should get me my HOME directory, I get c:/Users/myusername/AppData/Roaming, which does not seem to be the correct directory.

If I open https://mc-stan.org, select Installation, Rstan, and then the RStan Quick Start Guide, I see a section called Configuration of the C++ Toolchain. That has R code that creates a Makevars.win file in the appropriate directory.

The first thing to draw from that is the command file.path(Sys.getenv("HOME"), ".R") tells me where R thinks my HOME directory is, and that seems to work for me.

The second thing to draw from that is that what it puts in the file and what I’m using are different. Meichun found what I’m now using somewhere on Discourse, but I don’t have a link to give you. What’s worse, since I’m not a C++ programmer, nor do I have the time right now to become one, I can’t really make any arguments about why my file works or should work or why it should be better or worse than that recommended on the rstan Installation page.

If it makes you feel any better, I found this in a separate discussion:

“Calling install.packages(“rstan”, dependencies = TRUE) has rarely thrown an error and then doing library(rstan) allows it to sample from models that have already been compiled. Nor do the toolchain installers fail to install a toolchain. The primary problem has been the configuration step to use an installed toolchain among people who have never compiled code before, and even that is largely confined to Windows.” (emphasis mine)

I think that’s where my problem was, and I don’t know why it happened now; I seem to recall earlier installations working without incident.

I suspect what you found is a Makevars.win file that nimble will install into the correct location, so editing it won’t do much unless you force nimble to reinstall itself (I don’t know what nimble is or does, so I can’t help you there). See if you can find the installed Makevars.win using my instructions from a few minutes ago and then edit that with your favorite editor.

Thanks, I followed your advice and found the file in the .R directory and that seems to have worked! I can barely spell R let alone C++ :)

@Bill_Harris_2 I have tried to modify the Makevars.win file, but it does not seem to fix my problem. This is also the solution I saw in several other threads. I don’t have a good grasp of what the problem is in order to try to fix it myself. Are there any other ideas? Should I re-install R/RTools?

IT at work upgraded my R to 3.6.1 and deleted my old copy of Stan so I had to re-install and now it doesn’t work. I also have RTools 3.5.0.4 and rstan 2.19.2.

You should not need a ~/.R/Makevars or a ~/.R/Makevars.win file at all on Windows, so you can just delete them, at which point

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

should work.

@bgoodri Can I recommend that on the “RStan Getting Started” page, after the section " Checking the C++ Toolchain" and before the “Configuration of the C++ Toolchain” section, it might make sense to add an optional section that’s something like “Check rstan (Optional)” and then has the recommendation to try that.

2 Likes

I think I have the same problem. The makevars and makevars.win files I found in the skeleton folder only had comments no functional code, but the suggestion in the comment was to include CXX_STD = CXX11. The makemars.win file in the sys forder contained the code below. Should I delete the file below? make the change recommended in the files in skeleton in the sys file? I am reluctant to change files that I don’t understand. Do these files affect anything beyond R and Stan? Suggestions would really be appreciated!

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

CXX_STD = CXX14

all: $(SHLIB)

clean:
RM -rf *.so *.o

.phony: all clean

That should work

Unfortunately every time I try to run one of the stan vignettes from model <- stan_demo()

I get the same error msg:

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! g++.exe: error: Files/R/R-3.6.0/library/RcppEigen/include: No such file or directory
make: *** [C:/PROGRA~1/R/R-36~1.0/etc/x64/Makeconf:215: file387035863141.o] Error 1
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-36~1.0/bin/x64/R CMD SHLIB file387035863141.cpp 2> file387035863141.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

I’ve tried editing the Makevars file in this path C:\Users\lynda\Documents\R\win-library\3.6\rstantools\include\sys\Makevars.win adding various of the lines of suggestions offered in this thread. But I get the same error each time.

I don’t understand the syntax in the Makevars file, nor do I know how to investigate where presumably a directory connection is going wrong. I am working in Windows 10.

It looks like you need to get StanHeaders and rstan back in harmony with

remove.packages(c("StanHeaders", "rstan"))
file.remove(".RData")
# restart R
install.packages(c("StanHeaders", "rstan"), type = "source")

@bgoodri Thank you! yes this solved my problem.

I’m getting a similar error. I’m using R 64-bit 3.6.1 on Windows 10. I tried reinstalling rstan (version 2.19.3) but I still have the same error.

Here’s the error I get:

example(stan_model, package = “rstan”, run.dontrun = TRUE)

Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! g++.exe: error: Files/R/R-3.6.1/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp: No such file or directory
g++.exe: error: Files/R/R-3.6.1/library/StanHeaders/include: No such file or directory
g++.exe: error: Files/R/R-3.6.1/library/RcppEigen/include: No such file or directory
make: *** [C:/PROGRA~1/R/R-36~1.1/etc/x64/Makeconf:215: file8dc1eea140a.o] Error 1

The contents of my Makevars.win file are:

CXX14FLAGS=-O3 -march=corei7 -mtune=corei7
CXX11FLAGS=-O3 -march=corei7 -mtune=corei7

I tried setting CXX14= and tried deleting the file (per previous suggestions in this thread), but still got the same error.