如何在闪亮的应用程序中下载PDF文件

时间:2022-02-27 21:22:22

I have one PDF in the www directory of my shiny app. I would like that file to be available for download. How can i do that.

我在我闪亮的应用的www目录中有一个PDF。我希望这个文件可以下载。我该怎么做呢?

The download example works well, but no idea to use it for PDF download from www directory.

下载示例运行得很好,但是不知道如何从www目录中下载PDF。

## Only run examples in interactive R sessions
if (interactive()) {

ui <- fluidPage(
  downloadLink("downloadData", "Download")
)

server <- function(input, output) {
  # Our dataset
  data <- mtcars

  output$downloadData <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".csv", sep="")
    },
    content = function(file) {
      write.csv(data, file)
    }
  )
}

shinyApp(ui, server)
}

1 个解决方案

#1


3  

Take a look in the downloadHandler function documentation, it has two arguments without default values: filename and content.

看看downloadHandler函数文档,它有两个参数,没有默认值:文件名和内容。

filename is basecaly the name of the file that will be downloaded. It has not to be inside a function. filename = "your-pdf-name.pdf" works as much as defining it inside the argumentless function.

文件名是将被下载的文件的名称。它不是在函数里面。文件名= " your-pdf-name。pdf“与在无参数函数中定义它一样有效。

content, in the other hand, creates a tempfile with the content that is going to be downloaded. In the most cases you're going to create a file that is going to be fulfilled with something your created in you app.

另一方面,内容创建一个包含将要下载的内容的tempfile。在大多数情况下,你会创建一个文件用你在app中创建的东西来实现。

How that is not your case, my solution provides something we call "gambiarra" in Brasil: it copies the file you want to download to the tempfile that shiny needs to the downloadHandler works. (I've tried just define it as the path to the file but it doesn't work)

我的解决方案提供了我们在巴西称之为“冈比亚”的东西:它将您想要下载的文件复制到tempfile中,而该tempfile需要用到downloadHandler。(我试过把它定义为文件的路径,但它不起作用)

ui <- fluidPage(
  downloadLink("downloadData", "Download")
)

server <- function(input, output) {

  output$downloadData <- downloadHandler(
    filename = "your-pdf-name.pdf",
    content = function(file) {
      file.copy("www/teste.pdf", file)
    }
  )
}

shinyApp(ui, server)

#1


3  

Take a look in the downloadHandler function documentation, it has two arguments without default values: filename and content.

看看downloadHandler函数文档,它有两个参数,没有默认值:文件名和内容。

filename is basecaly the name of the file that will be downloaded. It has not to be inside a function. filename = "your-pdf-name.pdf" works as much as defining it inside the argumentless function.

文件名是将被下载的文件的名称。它不是在函数里面。文件名= " your-pdf-name。pdf“与在无参数函数中定义它一样有效。

content, in the other hand, creates a tempfile with the content that is going to be downloaded. In the most cases you're going to create a file that is going to be fulfilled with something your created in you app.

另一方面,内容创建一个包含将要下载的内容的tempfile。在大多数情况下,你会创建一个文件用你在app中创建的东西来实现。

How that is not your case, my solution provides something we call "gambiarra" in Brasil: it copies the file you want to download to the tempfile that shiny needs to the downloadHandler works. (I've tried just define it as the path to the file but it doesn't work)

我的解决方案提供了我们在巴西称之为“冈比亚”的东西:它将您想要下载的文件复制到tempfile中,而该tempfile需要用到downloadHandler。(我试过把它定义为文件的路径,但它不起作用)

ui <- fluidPage(
  downloadLink("downloadData", "Download")
)

server <- function(input, output) {

  output$downloadData <- downloadHandler(
    filename = "your-pdf-name.pdf",
    content = function(file) {
      file.copy("www/teste.pdf", file)
    }
  )
}

shinyApp(ui, server)