R shiny: open a pdf after clicking an actionButton
Asked Answered
S

1

7

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.

Stilton answered 5/8, 2016 at 19:44 Comment(0)
W
7

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)
Wickner answered 6/8, 2016 at 0:5 Comment(4)
This is just not working. "Not Found' appears on the new tab.Fokos
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? ThanksHippo
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?Wickner
@Rachel I know it is a bit late but I hope the updated solution works in all possible scenarios.Wickner

© 2022 - 2024 — McMap. All rights reserved.