From 3c5bb3619cbd7b49a2716d6c393e15bf35ee179c Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Fri, 6 Dec 2024 14:02:10 -0500 Subject: [PATCH] chore: document --- man/ExtendedTask.Rd | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/man/ExtendedTask.Rd b/man/ExtendedTask.Rd index 893017320b..ad2da1b1f1 100644 --- a/man/ExtendedTask.Rd +++ b/man/ExtendedTask.Rd @@ -45,6 +45,57 @@ is, a function that quickly returns a promise) and allows even that very session to immediately unblock and carry on with other user interactions. } +\examples{ +\dontshow{if (rlang::is_interactive() && rlang::is_installed("future")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} + +library(shiny) +library(bslib) +library(future) +plan(multisession) + +ui <- page_fluid( + titlePanel("Extended Task Demo"), + p( + 'Click the button below to perform a "calculation"', + "that takes a while to perform." + ), + input_task_button("recalculate", "Recalculate"), + p(textOutput("result")) +) + +server <- function(input, output) { + rand_task <- ExtendedTask$new(function() { + future( + { + # Slow operation goes here + Sys.sleep(2) + sample(1:100, 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("recalculate") + bind_task_button(rand_task, "recalculate") + + observeEvent(input$recalculate, { + # Invoke the extended in an observer + rand_task$invoke() + }) + + output$result <- renderText({ + # React to updated results when the task completes + number <- rand_task$result() + paste0("Your number is ", number, ".") + }) +} + +shinyApp(ui, server) + +\dontshow{\}) # examplesIf} +} \section{Methods}{ \subsection{Public methods}{ \itemize{