1

I am new to shiny and was wondering if there is a way to display a pdf file generated in "downloadHandler"?

I am using a package to do some biological analysis, and I can make it create a pdf file in downloadHandler. However, I am still struggling if I can view this pdf instead of downloading it.

This question is related to Shiny to output a function that generates a pdf file itself. Please see the below for the code that works for downloading the pdf output. Thanks so much!

library(shiny)
library(msa)
runApp(list(
   #Load the exmaple from the msa package.
   mySequenceFile <- system.file("examples", "exampleAA.fasta", package="msa"),
   mySequences <- readAAStringSet(mySequenceFile),
   myFirstAlignment <- msa(mySequences),
   # A simple shiny app.
   # Is it possible to see the generated pdf file on screen?
   ui = fluidPage(downloadButton('downloadPDF')),
   server = function(input, output) {
       output$downloadPDF = downloadHandler(
       filename = 'myreport.pdf',
       content = function(file) {
            msaPrettyPrint(
                myFirstAlignment
              , file = 'myreport.pdf'
              , output="pdf"
              , showNames="left"
              , showLogo="top"
              , consensusColor="BlueRed"
              , logoColors="accessible area"
              , askForOverwrite=FALSE)
       file.rename("myreport.pdf", file) # move pdf to file for downloading
       },
       contentType = 'application/pdf'
     )
  }
))
Community
  • 1
  • 1
l0110
  • 859
  • 1
  • 7
  • 17
  • Check out this SO question: [displaying a pdf from a local drive in shiny](http://stackoverflow.com/questions/19469978/displaying-a-pdf-from-a-local-drive-in-shiny) – Adam Birenbaum Sep 19 '16 at 18:51
  • Adam, could you please enlighten me how to find the correct src path (i.e. src=...) for the newly generated pdf file? I am running locally on a Mac. I tried hard but cannot find a right way. Thanks a lot! – l0110 Sep 19 '16 at 19:54

1 Answers1

3

If you intend to display the pdf, you should not use downloadHandler. Instead, just use your pdf printing function to generate the pdf file, but the key is

  1. Create a www folder under your Shiny project root
  2. Point the file argument of msaPrettyPrint to www/myreport.pdf
  3. Dynamically add an iframe to display the file. Note in the iframe you point to myreport.pdf directly without www, as Shiny automatically looks for static/media files inside the www folder.

See below for a working example (note I am not using the msa package here but the idea should be the same).

library(shiny)

ui <- shinyUI(fluidPage(

   titlePanel("Old Faithful Geyser Data"),

   sidebarLayout(
      sidebarPanel(
        actionButton("generate", "Generate PDF")
      ),

      mainPanel(
         uiOutput("pdfview")
      )
   )
))

server <- shinyServer(function(input, output) {

  observeEvent(input$generate, {
    output$pdfview <- renderUI({
      pdf("www/myreport.pdf")
      hist(rnorm(100))
      dev.off()
      tags$iframe(style="height:600px; width:100%", src="myreport.pdf")
    })
  })
})

shinyApp(ui = ui, server = server)
Xiongbing Jin
  • 11,779
  • 3
  • 47
  • 41
  • Hi warmoverflow, thanks so much for your help. I have trouble run the code above. However, if I split the code to the two file shiny (i.e. ui.R and service.R), it runs well. Can your code run on your system? I am using RStudio version 0.99.903, R 3.3.1, Shiny 0.14, Mac OS X El Capitan. – l0110 Sep 20 '16 at 17:53
  • It runs with no problem on my Windows machine. Not sure if it's something with OS X. What error messages did you see with the code? – Xiongbing Jin Sep 20 '16 at 18:53
  • It just said that it cannot find the file... Anyway, at least the two file code works. Thanks a lot for your help! – l0110 Sep 20 '16 at 18:58
  • I just tried on a Linux machine and the single file code works. Make sure you create the www folder bofore running. Anyway please mark the question as answered. – Xiongbing Jin Sep 20 '16 at 19:04