Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

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


```
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...