# R studio crash with simple example

**URL:** <https://discourse.mc-stan.org/t/r-studio-crash-with-simple-example/17597>\
**Category:** Modeling\
**Tags:** rstan\
**Created:** [August 21, 2020, 6:43pm UTC](https://discourse.mc-stan.org/t/r-studio-crash-with-simple-example/17597 "2020-08-21T18:43:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shanakap](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@shanakap](https://discourse.mc-stan.org/u/shanakap)\
**Post date:** [August 21, 2020, 6:43pm UTC](https://discourse.mc-stan.org/t/r-studio-crash-with-simple-example/17597/1 "2020-08-21T18:43:41Z")

</div>

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:

```
library(rstan)

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.

```
R.Version()
$platform
[1] "x86_64-apple-darwin15.6.0"

$arch
[1] "x86_64"

$os
[1] "darwin15.6.0"

$system
[1] "x86_64, darwin15.6.0"

$status
[1] ""

$major
[1] "3"

$minor
[1] "6.3"

$year
[1] "2020"

$month
[1] "02"

$day
[1] "29"

$`svn rev`
[1] "77875"

$language
[1] "R"

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

$nickname
[1] "Holding the Windsock"

```

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [August 22, 2020, 4:50pm UTC](https://discourse.mc-stan.org/t/r-studio-crash-with-simple-example/17597/2 "2020-08-22T16:50:27Z")

</div>

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")

```

---

<div class="post-metadata">

**Author:** ![mans\_magnusson](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mans_magnusson/32/7093_2.png) [@mans\_magnusson](https://discourse.mc-stan.org/u/mans_magnusson)\
**Post date:** [September 2, 2020, 11:53am UTC](https://discourse.mc-stan.org/t/r-studio-crash-with-simple-example/17597/3 "2020-09-02T11:53:14Z")

</div>

Hi!

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:

> **[stan-dev/rstan](https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started)**
>
> RStan, the R interface to Stan. Contribute to stan-dev/rstan development by creating an account on GitHub.

  
under  
“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

locale:
[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   

```

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [September 3, 2020, 12:35am UTC](https://discourse.mc-stan.org/t/r-studio-crash-with-simple-example/17597/4 "2020-09-03T00:35:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mans\_magnusson](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mans_magnusson/32/7093_2.png) [@mans\_magnusson](https://discourse.mc-stan.org/u/mans_magnusson)\
**Post date:** [September 3, 2020, 6:02am UTC](https://discourse.mc-stan.org/t/r-studio-crash-with-simple-example/17597/5 "2020-09-03T06:02:50Z")

</div>

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:

> **[stan-dev/rstan](https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started)**
>
> RStan, the R interface to Stan. Contribute to stan-dev/rstan development by creating an account on GitHub.

I just followed the instructions.

---

<div class="post-metadata">

**Author:** ![mans\_magnusson](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mans_magnusson/32/7093_2.png) [@mans\_magnusson](https://discourse.mc-stan.org/u/mans_magnusson)\
**Post date:** [September 3, 2020, 6:18am UTC](https://discourse.mc-stan.org/t/r-studio-crash-with-simple-example/17597/6 "2020-09-03T06:18:15Z")

</div>

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