From 23e47b0a60647e247a9c6cc3d578f6f471b3c735 Mon Sep 17 00:00:00 2001 From: Dean Attali Date: Wed, 5 Jun 2024 22:31:16 -0400 Subject: [PATCH] ExtendedTask: add example to docs --- R/extended-task.R | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/R/extended-task.R b/R/extended-task.R index 659a41f471..f03958d569 100644 --- a/R/extended-task.R +++ b/R/extended-task.R @@ -41,6 +41,45 @@ #' is, a function that quickly returns a promise) and allows even that very #' session to immediately unblock and carry on with other user interactions. #' +#' @examplesIf rlang::is_interactive() +#' +#' library(shiny) +#' library(bslib) +#' library(future) +#' plan(multisession) +#' +#' ui <- page_sidebar( +#' sidebar = sidebar( +#' input_task_button("recalc", "Recalculate") +#' ), +#' textOutput("outval") +#' ) +#' +#' server <- function(input, output) { +#' rand_task <- ExtendedTask$new(function() { +#' future({ +#' # Slow operation goes here +#' Sys.sleep(2) +#' runif(1) +#' }, seed = TRUE) +#' }) +#' +#' # Make button state reflect task. +#' # If using R >=4.1, you can do this instead: +#' # rand_task <- ExtendedTask$new(...) |> bind_task_button("recalc") +#' bind_task_button(rand_task, "recalc") +#' +#' observeEvent(input$recalc, { +#' rand_task$invoke() +#' }) +#' +#' output$outval <- renderText({ +#' rand_task$result() +#' }) +#' } +#' +#' shinyApp(ui, server) +#' #' @export ExtendedTask <- R6Class("ExtendedTask", portable = TRUE, cloneable = FALSE, public = list(