R Shiny:异步下载处理程序

Jos*_*tho 5 asynchronous r shiny

我有一个闪亮的应用程序,它需要大量时间下载 zip 文件。我正在尝试使用futurespromises包来管理下载,以便其他用户可以在下载过程中访问该应用程序。

该应用程序如下所示:

library(shiny)

ui <- fluidPage(
  downloadButton("Download", "Download")
)


server <- function(input, output){
  output$Download <- downloadHandler(
    filename = "Downloads.zip",
    content = function(file){
      withProgress(message = "Writing Files to Disk. Please wait...", {
        temp <- setwd(tempdir())
        on.exit(setwd(temp))
        files <- c("mtcars.csv", "iris.csv")

        write.csv(mtcars, "mtcars.csv")
        write.csv(iris, "iris.csv")



        zip(zipfile = file, files = files)
      })
    }
  )
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

我试过将函数write.csv内部包装起来future并设置`,虽然这不会引发错误,但在下载过程中其他用户无法使用该应用程序。

library(shiny)
library(promises)
library(future)
plan(multiprocess)

ui <- fluidPage(
  downloadButton("Download", "Download")
)


server <- function(input, output){
  output$Download <- downloadHandler(
    filename = "Downloads.zip",
    content = function(file){
      withProgress(message = "Writing Files to Disk. Please wait...", {
        temp <- setwd(tempdir())
        on.exit(setwd(temp))
        files <- c("mtcars.csv", "iris.csv")

        future(write.csv(mtcars, "mtcars.csv"))
        future(write.csv(iris, "iris.csv"))



        zip(zipfile = file, files = files)
      })
    }
  )
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

我也尝试将整个downloadHandler函数包装在future函数中,但出现错误:

.subset2(x, "impl")$defineOutput(name, value, label) 中的错误:
下载的意外 MulticoreFuture 输出下载的 Unexpected MultiprocessFuture 输出下载的意外未来输出下载的意外环境输出

我怎样才能downloadHandler异步处理整个过程?我正在使用闪亮服务器的开源版本。

tig*_*ers 6

不知道您是否仍然需要答案,但我认为您已经非常接近了。我已经将 write.csv 和 zip 包装在未来,如下所示,它适用于我的测试中的多个用户。

library(shiny)
library(promises)
library(future)
plan(multiprocess)

ui <- fluidPage(
  downloadButton("Download", "Download")
)


server <- function(input, output){
  output$Download <- downloadHandler(
    filename = "Downloads.zip",
    content = function(file){
      withProgress(message = "Writing Files to Disk. Please wait...", {
        temp <- setwd(tempdir())
        on.exit(setwd(temp))
        files <- c("mtcars.csv", "iris.csv")

        future({

        Sys.sleep(15)  
        write.csv(mtcars, "mtcars.csv")
        write.csv(iris, "iris.csv")



        zip(zipfile = file, files = files)})
      })
    }
  )
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)