在 R 中垂直绘制表格列表 kable::extra 和 kable?

问题描述 投票:0回答:1

我需要绘制一个表格列表,一个在另一个下面,垂直显示。有什么想法吗?

  • 问题从这里开始,然后我发现
    knitr::kable
    kableExtra
    可能会有帮助,但是 我无法设法将它们垂直放置。
  ### 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")

观察: 我看过很多帖子,例如..但我无法应用于我的案例 这个 这个 这个 还有这个

r list r-markdown kable
1个回答
2
投票

在转到

dplyr::bind_rows()
之前,在表格列表上使用
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


© www.soinside.com 2019 - 2024. All rights reserved.