R Shiny:异步下载处理程序 [英] R Shiny: async downloadHandler

查看:66
本文介绍了R Shiny:异步下载处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个闪亮的应用程序,需要花费大量时间下载zip文件.我正在尝试使用 futures promises 软件包来管理下载,以便其他用户可以在下载进行期间访问该应用程序.

I have a shiny app which takes a large amount of time downloading zip files. I am trying to use the futures and promises packages to manage the downloads so that other users can access the app while downloads are in progress.

该应用程序如下所示:

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 函数中并设置`,虽然这不会引发错误,但该应用程序不适用于其他用户在下载过程中.

I've tried wrapping the write.csv inside a future function and setting `and while this does not throw an error, the app is not available for other users during the download.

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 函数中,但出现错误:

I've also tried wrapping the entire downloadHandler function inside the future function, but I get the error:

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

Error in .subset2(x, "impl")$defineOutput(name, value, label) :
Unexpected MulticoreFuture output for DownloadUnexpected MultiprocessFuture output for DownloadUnexpected Future output for DownloadUnexpected environment output for Download

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

How can I handle the entire downloadHandler asyncronously? I am using the open source version of shiny server.

推荐答案

不知道您是否还需要答案,但是我认为您已经很接近了.以后,我已经包装了write.csv和zip,如下所示,它在我的测试中适用于多个用户.

Don't know if you still need an answer for this, but I think you were very close. I have wrapped both the write.csv and zip in future as below and it works for multiple users on my testing.

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)

这篇关于R Shiny:异步下载处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