Brms no longer works

I recently tried using the brms package in R (Windows 10) for the first time in a while, and I’ve found that it no longer works.

I’ve tried it in R 4.0.3 with brms 2.14.4 and rstan 2.21.2, and also in R 3.6.3 with brms 2.10.0 and rstan 2.21.2. In both cases it didn’t work.

I know that brms was working on this computer in October 2019 with brms 2.10.0 in R 3.6.1 (don’t know what version of rstan I had).

The code I tried was from the “Overview of the brms package” vignette and is shown below, together with the output when run in R 4.0.3.

library(brms)

data(kidney)

fit1 <- brm(formula = time | cens(censored) ~ age * sex + disease +
              (1 + age|patient),
            data = kidney, family = lognormal(),
            prior = c(set_prior("normal(0,5)", class = "b"),
                      set_prior("cauchy(0,2)", class = "sd"),
                      set_prior("lkj(2)", class = "cor")),
            warmup = 1000, iter = 2000, chains = 4,
            control = list(adapt_delta = 0.95))

## Compiling Stan program...
## make cmd is
##   make -f "c:/PROGRA~1/R/R-40~1.3/etc/x64/Makeconf" -f "c:/PROGRA~1/R/R-40~1.3/share/make/winshlib.mk" CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB="file4a1c79805383.dll" WIN=64 TCLBIN=64 OBJECTS="file4a1c79805383.o"

## make would use
## Error in compileCode(f, code, language = language, verbose = verbose) : 
## In addition: Warning message:
## In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
##   '-E' not found
## Error in sink(type = "output") : invalid connection

I’ve read through some similar threads here, and I’ve tried some of the suggestions:

  • Use rstan 2.19.3 instead of the latest version.
  • Load packages from a directory with no spaces in the name.
  • remotes::install_github("bgoodri/inline").
  • Reinstall RcppParallel.

None of those fixed the problem, so I reinstalled the latest rstan and inline, and went back to my usual package directory.

I have Rtools 40 in c:/rtools40 and I have Rtools 35 in c:/Rtools.

I’ve seen a lot of discussion about a “makevars” file but I don’t seem to have one and I don’t have a good understanding of what it is.

writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars")))

## Error in file(con, "r") : cannot open the connection
## In addition: Warning message:
## In file(con, "r") :
##   cannot open file 'C:\Users\seetom/.R/Makevars': No such file or directory


> devtools::session_info("rstan")
- Session info ---------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 4.0.3 (2020-10-10)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RTerm                       
 language (EN)                        
 collate  English_Australia.1252      
 ctype    English_Australia.1252      
 tz       Australia/Sydney            
 date     2021-01-23                  

