Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a PDF file in a Shiny app

Tags:

r

download

shiny

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.

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

## 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)
}
like image 331
AwaitedOne Avatar asked Nov 04 '16 10:11

AwaitedOne


People also ask

How do I create a downloadable PDF?

Open Acrobat and choose “Tools” > “Create PDF”. Select the file type you want to create a PDF from: single file, multiple files, scan, or other option. Click “Create” or “Next” depending on the file type. Follow the prompts to convert to PDF and save to your desired location.

How do I download a PDF for offline use?

You can also right-click the document and select Save as to save the PDF file. A window should appear, prompting you to specify the location where you'd like to save the file. Selecting the Desktop option makes it easy to find the PDF file later. If you like, you may rename the file at this point.


1 Answers

If the file is in the www folder then you simply have to provide a link to it in the UI

... (in UI)
  tags$a("Click here to get the PDF", href="your-pdf-name.pdf")
...

If the filename is not known at start time then use uiOutput/renderUI and set rv$filename to the filename when you generate it.

... (in UI)
  uiOutput("dlURL")
...


... (in server)
  rv <- reactiveValues(filename="")

  output$dlURL <- renderUI({
    tags$a("Click here to get the file", href=rv$filename)
  })
...
like image 188
Billy34 Avatar answered Sep 22 '22 17:09

Billy34