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
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:
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?
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)
> 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
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.
Furthermore, you can explicitly define the port by 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.