Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shiny: open a pdf after clicking an actionButton

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.

like image 476
SixSigma Avatar asked Aug 05 '16 19:08

SixSigma


1 Answers

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)
like image 139
Michal Majka Avatar answered Nov 05 '22 01:11

Michal Majka