Skip to content

Commit d37beee

Browse files
daattaligadenbuie
andauthored
ExtendedTask: add example to docs (#4087)
Co-authored-by: Garrick Aden-Buie <garrick@adenbuie.com>
1 parent 79ee256 commit d37beee

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
* Improve collection of deep stack traces (stack traces that are tracked across steps in an async promise chain) with `coro` async generators such as `elmer` chat streams. Previously, Shiny treated each iteration of an async generator as a distinct deep stack, leading to pathologically long stack traces; now, Shiny only keeps/prints unique deep stack trace, discarding duplicates. (#4156)
1010

11+
* Added an example to the `ExtendedTask` documentation. (@daattali #4087)
12+
1113
## Bug fixes
1214

1315
* Fixed a bug in `conditionalPanel()` that would cause the panel to repeatedly show/hide itself when the provided condition was not boolean. (@kamilzyla, #4127)

R/extended-task.R

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,54 @@
4141
#' is, a function that quickly returns a promise) and allows even that very
4242
#' session to immediately unblock and carry on with other user interactions.
4343
#'
44+
#' @examplesIf rlang::is_interactive() && rlang::is_installed("future")
45+
#'
46+
#' library(shiny)
47+
#' library(bslib)
48+
#' library(future)
49+
#' plan(multisession)
50+
#'
51+
#' ui <- page_fluid(
52+
#' titlePanel("Extended Task Demo"),
53+
#' p(
54+
#' 'Click the button below to perform a "calculation"',
55+
#' "that takes a while to perform."
56+
#' ),
57+
#' input_task_button("recalculate", "Recalculate"),
58+
#' p(textOutput("result"))
59+
#' )
60+
#'
61+
#' server <- function(input, output) {
62+
#' rand_task <- ExtendedTask$new(function() {
63+
#' future(
64+
#' {
65+
#' # Slow operation goes here
66+
#' Sys.sleep(2)
67+
#' sample(1:100, 1)
68+
#' },
69+
#' seed = TRUE
70+
#' )
71+
#' })
72+
#'
73+
#' # Make button state reflect task.
74+
#' # If using R >=4.1, you can do this instead:
75+
#' # rand_task <- ExtendedTask$new(...) |> bind_task_button("recalculate")
76+
#' bind_task_button(rand_task, "recalculate")
77+
#'
78+
#' observeEvent(input$recalculate, {
79+
#' # Invoke the extended in an observer
80+
#' rand_task$invoke()
81+
#' })
82+
#'
83+
#' output$result <- renderText({
84+
#' # React to updated results when the task completes
85+
#' number <- rand_task$result()
86+
#' paste0("Your number is ", number, ".")
87+
#' })
88+
#' }
89+
#'
90+
#' shinyApp(ui, server)
91+
#'
4492
#' @export
4593
ExtendedTask <- R6Class("ExtendedTask", portable = TRUE, cloneable = FALSE,
4694
public = list(

man/ExtendedTask.Rd

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)