shiny
shiny copied to clipboard
Example for ExtendedTask
Can someone provide an example of ExtendedTask in a simple shiny app? Any example would be helpful, even if it doesn't match the example code below.
library(shiny)
ui <- fluidPage(
titlePanel("Async Operation with ExtendedTask"),
actionButton("start_btn", "Start Long Operation"),
verbatimTextOutput("status"),
verbatimTextOutput("result")
)
server <- function(input, output, session) {
# Define the async function
longOperation <- function() {
Sys.sleep(5) # Simulate a time-consuming task
runif(1, 0, 100) # Return a random number as the result
}
# Create the ExtendedTask
myTask <- ExtendedTask$new(func = longOperation)
observeEvent(input$start_btn, {
myTask$invoke() # Start the operation
})
output$status <- renderText({
myTask$status()
})
output$result <- renderText({
req(myTask$result()) # Use req to wait for the result
paste("Operation result:", myTask$result())
})
}
shinyApp(ui, server)
Hi @dcaud, have you seen the article on non-blocking operations with ExtendedTask? It includes several examples.
Thanks @gadenbuie. That is great. Wasn't able to find it with a web search.
The mirai Shiny vignette has some further ExtendedTask examples: https://shikokuchuo.net/mirai/articles/shiny.html Credit to Joe, they are all derived from examples he has provided.