You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/shiny.Rmd
+14-38Lines changed: 14 additions & 38 deletions
Original file line number
Diff line number
Diff line change
@@ -17,17 +17,18 @@ knitr::opts_chunk$set(
17
17
18
18
`mirai` may be used as an asynchronous / distributed backend to scale [Shiny](https://shiny.posit.co/) applications.
19
19
20
-
Depending on the options suppled to `daemons()`, tasks may be distributed across local background processes or multiple networked servers in an efficient and performant manner.
20
+
Depending on the options suppled to `daemons()`, mirai tasks may be distributed across local background processes or multiple networked servers in an efficient and performant manner.
21
+
21
22
22
23
#### Shiny ExtendedTask Example: Clock and Plot
23
24
24
-
'mirai' may be used within Shiny's ExtendedTask framework to create scalable Shiny apps, which are more responsive for a single user, as well as for multiple concurrent users. 'mirai' are accepted anywhere a 'promise', 'future' or 'future_promise' is currently accepted.
25
+
mirai may be used within Shiny's ExtendedTask framework (in `shiny` >= 1.8.1) to create scalable Shiny apps, which are more responsive for a single user, as well as for multiple concurrent users.
25
26
26
-
The examples below requires Shiny >= 1.8.1 and promises >= 1.3.0.
27
+
'mirai' are accepted anywhere a 'promise', 'future' or 'future_promise' is currently accepted (with `promises` >= 1.3.0).
27
28
28
-
Importantly, the app remains responsive, as shown by the clock continuing to tick whilst the simulated expensive computation is running. Also the button is disabled and the plot greyed out until the computation is complete.
29
+
In the example below, the app remains responsive, with the clock continuing to tick whilst the simulated expensive computation is running. Also the button is disabled and the plot greyed out until the computation is complete.
29
30
30
-
By wrapping the `runApp()` call in `with(daemons(...), ...)`this conveniently configures the daemons for the duration of the app, and they automatically exit when the app is stopped.
31
+
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.
31
32
32
33
```{r shinyextended, eval=FALSE}
33
34
library(shiny)
@@ -75,25 +76,26 @@ The key components to using ExtendedTask are:
2. In the server, create an 'ExtendedTask' object by calling `ExtendedTask$new()` on a function which is a call to `mirai()`, and optionally bind it to the button created in (1).
79
+
2. In the server, create an 'ExtendedTask' object by calling `ExtendedTask$new()` on a function which is a call to `mirai()`, and optionally bind it to the button created in (1). Remember to pass in any custom functions or variables as `...` or `.args` arguments to `mirai()`.
79
80
80
81
```{r shinystep2, eval=FALSE}
81
82
extended_task <- ExtendedTask$new(
82
83
function(x, y) mirai({Sys.sleep(y); runif(x)}, x = x, y = y)
83
84
) |> bind_task_button("btn")
84
85
```
85
-
3. In the server, create an observer on the input button, which invokes the 'ExtendedTask' object with the appropriate inputs.
86
+
3. In the server, create an observer on the input button, which invokes the 'ExtendedTask' object with the required parameters.
*The above example builds on original code by Joe Cheng, Daniel Woodie and William Landau.*
182
184
183
-
Again, the key components to using ExtendedTask are:
184
-
185
-
1. In the UI, use `bslib::input_task_button()`. This is optional, but automates the button being disabled during computation.
186
-
187
-
```{r shinystep21, eval=FALSE}
188
-
input_task_button(ns("resample"), "Resample")
189
-
```
190
-
191
-
2. In the server, create an 'ExtendedTask' object by calling `ExtendedTask$new()` on a function which is a call to `mirai()`, and optionally bind it to the button created in (1). In this example, as `run_task()` is defined outside of a package, it is passed along with its parameters as arguments to `mirai()`.
Whilst it is generally recommended to use the ExtendedTask framework, it is also possible for 'mirai' to plug directly into the reactive framework, without the use of 'promises' (implicitly or explicitly). This may be required for advanced uses of asynchronous programming.
188
+
Whilst it is generally recommended to use the ExtendedTask framework, it is also possible for mirai to plug directly into Shiny's reactive framework, without the use of 'promises' either implicitly or explicitly. This may be required for advanced uses of asynchronous programming, or where the use case does not fit the semantics of ExtendedTask.
213
189
214
-
The following is similar to the previous example. There is a button to submit tasks, which will be processed by one of 3 daemons, outputting a pretty spiral pattern upon completion. If more than 3 tasks are submitted at once, the chart updates 3 at a time, limited by the number of available daemons.
190
+
The following is similar to the previous example, but allows multiple tasks to be submitted at once, rather than one after the other as required by ExtendedTask. There is a button to submit tasks, which will be processed by one of 3 daemons, outputting a pretty spiral pattern upon completion. If more than 3 tasks are submitted at once, the chart updates 3 at a time, limited by the number of available daemons.
215
191
216
-
It can be seen that more boilerplate code is required to manage the 'mirai' tasks, but it otherwise functions similarly to the ExtendedTask example.
192
+
It requires more boilerplate code to manage the mirai tasks, but otherwise functions similarly to the ExtendedTask example.
0 commit comments