Skip to content

How to use shiny.worker with downloadHandler #8

@latlio

Description

@latlio

Thanks for developing shiny.worker. I'm trying to use shiny.worker to handle downloads asynchronously. I'm aware that it's possible with future, but I'd like to be able to try this using shiny.worker. The scenario is that I'd like the renderText to update while a download is occurring in the background. I've placed the write.csv with Sys.sleep(10) within the worker$run_job to simulate a long-running process that I'd like to delegate to the background. However, there are two things that I think are wrong with this code, and I don't know how to approach them:

  1. I don't think write.csv(mtcars, file) is able to find the file arg in the parent content = function(file) environment
  2. It's not clear what I should specify in the args_reactive arg of worker$run_job given that it's a download button with no input$...

My minimal reprex is below:

library(shiny.worker)

worker <- initialize_worker()

ui <- fluidPage(
  downloadButton("download", "Download"),
  
  numericInput("count",
               NULL,
               1,
               step = 1),
  
  textOutput("text")
)

server <- function(input, output, session) {
  
  output$text <- renderText({
      paste(input$count)
  })
  
  output$download <- downloadHandler(
    filename = "data.csv",
    content = function(file) {
      worker$run_job("test", function(file = file) {
        temp <- setwd(tempdir())
        on.exit(setwd(temp))
        Sys.sleep(10)
        write.csv(mtcars, file)
    }
  )
}

shinyApp(ui, server)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions