# \`rstan\` API calls crash \`R\` in \`RStudio\` (reason for \`bridgesampling\` crashes)

**URL:** <https://discourse.mc-stan.org/t/rstan-api-calls-crash-r-in-rstudio-reason-for-bridgesampling-crashes/25388>\
**Category:** RStan\
**Created:** [November 21, 2021, 10:48am UTC](https://discourse.mc-stan.org/t/rstan-api-calls-crash-r-in-rstudio-reason-for-bridgesampling-crashes/25388 "2021-11-21T10:48:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henrik\_Singmann](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/henrik_singmann/32/119_2.png) [@Henrik\_Singmann](https://discourse.mc-stan.org/u/Henrik_Singmann)\
**Post date:** [November 21, 2021, 10:48am UTC](https://discourse.mc-stan.org/t/rstan-api-calls-crash-r-in-rstudio-reason-for-bridgesampling-crashes/25388/1 "2021-11-21T10:48:17Z")

</div>

After having received a few reports of `R` crashes from `bridgesampling` users, I seem to have isolated the problem as calls to the `rstan` API on fitted model objects in `RStudio`.

In particular, the last two lines of the following code crash the `R` session reliably when run inside `RStudio` but not when run inside “bare” `R`. Seems to happen on both Windows and Mac and even after updating `RStudio` to the latest version or development version.

```r
library("rstan")
stanmodelcode <- "
data {
  int<lower=0> N;
  real y[N];
} 

parameters {
  real mu;
} 

model {
  target += normal_lpdf(mu | 0, 10);
  target += normal_lpdf(y | mu, 1);
} 
"

y <- rnorm(20) 
dat <- list(N = 20, y = y); 
fit <- stan(model_code = stanmodelcode, model_name = "example", 
            data = dat, chains = 0) 
unconstrain_pars(fit, list(mu = 0))
log_prob(fit, c(mu = 0))

```

Not sure there is much that can be done, but still wanted to let people know.

Also, it does not seem to crash on RStudio Server, although I haven’t tested it thoroughly.

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [November 21, 2021, 11:20am UTC](https://discourse.mc-stan.org/t/rstan-api-calls-crash-r-in-rstudio-reason-for-bridgesampling-crashes/25388/2 "2021-11-21T11:20:06Z")

</div>

Does using the stan-dev version of rstan help?

```
remove.packages(c("StanHeaders", "rstan"))
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

```

---

<div class="post-metadata">

**Author:** ![Henrik\_Singmann](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/henrik_singmann/32/119_2.png) [@Henrik\_Singmann](https://discourse.mc-stan.org/u/Henrik_Singmann)\
**Post date:** [November 21, 2021, 11:29am UTC](https://discourse.mc-stan.org/t/rstan-api-calls-crash-r-in-rstudio-reason-for-bridgesampling-crashes/25388/3 "2021-11-21T11:29:58Z")

</div>

Thanks, that does fix it (at least with `RStudio` development version, haven’t tested further). Any idea when this will go to CRAN?

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [November 21, 2021, 11:38am UTC](https://discourse.mc-stan.org/t/rstan-api-calls-crash-r-in-rstudio-reason-for-bridgesampling-crashes/25388/4 "2021-11-21T11:38:31Z")

</div>

That is very hard to predict. Maybe @andrjohns or @hsbadr would know more.

---

<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:** [November 21, 2021, 11:45am UTC](https://discourse.mc-stan.org/t/rstan-api-calls-crash-r-in-rstudio-reason-for-bridgesampling-crashes/25388/5 "2021-11-21T11:45:41Z")

</div>

I’m a bit out of date on the CRAN progress sorry, will have to wait for Hamada

---

<div class="post-metadata">

**Author:** ![Henrik\_Singmann](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/henrik_singmann/32/119_2.png) [@Henrik\_Singmann](https://discourse.mc-stan.org/u/Henrik_Singmann)\
**Post date:** [November 21, 2021, 11:49am UTC](https://discourse.mc-stan.org/t/rstan-api-calls-crash-r-in-rstudio-reason-for-bridgesampling-crashes/25388/6 "2021-11-21T11:49:36Z")

</div>

No problem. We will inform users that they have to use the development version until then.

---

<div class="post-metadata">

**Author:** ![hsbadr](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/hsbadr/32/15069_2.png) [@hsbadr](https://discourse.mc-stan.org/u/hsbadr)\
**Post date:** [November 21, 2021, 1:55pm UTC](https://discourse.mc-stan.org/t/rstan-api-calls-crash-r-in-rstudio-reason-for-bridgesampling-crashes/25388/7 "2021-11-21T13:55:03Z")

</div>

It’s up to @bgoodri now. There’s one major issue on Windows that’s temporary and can be bypassed: `rstan` classes depend on the headers they used at compilation time and it should be rebuilt from source after updating `StanHeaders`. Since we plan to update `rstan 2.26` immediately after releasing `StanHeaders 2.26` (and reverse dependency checks are run on Linux), this is a temporary issue on Windows. So, we could start preparing for a quick release or delay everything until we make any changes required for `rstan` to be compatible with all versions of the headers at both compilation and runtime. I haven’t looked into it because I think it isn’t worth the time if we can bypass it or maybe coordinate with CRAN.

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [November 21, 2021, 2:57pm UTC](https://discourse.mc-stan.org/t/rstan-api-calls-crash-r-in-rstudio-reason-for-bridgesampling-crashes/25388/8 "2021-11-21T14:57:18Z")

</div>

Thanks for the update.
