Howto include js dependencies of DT datatable in R

2019-04-14 03:36发布

问题:

Is there a way to compile RMarkdown documents with datatables produced by DT outside of RStudio?

I'm trying to include a datatable widget in an RMarkdown document that I then want to convert to html using knitr and pandoc. This works fine with RStudio but if I try to do the same with knitr and pandoc I'm unable to get a working html file.

Here is a minimal example of an Rmd file that works with RStudio but not otherwise:

--- 
title: "Minimal DT example"
---

<style type="text/css"> table, table th, table td {   border: none; } </style>

```{r} 
library(DT) 
datatable(iris) 
```

Which I would then like to convert to html using:

knitr::knit('example.Rmd')
knitr::pandoc('example.md',format="html")

I am aware that RStudio uses a much more complicated pandoc call:

/usr/lib/rstudio/bin/pandoc/pandoc scratch.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output scratch.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template /home/user/R/x86_64-pc-linux-gnu-library/3.1/rmarkdown/rmd/h/default.html --variable 'theme:bootstrap' --include-in-header /tmp/RtmpMLtVfF/rmarkdown-str24935297671d.html --mathjax --variable 'mathjax-url:https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' --no-highlight --variable highlightjs=/home/user/R/x86_64-pc-linux-gnu-library/3.1/rmarkdown/rmd/h/highlight

which I can recreate up to the part where this tmp file is included in the header:

/tmp/RtmpMLtVfF/rmarkdown-str24935297671d.html

I assume that this file contains the js sources for jquery and datatables. I have tried to add them by hand from the package source - but not succeeded - and would anyways like a solution that works out of the box.

回答1:

The package RStudio is using under the hood is rmarkdown. It handles the Knit command and delegates work to knitr and pandoc, and it's also responsible for bundling dependencies and injecting them into the doc.

You likely already have the package installed, so you should be able to do this to produce your HTML file in one go:

rmarkdown::render('example.Rmd')

More resources:

RMarkdown on CRAN

RMarkdown Introduction