【问题标题】:R Shiny: async downloadHandlerR Shiny:异步下载处理程序
【发布时间】:2019-04-27 22:13:33
【问题描述】:

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

应用如下所示:

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)

我尝试将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)

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

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

如何异步处理整个downloadHandler?我正在使用闪亮服务器的开源版本。

【问题讨论】:

    标签: r asynchronous shiny


    【解决方案1】:

    不知道你是否还需要这个答案,但我认为你已经很接近了。我已经将 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)
    

    【讨论】:

    • 谢谢@tigerloveslobsters。只是附加注释,对于反应性值/表达式,必须在启动未来之前阅读它们。
    猜你喜欢
    • 2017-10-10
    • 2018-01-14
    • 2018-07-29
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2011-06-23
    • 1970-01-01
    相关资源
    最近更新 更多