DT table 不会显示在带有 Word 输出的 Rmarkdown 中

DT table will not show in Rmarkdown with Word output

我正在尝试将静态 DT 呈现为文字。

我有这个精简版的文件 example.Rmd,我正在使用 knitr 1.30 版和所有相关包的最新 stable 版。根据 https://bookdown.org/yihui/bookdown/html-widgets.html datatable 应该呈现,但我没有得到静态 DT 返回 echo=TRUE 部分。

如果可以,请帮助我 - 我无可救药地卡住了,我不能只使用另一个 table。


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)
```

这是我所做的,我不确定您是否可以像您想要的那样将 DT 渲染成静态 table 文件直接进入 Word,但也许我不知道。 DT 是一个 javascript 库,因此它应该是动态的并且可以在网络浏览器中与用户交互。但是 knitr::kable() 函数会将您的 table 完全直接转换为文字。

---
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)
```

也许只使用带有 knitr::kable 的简单 table 输出,这将使您的整个 table 直接呈现为来自 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)
```

这个问题的解决方案是用 datatable 替换 renderDataTable,这样 webshot 就可以开始了。

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)
```