Skip to content

rmarkdown::run and external resources

jmcphers edited this page Dec 1, 2014 · 2 revisions

What's the issue?

When you use rmarkdown::run to view an interactive presentation or document, you may notice that your references to external resources don't work, even if those references work in non-interactive documents.

For example, suppose you have a .html file alongside your R Markdown document, and you'd like to show it in a frame in the document:

# myfile.Rmd
## Enjoy this embedded graph

<iframe src="external.html"/>

This works fine in static R Markdown documents, but doesn't work in interactive R Markdown documents (i.e. documents containing runtime: shiny).

> rmarkdown::render("myfile.Rmd")      # Frame visible
> rmarkdown::run("myfile.Rmd")         # Frame not visible

What's going on?

When you use rmarkdown::run (or the Run Document button in RStudio), this is what's happening beneath the covers:

  1. Your R Markdown document is rendered to a temporary folder on disk.
  2. A Shiny web server is started to serve content from the temporary folder.
  3. A browser is opened and connected to the Shiny server.

In the example above, the file external.html doesn't exist in the temporary folder, so when the browser tries to load it, it won't be able to find it.

In order to make files from the document's original folder addressable in the rendered document, it would be necessary for the web server to expose that entire folder via HTTP. This isn't impossible but has significant security implications.

How can I work around the problem?

Currently, the best workaround is to host the external resources elsewhere, and reference them using absolute URLs (i.e. starting with HTTP or HTTPS).

## Enjoy this embedded graph

<iframe src="http://myhost.com/mydir/me/external.html"/>

When will this be fixed?

In the next few months we're hoping to design a mechanism by which you can declaratively describe your document's dependencies and selectively expose external resources, so that it's possible to use relative URLs in interactive documents without exposing entire host folders on the Shiny web server.

Watch this issue for status: #139