8
votes

Question

How could I create a function which would take a .Rmd file (containing htmlwidgets code) as its input, and output a html file containing its JavaScript/CSS dependencies?

Specifically, the tempfile rmarkdown generates for pandoc's --include-in-header argument, when rendering to html.

Details

Example -- myfile.Rmd:

This is some text

```{r}
library(dygraphs)
dygraph(nhtemp, main = "New Haven Temperatures")
```

Running rmarkdown::render('myfile.Rmd') passes a tempfile to the --include-in-header pandoc argument, containing all the JavaScript and CSS needed to render the interactive graphic (either compressed data:uri format, or html links to copied files). This is included in the final html file, so that everything renders in the browser. It's this tempfile I'd like to be able to generate.

It's appears to be output by passing a dependencies object to the internal function rmarkdown:::html_dependencies_as_string. I'd like to be able to generate this file for arbitrary .Rmd documents containing htmlwidgets code.

Context

I'm interested in blogging using knitr and Jekyll, as Yihui outlines here. However, the disadvantage of this approach is that none of the htmlwidgets functionality works, because the dependency injection step is missed. This is because pandoc is called by Ruby/Jekyll, and so does not benefit from htmlwidgets/rmarkdown's invisible magic. I'd like to add a function to my build process which writes the dependencies to a html file, which can be included in the html header by Jeykll if required.

1

1 Answers

-1
votes

This is a possible solution http://benjcunningham.org/2016/06/13/hacking-together-htmlwidgets-for-jekyll.html, that refers to an older post that also offers a solution: https://brendanrocks.com/htmlwidgets-knitr-jekyll/.

A bit hacky. An official solution to this problems is still pending. There is an open issue in yihui/knitr-jekyll: https://github.com/yihui/knitr-jekyll/issues/8