- Packages -------------------------------------------------------------------------------------------------
 ! package      * version   date       lib source        
   assertthat     0.2.1     2019-03-21 [1] CRAN (R 4.0.3)
   backports      1.2.0     2020-11-02 [1] CRAN (R 4.0.3)
   BH             1.72.0-3  2020-01-08 [1] CRAN (R 4.0.3)
   brio           1.1.0     2020-08-31 [1] CRAN (R 4.0.3)
   callr          3.5.1     2020-10-13 [1] CRAN (R 4.0.3)
   checkmate      2.0.0     2020-02-06 [1] CRAN (R 4.0.3)
   cli            2.2.0     2020-11-20 [1] CRAN (R 4.0.3)
   colorspace     2.0-0     2020-11-11 [1] CRAN (R 4.0.3)
   crayon         1.3.4     2017-09-16 [1] CRAN (R 4.0.3)
   curl           4.3       2019-12-02 [1] CRAN (R 4.0.3)
   desc           1.2.0     2018-05-01 [1] CRAN (R 4.0.3)
   diffobj        0.3.2     2020-10-05 [1] CRAN (R 4.0.3)
   digest         0.6.27    2020-10-24 [1] CRAN (R 4.0.3)
   ellipsis       0.3.1     2020-05-15 [1] CRAN (R 4.0.3)
   evaluate       0.14      2019-05-28 [1] CRAN (R 4.0.3)
   fansi          0.4.1     2020-01-08 [1] CRAN (R 4.0.3)
   farver         2.0.3     2020-01-16 [1] CRAN (R 4.0.3)
   ggplot2        3.3.2     2020-06-19 [1] CRAN (R 4.0.3)
   glue           1.4.2     2020-08-27 [1] CRAN (R 4.0.3)
   gridExtra      2.3       2017-09-09 [1] CRAN (R 4.0.3)
   gtable         0.3.0     2019-03-25 [1] CRAN (R 4.0.3)
   inline         0.3.17    2020-12-01 [1] CRAN (R 4.0.3)
   isoband        0.2.3     2020-12-01 [1] CRAN (R 4.0.3)
   jsonlite       1.7.2     2020-12-09 [1] CRAN (R 4.0.3)
   labeling       0.4.2     2020-10-20 [1] CRAN (R 4.0.3)
 D lattice        0.20-41   2020-04-02 [1] CRAN (R 4.0.3)
   lifecycle      0.2.0     2020-03-06 [1] CRAN (R 4.0.3)
   loo            2.4.1     2020-12-09 [1] CRAN (R 4.0.3)
   magrittr       2.0.1     2020-11-17 [1] CRAN (R 4.0.3)
 D MASS           7.3-53    2020-09-09 [1] CRAN (R 4.0.3)
   Matrix         1.2-18    2019-11-27 [1] CRAN (R 4.0.3)
   matrixStats    0.57.0    2020-09-25 [1] CRAN (R 4.0.3)
 D mgcv           1.8-33    2020-08-27 [1] CRAN (R 4.0.3)
   munsell        0.5.0     2018-06-12 [1] CRAN (R 4.0.3)
   nlme           3.1-151   2020-12-10 [1] CRAN (R 4.0.3)
   pillar         1.4.7     2020-11-20 [1] CRAN (R 4.0.3)
   pkgbuild       1.2.0     2020-12-15 [1] CRAN (R 4.0.3)
   pkgconfig      2.0.3     2019-09-22 [1] CRAN (R 4.0.3)
   pkgload        1.1.0     2020-05-29 [1] CRAN (R 4.0.3)
   praise         1.0.0     2015-08-11 [1] CRAN (R 4.0.3)
   prettyunits    1.1.1     2020-01-24 [1] CRAN (R 4.0.3)
   processx       3.4.5     2020-11-30 [1] CRAN (R 4.0.3)
   ps             1.5.0     2020-12-05 [1] CRAN (R 4.0.3)
   R6             2.5.0     2020-10-28 [1] CRAN (R 4.0.3)
   RColorBrewer   1.1-2     2014-12-07 [1] CRAN (R 4.0.3)
   Rcpp         * 1.0.5     2020-07-06 [1] CRAN (R 4.0.3)
   RcppEigen      0.3.3.7.0 2019-11-16 [1] CRAN (R 4.0.3)
 D RcppParallel   5.0.2     2020-06-24 [1] CRAN (R 4.0.3)
   rematch2       2.1.2     2020-05-01 [1] CRAN (R 4.0.3)
   rlang          0.4.9     2020-11-26 [1] CRAN (R 4.0.3)
   rprojroot      2.0.2     2020-11-15 [1] CRAN (R 4.0.3)
   rstan          2.21.2    2020-07-27 [1] CRAN (R 4.0.3)
   rstudioapi     0.13      2020-11-12 [1] CRAN (R 4.0.3)
   scales         1.1.1     2020-05-11 [1] CRAN (R 4.0.3)
   StanHeaders    2.21.0-7  2020-12-17 [1] CRAN (R 4.0.3)
   testthat       3.0.1     2020-12-17 [1] CRAN (R 4.0.3)
   tibble         3.0.4     2020-10-12 [1] CRAN (R 4.0.3)
   utf8           1.1.4     2018-05-24 [1] CRAN (R 4.0.3)
   V8             3.4.0     2020-11-04 [1] CRAN (R 4.0.3)
   vctrs          0.3.5     2020-11-17 [1] CRAN (R 4.0.3)
   viridisLite    0.3.0     2018-02-01 [1] CRAN (R 4.0.3)
   waldo          0.2.3     2020-11-09 [1] CRAN (R 4.0.3)
   withr          2.3.0     2020-09-22 [1] CRAN (R 4.0.3)

