Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for ExtendedTask #4024

Open
dcaud opened this issue Apr 5, 2024 · 3 comments
Open

Example for ExtendedTask #4024

dcaud opened this issue Apr 5, 2024 · 3 comments

Comments

@dcaud
Copy link

dcaud commented Apr 5, 2024

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)

@gadenbuie
Copy link
Member

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

@dcaud
Copy link
Author

dcaud commented Apr 6, 2024

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

@shikokuchuo
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants