R Shiny: Download existing file

蓝咒 提交于 2019-12-31 08:44:08

问题


Let's say I have an existing zip file (out.zip) in my shiny app (i.e. located on a server). I would like to have the user be able to download this file. This question is very similar to this one. However, that question zips files within the downloadHandler whereas the zip file already exists in my case.

library(shiny)

app <- list(
  ui = fluidPage(
    titlePanel(""),
    sidebarLayout(
      sidebarPanel(
        downloadButton("downloadData", label = "Download")
      ),
      mainPanel(h6("Sample download", align = "center"))
    )
  ),

  server = function(input, output) {  
    output$downloadData <- downloadHandler(
      filename <- function() {
        paste("output", "zip", sep=".")
      },

      content <- function(file) {
        # not sure what to put here???
      },
      contentType = "application/zip"
    )
  }
)

shiny::runApp(app)

回答1:


After poking around with different file handling functions I discovered that file.copy can be used to download the file.

I change downloadHandler to:

output$downloadData <- downloadHandler(
  filename <- function() {
    paste("output", "zip", sep=".")
  },

  content <- function(file) {
    file.copy("out.zip", file)
  },
  contentType = "application/zip"
)


来源:https://stackoverflow.com/questions/33416557/r-shiny-download-existing-file

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