[1] C:/Program Files/R/R-4.0.3/library

 D -- DLL MD5 mismatch, broken installation.

I guess the “broken installation” in the output above is a sign that something is wrong.

In case it’s useful, example(stan_model, package = "rstan", run.dontrun = TRUE) gave:

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

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] Resolving timed out after 10000 milliseconds
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  '-E' 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.17 
 >> setting environment variables: 
LOCAL_LIBS =  "C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Program Files/R/R-4.0.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Program Files/R/R-4.0.3/library/RcppParallel/lib/x64" -ltbb
PKG_CPPFLAGS =   -I"C:/Program Files/R/R-4.0.3/library/Rcpp/include/"  -I"C:/Program Files/R/R-4.0.3/library/RcppEigen/include/"  -I"C:/Program Files/R/R-4.0.3/library/RcppEigen/include/unsupported"  -I"C:/Program Files/R/R-4.0.3/library/BH/include" -I"C:/Program Files/R/R-4.0.3/library/StanHeaders/include/src/"  -I"C:/Program Files/R/R-4.0.3/library/StanHeaders/include/"  -I"C:/Program Files/R/R-4.0.3/library/RcppParallel/include/"  -I"C:/Program Files/R/R-4.0.3/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include "C:/Program Files/R/R-4.0.3/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp"  -std=c++1y
 >> Program source :
[Lines removed]

make cmd is
  make -f "c:/PROGRA~1/R/R-40~1.3/etc/x64/Makeconf" -f "c:/PROGRA~1/R/R-40~1.3/share/make/winshlib.mk" CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB="file4a1c7f433dfe.dll" WIN=64 TCLBIN=64 OBJECTS="file4a1c7f433dfe.o"

make would use

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source:
[Lines removed]

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) : 

Thanks for any help you can give to solve this problem.

Hi Mark,

So that I can get a bit more of an idea of your configuration, can you post the outputs from the following:

  • readLines("~/.Renviron")
  • readLines("~/.R/Makevars.win")
  • Sys.getenv("PATH")
  • Sys.which("make")
  • Sys.getenv("BINPREF")
1 Like

Thanks for your reply Andrew.

> readLines("~/.Renviron")
[1] "PATH=\"${RTOOLS40_HOME}\\usr\\bin;${PATH}\""

> readLines("~/.R/Makevars.win")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file 'C:\Users\seetom/.R/Makevars.win': No such file or directory

> Sys.getenv("PATH")
[1] "\\usr\\bin;C:\\Program Files\\Python\\Python38\\Scripts\\;C:\\Program Files\\Python\\Python38\\;C:\\Program Files\\Python\\Python39\\Scripts\\;C:\\Program Files\\Python\\Python39\\;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;;C:\\Program Files\\Common Files\\Datawatch\\Drivers\\7.1.5\\jre\\bin;C:\\Program Files\\Common Files\\Datawatch\\Drivers\\7.1.5\\jre\\bin\\server;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Users\\seetom\\AppData\\Local\\Programs\\Python\\Python36;C:\\Users\\seetom\\AppData\\Local\\Programs\\Python\\Python36\\Scripts;C:\\Users\\seetom\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64\\;C:\\Users\\seetom\\AppData\\Local\\Pandoc\\;C:\\Users\\seetom\\AppData\\Local\\Programs\\MiKTeX 2.9\\miktex\\bin\\x64\\;"

