简体   繁体   English

如何使用rmarkdown以pdf格式呈现DT :: datatables?

[英]how to render DT::datatables in a pdf using rmarkdown?

How can I display DT::datatable objects from a rmarkdown script onto a pdf document? 如何从rmarkdown脚本将rmarkdown DT::datatable对象显示到pdf文档? My code so far is breaks down with the following error: 到目前为止我的代码被分解为以下错误:

processing file: reportCopy.Rmd
output file: reportCopy.knit.md
Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML.

Including always_allow_html: yes in the YAML header suppresses the error, but nothing appears on the pdf. 在YAML标头中包含always_allow_html: yes抑制错误,但pdf上不会显示任何内容。

I would be grateful for any help. 我将不胜感激任何帮助。 My code is currently: 我的代码目前是:

---
title: "DT"
output: pdf_document
---

### Chart 1
```{r}
DT::datatable(head(mtcars))
```

( I don't know if it matters, but my datatables are in fact created in a shiny application. Ideally, I would have liked to have the prerendered tables simply dumped into the rmarkdown script... but I switched tactic and now try to render the tables directly in the rmarkdown code) (我不知道它是否重要,但我的数据表实际上是在一个shiny应用程序中创建的。理想情况下,我希望将预rmarkdown表简单地转储到rmarkdown脚本中...但我切换了策略,现在尝试直接在rmarkdown代码中呈现表格)

Since knitr v1.13, HTML widgets will be rendered automatically as screenshots taken via the webshot package. 自knitr v1.13起,HTML小部件将自动呈现为通过webshot包获取的屏幕截图。

You need to install the webshot package and PhantomJS: 您需要安装webshot包和PhantomJS:

install.packages("webshot")
webshot::install_phantomjs()

(see https://bookdown.org/yihui/bookdown/html-widgets.html ) (见https://bookdown.org/yihui/bookdown/html-widgets.html

You cannot use datatable in pdf_document ( datatable is interactive, pdf is static), only in html_document! 你不能在pdf_document使用datatabledatatable是交互式的,pdf是静态的),只能在html_document中使用! The only possibility for PDF is to use the kable or for example pandoc.table PDF的唯一可能性是使用kable或例如pandoc.table

--> if You really wanna get the look of datatable and as You said datatable is created in a shiny application, then You can have a look at the webshot package, which is going to create a screenshot of Your datatable from shiny app which You can use further in pdf as a image. - >如果你真的想要得到的外观datatable和你说datatable中创建一个shiny应用程序,那么你可以看看webshot包,这将创建你的截图datatable ,从shiny的应用程序,你可以在PDF格式中进一步使用作为图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM