larry01 larry01 - 3 months ago 43
R Question

R shiny uploading a pdf from local drive does not work

I am trying to upload a pdf to shiny. If the pdf file is from the Internet, the following code works well:

library(shiny)
runApp(list(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
h5("use case - embed a pdf user guide in the app - embed as a local pdf or from web URL")
),
mainPanel(
tabsetPanel(
# using iframe along with tags() within tab to display pdf with scroll, height and width could be adjusted
tabPanel("Reference",
tags$iframe(style="height:800px; width:100%; scrolling=yes",
src="https://cran.r-project.org/doc/manuals/r-release/R-intro.pdf")),
tabPanel("Summary"),
tabPanel("Plot")
)
))

),
server = function(input, output,session){}
))


However, when I tried to upload a pdf saved in Desktop, which is also the working directory, I cannot see the pdf file. I used
src="example.pdf"
to replaced the web file link. As suggested by some other StackOverflow posts, I saved the pdf file in a folder named
www
, but it still not working.
The system is MacOS X El Capiton and safari browser. I am not sure if that makes any difference.
Thanks a lot!

Answer

You have two options. The first one: just put your file example.pdf on a /www directory where your app file is. The second: use the addResourcePath function before running your app to make a local directory accessible.

addResourcePath("pdfs", "c:/temp/mypdfs")

later use it as

src="pdfs/example.pdf"