Problem running Rstan 2.19.3 in R version 4.0.1

I am on a Windows 10 laptop, running R 4.0.1, Rtools 4.0 and cannot get rstan running. I have been through various cycles of uninstalling and re-installing everything R, Rtools, rstan, StanHeaders, following suggestions on various threads but get the same basic result:
if I run, for example

example(stan_model, run.dontrun = TRUE)

I get
"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’.
COMPILING THE C++ CODE FOR MODEL ‘73fc79f8b1915e8208c736914c86d1a1’ NOW.
OS: x86_64, mingw32; rstan: 2.19.3; Rcpp: 1.0.4.6; inline: 0.3.15

setting environment variables:
PKG_LIBS = -L"C:/Users/Patrick Graham/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders
PKG_CPPFLAGS = -I"C:/Users/Patrick Graham/Documents/R/win-library/4.0/Rcpp/include/" -I"C:/Users/Patrick Graham/Documents/R/win-library/4.0/RcppEigen/include/" -I"C:/Users/Patrick Graham/Documents/R/win-library/4.0/RcppEigen/include/unsupported" -I"C:/Users/Patrick Graham/Documents/R/win-library/4.0/BH/include" -I"C:/Users/Patrick Graham/Documents/R/win-library/4.0/StanHeaders/include/src/" -I"C:/Users/Patrick Graham/Documents/R/win-library/4.0/StanHeaders/include/" -I"C:/Users/Patrick Graham/Documents/R/win-library/4.0/rstan/include" -DEIGEN_NO_DEBUG -D_REENTRANT -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -include stan/math/prim/mat/fun/Eigen.hpp -std=c++1y "

which looks vaguely promising - it seems to be compiling.
Then there a few hundred lines of similar looking text, finally ending in

“g++.exe: error: Graham/Documents/R/win-library/4.0/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp: No such file or directory
make: *** [C:/R-40~1.1/etc/x64/Makeconf:229: file6d4051783254.o] Error 1
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘file6d4051783254.cpp.err.txt’: No such file or directory”

I have checked that the file Eigen.hpp does actually exist on the path listed,as does the Makeconf file.

I get the impression rstan is looking for something that is not in the right place.

Any ideas?

Welcome to the forums!

This is a bit of a known error, where the compiler flag for including that Eigen.hpp file isn’t in quotations, so filenames with spaces cause an error. You can see this in your output:

“g++.exe: error: Graham/Documents/R/win-library/4.0/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp

Where the filename starts with ‘Graham’, rather than ‘C:/Users/Patrick Graham’.

One workaround is to not load the RStan library, and just use the rstan:: qualifier instead.

So rather than:

library(rstan)
stan(model, data)

Use:

rstan::stan(model, data)

As this causes the Eigen.hpp file to be included in a different way (more info here)

2 Likes

Thanks! That works perfectly. And solved all of 20 minutes after posting, after I had been puzzling over it for several hours over the course of a couple of days. Looks like I am back in business, so thanks again for that explanation.

3 Likes