> Sys.which("make")
make 
  "" 

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

If I look at the Windows Environment Variables, RTOOLS40_HOME is C:\rools40, which is where Rtools40 is installed.

What does

Sys.getenv("RTOOLS40_HOME")

return.

The reason why I am asking is that you posted this

> Sys.getenv("PATH")
[1] "\\usr\\bin;C:\\Program Files\\Python\\Python38\\Scripts\\;C:\\Program Files\\Python\\Python38\\;C:\\Program Files\\Python\\Python39\\Scripts\\;C:\\Program Files\\Python\\Python39\\;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;;C:\\Program Files\\Common Files\\Datawatch\\Drivers\\7.1.5\\jre\\bin;C:\\Program Files\\Common Files\\Datawatch\\Drivers\\7.1.5\\jre\\bin\\server;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Users\\seetom\\AppData\\Local\\Programs\\Python\\Python36;C:\\Users\\seetom\\AppData\\Local\\Programs\\Python\\Python36\\Scripts;C:\\Users\\seetom\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64\\;C:\\Users\\seetom\\AppData\\Local\\Pandoc\\;C:\\Users\\seetom\\AppData\\Local\\Programs\\MiKTeX 2.9\\miktex\\bin\\x64\\;"

This should start with C:\rtools40\usr\bin;

Thanks for your reply Rok.

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

So I added C:\rtools40\usr\bin to my Path (using the Windows “Edit system environment variables”) and restarted R.

Strangely, Sys.getenv("RTOOLS40_HOME") now returns C:\\rtools40, and Sys.getenv("PATH") includes C:\\rtools40\\usr\\bin twice, presumably once from .Renviron and once from the one I added manually.

Sys.which("make") now returns "C:\\rtools40\\usr\\bin\\make.exe".

But brms still doesn’t work. I get:

Compiling Stan program...
make cmd is
  make -f "c:/PROGRA~1/R/R-40~1.3/etc/x64/Makeconf" -f "c:/PROGRA~1/R/R-40~1.3/share/make/winshlib.mk" CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB="file57ec326f3662.dll" WIN=64 TCLBIN=64 OBJECTS="file57ec326f3662.o"

make would use
Error in compileCode(f, code, language = language, verbose = verbose) : 
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found
Error in sink(type = "output") : invalid connection

Sys.getenv("BINPREF") is still "".

I suspect that R isn’t finding the RTools g++ compilers. As a quick test, can you post the outputs from:

system("touch foo.cpp")
system("R CMD SHLIB foo.cpp")

Thanks for your reply Andrew.

I have two R sessions open: the original one, before adding C:\rtools40\usr\bin to the path, and a new one, after adding C:\rtools40\usr\bin to the path.

Original R session:

> system("touch foo.cpp")
[1] 127
Warning message:
In system("touch foo.cpp") : 'touch' not found
> system("R CMD SHLIB foo.cpp")
Warning message:
In system(cmd) : 'make' not found
[1] 1

New R session:

> system("touch foo.cpp")
[1] 868
> system("R CMD SHLIB foo.cpp")
[1] 1

Definitely close the old R session, that’s not needed any more.

There is something strange going on with your environment variables here. Normally R also prepends the R bin directory to the path at startup, but that’s not happening here. Let’s try adding that to your .Renviron:

writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${R_HOME}\\bin\\x64;${PATH}"', con = "~/.Renviron")

Then restart R and try the foo.cpp commands above again? Can you also post the full output from Sys.getenv("PATH") again?

Thanks Andrew.

