Problem running RStan in RStudio

HI - I’m new to Stan and working through a course on Bayesian modelling.

When I run this code;
library(rethinking)
data(Wines2012)
d <- Wines2012

dat_list <- list(S = standardize(d$score),
jid = as.integer(d$judge),
wid = as.integer(d$wine))

m1 <- ulam(alist(
S ~ dnorm( mu , sigma ),
mu <- a[jid] + w[wid],
a[jid] ~ dnorm(0,0.5),
w[wid] ~ dnorm(0,0.5),
sigma ~ dexp(1)), data=dat_list , chains=4 , cores=4)

I get this issue;
The NEXT version of Stan will not be able to pre-process your Stan program.
Please open an issue at


if you can share or at least describe your Stan program. This will help ensure that Stan
continues to work on your Stan programs in the future. Thank you!
This message can be avoided by wrapping your function call inside suppressMessages().
Error in file(con, “r”) : cannot open the connection
In addition: Warning messages:
1: In file.remove(c(unprocessed, processed)) :
cannot remove file ‘C:\Users\BEN101\AppData\Local\Temp\RtmpS6E4Od\file21504f9e6cf2.stan’, reason ‘No such file or directory’
2: In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA
1/R/R-40~1.2/bin/x64/R CMD SHLIB file2150102d7cbc.cpp 2> file2150102d7cbc.cpp.err.txt’ had status 1
3: In file(con, “r”) :
cannot open file ‘file2150102d7cbc.cpp.err.txt’: No such file or directory
Error in sink(type = “output”) : invalid connection

I’m running R Studio
Version 1.3.959
© 2009-2020 RStudio, PBC
“Middlemist Red” (3a09be39, 2020-05-18) for Windows
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.6 Chrome/69.0.3497.128 Safari/537.36

Stan version 2.21.0

UPDATE: I updated RStan to 2.21.2 latest version but it still generates an error…

Error in file(con, “r”) : cannot open the connection
In addition: Warning messages:
1: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/rtools40/usr/mingw_/bin/g++’ not found
2: In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA1/R/R-401.2/bin/x64/R CMD SHLIB file550c6614374a.cpp 2> file550c6614374a.cpp.err.txt’ had status 1
3: In file(con, “r”) :
cannot open file ‘file550c6614374a.cpp.err.txt’: No such file or directory
Error in sink(type = “output”) : invalid connection

@andrjohns might be able to help you since you’re on a Windows machine. I believe I’ve seen this several times on this forum. @andrjohns, do you have one page with your knowledge encoded somewhere? :)

2 Likes

Thanks Richard! All I mainly look for is:

  • inline/StanHeaders/rstan out of date
  • RTools4 not added to the PATH (via .Renviron)
  • R configured to use RTools 3.5 with R4.0 (via BINPREF)

Paul - Can I get a bit more information about your system? What output do you get from:

Sys.getenv("PATH")
Sys.getenv("BINPREF")
readLines("~/.R/Makevars.win")
readLines("~/.Rprofile")
readLines("~/.Renviron")
devtools::session_info("rstan")
2 Likes

Hi @andrjohns ! Thanks for your reply…

I get this…

Sys.getenv(“PATH”)
[1] “C:\rtools40\usr\bin;C:\Program Files\R\R-4.0.2\bin\x64;C:\ProgramData\Oracle\Java\javapath;C:\Program Files\Common Files\microsoft shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files (x86)\Windows Live\Shared;C:\Windows\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\Ben 10\AppData\Local\Microsoft\WindowsApps”

