rlistr-markdownkable

plot a list of tables vertically kable::extra and kable in R?


I need to plot a list of tables one below the other, displayed vertically. Any ideas?

  ### empty list:
  test_list <- list()
### make two tests to provide an example: 
for (a in 1:2) {
  
  myVar <- data[a]    
  myVarn <- names(myVar)
  data$myVarUnlist <- unlist(myVar)
  
  test_list[[a]] <- cor.test(data$myVar, data$VarC, data = data)
  
}
  

my_report <- list()
my_report_partial <- list()

library(report)

for (b in 1:2) {
  
    my_report[[b]] <- report_table(test_list[[b]])
}
my_report %>%
  kableExtra::kbl(caption = "Recreating booktabs style table",  longtable = TRUE) %>%
  kableExtra::kable_classic(full_width = F, html_font = "Cambria")

i get this

structure(list(VarA = c(263L, 223L, NA, 257L, 285L, 211L, 210L, 
NA, 147L, 311L, 342L, 97L, 216L, 241L, 296L, 296L, 211L, 60L, 
339L, 318L, 358L, 167L, NA, 183L, 92L, 283L, 169L, NA, 298L, 
NA, 162L, NA, 211L, 308L, 92L, 269L, NA, 197L, 280L, 259L, 313L, 
252L, 98L, 258L, 201L, 341L, 456L, 308L, 252L, 64L, 259L, 158L, 
161L, NA, NA, 129L, 264L, NA, 216L, 109L, 91L, 236L, 275L, 254L, 
221L, NA, NA, NA, NA, NA, NA), VarB = c(145L, 120L, NA, 119L, 
142L, 132L, 100L, NA, 64L, 144L, 164L, 56L, 102L, 136L, 139L, 
135L, 91L, 32L, 123L, 164L, 145L, 93L, NA, 99L, 51L, 143L, 98L, 
NA, 158L, NA, 79L, NA, 96L, 149L, 55L, 114L, NA, 94L, 137L, 130L, 
135L, 113L, 61L, 113L, 117L, 154L, 199L, 152L, 142L, 42L, 111L, 
74L, 92L, NA, NA, 85L, 116L, NA, 99L, 64L, 60L, 114L, 151L, 136L, 
116L, NA, NA, NA, NA, NA, NA), VarC = c(145L, 121L, NA, 120L, 
145L, 133L, 101L, NA, 64L, 146L, 166L, 58L, 103L, 136L, 142L, 
135L, 91L, 34L, 123L, 167L, 148L, 93L, NA, 99L, 51L, 145L, 98L, 
NA, 159L, NA, 81L, NA, 97L, 149L, 56L, 115L, NA, 96L, 137L, 132L, 
135L, 113L, 62L, 113L, 118L, 154L, 199L, 154L, 145L, 43L, 112L, 
74L, 92L, NA, NA, 86L, 116L, NA, 100L, 66L, 60L, 114L, 153L, 
136L, 120L, NA, NA, NA, NA, NA, NA), myVarUnlist = c(145L, 120L, 
NA, 119L, 142L, 132L, 100L, NA, 64L, 144L, 164L, 56L, 102L, 136L, 
139L, 135L, 91L, 32L, 123L, 164L, 145L, 93L, NA, 99L, 51L, 143L, 
98L, NA, 158L, NA, 79L, NA, 96L, 149L, 55L, 114L, NA, 94L, 137L, 
130L, 135L, 113L, 61L, 113L, 117L, 154L, 199L, 152L, 142L, 42L, 
111L, 74L, 92L, NA, NA, 85L, 116L, NA, 99L, 64L, 60L, 114L, 151L, 
136L, 116L, NA, NA, NA, NA, NA, NA)), row.names = c(NA, -71L), class = "data.frame")

Obs: I've seen many posts, such as.. but I couldn't apply to my case this this this and this


Solution

  • Use dplyr::bind_rows() on the list of tables before passing to the kbl().


    ```{r}
    ### empty list:
    test_list <- list()
    ### make two tests to provide an example:
    for (a in 1:2) {
        myVar <- data[[a]]
        myVarn <- names(myVar)
        data$myVarUnlist <- unlist(myVar)
    
        test_list[[a]] <- cor.test(~ myVar + VarC, data = data)
    }
    
    
    my_report <- list()
    my_report_partial <- list()
    
    library(report)
    library(kableExtra)
    
    for (b in 1:2) {
        my_report[[b]] <- report_table(test_list[[b]])
    }
    
    dplyr::bind_rows(my_report) %>% 
        kbl(caption = "Recreating booktabs style table") %>% 
      kableExtra::kable_classic(full_width = F, html_font = "Cambria")
    
    ```
    

    kable table joined vertically