object tag in an iframe tag in a shiny app can't find file in www/ directory

1,098 views
Skip to first unread message

Bryan Britten

unread,
Dec 1, 2015, 3:33:44 PM12/1/15
to Shiny - Web Framework for R
I've got an app that is trying to make a "localized" version of the JS Timeline that was developed by Northwestern University where localized means that data doesn't have to be sent to the cloud in order for timelineJS to use it. The app itself serves as an interactive builder for the timeline, which updates with each addition of an element. The full code is too much to post in this forum, but can be found here.

My issue is this: In order to keep confidential information (files such as PDFs, sound files, images of suspects, etc) local, I do some behind the scenes checks about the files and if it needs to be local, I wrap it into an html file, which gets fed to the timeline, which gets wrapped in an iframe. When I try to do this with PDF documents, it coerces the HTML to include an iframe tag and it links the appropriate HTML file. The problem is that the nested file path, the one that links to the PDF document isn't being executed properly. It seems like no matter what I try, Shiny can't find the file.

Here's the code that's relevant to this process:

```
file_name <- file.path(...) # this is what I need to fix, I think
pdf_string <- paste('<object data = "', file_name, '" type = "application/pdf" height = "100%" width = "100%">',
                            '<p>It appears you don\'t have a PDF plugin for this browser.',
                            '<a href = "', file_name, '"> Click here to download the PDF file.</a></p></object>', sep = "")
write(pdf_string, file = "www/some_pdf.html")
```

When the HTML renders, it comes out like this:

```
<iframe src = "some_pdf.html">
  <h1>Not found</h1>
</iframe>
```

Opening some_pdf.html outside of the shiny app works fine. So how do I leverage my file path inside of Shiny to get this to work?

Thanks!

Joe Cheng

unread,
Dec 1, 2015, 3:41:45 PM12/1/15
to Bryan Britten, Shiny - Web Framework for R
So you're saying that users will run these Shiny apps on their own computers (i.e. running R on their own machines)? And you're trying to have the <a href="..."> point directly to the file path on the hard drive?

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/37627b99-b5c6-4757-8f90-eccb2770e093%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bryan Britten

unread,
Dec 1, 2015, 3:49:39 PM12/1/15
to Shiny - Web Framework for R, britte...@gmail.com
Correct. Each user will run this app locally. Any files they wish to use in the timeline should be saved in the www folder on their hard drive.

Bryan Britten

unread,
Dec 1, 2015, 5:46:37 PM12/1/15
to Shiny - Web Framework for R, britte...@gmail.com
It's worth pointing out that the solutions I have tried include pointing to the file directly on the hard drive. I've tried "file:://path/to/some_pdf.html", "www/path/to/some_pdf.html", "file://www/path/to/some_pdf.html", and any other combination that seemed to make sense.

Bryan Britten

unread,
Dec 3, 2015, 2:03:34 AM12/3/15
to Shiny - Web Framework for R, britte...@gmail.com
So I figured out the answer. Modern web browsers don't allow for a cross between http://localhost and file:/// when referencing files. Since Shiny runs on http://localhost, the use of file:/// was throwing everything off. In addition, when an iframe attempts to load a file, such as a PDF, the directory in which the html files that served as the source for the iframe is saved is the working directory. So the filepath for the PDF needs to account for that, either by having the file in the same folder, or using "../some.pdf" notation. Lastly, file names cannot have spaces in them. This was the biggest thing causing my file to not be found. 

In the end, the two fixes were this: 1) make sure the HTML files I generate dynamically for the iframes and any files I want to embed are saved in the same directory, and 2) standardize the file names.

On Tuesday, December 1, 2015 at 3:41:45 PM UTC-5, Joe Cheng [RStudio] wrote:

Joe Cheng

unread,
Dec 3, 2015, 4:07:29 AM12/3/15
to Bryan Britten, Shiny - Web Framework for R
Sorry I didn't have a chance to follow up--glad you were able to figure it out!

Rubén Álvarez Fernández

unread,
Aug 18, 2016, 10:02:11 AM8/18/16
to Shiny - Web Framework for R, britte...@gmail.com
Hi there... since this post is very related to my problem, I wonder if someone could help? I have just started this thread. Thanks!
Reply all
Reply to author
Forward
0 new messages