0

How to load PDF file in same window of R Shiny application?

Here is an example:

library(shiny)

ui <- fluidPage(
   navbarPage("Demo",
              tabPanel("Overview", fluidPage(fluidRow("Overview page"))),
              tabPanel("PDF file", fluidRow(uiOutput("load_pdf_file")))))

server <- function(input, output) {

  # 1. Load PDF file
  output$load_pdf_file <- renderUI({

    # 1.1. This loads pdf file in a 'new' window
    browseURL("http://www.africau.edu/images/default/sample.pdf")

    # 1.2. How to load pdf file in the 'same' window where R Shiny app works
    #      Expect to see pdf file on the page in 'PDF file' section
    # ...
  })

}

shinyApp(ui = ui, server = server)
James Z
  • 12,209
  • 10
  • 24
  • 44
Andrii
  • 2,843
  • 27
  • 33

1 Answers1

2

The method that I know of, does not rely on serving up PDF's it uses the standard HTML methods one would use in a traditional webpage, with the shiny code js wrappers to place it in an iFrame.

With this method, in the tabPanel you set up an iFrame and give it some dimension ( I picked arbitrary ones) the decide if it scrolls or not, the use src to supply it with the path to your pdf file. The condition of course is that your PDF file is discoverable by some kind of a local path or URL to the outside world.

library(shiny)

ui <- fluidPage(
   navbarPage("Demo",
              tabPanel("Overview", fluidPage(fluidRow("Overview page"))),
              tabPanel("PDF file", 
                          tags$iframe(style="height:800px; 
                                             width:200%; 
                                             scrolling=no", 
              src="https://your-path/your-file.pdf"))))
sconfluentus
  • 4,693
  • 1
  • 21
  • 40
  • Thanks for the idea. It works but not the way I expected to get: 1) open pdf page automatic when run app; 2) loaded pdf in not nice way. In any case thanks a lot! – Andrii Feb 23 '20 at 16:30
  • 2
    You will have to format the size dimensions to your document and space to get the 'nice way' and then decide if you want scrolling, probably you do if you would prefer that the PDF is not the whole screen. – sconfluentus Feb 23 '20 at 16:37
  • If you want to try the server method, I found this old post: https://stackoverflow.com/questions/19469978/displaying-a-pdf-from-a-local-drive-in-shiny – sconfluentus Feb 23 '20 at 16:38
  • Thanks. I saw it before. – Andrii Feb 23 '20 at 16:40