> system("touch foo.cpp")
[1] 868
> system("R CMD SHLIB foo.cpp")
[1] 1
> Sys.getenv("PATH")
[1] "C:\\rtools40\\usr\\bin;c:\\PROGRA~1\\R\\R-40~1.3\\bin\\x64;C:\\Program Files\\Python\\Python38\\Scripts\\;C:\\Program Files\\Python\\Python38\\;C:\\Program Files\\Python\\Python39\\Scripts\\;C:\\Program Files\\Python\\Python39\\;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;;C:\\Program Files\\Common Files\\Datawatch\\Drivers\\7.1.5\\jre\\bin;C:\\Program Files\\Common Files\\Datawatch\\Drivers\\7.1.5\\jre\\bin\\server;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Users\\seetom\\AppData\\Local\\Programs\\Python\\Python36;C:\\Users\\seetom\\AppData\\Local\\Programs\\Python\\Python36\\Scripts;C:\\rtools40\\usr\\bin;C:\\Users\\seetom\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64\\;C:\\Users\\seetom\\AppData\\Local\\Pandoc\\;C:\\Users\\seetom\\AppData\\Local\\Programs\\MiKTeX 2.9\\miktex\\bin\\x64\\;"

I don’t know if it’s relevant, but this is a work computer with certain restrictions, but brms/rstan used to work on it.

I don’t know if it’s relevant, but this is a work computer with certain restrictions, but brms/rstan used to work on it.

I was thinking this might be the case. There may have been changes to permissions on running executables on your system.

Two more things that may help shed some light here. Can you install R packages from source which require compilation:

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

If that errors, can you post the full output?

Next, we’ll try running g++ directly, and see what errors come up. Can you open up command prompt or Powershell and run: C:\rtools40\mingw64\bin\g++.exe and post the resulting error?

Running R “normally”, I get:

> install.packages("jsonlite",type="source")
Warning in install.packages("jsonlite", type = "source") :
  'lib = "C:/Program Files/R/R-4.0.3/library"' is not writable
Would you like to use a personal library instead? (yes/No/cancel)

But running as administrator, it appears to work.

C:\Users\seetom>c:\rtools40\mingw64\bin\g++.exe
g++.exe: fatal error: no input files
compilation terminated.

I think there may be a restriction that executables can only run from within c:\Program Files. Maybe I should try installing Rtools into the Program Files directory instead.

Oh that’s also odd. It looks like all R packages are installed into C:/Program Files instead of your home directory and personal library. I’m guessing that your organisation manages your installation?

Does the RStan example model give a different error if you run R as an administrator?

Can you also try running: C:\rtools40\usr\bin\touch.exe in command prompt? So I can see what that 868 error was.

Yes, packages are in c:\Program Files. I have a separate administrator account that I can use for R- and Python-related installations.

Running as administrator, the brms example gave:

Compiling Stan program...
make cmd is
  make -f "C:/PROGRA~1/R/R-40~1.3/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-40~1.3/share/make/winshlib.mk" CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB="file1da012a9cf.dll" WIN=64 TCLBIN=64 OBJECTS="file1da012a9cf.o"

make would use
if test "zfile1da012a9cf.o" != "z"; then \
  if test -e "file1da012a9cf-win.def"; then \
    echo "C:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file1da012a9cf.dll file1da012a9cf-win.def file1da012a9cf.o   "C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Program Files/R/R-4.0.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Program Files/R/R-4.0.3/library/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.3/bin/x64" -lR ; \
    "C:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file1da012a9cf.dll file1da012a9cf-win.def file1da012a9cf.o   "C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Program Files/R/R-4.0.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Program Files/R/R-4.0.3/library/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.3/bin/x64" -lR ; \
  else \
    echo EXPORTS > tmp.def; \
    "C:/rtools40/mingw64/bin/"nm file1da012a9cf.o | sed -n 's/^.* [BCDRT] / /p' | sed -e '/[.]refptr[.]/d' -e '/[.]weak[.]/d' | sed 's/[^ ][^ ]*/"&"/g'  >> tmp.def; \
    echo "C:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file1da012a9cf.dll tmp.def file1da012a9cf.o   "C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Program Files/R/R-4.0.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Program Files/R/R-4.0.3/library/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.3/bin/x64" -lR ; \
    "C:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file1da012a9cf.dll tmp.def file1da012a9cf.o   "C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Program Files/R/R-4.0.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Program Files/R/R-4.0.3/library/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.3/bin/x64" -lR ; \
    rm -f tmp.def; \
  fi \
fi
Error in compileCode(f, code, language = language, verbose = verbose) : 
  C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.rdata$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): multiple definition of `boost::math::detail::bessel_j0<long double>(long double)::P1'; file1da012a9cf.o:file1da012a9cf.cpp:(.data$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): first defined hereC:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.rdata$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QC[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QC]+0x0): multiple definition of `boost::math::detail::bessel_j0<long double>(long double)::QC'; file1da012a9cf.o:file1da012a9cf.cpp:(.data$_ZZN5boos
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found
Error in sink(type = "output") : invalid connection

And example(stan_model, package = "rstan", run.dontrun = TRUE) ended with:

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) : 
  C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.rdata$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): multiple definition of `boost::math::detail::bessel_j0<long double>(long double)::P1'; file1da060262173.o:file1da060262173.cpp:(.data$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): first defined hereC:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.rdata$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QC[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QC]+0x0): multiple definition of `boost::math::detail::bessel_j0<long double>(long double)::QC'; file1da060262173.o:file1da060262173.cpp:(.data$_
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found

When I ran C:\rtools40\usr\bin\touch.exe at a command prompt, a box popped up saying The application was unable to start correctly (0xc0000364). Click OK to close the application.

Ah now we’re getting somewhere! If you don’t run R as an administrator, do you still see the following lines in the error output:

Error in compileCode(f, code, language = language, verbose = verbose) : 
  C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.rdata$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): multiple definition of `boost::math::detail::bessel_j0<long double>(long double)::P1'; file1da012a9cf.o:file1da012a9cf.cpp:(.data$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): first defined hereC:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.rdata$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QC[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QC]+0x0): multiple definition of `boost::math::detail::bessel_j0<long double>(long double)::QC'; file1da012a9cf.o:file1da012a9cf.cpp:(.data$_ZZN5boos

I’m guessing that you’ve also updated your BH package since your original post, and now have version 1.75.0-0?

Running example(stan_model, package = "rstan", run.dontrun = TRUE) not as administrator:

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) : 
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found

I haven’t updated the BH package, as far as I know. I don’t know what that package is. According to sessionInfo(), I have BH_1.72.0-3.

Alright, I think this is because your R packages are installed in C:/Program Files which needs admin access. To verify, I’ll get you to fix that error that under the admin session and then see if things run.

First, in a new admin R session (closing all others), can you reinstall RStan and StanHeaders from source:

# Compile packages using all cores
Sys.setenv(MAKEFLAGS = paste0("-j",parallel::detectCores()))

install.packages(c("StanHeaders","rstan"),type="source")

Then restart R and try the example model again (in an admin session). If that runs, then try it in a non-admin session as well. If it works under an admin session, but not a non-admin session, then you’ll need to create a personal library for your R packages in a directory you have permissions to

After reinstalling RStan and StanHeaders, running example(stan_model, package = "rstan", run.dontrun = TRUE) as administrator appeared to work. It ended with:

Chain 4:  Elapsed Time: 0.032 seconds (Warm-up)
Chain 4:                0.015 seconds (Sampling)
Chain 4:                0.047 seconds (Total)
Chain 4: 
Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found

When not running as administrator, it didn’t work:

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) :

I think it might be worth trying putting Rtools into c:\Program Files\rtools40 instead of in c:\rtools40. If that doesn’t work, I’ll try putting packages in a personal library.

Thanks for all your help.

1 Like

Installing rtools in Program Files will not work unfortunately as in that case no R package will compile due to the space in path to rtools.

It *might* work, but not 100% sure, since R normally calls with the path in quotes:

"C:/rtools40/mingw64/bin/"g++ -std=gnu++11

Thanks Rok and Andrew. I’ll give it a try.