Rstan on Windows

Humn, that there must something else which causes R to crash… damn.

Yes, its the march=native in makevars file That should be removed.

There should me a makevars file somewhere in c:/Users/steven/Documents/R/

Remove any mention of march=native anywhere in that file. Then restart R/Rstudio.

Will add that to the summary.

I cannot find a makevars file … Could it be that I’m on a non-admin account and the the file is loacted at the admin account that installed Rtools? I will check with the tech guys tomorrow…

@Kalaschnik Does anything come up if you run

file.edit(file.path(Sys.getenv("HOME"), ".R", "Makevars.win"))
file.edit(file.path(Sys.getenv("HOME"), ".R", "Makevars"))

from your R session?

Nothing comes up … I’m not even have the .R directory

Can you check eiher

file.path(Sys.getenv(“HOME”), “.R”)

or

file.path(Sys.getenv(“HOME”), “R”)

exists and if there are any Makevars files inside?

I indeed found some files, but they are much more nested — are we looking for one of these:

Note; My Document is an Alias for “Documents”

I think there’s also possibly an .Renviron in your home folder (it sounds like we’re trying to track down a file with -march=native in it and this file might define it).

1 Like

I encountered similar problems.

> stanmodel2 <- rstan::stan_model(model_code = stancode2)
Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  '-E' not found

And I was using R 4.0.2 and rstan 2.21.2.

Update:
It’s funny, I reinstalled rtools and rstan package,
now the warning is

Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/Rtools/mingw_/bin/g++' not found

OK, I realized that it is rtools40’s problem.
It will not affect the rstan 2.21.2.

1 Like

Great that it is working. You can safely ignore this warning, it will go away in the next versions of rstan.

StanHeaders seems to require RTools to be installed, but the instructions here [1] have checking RTools done after updating rstan.

[1] https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started

Hmm, yeah that does seem backwards but maybe there’s a reason. @bgoodri is there a reason the section in the instructions about checking RTools comes after the section that includes the install.packages() commands? Or is that something that should be fixed?

I have just installed version 2.21.2 on my work computer (behind a corporate fire wall) and I am getting a message when calling schools.stan below. I don’t recall getting this before and have never had a problem using stan in an offline situation (after already installed).

> fit <- stan(file = 'schools.stan', data = schools_dat)
Error in open.connection(con, open = mode) : 
  Failed to connect to github.com port 443: Connection refused
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  '-E' not found
Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/R-40~1.2/bin/x64/R CMD SHLIB file1056849dc5431.cpp 2> file1056849dc5431.cpp.err.txt' had status 1
2: In file(con, "r") :
  cannot open file 'file1056849dc5431.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection

Hi John,

Can you try the instructions in this post: Workarounds for current Rstan Windows issues

Oh sorry, just noticed this part of your output:

Error in open.connection(con, open = mode) : 
  Failed to connect to github.com port 443: Connection refused

I believe this is linked to RStan downloading the latest version of the Stanc3 transpiler (the program that translates Stan syntax to c++) when the model is run. I think there was a way to disable this, but I’m not sure. @bgoodri or @jonah?

It looks like the #1 warning is similar to #2 on that workarounds page, which recommends installing something from github (which probably wouldn’t work for me).

Are you able to install local packages? If so, you could download the github repository (https://github.com/bgoodri/inline) as a .zip file (link here if that’s easier) on a separate computer, and then on your work computer unzip the repository and install the package in R.

For example, once you’ve downloaded the .zip file and moved it your work computer, navigate R to the directory containing the inline-master.zip downloaded from github and run:

unzip("inline-master.zip")
devtools::install("inline-master/")

Thanks. That seems to remove one of the warnings. The new output is below. At several points it looks like it is trying to use C:/Rtools instead of C:/rtools40.

> fit <- stan(file = 'schools.stan', data = schools_dat)
Error in open.connection(con, open = mode) : 
  Failed to connect to github.com port 443: Connection refused
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found
make cmd is
  make -f "C:/PROGRA~1/R/R-40~1.2/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-40~1.2/share/make/winshlib.mk" CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB="file1ae74765da5d.dll" WIN=64 TCLBIN=64 OBJECTS="file1ae74765da5d.o"

make would use
C:/Rtools/mingw_64/bin/g++  -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.2/include" -DNDEBUG   -I"C:/Users/jhall/Documents/R/win-library/4.0/Rcpp/include/"  -I"C:/Users/jhall/Documents/R/win-library/4.0/RcppEigen/include/"  -I"C:/Users/jhall/Documents/R/win-library/4.0/RcppEigen/include/unsupported"  -I"C:/Users/jhall/Documents/R/win-library/4.0/BH/include" -I"C:/Users/jhall/Documents/R/win-library/4.0/StanHeaders/include/src/"  -I"C:/Users/jhall/Documents/R/win-library/4.0/StanHeaders/include/"  -I"C:/Users/jhall/Documents/R/win-library/4.0/RcppParallel/include/"  -I"C:/Users/jhall/Documents/R/win-library/4.0/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include "C:/Users/jhall/Documents/R/win-library/4.0/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp"  -std=c++1y       -include C:/Users/jhall/Documents/R/win-library/4.0/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp -I "C:/Users/jhall/Documents/R/win-library/4.0/StanHeaders/include" -I "C:/Users/jhall/Documents/R/win-library/4.0/RcppEigen/include"   -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign -c file1ae74765da5d.cpp -o file1ae74765da5d.o
if test "zfile1ae74765da5d.o" != "z"; then \
  if test -e "file1ae74765da5d-win.def"; then \
    echo C:/Rtools/mingw_64/bin/g++  -shared -s -static-libgcc -o file1ae74765da5d.dll file1ae74765da5d-win.def file1ae74765da5d.o   "C:/Users/jhall/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a" -L"C:/Users/jhall/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/jhall/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.2/bin/x64" -lR ; \
    C:/Rtools/mingw_64/bin/g++  -shared -s -static-libgcc -o file1ae74765da5d.dll file1ae74765da5d-win.def file1ae74765da5d.o   "C:/Users/jhall/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a" -L"C:/Users/jhall/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/jhall/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.2/bin/x64" -lR ; \
  else \
    echo EXPORTS > tmp.def; \
    C:/Rtools/mingw_64/bin/nm file1ae74765da5d.o | sed -n 's/^.* [BCDRT] / /p' | sed -e '/[.]refptr[.]/d' -e '/[.]weak[.]/d' | sed 's/[^ ][^ ]*/"&"/g'  >> tmp.def; \
    echo C:/Rtools/mingw_64/bin/g++  -shared -s -static-libgcc -o file1ae74765da5d.dll tmp.def file1ae74765da5d.o   "C:/Users/jhall/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a" -L"C:/Users/jhall/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/jhall/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.2/bin/x64" -lR ; \
    C:/Rtools/mingw_64/bin/g++  -shared -s -static-libgcc -o file1ae74765da5d.dll tmp.def file1ae74765da5d.o   "C:/Users/jhall/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a" -L"C:/Users/jhall/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/jhall/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.2/bin/x64" -lR ; \
    rm -f tmp.def; \
  fi \
fi
Error in compileCode(f, code, language = language, verbose = verbose) : 
  sh: C:/Rtools/mingw_64/bin/g++: No such file or directorymake: *** [C:/PROGRA~1/R/R-40~1.2/etc/x64/Makeconf:229: file1ae74765da5d.o] Error 127
Error in sink(type = "output") : invalid connection

There are a couple of places that error can come from. The first check is to Sys.getenv("PATH"), the output should start with "C:\rtools40\usr\bin;". If it doesn’t, run:

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

And restart R/RStudio.

If that’s not the issue, run Sys.getenv("BINPREF"). If that returns anything other than "", it means that you’ve specified the BINPREF variable in your Makevars.win or Rprofile file, and that needs to be removed.

Let me know if you’re not sure how to do that last step, or if that doesn’t resolve the issue

2 Likes

I had checked Sys.getenv(“PATH”), but not Sys.getenv(“BINPREF”). It looks like my Rprofile from 2018 was responsible! So it looks like Stan runs properly now, though I was surprised to see a divergent transition fitting the 8 schools model.

2 Likes