如何在rmarkdown中使用DT::datatables将数据呈现为PDF格式?

7
我该如何在pdf文档中显示来自rmarkdown脚本的DT::datatable对象?到目前为止,我的代码出现以下错误:
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.

在YAML头中包含always_allow_html: yes可以抑制错误,但是pdf上没有任何内容显示。
感谢任何帮助。 我目前的代码是:
---
title: "DT"
output: pdf_document
---

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

我不知道这是否重要,但我的datatables实际上是在一个shiny应用程序中创建的。理想情况下,我希望将预呈现的表格简单地转储到rmarkdown脚本中...但我改变了策略,现在尝试直接在rmarkdown代码中呈现表格。


1
如果您对渲染表格的人没有特别要求,R Markdown 还有其他表格选项,例如 knitr::kable - Adam Spannbauer
@AdamSpannbauer 感谢您的回答。我想我可能确实需要切换到kable..但我宁愿不这样做,因为整个代码库(来自Shiny应用程序)都是使用DT::datatables编写的。 - hartmut
1
你不能在 pdf_document 中使用 datatable(因为 datatable 是交互式的),只能在 html_document 中使用!在 PDF 中唯一的可能性是使用 kable 或者例如 pandoc.table - Mal_a
@Malvina_a 感谢您的评论...这正是我最担心的。如果您确定,请毫不犹豫地将其作为答案,我会接受它。 - hartmut
2个回答

5

不幸的是,我无法执行第二行代码(我在 https://stackoverflow.com/questions/44563874/r-webshotinstall-phantomjs-error-403 上提出了这个问题,但还没有得到答案)。但我很高兴知道有一个解决初始问题的方法存在! - hartmut
library(DT) library(webshot) if(is.null(webshot:::find_phantom())){webshot::install_phantomjs()} DT::datatable(iris) ''' `````````` - J. Doe.

3
您不能在pdf_document中使用datatabledatatable是交互式的,pdf是静态的),只能在html_document中使用!唯一的PDF可能性是使用kable或例如pandoc.table --> 如果您真的想要获得datatable的外观,并且正如您所说,datatable是在shiny应用程序中创建的,则可以查看webshot包,该包将从shiny应用程序中创建您的datatable的屏幕截图,您可以将其作为图像进一步用于pdf。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接