Problems compiling rstan example

I’ve tried to install rstan and rstanHeaders from source. I’m working from an environment where I can’t reinstall R and Rtools (not installed in C: but in P:), but they are installed and should (?) work according to ICT. Unfortunately I am bound to this workspace. Tried a whole bunch of things, but I keep running into error 127.

When I run:

rstan_options(auto_write = TRUE)
example(stan_model, package = "rstan", run.dontrun = TRUE)

Error:
Error in compileCode(f, code, language = language, verbose = verbose) :
/bin/sh: line 1: P:/Rtools/mingw_64/bin/g++.exe: No such file or directorymake: *** [P:/R/R-4.2.1/etc/x64/Makeconf:260: file35ac5b705c5.o] Error 127
13.
stop(errmsg)
12.
compileCode(f, code, language = language, verbose = verbose)
11.
cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes,
settings = settings, …, verbose = verbose)
10.
force(code)
9.
withr_with_path(rtools_path(), code)
8.
pkgbuild::with_build_tools(cxxfunction(sig = sig, body = body,
plugin = plugin, includes = includes, settings = settings,
…, verbose = verbose), required = rstan_options(“required”) &&
!identical(Sys.getenv(“WINDOWS”), “TRUE”) && !identical(Sys.getenv(“R_PACKAGE_SOURCE”), …
7.
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)
6.
stan_model(model_code = stancode, verbose = TRUE) at C:\Users\ME\AppData\Local\Temp\RtmpspabCA\Rex35ac54f17db8#9
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(stan_model, package = “rstan”, run.dontrun = TRUE)

  • Operating System
  • RStan Version
  • Output of writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars")))
    Error in file(con, “r”) : cannot open the connection

Relatedly running:

pkgbuild::check_build_tools(debug = TRUE)
pkgbuild::check_compiler(debug = TRUE)

Gives:
Scanning R CMD config CC…
cc_path: P:/Rtools/mingw_64/bin/gcc
‘P:/Rtools/mingw_64/bin/gcc’ does not exist
Scanning path…
ls: P:\rtools\usr\bin\ls.exe
gcc_path:
‘P:/rtools/usr/mingw_64/bin/gcc.exe’ does not exist
Scanning registry…
Found P:/rtools for 4.2
Your system is ready to build packages!
Trying to compile a simple C file
Running “P:/R/R-4.2.1/bin/x64/Rcmd.exe” SHLIB foo.c
/ME/My Documents/R/win-library/4.2/StanHeaders/include/stan/math/prim/fun/Eigen.hpp" -std=c++1y -I"c:/rtools42/x86_64-w64-mingw32.static.posix/include" -O2 -Wall -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c foo.c -o foo.onclude/" -I"\storage.WORK.nl/m/MyDocs/ME/My Documents/R/win-library/4.2/RcppEigen/include/unsupported" -I"\storage.WORK.nl/m/MyDocs/ME/My Documents/R/win-library/4.2/BH/include" -I"\storage.WORK.nl/m/MyDocs/ME/My Documents/R/win-library/4.2/StanHeaders/include/src/" -I"\storage.WORK.nl/m/MyDocs/ME/My Documents/R/win-library/4.2/StanHeaders/include/" -I"\storage.WORK.nl/m/MyDocs/ME/My Documents/R/win-library/4.2/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -I"P:/R/R-4.2.1/library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -DBOOST_NO_AUTO_PTR -include "\storage.WORK.nl/m/MyDocs
/bin/sh: line 1: P:/Rtools/mingw_64/bin/gcc: No such file or directory
make: *** [P:/R/R-4.2.1/etc/x64/Makeconf:253: foo.o] Error 127

Error: Failed to compile C code
2.
stop(“Failed to compile C code”, call. = FALSE)
1.
pkgbuild::check_compiler(debug = TRUE)

  • Output of devtools::session_info("rstan")
    Session info ────────────────────────────────────────────────────────────────────────────────────────────────────────
    setting value
    version R version 4.2.1 (2022-06-23 ucrt)
    os Windows 10 x64 (build 19045)
    system x86_64, mingw32
    ui RStudio
    language (EN)
    collate English_United States.utf8
    ctype English_United States.utf8
    tz Europe/Berlin
    date 2023-10-06
    rstudio 2022.07.2+576 Spotted Wakerobin (desktop)
    pandoc NA

─ Packages ────────────────────────────────────────────────────────────────────────────────────────────────────────────
! package * version date (UTC) lib source
backports 1.4.1 2021-12-13 [1] CRAN (R 4.3.0)
BH 1.81.0-1 2023-01-22 [1] CRAN (R 4.3.0)
callr 3.7.3 2022-11-02 [1] CRAN (R 4.3.1)
checkmate 2.2.0 2023-04-27 [1] CRAN (R 4.3.1)
cli 3.6.1 2023-03-23 [1] CRAN (R 4.3.1)
colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.3.1)
crayon 1.5.2 2022-09-29 [1] CRAN (R 4.3.1)
desc 1.4.2 2022-09-08 [1] CRAN (R 4.3.1)
fansi 1.0.4 2023-01-22 [1] CRAN (R 4.3.1)
farver 2.1.1 2022-07-06 [1] CRAN (R 4.3.1)
ggplot2 * 3.4.3 2023-08-14 [1] CRAN (R 4.3.1)
glue 1.6.2 2022-02-24 [1] CRAN (R 4.3.1)
gridExtra 2.3 2017-09-09 [1] CRAN (R 4.3.1)
gtable 0.3.4 2023-08-21 [1] CRAN (R 4.3.1)
inline 0.3.19 2021-05-31 [1] CRAN (R 4.3.1)
isoband 0.2.7 2022-12-20 [1] CRAN (R 4.3.1)
jsonlite 1.8.7 2023-06-29 [1] CRAN (R 4.3.1)
labeling 0.4.3 2023-08-29 [1] CRAN (R 4.3.1)
lattice 0.21-8 2023-04-05 [2] CRAN (R 4.2.3)
lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.3.1)
loo 2.6.0 2023-03-31 [1] CRAN (R 4.3.1)
magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.1)
MASS 7.3-60 2023-05-04 [2] CRAN (R 4.2.3)
Matrix 1.5-4.1 2023-05-18 [2] CRAN (R 4.2.3)
matrixStats 1.0.0 2023-06-02 [1] CRAN (R 4.3.1)
mgcv 1.8-42 2023-03-02 [2] CRAN (R 4.2.3)
munsell 0.5.0 2018-06-12 [1] CRAN (R 4.3.1)
nlme 3.1-162 2023-01-31 [2] CRAN (R 4.2.3)
pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.1)
pkgbuild 1.4.2 2023-06-26 [1] CRAN (R 4.3.1)
pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.1)
prettyunits 1.2.0 2023-09-24 [1] CRAN (R 4.3.1)
processx 3.8.2 2023-06-30 [1] CRAN (R 4.3.1)
ps 1.7.5 2023-04-18 [1] CRAN (R 4.3.1)
QuickJSR 1.0.6 2023-09-12 [1] CRAN (R 4.3.1)
R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.1)
RColorBrewer 1.1-3 2022-04-03 [1] CRAN (R 4.3.0)
Rcpp * 1.0.11 2023-07-06 [1] CRAN (R 4.3.1)
RcppEigen 0.3.3.9.3 2022-11-05 [1] CRAN (R 4.3.1)
D RcppParallel 5.1.7 2023-02-27 [1] CRAN (R 4.3.1)
rlang 1.1.1 2023-04-28 [1] CRAN (R 4.3.1)
rprojroot 2.0.3 2022-04-02 [1] CRAN (R 4.3.1)
rstan * 2.26.23 [2]
scales 1.2.1 2022-08-20 [1] CRAN (R 4.3.1)
StanHeaders * 2.26.28 2023-09-07 [1] CRAN (R 4.2.3)
tibble * 3.2.1 2023-03-20 [1] CRAN (R 4.3.1)
utf8 1.2.3 2023-01-31 [1] CRAN (R 4.3.1)
vctrs 0.6.3 2023-06-14 [1] CRAN (R 4.3.1)
viridisLite 0.4.2 2023-05-02 [1] CRAN (R 4.3.1)
withr 2.5.1 2023-09-26 [1] CRAN (R 4.3.1)

[1] \storage.WORK.nl/m/MyDocs/ME/My Documents/R/win-library/4.2
[2] P:/R/R-4.2.1/library

I censored my workplace. Everything runs absolutely fine on my own desktop where I can do clean installs, but I’m not allowed to access the data there… So now I’m stuck.

I know there’s lots of similar post, but I’ve just not been able to figure it out on my own. Any help would be very welcome.

Your toolchain is configured for R3.x, see this section of the wiki for steps on removing this configuration: Configuring C Toolchain for Windows · stan-dev/rstan Wiki · GitHub

1 Like

Also, make sure you don’t follow the further steps which tell you to install Rtools40, that’s not needed for you

Sys.getenv(“BINPREF”)

gives

[1] “”

So that should be ok right?

What output do you get from:

readLines("~/.Rprofile")
readLines ("~/.Renviron")
readLines ("~/.R/Makevars.win")

Error in file(con, “r”) : cannot open the connection
Error in file(con, “r”) : cannot open the connection
[1] “CXX14FLAGS += -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2”
[2] “CXX14 = g++ -std=c++1y”
[3] “CXX14FLAGS = -O3”
[4] “CXX14 = "P:/Rtools/mingw64/bin/g++.exe"”
[5] “BINPREF = "P:/Rtools/mingw64/bin/"”
[6] “”

