DT表格在Rmarkdown中使用Word输出时无法显示。

4
我正在尝试将静态的DT渲染成Word文档。
我有一个剥离后的example.Rmd文件,并使用knitr版本1.30和所有相关软件包的最新稳定版本。根据https://bookdown.org/yihui/bookdown/html-widgets.html上的说明,datatable应该被渲染,但我只得到了echo = TRUE部分而没有静态的DT
如果您能帮助我,那就太好了 - 我已经陷入了困境,而且我不能仅使用另一个表格。
title: 'Main title'
subtitle: 'Subtitle here'
always_allow_html: yes
# params:these will come from SHINY APP
#   x1: x1
#   x2: x2
#   x3 :x3
output:
   word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(out.width = '100%', dpi=300)

```

```{r, fig.align='center', echo=TRUE, cache=FALSE, warning = TRUE, message = TRUE, tidy=TRUE}
library(DT)
library(webshot)
if(is.null(webshot:::find_phantom())){webshot::install_phantomjs()}

DT::renderDataTable(iris)
```
2个回答

2
这个问题的解决方案是用允许 webshot 启动的 datatable 替换 renderDataTable
title: 'Main title'
subtitle: 'Subtitle here'
always_allow_html: yes
# params:these will come from SHINY APP
#   x1: x1
#   x2: x2
#   x3 :x3
output:
   word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(out.width = '100%', dpi=300)

```

```{r, fig.align='center', echo=TRUE, cache=FALSE, warning = TRUE, message = TRUE, tidy=TRUE}
library(DT)
library(webshot)
if(is.null(webshot:::find_phantom())){webshot::install_phantomjs()}

 DT::datatable(iris)
```

0

这是我做的,我不确定你是否可以直接将DT渲染为静态表文件并直接放入Word中,但也许我对此不太了解。DT是一个JavaScript库,所以它应该是在Web浏览器中动态和用户交互的。但knitr::kable()函数将完整地将您的表格渲染到Word中。

---
title: 'Main title'
subtitle: 'Subtitle here'
always_allow_html: yes
runtime: shiny
output:
    html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(out.width = '100%', dpi=300)

```

```{r, fig.align='center', cache=FALSE, warning = FALSE, message = FALSE}
library(DT)
library(webshot)
if(is.null(webshot:::find_phantom())){webshot::install_phantomjs()}

DT::renderDataTable(iris)
```

enter image description here

也许只需使用 knitr::kable 创建一个简单的表格输出,这将直接将整个表格从 Rmarkdown 渲染到 Word 文档中。

---
title: 'Main title'
subtitle: 'Subtitle here'
output:
    word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(out.width = '100%', dpi=300)

```

```{r, fig.align='center', cache=FALSE, warning = FALSE, message = FALSE}
library(DT)
library(knitr)

knitr::kable(iris)
```

enter image description here


谢谢@Daniel。不幸的是,我不能放弃使用datatable,因为我需要在之前的shiny应用程序中对HTML内容进行大量修改后显示。 - undefined

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