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

后端 未结 2 763
闹比i
闹比i 2021-01-23 10:11

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 rig

2条回答
  •  春和景丽
    2021-01-23 10:44

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

提交回复
热议问题