shiny icon indicating copy to clipboard operation
shiny copied to clipboard

Example for ExtendedTask

Open dcaud opened this issue 1 year ago • 3 comments

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)

dcaud avatar Apr 05 '24 02:04 dcaud

Hi @dcaud, have you seen the article on non-blocking operations with ExtendedTask? It includes several examples.

gadenbuie avatar Apr 05 '24 11:04 gadenbuie

Thanks @gadenbuie. That is great. Wasn't able to find it with a web search.

dcaud avatar Apr 06 '24 23:04 dcaud

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.

shikokuchuo avatar Apr 18 '24 19:04 shikokuchuo