Looks like more old config in your Makevars.win file, try removing everything except for the first line

gives:
Error in file(con, “r”) : cannot open the connection
Error in file(con, “r”) : cannot open the connection
[1] “CXX14FLAGS += -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2”
[2] “”

example model:
[long output 423 lines]
Error in compileCode(f, code, language = language, verbose = verbose) :
/bin/sh: line 1: g++: command not foundmake: *** [P:/R/R-4.2.1/etc/x64/Makeconf:260: file3be8438f4082.o] Error 127
13.
stop(errmsg)
12.
compileCode(f, code, language = language, verbose = verbose)
11.
cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes,
settings = settings, …, verbose = verbose)
10.
force(code)
9.
withr_with_path(rtools_path(), code)
8.
pkgbuild::with_build_tools(cxxfunction(sig = sig, body = body,
plugin = plugin, includes = includes, settings = settings,
…, verbose = verbose), required = rstan_options(“required”) &&
!identical(Sys.getenv(“WINDOWS”), “TRUE”) && !identical(Sys.getenv(“R_PACKAGE_SOURCE”), …
7.
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)
6.
stan_model(model_code = stancode, verbose = TRUE) at C:\Users\ME\AppData\Local\Temp\RtmpaeqYem\Rex3be842111c82#9
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(stan_model, package = “rstan”, run.dontrun = TRUE)

Now you need to tell R where your toolchain is, since you’re using non-standard install directories.

Run:

write('PATH="${RTOOLS42_HOME}\\usr\\bin;${PATH}"', file = "~/.Renviron", append = TRUE)

Then restart R and try again

Error in file(con, "r") : cannot open the connection
`[1] "PATH=\"${RTOOLS42_HOME}\\usr\\bin;${PATH}\""`
[1] "CXX14FLAGS += -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2"
[2] ""

then still the example model same error.

Hmm, are you able to install any R packages from source? For example:

install.packages("Rcpp", type="source")

And what output do you get from:

Sys.getenv("PATH")

Fails to install from source.

install.packages("Rcpp", type = "source")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘\storage.WORK.nl/m/MyDocs/ME/My Documents/R/win-library/4.2’
(as ‘lib’ is unspecified)
trying URL ‘https://cran.rstudio.com/src/contrib/Rcpp_1.0.11.tar.gz
Content type ‘application/x-gzip’ length 2994004 bytes (2.9 MB)
downloaded 2.9 MB

Loading required package: usethis
Warning messages:
1: package ‘devtools’ was built under R version 4.2.3
2: package ‘usethis’ was built under R version 4.2.3

  • installing source package ‘Rcpp’ …
    ** package ‘Rcpp’ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** libs
    Warning in system(cmd) : ‘make’ not found
    ERROR: compilation failed for package ‘Rcpp’
  • removing ‘\storage.WORK.nl/m/MyDocs/ME/My Documents/R/win-library/4.2/Rcpp’
    Warning in install.packages :
    installation of package ‘Rcpp’ had non-zero exit status

The downloaded source packages are in
‘C:\Users\ME\AppData\Local\Temp\RtmpCyWLmC\downloaded_packages’

install.packages(“Rcpp”, type = “source”)
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘\storage.WORK.nl/m/MyDocs/ME/My Documents/R/win-library/4.2’
(as ‘lib’ is unspecified)
trying URL ‘https://cran.rstudio.com/src/contrib/Rcpp_1.0.11.tar.gz
Content type ‘application/x-gzip’ length 2994004 bytes (2.9 MB)
downloaded 2.9 MB

[1] “c:\rtools42\x86_64-w64-mingw32.static.posix\bin;c:\rtools42\usr\bin;V:\usr\bin;P:\RStudio;P:\rtools;P:\Rtools\mingw_64\bin;P:\Rtools\mingw_32\bin;P:\R\R-4.2.1\bin\x64;C:\Program Files\Common Files\TEC-IT\TBarCode\10.2;C:\Program Files (x86)\Common Files\TEC-IT\TBarCode\10.2;C:\Oracle\instantclient_19_12;C:\Program Files (x86)\Common Files\Oracle\Java\javapath_target_300968;C:\ProgramData\Oracle\Java\javapath;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files (x86)\Citrix\HDX\bin;C:\Program Files\Citrix\HDX\bin;C:\Apps\Mira\SenS;C:\Users\ME\AppData\Local\Microsoft\WindowsApps;;P:\RStudio\bin\quarto\bin;P:/RStudio/bin/quarto/bin”

Yep, looks like R still can’t find your rtools installation:

WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:

You might have to go back to your IT department for help but a couple more things to check. First, let’s see if pkgbuild can find the toolchain:

pkgbuild::has_rtools(debug=TRUE)
pkgbuild::rtools_path()

And let’s check that the version of rtools you have installed is the right one:

Sys.getenv("RTOOLS40_HOME")

[1] “”

That’s progress, I just need to double-check the folders in that rtools directory and then we can add it to your path for R to find.

What output do you get from:

list.files("P:/rtools")

Exciting :)

