DT table will not show in Rmarkdown with Word output

Viewed 640

I am trying to render a static DT to word.

I have this stripped down version of a file example.Rmd and I am using knitr version 1.30 and the latest stable versions of all relevant packages. Per https://bookdown.org/yihui/bookdown/html-widgets.html the datatable should render but I am not getting the static DT back just the echo=TRUE part.

Please help me if you can - I am hopelessly stuck and I cannot just use another 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)
```
2 Answers

The solution to this problem is to replace renderDataTable with datatable which allows webshot to kick in.

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

Here is what I did, I am not sure if you can render DT into a static table file straight into Word like you are wanting, but maybe I am unaware. The DT is a javascript library so it's suppose to be dynamic and user interactive in a web browser. but the knitr::kable() function will render your table entirely straight into 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

Maybe use just a simple table output with knitr::kable, which will render your entire table straight to a word doc from Rmarkdown.

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

Related