Howto include js dependencies of DT datatable in Rmarkdown using knitr and pandoc

烂漫一生 提交于 2019-12-21 23:09:08

问题


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



来源:https://stackoverflow.com/questions/28303827/howto-include-js-dependencies-of-dt-datatable-in-rmarkdown-using-knitr-and-pando

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!