【问题标题】:DT table will not show in Rmarkdown with Word output带有 Word 输出的 Rmarkdown 中不会显示 DT 表
【发布时间】:2021-02-12 18:53:07
【问题描述】:

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

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

如果可以的话,请帮助我 - 我被困住了,我不能只使用另一张桌子。


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

【问题讨论】:

    标签: datatables r-markdown knitr dt


    【解决方案1】:

    这就是我所做的,我不确定您是否可以像您想要的那样将 DT 直接渲染到 Word 中的静态表格文件中,但也许我不知道。 DT 是一个 javascript 库,因此它应该是动态的,并且可以在 Web 浏览器中进行用户交互。但是knitr::kable() 函数会将您的表格完全直接呈现为单词。

    ---
    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 的简单表格输出,这会将您的整个表格直接呈现为来自 Rmarkdown 的 word doc。

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

    【讨论】:

    • 谢谢@Daniel。不幸的是,我无法离开 datatable,因为我需要显示在前面的 shiny 应用程序中大量修改的 HTML 内容。
    【解决方案2】:

    解决此问题的方法是将renderDataTable 替换为datatable,这允许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)
    ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 2018-02-10
      相关资源
      最近更新 更多