Problems getting Stan to run after upgrading to R 4.0.2

Hi Andrew,

Thank you for all the advice on how to fix our errors. I particularly start with the following error by running my model:

Error in compileCode(f, code, language = language, verbose = verbose) : **
** sh: c:/rtools40/mingw64/bin/g++: No such file or directorymake: *** [C:/PROGRA~1/R/R-40~1.3/etc/x64/Makeconf:229: file581c41a54d87.o] Error 127

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 followed all the steps you are referring to above:

Sys.getenv(“BINPREF”)
[1] “”
Sys.getenv(“PATH”)
[1] “C:\Program Files\R\R-4.0.3\bin\x64;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\PuTTY;C:\Users\elena\AppData\Local\Microsoft\WindowsApps”
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/elena/Documents/.R/Makevars.win’: No such file or directory
readLines(“~/.Renviron”)
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘C:/Users/elena/Documents/.Renviron’: No such file or directory
readLines(“~/.Rprofile”)
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘C:/Users/elena/Documents/.Rprofile’: No such file or directory

I also created the file Makevars.win as it was not present. Nothing seems to work though.

Any help would be greatly appreciated.

Thanks a lot in advance

This indicates that your R installation isn’t configured, can you try the instructions over in the getting started guide: Configuring C Toolchain for Windows · stan-dev/rstan Wiki · GitHub

Hello @andrjohns
How are you? I need a small help from you. I have been stuck in running these lines.

model_file='Bernoulli_beta.stan'
N=10
p=0.4
alpha0=1 
beta0=1 
x=rbinom(N,1,p)

stan_data = list(N=N,x=x,alpha0=alpha0,beta0=beta0)
stan_model=stan(model_file,data=stan_data,chains=4,iter=2000,warmup= 1000)
print(stan_model,digits=2)

Then i got error as following

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

make would use
"c:/rtools40/mingw64/bin/"g++  -std=gnu++14 -I"C:/PROGRA~1/R/R-41~1.0/include" -DNDEBUG   -I"C:/Users/admin/Documents/R/win-library/4.1/Rcpp/include/"  -I"C:/Users/admin/Documents/R/win-library/4.1/RcppEigen/include/"  -I"C:/Users/admin/Documents/R/win-library/4.1/RcppEigen/include/unsupported"  -I"C:/Users/admin/Documents/R/win-library/4.1/BH/include" -I"C:/Users/admin/Documents/R/win-library/4.1/StanHeaders/include/src/"  -I"C:/Users/admin/Documents/R/win-library/4.1/StanHeaders/include/"  -I"C:/Users/admin/Documents/R/win-library/4.1/RcppParallel/include/"  -I"C:/Users/admin/Documents/R/win-library/4.1/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include "C:/Users/admin/Documents/R/win-library/4.1/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp"  -std=c++1y         -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -c file1468523e7fe2.cpp -o file1468523e7fe2.o
if test "zfile1468523e7fe2.o" != "z"; then \
  if test -e "file1468523e7fe2-win.def"; then \
    echo "c:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file1468523e7fe2.dll file1468523e7fe2-win.def file1468523e7fe2.o   "C:/Users/admin/Documents/R/win-library/4.1/rstan/lib/x64/libStanServices.a" -L"C:/Users/admin/Documents/R/win-library/4.1/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/admin/Documents/R/win-library/4.1/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-41~1.0/bin/x64" -lR ; \
    "c:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file1468523e7fe2.dll file1468523e7fe2-win.def file1468523e7fe2.o   "C:/Users/admin/Documents/R/win-library/4.1/rstan/lib/x64/libStanServices.a" -L"C:/Users/admin/Documents/R/win-library/4.1/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/admin/Documents/R/win-library/4.1/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-41~1.0/bin/x64" -lR ; \
  else \
    echo EXPORTS > tmp.def; \
    "c:/rtools40/mingw64/bin/"nm file1468523e7fe2.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 file1468523e7fe2.dll tmp.def file1468523e7fe2.o   "C:/Users/admin/Documents/R/win-library/4.1/rstan/lib/x64/libStanServices.a" -L"C:/Users/admin/Documents/R/win-library/4.1/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/admin/Documents/R/win-library/4.1/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-41~1.0/bin/x64" -lR ; \
    "c:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file1468523e7fe2.dll tmp.def file1468523e7fe2.o   "C:/Users/admin/Documents/R/win-library/4.1/rstan/lib/x64/libStanServices.a" -L"C:/Users/admin/Documents/R/win-library/4.1/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/admin/Documents/R/win-library/4.1/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-41~1.0/bin/x64" -lR ; \
    rm -f tmp.def; \
  fi \
fi
Error in compileCode(f, code, language = language, verbose = verbose) : 
  sh: c:/rtools40/mingw64/bin/g++: No such file or directorymake: *** [C:/PROGRA~1/R/R-41~1.0/etc/x64/Makeconf:245: file1468523e7fe2.o] Error 127
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

What to do now?
Thanks in advance

It looks like R is having trouble finding RTools. Try reinstalling RTools using the official instructions: Using Rtools40 on Windows

And make sure that you can run the example code that they include

I am also having a similar problem when trying to get started with Stan, and am hoping someone can help @andrjohns ?

I ran through the getting started instructions on https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started

and had trouble installing rstan and StanHeaders but resolved it using:

