R 4.0, rstan, and you

I was planning on updating the install instructions over the weekend, but no better time for a first draft!

I’m going to assume that you’re on windows, let me know if that’s not the case.

Install RStan on R4.0 (Windows)

Remove old RStan configuration

If you had previously installed RStan under R3.6, you may have some configuration files left over that will cause issues under R4.0.

The first thing to check is whether the BINPREF environment variable is pointing to the old RTools installation by running Sys.getenv("BINPREF").

You can skip to the next section if your output looks like the following:

Sys.getenv("BINPREF")
[1] ""

However, if your output looks like:

Sys.getenv("BINPREF")
[1] "C:/Rtools/mingw_$(WIN)/bin/"

Then you need to check whether your .Rprofile file has been configured to set this variable on R startup. If you run the following command and get the output:

readLines("~/.Rprofile")
[1] "Sys.setenv(BINPREF = "C:/Rtools/mingw_$(WIN)/bin/")"

Then you need to delete your .Rprofile file. You can get the location of this file by running:

file.path(Sys.getenv("HOME"), ".Rprofile")

Delete that file and then restart R. If BINPREF is no longer set, then you should get the following output:

Sys.getenv("BINPREF")
[1] ""

RTools4 Installation

The next step is to install RTools4 following the instructions here: https://cran.r-project.org/bin/windows/Rtools/

Make sure that you follow the instructions that put RTools on the path. Repeated here for clarity:

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

Then restart R and verify that RTools is being found via:

Sys.which("make")
## "C:\\rtools40\\usr\\bin\\make.exe"

And that you can install packages from source:

install.packages("jsonlite", type = "source")

Install RStan

Install Packages

You can now install RStan via:

install.packages("rstan", repos = "https://cloud.r-project.org/",
                 dependencies = TRUE)

Before moving to the next section verify that the following packages are at or above the indicated version:

  • rstan: 2.21.2
  • StanHeaders: 2.21.0-6
  • inline: 0.3.16

Optimisations

You can speed up the estimation of your models by enabling some compiler options. To do this, you should add the following to your Makevars.win file:

CXX14FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2

Note that earlier installation guides also recommended the -march=native flag, but this will cause R to crash with RTools4 and is no longer recommended.

If you have other CXXFLAGS, CXX11FLAGS, or CXX14FLAGS lines already in your Makevars.win file, make sure that you delete those first. If you do not already have a Makevars.win file, or do not know where it is, then you can create this via R:

dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
M <- file.path(dotR, "Makevars.win")
if (!file.exists(M)) file.create(M)
cat("\nCXX14FLAGS += -O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2",
     file = M, sep = "\n", append = FALSE)

Verify Installation

To verify your installation, you can run the RStan example/test model:

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

The model should then compile and sample. You may also see the warning:

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

This is safe to ignore and will be removed in the next RStan release.

Encountering Errors

If you are unable to install RStan or successfully run the example/test model, then please open a new thread on the Stan forums (https://discourse.mc-stan.org) with a description of your problem and any error message, and we will help troubleshoot your installation.

2 Likes