Brilliant application startup code to create a pdf file, then suggest this file to be downloaded to the user

In my brilliant application, I want to be able to click the download button, execute the function that I have (in the package) that creates a pdf file in the / results folder, and then offers this brilliant application user as the download. I pasted my current download_portfolio download code from the server below (so many parts are not sure how I can make it reproducible). I wanted to find out if anyone has an idea of ​​what is going wrong, I get an error message below, however, the FUNCTION_TO_GENERATE_PDF_IN_ / results () function starts and creates a PDF file, but then the application is restarted and the user is never asked to download.

The error I get (but the pdf code is still generated correctly from my function, just restarting the application and there is no suggestion to download the pdf file).

Error in self$downloads$set(name, list(filename = filename, contentType = contentType, : argument "content" is missing, with no default 

app.R code where I work on loading

 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) } }) }) 
+5
source share
1 answer

You are using downloadHandler incorrectly. Here you do not need the observe() function, since shiny provides "reactivity" , which means that you can associate objects with updated ui elements whenever the object changes. Therefore, you only need to assign downloadHandler to the downloadHandler button and tell how to create the file you want to provide:

 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) 

However, at first you do not need to store the files, you can directly save them in the "download button":

 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) 
+4
source

All Articles