Skip to content

Instantly share code, notes, and snippets.

@jcheng5
Last active June 23, 2024 18:51
Show Gist options
  • Save jcheng5/b65fe70899408cf8ce6ba887ca8a4880 to your computer and use it in GitHub Desktop.
Save jcheng5/b65fe70899408cf8ce6ba887ca8a4880 to your computer and use it in GitHub Desktop.
Extended task example app
library(shiny)
library(bslib)
library(future)
library(promises)
future::plan(future::multisession)
ui <- page_sidebar(
sidebar = sidebar(
numericInput("x", "x", 5),
numericInput("y", "y", 6),
input_task_button("calc", "Calculate", type = "primary"),
p(
strong("Status: "),
textOutput("status", inline = TRUE)
),
),
h4("x * y (fast)"),
verbatimTextOutput("result_fast"),
h4("x + y (extended/slow)"),
verbatimTextOutput("result", placeholder = TRUE)
)
server <- function(input, output, session) {
adder_task <- ExtendedTask$new(function(x, y) {
# This line would throw!
# input$x
future_promise({
Sys.sleep(3)
x + y
})
}) |> bind_button_to_task("calc")
observeEvent(input$calc, {
adder_task$invoke(input$x, input$y)
})
output$status <- renderText({
adder_task$status()
})
output$result <- renderText({
adder_task$result()
})
output$result_fast <- renderText({
input$x * input$y
})
}
shinyApp(ui, server)
@vedoa
Copy link

vedoa commented Jun 23, 2024

Line 33 should be
}) |> bind_task_button("calc")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment