Shiny : Download zip archive

[亡魂溺海] 提交于 2021-01-28 06:51:53

问题


I am creating a Shiny application and I want to download my results in a zip archive. Before, it was working for me because I was using Rx34 bits. Unfortunately, it is not working since I have used Rx64bits. I found some example which isn’t working for me:

library(shiny)

ui <- shinyUI(fluidPage(

  titlePanel("Old Faithful Geyser Data"),

  sidebarLayout(
    sidebarPanel(
      downloadButton("downloadData", "Download")
    ),

    mainPanel(
      DT::dataTableOutput('myTable1')
    )
  )
))

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

  output$myTable1 <- DT::renderDataTable(iris)

  output$downloadData <- downloadHandler(
    filename = function() {
      paste0("output", ".zip")
    },
    content = function(file) {
      k <- input$myTable1_rows_selected
      fs <- c()
      for (i in k) {
        path <- paste0(i, ".docx")
        rmarkdown::render("test.rmd", rmarkdown::word_document(), output_file = path)
        fs <- c(fs, path)
      }
      zip(file, fs)
    },
    contentType = "application/zip"
  )

})

shinyApp(ui = ui, server = server)

The error is the following one:

Warning: Error in zip: 'files' must a character vector specifying one or more filepaths
Stack trace (innermost first):
    54: zip
    53: download$func [#17]
     4: <Anonymous>
     3: do.call
     2: print.shiny.appobj
     1: <Promise>
Error : 'files' must a character vector specifying one or more filepaths

I work on R64bits in Windows 7. Thanks


回答1:


When there are 0 rows selected, the call to zip fails because the files parameter, fs, is NULL.

Obviously, it depends what you want to do, but a quick and dirty hack is to explicitly create an empty file and place that in the output zip.

if (is.null(k)) {
    file.create('empty')
    zip(file, 'empty')
} else {
    zip(file, fs)
}


来源:https://stackoverflow.com/questions/47072918/shiny-download-zip-archive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!