R 闪亮应用程序运行代码生成pdf,然后将该pdf提供给用户下载

R 闪亮应用程序运行代码生成pdf,然后将该pdf提供给用户下载,r,shiny,R,Shiny,在我的Shining应用程序中,我希望能够单击下载按钮,让它执行我拥有的功能(在包中),在/results文件夹中创建pdf,然后将创建的文件作为下载提供给Shining应用程序用户。我从下面的服务器粘贴了我当前的下载按钮代码(这么多的部分不知道如何使其可复制)。想知道是否有人知道出了什么问题,我得到了下面的错误消息,但是函数\u to\u GENERATE\u PDF\u IN_u/results()确实运行并创建了PDF,但随后应用程序重新加载,用户从未收到下载提示 我收到错误(但pdf仍然

在我的Shining应用程序中,我希望能够单击下载按钮,让它执行我拥有的功能(在包中),在/results文件夹中创建pdf,然后将创建的文件作为下载提供给Shining应用程序用户。我从下面的服务器粘贴了我当前的下载按钮代码(这么多的部分不知道如何使其可复制)。想知道是否有人知道出了什么问题,我得到了下面的错误消息,但是函数\u to\u GENERATE\u PDF\u IN_u/results()确实运行并创建了PDF,但随后应用程序重新加载,用户从未收到下载提示

我收到错误(但pdf仍然是从我的功能正确生成的,只是应用程序重新加载,没有提供下载pdf的服务)。

   Error in self$downloads$set(name, list(filename = filename, contentType = contentType,  : 
      argument "content" is missing, with no default
我正在下载的app.R代码

observe({
  output$download_portfolio <- downloadHandler({
    FUNCTION_TO_GENERATE_PDF_IN_/results()
    filename = function() { paste(input$pdfname) }
    content = function(file) {
      file.copy(paste("results/",input$pdfname, file, overwrite = TRUE)
    } 
  })
  })
观察({

output$download\u portfolio您错误地使用了
downloadHandler
。您不需要像shiny提供的那样在此处使用
observe()
-函数,这意味着您可以将对象绑定到ui元素,只要对象发生更改,ui元素就会更新。 因此,您只需将
downloadHandler
分配给您的下载按钮,并告诉它如何生成要提供的文件:

library(shiny)

ui <- fluidPage( # just the download-button and a textInput for the filename
  textInput("pdfname", "Filename", "My.pdf"),
  downloadButton("outputButton", "Download PDF")
  )

server <- function(session, input, output) {
  # No need for an observer! Just assign the downloadHandler to the button.
  output$outputButton <- downloadHandler(input$pdfname, function(theFile) {
    # The first parameter is the name given to the file provided for download to the user.
    # The parameter in the function (theFile) is a placeholder for the name that is later
    # assigned to the download-file. 

    # Now you can call your pdf-generating function...
    makePdf()

    # ... and use file.copy to provide the file "in" the save-button
    file.copy(from = "/results/myGenerated.pdf", to = theFile)
  })
}

# Sample pdf-generating function:
makePdf <- function(){
  pdf(file = "/results/myGenerated.pdf")
  plot(cars)
  dev.off()
}

shinyApp(ui = ui, server = server)
库(闪亮)
用户界面
library(shiny)

ui <- fluidPage( # As above
  textInput("pdfname", "Filename", "My.pdf"),
  downloadButton("outputButton", "Download PDF")
  )

server <- function(input, output) {
  output$outputButton <- downloadHandler(input$pdfname, function(theFile) {
    # Here, your pdf-generator is provided with the "filename" that is used
    # to provide the file for the user.
    makePdf(theFile)
  })
}

# Sample pdf-generating function:
makePdf <- function(filename){
  pdf(file = filename)
  plot(cars)
  dev.off()
}

shinyApp(ui = ui, server = server)