Sys.getenv(“BINPREF”)
[1] “”
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/Ben 10/Documents/.R/Makevars.win’: 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/Ben 10/Documents/.Rprofile’: No such file or directory
readLines("~/.Renviron")
[1] “PATH=”{RTOOLS40_HOME}\\usr\\bin;{PATH}""
devtools::session_info(“rstan”)

  • Session info ---------------------------------------------------------------------------
    setting value
    version R version 4.0.2 (2020-06-22)
    os Windows 10 x64
    system x86_64, mingw32
    ui RStudio
    language (EN)
    collate English_United Kingdom.1252
    ctype English_United Kingdom.1252
    tz Europe/London
    date 2020-09-29

  • Packages -------------------------------------------------------------------------------
    ! package * version date lib source
    assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.0)
    backports 1.1.8 2020-06-17 [1] CRAN (R 4.0.0)
    BH 1.72.0-3 2020-01-08 [1] CRAN (R 4.0.0)
    callr 3.4.3 2020-03-28 [1] CRAN (R 4.0.0)
    checkmate 2.0.0 2020-02-06 [1] CRAN (R 4.0.2)
    cli 2.0.2 2020-02-28 [1] CRAN (R 4.0.0)
    colorspace 1.4-1 2019-03-18 [1] CRAN (R 4.0.0)
    crayon 1.3.4 2017-09-16 [1] CRAN (R 4.0.0)
    curl 4.3 2019-12-02 [1] CRAN (R 4.0.0)
    desc 1.2.0 2018-05-01 [1] CRAN (R 4.0.0)
    digest 0.6.25 2020-02-23 [1] CRAN (R 4.0.0)
    ellipsis 0.3.1 2020-05-15 [1] CRAN (R 4.0.2)
    evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.0)
    fansi 0.4.1 2020-01-08 [1] CRAN (R 4.0.0)
    farver 2.0.3 2020-01-16 [1] CRAN (R 4.0.0)
    ggplot2 * 3.3.2 2020-06-19 [1] CRAN (R 4.0.2)
    glue 1.4.1 2020-05-13 [1] CRAN (R 4.0.2)
    gridExtra 2.3 2017-09-09 [1] CRAN (R 4.0.0)
    gtable 0.3.0 2019-03-25 [1] CRAN (R 4.0.0)
    inline 0.3.15 2020-09-28 [1] local
    isoband 0.2.2 2020-06-20 [1] CRAN (R 4.0.2)
    jsonlite 1.7.0 2020-06-25 [1] CRAN (R 4.0.2)
    labeling 0.3 2014-08-23 [1] CRAN (R 4.0.0)
    lattice 0.20-41 2020-04-02 [2] CRAN (R 4.0.2)
    lifecycle 0.2.0 2020-03-06 [1] CRAN (R 4.0.0)
    loo 2.3.0 2020-07-07 [1] CRAN (R 4.0.2)
    magrittr 1.5 2014-11-22 [1] CRAN (R 4.0.0)
    MASS 7.3-51.6 2020-04-26 [2] CRAN (R 4.0.2)
    Matrix 1.2-18 2019-11-27 [2] CRAN (R 4.0.2)
    matrixStats 0.56.0 2020-03-13 [1] CRAN (R 4.0.2)
    mgcv 1.8-31 2019-11-09 [2] CRAN (R 4.0.2)
    munsell 0.5.0 2018-06-12 [1] CRAN (R 4.0.0)
    nlme 3.1-148 2020-05-24 [2] CRAN (R 4.0.2)
    pillar 1.4.6 2020-07-10 [1] CRAN (R 4.0.0)
    pkgbuild 1.1.0 2020-07-13 [1] CRAN (R 4.0.0)
    pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.0)
    pkgload 1.1.0 2020-05-29 [1] CRAN (R 4.0.2)
    praise 1.0.0 2015-08-11 [1] CRAN (R 4.0.0)
    prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.0)
    processx 3.4.3 2020-07-05 [1] CRAN (R 4.0.2)
    ps 1.3.3 2020-05-08 [1] CRAN (R 4.0.2)
    R6 2.4.1 2019-11-12 [1] CRAN (R 4.0.0)
    RColorBrewer 1.1-2 2014-12-07 [1] CRAN (R 4.0.0)
    Rcpp 1.0.5 2020-07-06 [1] CRAN (R 4.0.2)
    RcppEigen 0.3.3.7.0 2019-11-16 [1] CRAN (R 4.0.0)
    D RcppParallel 5.0.2 2020-06-24 [1] CRAN (R 4.0.2)
    rlang 0.4.7 2020-07-09 [1] CRAN (R 4.0.0)
    rprojroot 1.3-2 2018-01-03 [1] CRAN (R 4.0.0)
    rstan * 2.21.2 2020-07-27 [1] CRAN (R 4.0.2)
    rstudioapi 0.11 2020-02-07 [1] CRAN (R 4.0.0)
    scales 1.1.1 2020-05-11 [1] CRAN (R 4.0.2)
    StanHeaders * 2.21.0-5 2020-06-09 [1] CRAN (R 4.0.2)
    testthat 2.3.2 2020-03-02 [1] CRAN (R 4.0.0)
    tibble 3.0.3 2020-07-10 [1] CRAN (R 4.0.0)
    utf8 1.1.4 2018-05-24 [1] CRAN (R 4.0.0)
    V8 3.2.0 2020-06-19 [1] CRAN (R 4.0.2)
    vctrs 0.3.1 2020-06-05 [1] CRAN (R 4.0.2)
    viridisLite 0.3.0 2018-02-01 [1] CRAN (R 4.0.0)
    withr 2.2.0 2020-04-20 [1] CRAN (R 4.0.0)

[1] C:/Users/Ben 10/Documents/R/win-library/4.0
[2] C:/Program Files/R/R-4.0.2/library

D – DLL MD5 mismatch, broken installation.

