R studio crash with simple example

I have been running Rstan on my mac one month ago and now when I try, the exact same code is failing. So I went back to the popular example:


model_code <- 'data {
  int<lower=0> J;         // number of schools 
  real y[J];              // estimated treatment effects
  real<lower=0> sigma[J]; // standard error of effect estimates 
parameters {
  real mu;                // population treatment effect
  real<lower=0> tau;      // standard deviation in treatment effects
  vector[J] eta;          // unscaled deviation from mu by school
transformed parameters {
  vector[J] theta = mu + tau * eta;        // school treatment effects
model {
  target += normal_lpdf(eta | 0, 1);       // prior log-density
  target += normal_lpdf(y | theta, sigma); // log-likelihood

schools_dat <- list(J = 8, 
                    y = c(28,  8, -3,  7, -1,  1, 18, 12),
                    sigma = c(15, 10, 16, 11,  9, 11, 10, 18))

m<- stan_model(model_code = model_code ,verbose = TRUE)

Everything above runs without any issues but when i try running the sampling line below the Rstudio crashes.

fit1 <- sampling(m,  data = schools_dat,chains = 1)

I tried running this many times but every time the R-studio crashes. Can you please help me?

Makevars file :

CFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CCFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CXXFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CPPFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include
SHLIB_CXXLDFLAGS+=-Wl,-rpath,/Library/Frameworks/R.framework/Resources/lib /Library/Frameworks/R.framework/Resources/lib/libc++abi.1.dylib
SHLIB_CXX11LDFLAGS+=-Wl,-rpath,/Library/Frameworks/R.framework/Resources/lib /Library/Frameworks/R.framework/Resources/lib/libc++abi.1.dylib
SHLIB_CXX14LDFLAGS+=-Wl,-rpath,/Library/Frameworks/R.framework/Resources/lib /Library/Frameworks/R.framework/Resources/lib/libc++abi.1.dylib
CXX14FLAGS=-O3 -march=native -mtune=native
CXX14FLAGS += -arch x86_64 -ftemplate-depth-256

OS : macOS Catalina 10.15.6.

R Studio version is 1.2.5001 and R version details are as below.

[1] "x86_64-apple-darwin15.6.0"

[1] "x86_64"

[1] "darwin15.6.0"

[1] "x86_64, darwin15.6.0"

[1] ""

[1] "3"

[1] "6.3"

[1] "2020"

[1] "02"

[1] "29"

$`svn rev`
[1] "77875"

[1] "R"

[1] "R version 3.6.3 (2020-02-29)"

[1] "Holding the Windsock"

RStan is much easier to work with on Macs if you have R 4.0 installed, which uses the Xcode clang++ rather than the LLVM clang++ and thus does not need all that stuff in ~/.R/Makevars. If you want to continue to use RStan with R 3.x on Macs, you probably are going to need to install RStan and any packages that come with Stan programs (such as rstanarm) from source with

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


I got the same error but for R 4.0.2, i.e. R-Studio crashed when running (but compiling works). The problem seem to come from the Makevar file suggested here:

“Configuration of the C++ Toolchain”

When I added the Makevar it crashes, when I removed it everything work smoothly.

R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.6

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

[1] sv_SE.UTF-8/sv_SE.UTF-8/sv_SE.UTF-8/C/sv_SE.UTF-8/sv_SE.UTF-8

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

other attached packages:
[1] rstan_2.21.2         ggplot2_3.3.2        StanHeaders_2.21.0-6

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5.2       pillar_1.4.6       compiler_4.0.2    
 [4] prettyunits_1.1.1  tools_4.0.2        pkgbuild_1.1.0    
 [7] jsonlite_1.7.0     lifecycle_0.2.0    tibble_3.0.3      
[10] gtable_0.3.0       pkgconfig_2.0.3    rlang_0.4.7       
[13] cli_2.0.2          rstudioapi_0.11    curl_4.3          
[16] parallel_4.0.2     loo_2.3.1          gridExtra_2.3     
[19] withr_2.2.0        vctrs_0.3.4        stats4_4.0.2      
[22] grid_4.0.2         glue_1.4.2         inline_0.3.15     
[25] R6_2.4.1           processx_3.4.3     fansi_0.4.1       
[28] callr_3.4.3        magrittr_1.5       scales_1.1.1      
[31] ps_1.3.4           codetools_0.2-16   ellipsis_0.3.1    
[34] matrixStats_0.56.0 assertthat_0.2.1   colorspace_1.4-1  
[37] V8_3.2.0           RcppParallel_5.0.2 munsell_0.5.0     
[40] crayon_1.3.4   

Hi Mans,

It’s previously been reported that including -march=native in the Makevars file will cause a crash on macOS. Would you mind trying with the Makevars file again, but with that flag removed?

Currently, my computer is running simulations, but Ill try as soon as its done.

If this is known, maybe change the code to create the Makevars here:

I just followed the instructions.

Yes, removing -march=native solves the problem. Do you want me to do a PR for the description at the rstan page?