Fons MA
Fons MA

Reputation: 1282

Printing any number of dataframes stored in list as paged tables in rmarkdown

I often want to print out the dataframes contained in a list as paged tables in my rmarkdown documents. Calling each dataframe individually renders the desired ouptut if the right df_print option is selected. However, the point of having a list is that the number of dataframes varies depending on the parameters passed to the rmarkdown document; so that's no real solution.

Based on Vincent Guyader's answer to this question and on this example of rmarkdown::paged_table, I've tried to do the following without success.

Is there a way to achieve this at all? I'd be happy to use any package that supports pagination remotely resembling the df_print option.

---
title: "Printing paged tables from a list of dataframes in Rmarkdown"
output: 
  html_document:
    df_print: paged
---


```{r}

library(DT)
library(rmarkdown)
library(purrr)
library(knitr)


df_list <- list("cars" = mtcars, "flowers" = iris)


knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, results='asis')

```

### Desired output but impossible to generalise 


```{r}

df_list[["cars"]]

```


```{r}

df_list[["flowers"]]

```

### datatable shows as blanks on the page


```{r}

map(df_list, ~DT::datatable(.x) %>%
      htmltools::tagList() %>%
      print())

```


### rmarkdown outputs dataframe contents as one very long string


```{r}

map(df_list, rmarkdown::paged_table)


```

Upvotes: 3

Views: 1567

Answers (2)

Waldi
Waldi

Reputation: 41260

When using results='asis' argument, the renderer (here DT) has to be initialized once before being applied on a asis list.
This seems to be a general problem, see here with leaflet, and here with Highcharter.

The answer to this general question has been given here.

In this case:

---
title: "Printing paged tables from a list of dataframes in Rmarkdown"
output:
  html_document:
  df_print: paged
---
  
```{r,}

library(DT)
library(rmarkdown)
library(purrr)
library(knitr)


df_list <- list("cars" = mtcars, "flowers" = iris)


knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, results='asis')
# initialize the renderer
data.frame() %>%
  DT::datatable() %>%
  knitr::knit_print() %>%
  attr('knit_meta') %>%
  knitr::knit_meta_add() %>%
  invisible()

```


```{r , results='asis'}
#Remove already printed element and print the rest
df_list[[1]] <- NULL

map(df_list, ~DT::datatable(.x) %>%
      htmltools::tagList() %>%
      print())

```

Upvotes: 2

stefan
stefan

Reputation: 125797

The issue is that the JS dependencies needed to render the Datatable are not included in the HTML output. A workaround which I borrowed from here is to add a code chunk

```{r init-step, include=FALSE}
DT::datatable(mtcars)
```

outside of the loop or map statement which ensures that the JS dependencies are included. Also, I would recommend to switch to purrr::walk as using map has the effect that the tables are plotted twice.

---
title: "Printing paged tables from a list of dataframes in Rmarkdown"
output: 
  html_document:
    df_print: paged
---


```{r}
library(DT)
library(rmarkdown)
library(purrr)
library(knitr)

df_list <- list("cars" = mtcars, "flowers" = iris)

knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, results='asis')
```

### Desired output but impossible to generalise 

```{r}
df_list[["cars"]]
```  

```{r}
df_list[["flowers"]] 
```

### datatable shows as blanks on the page

```{r init-step, include=FALSE}
DT::datatable(mtcars)
```  

```{r}
walk(df_list, ~DT::datatable(.x) %>%
      htmltools::tagList() %>%
      print())
```

Upvotes: 4

Related Questions