A parade of errors trying to run rstan on Windows 10

###################################################################
Operating System: Windows 10 x64, Intel i7-8850H, 64GB RAM
Rstan Version: 2.19.3
R Version: 4.0.0 (beta) and 3.6.3 and 3.6.2
Makevars:
CXXFLAGS=-O3 -Wno-unused-variable -Wno-unused-function

CXXFLAGS += -Wno-ignored-attributes -Wno-deprecated-declarations

CXXFLAGS=-O3 -Wno-unused-variable -Wno-unused-function

CXXFLAGS += -Wno-ignored-attributes -Wno-deprecated-declarations
##################################################################

It’s hard to say where this started, but Ill try to summarize everything I’ve experienced and tell you where I’m at now that has me tearing my hair out and begging for help. I am on a newish-to-me laptop teleworking from home. I work for the USGS, so my laptop has various levels of protection and lockdowns, but I am able to install software on my own. Yesterday, I tried to rerun a fairly simple set of Stan models (a pretty basic beta regression) with the goal of replicating some WAIC values. I know the Stan code works and the data is fine because I was able to successfully run this on my old laptop. Yesterday afternoon, I had been running R 3.6.2 and (I think) rstan 2.19.2. I was not able to get the model to compile or run. It gave me an error, but I did not save the error output at that time. I haven’t run any Stan models on this hand-me-down laptop, so I figured I might as well just update R and rstan.

When I went to CRAN I saw that R 4.0.0 is out for pre-release, so I figured I’d try it out. I installed R 4.0.0 and the beta Rtools 4.0. I installed rstan following the standard instructions. I was able to get 8schools.stan to run and my model to run. I had 16 models to run from the “fullest” model down to a suite of simpler models. The “fullest” model ran fine yesterday afternoon. So I kicked off the rest for an overnight run and unplugged (my brain, not my computer) for the day.

This morning I come to look at the results and only about 10 or 11 of the models had run. The 5 or 6 that did not run where the set of “most complex” models, but still one step down in complexity from the “full” model. All I was doing however was systematically reducing the number of terms in a model matrix for the precision term of a beta regression. I had three covariates, one continuous and two indicator variables the fullest model was every term up to and including a three-way interaction. These details are probably irrelevant however.

The error message I was getting just a few sweet hours ago for the models that did not run was the Error in unserialize(socklist[[n]]) : error reading from connection that I had run into with previous versions of R on my old laptop. Previously, and based on this thread I had resolved this problem by deleting the -march=native and -mtune=native lines in my Makevars.win file. However, that did not work this time. Nor did deleting my Makevars.win file entirely. Nor did running stanc(), stan_model() and sampling() sequentially as somebody else suggested. Attempting run a single chain crashed R.

I chalked this up to beta software (R 4.0.0 and Rtools 4.0) and instead decided to try the last stable version of R (3.6.3). So I installed R 3.6.3 and freshly installed rstan and dependencies again following the standard rstan install instructions. When I tried to run 8schools.stan, I get an actual Windows error box that pops up: “The procedure entry point CAR0 could not be located in the dynamic link library. C:/Users/dhance/AppData/Local/Temp/1/Rtmp4CjXmr/file11fc40191e1a.dll” and in the R session I get the error:
Error in inDL(x, as.logical(local), as.logical(now), ...) : unable to load shared object 'C:/Users/dhance/AppData/Local/Temp/1/Rtmp4CjXmr/file11fc40191e1a.dll': LoadLibrary failure: The specified procedure could not be found.

If I try it again the dll file name changes to something else, but similar. I’ve tried completely uninstalling both versions the beta version of Rtools and Rtools 3.5 and reinstalling Rtools. No dice same error. The (temp) file does exist:

file.exists('C:/Users/dhance/AppData/Local/Temp/1/Rtmp4CjXmr/file11fc389d5e5a.dll')
[1] TRUE 

After cup of coffee number three, lukewarm and black, I went back to R 4.0.0 and Rtools 4.0. Now I am able to run 8schools.stan But when I go to run the beta regression model (10 or 11 of which just ran yesterday with basically the same setup), now I get a new and different error.

hash mismatch so recompiling; make sure Stan code ends with a blank line
Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(cmd, intern = !verbose) :
  running command 'C:/Users/dhance/DOCUME~1/R/R-devel/bin/x64/R CMD SHLIB file57b41d0835ed.cpp 2> file57b41d0835ed.cpp.err.txt' had status 1
2: In file(con, "r") :
  cannot open file 'file57b41d0835ed.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection

What in the ever-loving fluff is going on? I’m sort of out of ideas here. Please help. I’d love to get rstan running on either version of R.

EDIT: Well this is just hilarious. Now using R 3.6.3 I am able to get the model I want to run now without errors, BUT NOT 8schools.stan. With R 4.0.0 I can get 8schools.stan to run, but not the model I want.

If you want to use R 3.6.3, you need the StanHeaders version to match the rstan version following

If you are trying R 4.0, does the full model work if you just do 1 chain?

If you are trying R 4.0, does the full model work if you just do 1 chain?

No. Same error code there.

My rstan version is 2.19.3 in both R 4.0 and R 3.6.3 and StanHeaders is 2.21.0-1 in both. So StanHeaders 2.19.2 is the appropriate version for 2.19.3?

StanHeaders 2.19.2 gives me the same error code for the 8schools.stan model (Error in inDL) under R 3.6.3.

I think you need to delete the .rda file from the working directory and compile it again.

I think you meant .rds. That did it for running 8schools.stan in rstan 2.19.3 with StanHeaders 2.19.2 under R 3.6.3. So it looks like I’m able to run Stan again.

Any idea about my errors under R 4.0.0? Should I just wait for the stable release? I did just try deleting the .rds file for that model in the appropriate working directory, but no dice under 4.0.0. That said StanHeaders is 2.21.0-1 for R 4.0.0 still.

And just another update, after deleting the .rds file for 8schools I’m now getting the same error for the 8schools model as I was getting for the other model under R 4.0.0. So now at least things are consistently broken for one version of R. Give that the warning message is pointing to something in the R-devel folder, I suspect this might be an issue with the development version of R.

Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(cmd, intern = !verbose) :
  running command 'C:/Users/dhance/DOCUME~1/R/R-devel/bin/x64/R CMD SHLIB fileac8368f296.cpp 2> fileac8368f296.cpp.err.txt' had status 1
2: In file(con, "r") :
  cannot open file 'fileac8368f296.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection

For the broken setup, can you call

library(rstan)
example(stan_model, run.dontrun = TRUE)

and post the part of the messages that has error: (with the colon)?

Compilation argument:
     C:/Users/dhance/DOCUME~1/R/R-devel/bin/x64/R CMD SHLIB fileac851cc2e00.cpp 2> fileac851cc2e00.cpp.err.txt 
    Error: C++14 standard requested but CXX14 is not defined

OK, that is fixable just by running

Ah, that did it. So the takeaway is that if you are (currently) trying to get rstan running on Windows 10 with R 4.0.0 follow the instructions to install from source rather than the “Getting Started” instructions. I imagine the 8schools.rds file that apparently caused the error after I switched back to R 3.6.3 was created under R 4.0.0 and so there was a version conflict which caused my later problems.

Thank you so much @bgoodri!