Last active
June 23, 2024 18:51
-
-
Save jcheng5/b65fe70899408cf8ce6ba887ca8a4880 to your computer and use it in GitHub Desktop.
Extended task example app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 33 should be
}) |> bind_task_button("calc")