如何使用 knitr 和 pandoc 在 R Markdown 中包含 DT 数据表的 js 依赖项

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

有没有办法在 RStudio 之外用 DT 生成的数据表编译 RMarkdown 文档?

我正在尝试在 RMarkdown 文档中包含一个数据表小部件,然后我想使用 knitr 和 pandoc 将其转换为 html。这适用于 RStudio,但如果我尝试对 knitr 和 pandoc 做同样的事情,我将无法获得工作的 html 文件。

这是一个 Rmd 文件的最小示例,它可以与 RStudio 一起使用,但不能与其他方式一起使用:

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

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

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

然后我想将其转换为 html 使用:

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

我知道 RStudio 使用更复杂的 pandoc 调用:

/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

我可以重新创建此 tmp 文件包含在 header:

中的部分
/tmp/RtmpMLtVfF/rmarkdown-str24935297671d.html

我假设此文件包含 jquery 和数据表的 js 源代码。我试图从包源中手动添加它们 - 但没有成功 - 无论如何都想要一个开箱即用的解决方案。

RStudio 在底层使用的包是 rmarkdown。它处理 Knit 命令并将工作委托给 knitr 和 pandoc,它还负责捆绑依赖项并将它们注入到文档中。

您可能已经安装了软件包,因此您应该能够一次性生成 HTML 文件:

rmarkdown::render('example.Rmd')

更多资源:

RMarkdown on CRAN

RMarkdown Introduction