Learn R Programming

bslib (version 0.8.0)

bind_task_button: Bind input_task_button to ExtendedTask

Description

Sets up a shiny::ExtendedTask to relay its state to an existing input_task_button(), so the task button stays in its "busy" state for as long as the extended task is running.

Note that bind_task_button does not automatically cause button presses to invoke the extended task; you still need to use shiny::bindEvent() (or shiny::observeEvent()) to cause the button press to trigger an invocation, as in the example below.

bind_task_button cannot be used to bind one task button to multiple ExtendedTask objects; if you attempt to do so, any bound ExtendedTask that completes will cause the button to return to "ready" state.

Usage

bind_task_button(target, task_button_id, ...)

# S3 method for default bind_task_button(target, task_button_id, ...)

# S3 method for ExtendedTask bind_task_button(target, task_button_id, ..., session = get_current_session())

Value

The target object that was passed in.

Arguments

target

The target object (i.e. ExtendedTask).

task_button_id

A string matching the id argument passed to the corresponding input_task_button() call.

...

Further arguments passed to other methods.

session

A Shiny session object (the default should almost always be used).

Examples

Run this code
if (FALSE) { # rlang::is_interactive()

library(shiny)
library(bslib)
library(future)
plan(multisession)

ui <- page_sidebar(
  sidebar = sidebar(
    input_task_button("recalc", "Recalculate")
  ),
  textOutput("outval")
)

server <- function(input, output) {
  rand_task <- ExtendedTask$new(function() {
    future({
      # Slow operation goes here
      Sys.sleep(2)
      runif(1)
    }, seed = TRUE)
  })

  # Make button state reflect task.
  # If using R >=4.1, you can do this instead:
  # rand_task <- ExtendedTask$new(...) |> bind_task_button("recalc")
  bind_task_button(rand_task, "recalc")

  observeEvent(input$recalc, {
    rand_task$invoke()
  })

  output$outval <- renderText({
    rand_task$result()
  })
}

shinyApp(ui, server)
}

Run the code above in your browser using DataLab