[1] “autorebase.bat” “clang32”
[3] “clang32.exe” “clang32.ico”
[5] “clang32.ini” “clang64”
[7] “clang64.exe” “clang64.ico”
[9] “clang64.ini” “clangarm64”
[11] “clangarm64.exe” “clangarm64.ico”
[13] “clangarm64.ini” “dev”
[15] “etc” “home”
[17] “mingw32” “mingw32.exe”
[19] “mingw32.ico” “mingw32.ini”
[21] “mingw64” “mingw64.exe”
[23] “mingw64.ico” “mingw64.ini”
[25] “msys2.exe” “msys2.ico”
[27] “msys2.ini” “msys2_shell.cmd”
[29] “opt” “tmp”
[31] “ucrt64” “ucrt64.exe”
[33] “ucrt64.ico” “ucrt64.ini”
[35] “unins000.dat” “unins000.exe”
[37] “usr” “var”
[39] “x86_64-w64-mingw32.static.posix”

Alright that all looks fine for 4.2, can you run:

writeLines('PATH="P:\\rtools\\x86_64-w64-mingw32.static.posix\\bin;P:\\rtools\\usr\\bin;${PATH}"', con = "~/.Renviron")

Then restart R and check that it can find the toolchain by running:

Sys.which("make")
                        make 

“P:\rtools\usr\bin\make.exe”

That’s a good sign! And the example model?

A different error, is that progress? :/

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) :
cc1plus.exe: fatal error: \storage.WORK.nl/m/MyDocs/ME/My Documents/R/win-library/4.2/StanHeaders/include/stan/math/prim/fun/Eigen.hpp: No such file or directorycompilation terminated.make: *** [P:/R/R-4.2.1/etc/x64/Makeconf:260: file3bd0646b64e1.o] Error 1
13.
stop(errmsg)
12.
compileCode(f, code, language = language, verbose = verbose)
11.
cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes,
settings = settings, …, verbose = verbose)
10.
force(code)
9.
withr_with_path(rtools_path(), code)
8.
pkgbuild::with_build_tools(cxxfunction(sig = sig, body = body,
plugin = plugin, includes = includes, settings = settings,
…, verbose = verbose), required = rstan_options(“required”) &&
!identical(Sys.getenv(“WINDOWS”), “TRUE”) && !identical(Sys.getenv(“R_PACKAGE_SOURCE”), …
7.
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)
6.
stan_model(model_code = stancode, verbose = TRUE) at C:\Users\ME\AppData\Local\Temp\RtmpaWcj9v\Rex3bd04e1a47df#9
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(stan_model, package = “rstan”, run.dontrun = TRUE)