Thanks Paul! Your inline and StanHeaders packages are both out of date, which can cause the errors that you’re seeing (each for different reasons). Try updating these packages and running your model again

OK - that seemed to go a bit better, but still not fully woking:

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=“file20d4364a7140.dll” WIN=64 TCLBIN=64 OBJECTS=“file20d4364a7140.o”

make would use
“C:/rtools40/mingw64/bin/“g++ -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.2/include” -DNDEBUG -I"C:/Users/Ben 10/Documents/R/win-library/4.0/Rcpp/include/” -I"C:/Users/Ben 10/Documents/R/win-library/4.0/RcppEigen/include/" -I"C:/Users/Ben 10/Documents/R/win-library/4.0/RcppEigen/include/unsupported" -I"C:/Users/Ben 10/Documents/R/win-library/4.0/BH/include" -I"C:/Users/Ben 10/Documents/R/win-library/4.0/StanHeaders/include/src/" -I"C:/Users/Ben 10/Documents/R/win-library/4.0/StanHeaders/include/" -I"C:/Users/Ben 10/Documents/R/win-library/4.0/RcppParallel/include/" -I"C:/Users/Ben 10/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/Ben 10/Documents/R/win-library/4.0/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp” -std=c++1y -include C:/Users/Ben 10/Documents/R/win-library/4.0/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp -I “C:/Users/Ben 10/Documents/R/win-library/4.0/StanHeaders/include” -I “C:/Users/Ben 10/Documents/R/win-library/4.0/RcppEigen/include” -O2 -Wall -mfpmath=sse -msse2 -mstackrealign -c file20d4364a7140.cpp -o file20d4364a7140.o
if test “zfile20d4364a7140.o” != “z”; then
if test -e “file20d4364a7140-win.def”; then
echo “C:/rtools40/mingw64/bin/“g++ -shared -s -static-libgcc -o file20d4364a7140.dll file20d4364a7140-win.def file20d4364a7140.o “C:/Users/Ben 10/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a” -L"C:/Users/Ben 10/Documents/R/win-library/4.0/StanHeaders/libs/x64” -lStanHeaders -L"C:/Users/Ben 10/Documents/R/win-library/4.0/RcppParallel/lib/x64” -ltbb -L"C:/PROGRA~1/R/R-40~1.2/bin/x64" -lR ;
“C:/rtools40/mingw64/bin/“g++ -shared -s -static-libgcc -o file20d4364a7140.dll file20d4364a7140-win.def file20d4364a7140.o “C:/Users/Ben 10/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a” -L"C:/Users/Ben 10/Documents/R/win-library/4.0/StanHeaders/libs/x64” -lStanHeaders -L"C:/Users/Ben 10/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:/rtools40/mingw64/bin/“nm file20d4364a7140.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 file20d4364a7140.dll tmp.def file20d4364a7140.o “C:/Users/Ben 10/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a” -L"C:/Users/Ben 10/Documents/R/win-library/4.0/StanHeaders/libs/x64” -lStanHeaders -L"C:/Users/Ben 10/Documents/R/win-library/4.0/RcppParallel/lib/x64” -ltbb -L"C:/PROGRA~1/R/R-40~1.2/bin/x64" -lR ;
“C:/rtools40/mingw64/bin/“g++ -shared -s -static-libgcc -o file20d4364a7140.dll tmp.def file20d4364a7140.o “C:/Users/Ben 10/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a” -L"C:/Users/Ben 10/Documents/R/win-library/4.0/StanHeaders/libs/x64” -lStanHeaders -L"C:/Users/Ben 10/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) :
g++.exe: error: 10/Documents/R/win-library/4.0/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp: No such file or directorymake: *** [C:/PROGRA~1/R/R-40~1.2/etc/x64/Makeconf:229: file20d4364a7140.o] Error 1
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

Thanks Paul. That error implies that your StanHeaders package is still out of date. You can check this by running:

packageVersion("StanHeaders")

Where the output should be:

[1] ‘2.21.0.6’

(I suspect that you’ll be on 2.21.0.5)

Hmm, nope…

packageVersion(“StanHeaders”)
[1] ‘2.21.0.6’

Odd! The next thing to try would be to restart R, and then reinstall StanHeaders & RStan from source:

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

To speed up the install, make sure you set the number of cores to use for compilation with:

Sys.setenv(MAKEFLAGS="-jC")

Where C is the number of cores in your computer

OK - doing that now - it takes ages! (I only have 2 cores), let you know when done!

The model has run! Yippee!!
I did get this warning message though…
Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/rtools40/usr/mingw_/bin/g++’ not found

Is that anything to worry about??

1 Like

Great! That warning is safe to ignore and should be removed in the next release

2 Likes