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

meta code for shiny::ExtendedTask() #123

Open
nhward opened this issue Feb 14, 2025 · 0 comments
Open

meta code for shiny::ExtendedTask() #123

nhward opened this issue Feb 14, 2025 · 0 comments

Comments

@nhward
Copy link

nhward commented Feb 14, 2025

I have no success getting code within an extended task (i.e. shiny::ExtendedTask()) to become available via shinymeta. I imagine a metaExtendedTask() is missing to make this happen but, perhaps, I am over-complicating the problem.

Is there a simple workaround that I am not aware of?
If not, is this on anyone's feature list?

The code below is based on the ExtendedTask documentation with the output made meta-enabled. I think metaAction wrapped around the relevant code ( see "# Slow operation goes here" ) is the way forward, but not the complete answer.

library(shiny)
library(shinymeta)
ui <- page_fluid(
  titlePanel("Extended Task Demo"),
  p(
    'Click the button below to perform a "calculation"',
    "that takes a while to perform."
  ),
  input_task_button("recalculate", "Recalculate"),
  p(textOutput("result"))
)

server <- function(input, output) {
  rand_task <- ExtendedTask$new(function() {
    future(
      {
        # Slow operation goes here
        Sys.sleep(2)
        sample(1:100, 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("recalculate")
  bind_task_button(rand_task, "recalculate")

  observeEvent(input$recalculate, {
    # Invoke the extended in an observer
    rand_task$invoke()
  })

  output$result <- shinymeta:metaRender(
    shiny::renderText,
  {
    # React to updated results when the task completes
    number <- rand_task$result()
    paste0("Your number is ", number, ".")
  })
}

shinyApp(ui, server)
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

1 participant