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

时间:2015-02-03 16:24:14

标签: r datatable rstudio r-markdown

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

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

以下是与RStudio一起使用的Rmd文件的最小示例,但不是:

--- 
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文件的部分:

/tmp/RtmpMLtVfF/rmarkdown-str24935297671d.html

我假设此文件包含jquery和datatables的js源。我试图从包源手动添加它们 - 但没有成功 - 并且无论如何都会像解决方案一样开箱即用。

1 个答案:

答案 0 :(得分:4)

RStudio正在使用的软件包是rmarkdown。它处理Knit命令并委托knitr和pandoc工作,它还负责捆绑依赖关系并将它们注入doc。

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

rmarkdown::render('example.Rmd')

更多资源:

RMarkdown on CRAN

RMarkdown Introduction