Dealing with R 4.0

R version 4.0 was released on April 24. I would recommend that anyone who can update R do so. You have to reinstall all you packages, so do

install.packages("rstan", dependencies = TRUE)

And there are a few things that you may have to deal with.

Windows

You should uninstall previous versions of R and RTools. Then do,

file.rename(from = "~/.R/Makevars.win", to = "~/.R/Makevars_old")
file.remove("~/.R/Makevars")
cat("CXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarations", file = "~/.R/Makevars.win")
pkgbuild::has_build_tools(debug = TRUE)

which will download Rtools40.

If that does not work, you can always hard-code the path to the C++ compiler by putting

CXX14 = C:\Rtools\mingw64\bin\g++ -m$(WIN)

in your ~/.R/Makevars.win file.

Mac

In R, call

file.edit("~/.Renviron")

and delete any line that you may have adding something like /usr/local/clang? to the PATH.

If you use RStudio, there may be problems sampling in parallel from an interactive session, but a workaround is given here

Linux

There are no known new issues, but the most common old issue is trying to use a C++ compiler without C++14 support, particularly on RHEL and CentOS which need the RedHat developer toolset.

Solaris

There are no new issues, but you have to use the GNU toolchain rather than the Oracle one.

10 Likes

I did not need to install pkgbuild from github for Windows.
I spent the day updating an installation guide of mine for Windows and OSX (Mojave, and Catalina)
; install.txt (781.9 KB) <-- An HTML file, but it wouldnā€™t let me attach it.
I followed those exact steps listed, from clean installations of each OS. No github needed. What issue did you run into with pkgbuild?

Edit: Iā€™ll add that the only ā€˜issueā€™ Iā€™ve seen that isnā€™t addressed in the Troubleshooting section of that document, is on OSX: When using cores > 1, it will open safari with a blank page. No idea why. Does that for Mojave/High Sierra, and Catalina.

Hi @bgoodri -

What are the advantages for rstan in upgrading? Are there any breaking changes? Any big changes that will happen bc of this update? How will it impact rstantools?

Thanks much

Bob

From my testing today - The only changes required are to your Rtools toolchain.

On linux: Nothing changes, donā€™t worry about it.

On Windows: Uninstall R and Rtools35; Install R 4.0, and Rtools40. You donā€™t need any changes to Makevars.win, but you could clean it up by removing everything except what Ben posted above, primarily for optimizationā€™s sake. I did not need a Makevars.win at all to compile run rstan models. R 4.0 only needs Rtools40 for rstan.

On OSX (All flavors): Install R 4.0. Install Xtools if you donā€™t already have it. If you previously ran R 3.6.*, and installed macos-rtools, you can literally remove ~/.Renviron and ~/.R/Makevars (or rename them, or remove anything that configures a compilerā€™s PATH). R 4.0 only needs Xtools installed for rstan.

As for rstantools, I have not changed my bmgarch, iccier, nor omegad packages, all which were created on R 3.6 with rstantools, and they all compiled fine under R 4.0 [on linux, at least; did not test that on OSX or Windows].

1 Like

The main change is with the Windows toolchain, which is now based on gcc-8.4. This should eliminate any surprises from having an incomplete implementation of the C++14 standard in the old toolchain, based on gcc-4.9. In addition, it should be much less inconsistent when you put -mtune=native -march=native into you C++14FLAGS. In short, Stan programs should run faster, although they may compile slower.

On Macs, R is now compiled with Appleā€™s Xcode toolchain, rather than the open-source clang. That is disappointing, but it should eliminate the need to install rstan and packages that come with Stan programs from source in order for exceptions to be caught properly.

Adding

CXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated declarations

to my makevars.win file results in the following error:

library(rstan)
scode <- "
parameters {
  real y[2]; 
} 
model {
  y[1] ~ normal(0, 1);
  y[2] ~ double_exponential(0, 2);
} 
"
fit1 <- stan(model_code = scode, iter = 10, verbose = FALSE)

> 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.0/bin/x64/R CMD SHLIB file4bd848247cc9.cpp 2> file4bd848247cc9.cpp.err.txt' had status 1
2: In file(con, "r") :
  cannot open file 'file4bd848247cc9.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection

My makevars.win file looks as follows:

CXX14FLAGS = -O3
CXX14 = $(BINPREF)g++ -m$(WIN) -std=c++1y
CXX11FLAGS = -O3
CXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated declarations

Without the last line in question, compilation works fine.

I had problem and got error on sv4vectors package!!

Iā€™m not able to run compile any model under R 4.0 and Catalina. Do you know how can I fix it?

> library(rstan)
**Loading required package: StanHeaders**
**Loading required package: ggplot2**

**rstan (Version 2.19.3, GitRev: 2e1f913d3ca3)**

**For execution on a local, multicore CPU with excess RAM we recommend calling**

**options(mc.cores = parallel::detectCores()).**

**To avoid recompilation of unchanged Stan programs, we recommend calling**

**rstan_options(auto_write = TRUE)**

scode <- "

parameters {

real y[2];

}

model {

y[1] ~ normal(0, 1);

y[2] ~ double_exponential(0, 2);

}

"

fit1 <- stan(model_code = scode, iter = 10, verbose = FALSE)

print(fit1)

fit2 <- stan(fit = fit1, iter = 10000, verbose = FALSE) ]> scode <- "

+ parameters {

+ real y[2];

+ }

+ model {

+ y[1] ~ normal(0, 1);

+ y[2] ~ double_exponential(0, 2);

+ }

+ "

> fit1 <- stan(model_code = scode, iter = 10, verbose = FALSE)

**Error in compileCode(f, code, language = language, verbose = verbose) :**

**Compilation ERROR, function(s)/method(s) not created! In file included from <built-in>:1:**

**In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:**

**In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Dense:1:**

**In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Core:96:**

**In file included from /usr/local/opt/llvm/bin/../include/c++/v1/complex:245:**

**/usr/local/opt/llvm/bin/../include/c++/v1/cmath:317:9: error: no member named 'signbit' in the global namespace**

**using ::signbit;**

**~~^**

**/usr/local/opt/llvm/bin/../include/c++/v1/cmath:318:9: error: no member named 'fpclassify' in the global namespace**

**using ::fpclassify;**

**~~^**

**/usr/local/opt/llvm/bin/../include/c++/v1/cmath:319:9: error: no member named 'isfinite' in the global namespace; did you mean 'finite'?**

**using ::isfinite;**

**~**

**In addition: Warning message:**

**In system(cmd, intern = !verbose) :**

**running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB filea8c352a8a3e3.cpp 2> filea8c352a8a3e3.cpp.err.txt' had status 1**

**Error in sink(type = "output") : invalid connection**

> print(fit1)

**Error in print(fit1) : object 'fit1' not found**





   > sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.4

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rstan_2.19.3       ggplot2_3.3.0      StanHeaders_2.19.2

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6       pillar_1.4.3       compiler_4.0.0     prettyunits_1.1.1 
 [5] tools_4.0.0        pkgbuild_1.0.6     packrat_0.5.0      lifecycle_0.2.0   
 [9] tibble_3.0.1       gtable_0.3.0       pkgconfig_2.0.3    rlang_0.4.5       
[13] cli_2.0.2          rstudioapi_0.11    parallel_4.0.0     loo_2.2.0         
[17] gridExtra_2.3      withr_2.2.0        dplyr_0.8.5        vctrs_0.2.4       
[21] stats4_4.0.0       grid_4.0.0         tidyselect_1.0.0   glue_1.4.0        
[25] inline_0.3.15      R6_2.4.1           processx_3.4.2     fansi_0.4.1       
[29] purrr_0.3.4        callr_3.4.3        magrittr_1.5       scales_1.1.0      
[33] ps_1.3.2           ellipsis_0.3.0     matrixStats_0.56.0 assertthat_0.2.1  
[37] colorspace_1.4-1   munsell_0.5.0      crayon_1.3.4

What is the error when verbose = TRUE?

What are

readLines("~/.Renviron")
readLines("~/.R/Makevars")

?

1 Like

To Paul,
Arenā€™t you missing an hyphen here ?

-Wno-deprecated declarations

changing it to

-Wno-deprecated-declarations
1 Like

Yeah, sorry. I just now removed that space in the OP.

Now there is nothing
> readLines("~/.Renviron")
character(0)
> readLines("~/.R/Makevars")
character(0)

Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! In file included from <built-in>:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Core:96:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/complex:245:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/cmath:317:9: error: no member named 'signbit' in the global namespace
using ::signbit;
      ~~^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/cmath:318:9: error: no member named 'fpclassify' in the global namespace
using ::fpclassify;
      ~~^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/cmath:319:9: error: no member named 'isf

Right, thanks, I overlooked that.

Does

example(cxxfunction, package = "inline", run.dontrun = TRUE)

run?

2 Likes

Nope!

If I run that call Ben I get:

ld: warning: directory not found for option '-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin18/8.2.0'

And sure,

/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0

is what I have.

EDIT:

I added this to my .R/Makevars

FC = /usr/local/gfortran/bin/gfortran
FLIBS = -L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm

@torkar: But does Stan work?

Yes, except for when using cores=4, but thatā€™s an RStudio bug which thereā€™s a workaround for :)