remove.packages(c("StanHeaders", "rstan"))
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

I ran the example(stan_model, package = "rstan", run.dontrun = TRUE)
and am currently getting the following error msg:

Error in compileCode(f, code, language = language, verbose = verbose) : 
  C:/Users/flip/Dropbox/My PC (6300l-153254-e)/Documents/R/win-library/4.1/StanHeaders/include/src/stan/mcmc/hmc/hamiltonians/dense_e_metric.hpp:22:56:   required from 'double stan::mcmc::dense_e_metric<Model, BaseRNG>::T(stan::mcmc::dense_e_point&) [with Model = model1ae462b773d2__namespace::model1ae462b773d2_; BaseRNG = boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> >]'C:/Users/flip/Dropbox/My PC (6300l-153254-e)/Documents/R/win-library/4.1/StanHeaders/include/src/stan/mcmc/hmc/hamiltonians/dense_e_metric.hpp:21:10:   required from hereC:/Users/flip/Dropbox/My PC (6300l-153254-e)/Documents/R/win-library/4.1/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:55:30: warning: ignoring attributes on template argument 'Eigen::internal::packet_traits<double>::type' {aka '__vector(2) double'} [-Wignored-attributes]sh: -c: line 3: s

Can you advise how to resolve this?

In case it helps I have also run the following steps:

> Sys.getenv("BINPREF")
[1] ""
> Sys.getenv("PATH")
[1] "C:\\rtools40\\usr\\bin;C:\\Program Files\\R\\R-4.1.0\\bin\\x64;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)\\Sennheiser\\SenncomSDK\\;C:\\Program Files\\Git\\cmd;C:\\Users\\flip\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\flip\\AppData\\Local\\GitHubDesktop\\bin"
> Sys.which("make")
                              make 
"C:\\rtools40\\usr\\bin\\make.exe" 
> readLines("~/.Renviron")
[1] "PATH=\"${RTOOLS40_HOME}\\usr\\bin;${PATH}\""
> readLines("~/.Rprofile")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file 'C:/Users/flip/Dropbox/My PC (6300l-153254-e)/Documents/.Rprofile': No such file or directory

Hmm that’s a new one for me. There are two things I think might be causing this:

  • Dropbox attempting to synchronise files while compiling

  • The spaces and parentheses in the folder name (My PC (6300l-153254-e))

Can you try disabling the synchronisation/backup and re-running the example?

If that doesn’t work, can you re-run the example with the verbose=T flag and post the full output here? There might be a clue in that.

Ok, thanks, and yes they seem like potential solutions - I have some ‘large’ JAGS models running at the moment so will try these things as soon as they have finished. I am also wondering if I need to move everything directly to the Documents folder rather than going through Dropbox

Hi Andrew,
I got similar problem here.
I tried this code

library(rethinking)
library(MASS)
library(rstan)
library(ggplot2)
library(tidybayes)
data(eagles)
d <- eagles
d$pirateL <- ifelse(d$P == "L", 1, 0)
d$victimL <- ifelse(d$V == "L", 1, 0)
d$pirateA <- ifelse(d$A == "A", 1, 0)
# define model list specification
f <- alist(
  y ~ dbinom(n, p),
  logit(p) <- a + bP * pirateL + bV * victimL + bA * pirateA,
  a ~ dnorm(0, 1.5),
  bP ~ dnorm(0, .5),
  bV ~ dnorm(0, .5),
  bA ~ dnorm(0, .5)
)
## quap model
mH3quap <- quap(f, data = d)
## ulam model
mH3ulam <- ulam(f, data = d, chains = 4, log_lik = TRUE)

I kept getting errors running ulam

Error in compileCode(f, code, language = language, verbose = verbose) : 
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file42b458ce6fa4.o:file42b458ce6fa4.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[_ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1d): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file42b458ce6fa4.o:file42b458ce6fa4.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x1d): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file42b458ce6fa4.o:file42b458ce6fa4.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x3a): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file42b458ce6fa4.o:file42b458ce6fa4.cpp:(.text$_ZN3tbb10interface
Error in sink(type = "output") : invalid connection

sincerely,
Andrew

Hello, I’ve been reading through this thread and uninstalled R to earlier versions and ran some of the above suggested code but I still get errors messages regarding stan.

I appreciate any help.

I’m using R 4.2.3 and R tool 4.2, on Windows 11.

I also have problems with “stan” while running this code:

bform1 ← bf(mvbind(mean.poidsp, pulecl) ~ scale.mf.mean.poids+scale.mf.mean.tarsed+m.age.JA+f.age.JA+(1|ID1|mbag)+(1|ID2|fbag)+(1|ID3|nest)+(1|ID4|an))+set_rescor(TRUE)

fit1 <-brm(bform1,data=DR.demo.chick.morph, chains = 2, cores =2

I get the following error message:

Code / Program output`Error in compileCode(f, code, language = language, verbose = verbose) :
/bin/sh: line 1: C:/RBuildTools/4.2/usr/mingw_64/bin/g++: No such file or directorymake: *** [C:/PROGRA~1/R/R-42~1.3/etc/x64/Makeconf:260: file21425ef2684.o] Error 127
In addition: Warning message:
Rows containing NAs were excluded from the model.
Error in sink(type = “output”) : invalid connection