1

Intention:

I'm trying to build a Shiny app that contains a pdf viewer. I'm trying my best to replicate this example:

displaying a pdf from a local drive in shiny

Problem:

In the reprex below, I download a dummy pdf into the 'www' directory in the same root as 'App.R'.

As far as I can understand, the http protocols innate to Shiny apps require me to set the source for the iframe as "http://localhost/test.pdf", as opposed to "www/test.pdf". But when I run this code, I get an iframe with nothing but this in:

"Aw Snap!" emote, with message: localhost didn't send any data

I've also followed the additional advice in the previously mentioned SO thread - running the app using the 'Run App' button in RStudio, instead of Ctrl+return-ing through the code. No success. Anyone know how I can make this work?

Reprex:

require(shiny)
#> Loading required package: shiny

ui = fluidPage(
  htmlOutput("pdfViewer")
)

server = function(input, output, session) {
  ### Libraries
  require(pdftools)
  
  ### Create 'www' dir (understand that this
  ### is where 'localhost' stuff is kept?)
  suppressWarnings(dir.create("www"))
  
  ### Make a test pdf file using the w3 dummy pdf
  pdf = "www/test.pdf"
  download.file(url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
                       destfile = pdf)
  
  ### Define the http: address for the pdf
  pdfHttp = "http://localhost/test.pdf"
  
  output$pdfViewer = 
    renderText({
      ### Copied this from here: "https://stackoverflow.com/a/21024943/11149547"
      return(paste('<iframe style="height:600px; width:100%" src="', pdfHttp, '"></iframe>', sep = ""))
    })
}

shinyApp(ui = ui, server = server)

Appendix (R version data)

> R.version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          6.1                         
year           2019                        
month          07                          
day            05                          
svn rev        76782                       
language       R                           
version.string R version 3.6.1 (2019-07-05)
nickname       Action of the Toes
Community
  • 1
  • 1
Captain Hat
  • 2,444
  • 1
  • 14
  • 31
  • have you tried using a relative path for the pdf file. Like `test.pdf` or `www/test.pdf`? – cory Feb 17 '20 at 19:54
  • From what I've read (see the link above), the iframe requires a reference to `localhost`, rather than a relative path. If you can get a relative path to work I'd be very interested in hearing about it. – Captain Hat Feb 17 '20 at 23:38
  • You save it to a `www` folder, but then your pdfHttp path doesn't include that `www` folder. Why? – cory Feb 18 '20 at 00:11
  • Because `pdfHttp` isn't a path to a local file, but a web address within a local server. – Captain Hat Feb 18 '20 at 09:26

1 Answers1

1

I needed to add a port to the local host address.

I've learnt that http://localhost is synonymous with http://127.0.0.1. I then noticed that when my Shiny App was running in browser, the omnibox listed the address as: 127.0.0.1:5056.

I replaced http://localhost/test.pdf in the above code with http://127.0.0.1:6056/test.pdf, and now the pdf loads.

I imagine this is related to the fact that I'm using my work computer, which part of a local network of some kind.

Edit:

Furthermore, you can explicitly define the port my running options(shiny.port = 6056) before you run runApp(server = server, ui = ui). I needed to do this in the end, because my other Shiny App (not reprex) launched to a different port.

Community
  • 1
  • 1
Captain Hat
  • 2,444
  • 1
  • 14
  • 31