# Launching ShinyStan without blocking R session

**URL:** <https://discourse.mc-stan.org/t/launching-shinystan-without-blocking-r-session/3286>\
**Category:** RStan\
**Tags:** shinystan\
**Created:** [February 13, 2018, 2:39pm UTC](https://discourse.mc-stan.org/t/launching-shinystan-without-blocking-r-session/3286 "2018-02-13T14:39:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [February 13, 2018, 2:39pm UTC](https://discourse.mc-stan.org/t/launching-shinystan-without-blocking-r-session/3286/1 "2018-02-13T14:39:07Z")

</div>

This is a neat trick I found on [Tyler Morgan-Wall’s Twitter](https://twitter.com/tylermorganwall/status/962074911949840387) and originally attributed to [Joe Cheng](https://twitter.com/jcheng). You can run ShinyStan (or anything else Shiny) without blocking the session (which was always a pain point for me). My helper function to run ShinyStan without blocking is below:

```
launch_shinystan_nonblocking <- function(fit) {
  library(future)
  plan(multisession)
  future(
    launch_shinystan(fit) #You can replace this with any other Shiny app
  )
}

```

Hope that helps somebody!

---

<div class="post-metadata">

**Author:** ![famuvie](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/famuvie/32/497_2.png) [@famuvie](https://discourse.mc-stan.org/u/famuvie)\
**Post date:** [February 28, 2018, 9:59am UTC](https://discourse.mc-stan.org/t/launching-shinystan-without-blocking-r-session/3286/2 "2018-02-28T09:59:48Z")

</div>

Cool trick.  
What I have been doing is to save the fit object, reload it from another R session in the Terminal and launch shinystan from there.  
It has the advantage that does not require further libraries, and can also be made into a function:

```R
launch_shinystan_bkg <- function(fit) {
  tf <- tempfile()
  saveRDS(fit, tf)
  code_read <- deparse(call("<-", "fit", call("readRDS", tf)))
  code_launch <- "shinystan::launch_shinystan(fit)"
  code <- paste(code_read, code_launch, sep = "; ")
  system(paste("Rscript -e '", code, "' &"))
}

```

On the downside, it duplicates the object in memory which could be problematic for large objects. Although I’m guessing the `future` package probably duplicates behind the scene as well.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [March 2, 2018, 12:00am UTC](https://discourse.mc-stan.org/t/launching-shinystan-without-blocking-r-session/3286/3 "2018-03-02T00:00:02Z")

</div>

I get asked about this a lot, so I’m glad these workarounds do the job. Thanks for sharing these!

---

<div class="post-metadata">

**Author:** ![lukas-rokka](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lukas-rokka/32/4666_2.png) [@lukas-rokka](https://discourse.mc-stan.org/u/lukas-rokka)\
**Post date:** [July 29, 2019, 12:06pm UTC](https://discourse.mc-stan.org/t/launching-shinystan-without-blocking-r-session/3286/4 "2019-07-29T12:06:12Z")

</div>

I did not get either of the proposed methods to work on my Windows system.

What did work for me was to save the stan fit object to an .rds file, writing a R script with the line `shinystan::launch_shinystan(readRDS("stan_fit.rds"))`, and then source that file as a Rstudio Local Job. Very convenient for opening several Shinystan instances in the same time.
