6

I am wondering whether it's possible to link a local pdf file to an action button in Shiny. For example, I have a manual button. A pdf file will be opened once the user clicks the "Manual" action button.

Thanks in advance.

SixSigma
  • 2,808
  • 2
  • 18
  • 21

1 Answers1

6

This is a solution that is going to display your pdf file in a new browser window after clicking on a button.

  • create a new www folder in the same directory as the ui.R script
  • put your pdf file, say, xyz.pdf in www folder
  • add a new parameter (HTML attribute) onclick to the actionButton and set it to "window.open('xyz.pdf')"

Example:

library(shiny)

ui <- fluidPage( 
  actionButton("pdf", "Manual", onclick = "window.open('xyz.pdf')")
)

server <- function(input, output) { }

shinyApp(ui = ui, server = server)

UPDATE:

Another way to open a pdf stored on the local drive is to observe for an event when an action button is pressed and then use built-in R function file.show():

library(shiny)

ui <- fluidPage( 
  actionButton("pdf", "Manual")
)

server <- function(input, output) { 

  observeEvent(input$pdf, {
    # Absolute path to a pdf
    file.show(file.path(R.home(), "doc", "NEWS.pdf"))
  })

  }

shinyApp(ui = ui, server = server)
Michal Majka
  • 5,332
  • 2
  • 29
  • 40
  • 2
    This is just not working. "Not Found' appears on the new tab. – Datium Aug 30 '19 at 15:52
  • That happens to me as well. The command works fine if I lunch the app from R studio using "shinyApp", but it does not work when I lunch it with "runApp" from the command line. Did you manage to solve it? Thanks – Rachael Nov 08 '19 at 12:00
  • 1
    Hi, I'm travelling till Monday and won't have time to look at it. Did you try to use the absolute path to the PDF? – Michal Majka Nov 08 '19 at 15:44
  • @Rachel I know it is a bit late but I hope the updated solution works in all possible scenarios. – Michal Majka Feb 03 '20 at 14:42