如何使用Knitr和Pandoc在Rmarkdown中包含DT数据表的js依赖项 [英] Howto include js dependencies of DT datatable in Rmarkdown using knitr and pandoc
问题描述
有没有一种方法可以用DT在RStudio之外生成的数据表来编译RMarkdown文档?
Is there a way to compile RMarkdown documents with datatables produced by DT outside of RStudio?
我试图在我的RMarkdown文档中包括一个数据表小部件然后要使用knitr和pandoc转换为html。这在RStudio上可以正常使用,但是如果我尝试对knitr和pandoc进行相同操作,则无法获取有效的html文件。
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.
这里是一个最小的示例可以与RStudio一起使用的Rmd文件,但不适用于其他版本:
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)
```
然后我想使用以下方式转换为html:
Which I would then like to convert to html using:
knitr::knit('example.Rmd')
knitr::pandoc('example.md',format="html")
我知道RStudio使用了更为复杂的pandoc调用:
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
我可以重新创建该部分该tmp文件包含在标头中的位置:
which I can recreate up to the part where this tmp file is included in the header:
/tmp/RtmpMLtVfF/rmarkdown-str24935297671d.html
我假定此文件包含jquery和datatables的js源。我试图从软件包源中手动添加它们-但没有成功-并且无论如何都希望有一种开箱即用的解决方案。
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.
推荐答案
RStudio正在使用的软件包为 rmarkdown
。它处理Knit命令,并将工作委托给knitr和pandoc,还负责捆绑依赖项并将它们注入到文档中。
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.
您可能已经安装了该软件包,因此您应该能够一次生成HTML文件:
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')
更多资源:
< a href = http://cran.r-project.org/web/packages/rmarkdown/index.html rel = nofollow> CRAN上的RMarkdown
这篇关于如何使用Knitr和Pandoc在Rmarkdown中包含DT数据表的js依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!