R语言 如何在m x n网格中显示多个可显示表?

yvgpqqbh  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(119)

我有6个表,我希望他们在一个网格显示3 × 2.我一直在使用Kable来显示我的表,这对我来说工作得相当好,但是当我将表作为列表传递到Kables时,它只能水平地显示它们。
使用以下代码作为示例:

  1. t1 <- mtcars[1:3,]
  2. t2 <- mtcars[4:6,]
  3. t3 <- mtcars[7:9,]
  4. t4 <- mtcars[10:12,]
  5. t5 <- mtcars[13:15,]
  6. t6 <- mtcars[16:18,]
  7. kable(list(t1,t2,t3,t4,t5,t6)) %>% kable_styling()

所有6张table都水平排列。有没有办法把这3x2的东西叠起来?

gopyfrb3

gopyfrb31#

我认为您可以使用knitr包中的kablekableExtra功能的组合。
首先,对于每一行,我们可以使用kablepack_rows水平地合并合并表格。然后,我们垂直地合并组合行。
这是我得到的一个例子:

  1. library(knitr)
  2. library(kableExtra)
  3. t1 <- mtcars[1:3,]
  4. t2 <- mtcars[4:6,]
  5. t3 <- mtcars[7:9,]
  6. t4 <- mtcars[10:12,]
  7. t5 <- mtcars[13:15,]
  8. t6 <- mtcars[16:18,]
  9. # Create individual tables
  10. k1 <- kable(t1, caption = "t1", format = "html") %>% kable_styling(bootstrap_options = c("striped"))
  11. k2 <- kable(t2, caption = "t2", format = "html") %>% kable_styling(bootstrap_options = c("striped"))
  12. k3 <- kable(t3, caption = "t3", format = "html") %>% kable_styling(bootstrap_options = c("striped"))
  13. k4 <- kable(t4, caption = "t4", format = "html") %>% kable_styling(bootstrap_options = c("striped"))
  14. k5 <- kable(t5, caption = "t5", format = "html") %>% kable_styling(bootstrap_options = c("striped"))
  15. k6 <- kable(t6, caption = "t6", format = "html") %>% kable_styling(bootstrap_options = c("striped"))
  16. # Combine horizontally for each row
  17. row1 <- cbind(k1, k2) %>% kable_styling()
  18. row2 <- cbind(k3, k4) %>% kable_styling()
  19. row3 <- cbind(k5, k6) %>% kable_styling()
  20. # Combine rows vertically
  21. grid <- rbind(row1, row2, row3)
  22. grid

这种方法应该将表格排列在3x2网格中。样式和引导选项仅用于说明目的,您可以根据需要对其进行调整。

展开查看全部

相关问题