Skip to content

Commit

Permalink
use onStop() for one of the Shiny vignette examples
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jan 5, 2025
1 parent 785aac1 commit f6b376c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
13 changes: 9 additions & 4 deletions vignettes/shiny.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mirai natively supports Shiny ExtendedTask to create scalable Shiny apps, which

In the example below, the app remains responsive, with the clock continuing to tick whilst the simulated expensive computation is running asynchronously in a parallel process. Also the button is disabled and the plot greyed out until the computation is complete.

By wrapping the `runApp()` call in `with(daemons(...), ...)` the daemons are set up for the duration of the app, exiting automatically when the app is stopped.
The call to `daemons()` is made at the top level, and `onStop()` may be used to automatically shut them down when the app exits.


``` r
Expand Down Expand Up @@ -59,10 +59,13 @@ server <- function(input, output, session) {

}

app <- shinyApp(ui = ui, server = server)

# run app using 2 local daemons
with(daemons(2), runApp(app))
daemons(2)

# automatically shutdown daemons when app exits
onStop(function() daemons(0))

shinyApp(ui = ui, server = server)
```
*Thanks to Joe Cheng for providing examples on which the above is based.*

Expand Down Expand Up @@ -107,6 +110,8 @@ The user can add multiple plots, making use of Shiny modules, each having a diff

The plots are generated asynchronously, and it is easy to see the practical limitations of the number of daemons set. For example, if updating 4 plots, and there are only 3 daemons, the 4th plot will not start to be generated until one of the other plots has finished.

By wrapping the `runApp()` call in `with(daemons(...), ...)` the daemons are set up for the duration of the app, exiting automatically when the app is stopped.


``` r
library(shiny)
Expand Down
13 changes: 9 additions & 4 deletions vignettes/shiny.Rmd.orig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mirai natively supports Shiny ExtendedTask to create scalable Shiny apps, which

In the example below, the app remains responsive, with the clock continuing to tick whilst the simulated expensive computation is running asynchronously in a parallel process. Also the button is disabled and the plot greyed out until the computation is complete.

By wrapping the `runApp()` call in `with(daemons(...), ...)` the daemons are set up for the duration of the app, exiting automatically when the app is stopped.
The call to `daemons()` is made at the top level, and `onStop()` may be used to automatically shut them down when the app exits.

```{r shinyextended, eval=FALSE}
library(shiny)
Expand Down Expand Up @@ -58,10 +58,13 @@ server <- function(input, output, session) {

}

app <- shinyApp(ui = ui, server = server)

# run app using 2 local daemons
with(daemons(2), runApp(app))
daemons(2)

# automatically shutdown daemons when app exits
onStop(function() daemons(0))

shinyApp(ui = ui, server = server)
```
*Thanks to Joe Cheng for providing examples on which the above is based.*

Expand Down Expand Up @@ -102,6 +105,8 @@ The user can add multiple plots, making use of Shiny modules, each having a diff

The plots are generated asynchronously, and it is easy to see the practical limitations of the number of daemons set. For example, if updating 4 plots, and there are only 3 daemons, the 4th plot will not start to be generated until one of the other plots has finished.

By wrapping the `runApp()` call in `with(daemons(...), ...)` the daemons are set up for the duration of the app, exiting automatically when the app is stopped.

```{r shinyextend2, eval=FALSE}
library(shiny)
library(mirai)
Expand Down

0 comments on commit f6b376c

Please sign in to comment.