R闪亮的异步下载XLSX

时间:2019-11-12 09:08:10

标签: r asynchronous shiny

我想让多个用户下载excel文件。 This is我指的是;使用异步下载csv。

我使用 openxlsx 包,但失败了,最小示例如下,在此先感谢:

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

ui <- fluidPage(
  h2("Async downloadHandler test"),
  hr(),
  downloadButton("download", "Download ")
)

server <- function(input, output, session) {
  output$download <- downloadHandler("example.xlsx", function(file) {

        future
        ({
          wb <- createWorkbook()
          addWorksheet(wb, "Cars")
          addWorksheet(wb, "Formula")
          x <- mtcars[1:6,]
          writeData(wb, "Cars", x, startCol = 2, startRow = 3, rowNames = TRUE)
        }) %...>%
        {             
          saveWorkbook(wb, file = "addFilterExample.xlsx", overwrite = TRUE)
        }

  })

}
shinyApp(ui, server)

1 个答案:

答案 0 :(得分:0)

我在这里发布了工作示例:

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

ui <- fluidPage(
  h2("Async downloadHandler test"),
  hr(),
  downloadButton("download", "Download ")
)

server <- function(input, output, session) {

  output$download <- downloadHandler(
    filename = function() { 
      paste(Sys.Date(),".xlsx", sep="_")
    },
    content = function(file) {
      future({
        Sys.sleep(15)
      }) %...>%  
      { wb <- createWorkbook()
        addWorksheet(wb, "Cars")
        addWorksheet(wb, "Formula")
        x <- mtcars[1:6,]
        writeData(wb, "Cars", x, startCol = 2, startRow = 3, rowNames = TRUE) 
        saveWorkbook(wb, file, overwrite = TRUE)}
    }
  )
}
shinyApp(ui, server)