提问人:jon 提问时间:10/5/2023 更新时间:10/5/2023 访问量:30
如何在 m x n 网格中显示多个 kable 表?
How do I display multiple kable tables in a m x n grid?
问:
我有 6 个表,我希望它们在网格中显示 3 x 2。我一直在使用 Kable 来显示我的表格,这对我来说效果很好,但是当我将表格作为列表传递到 kables 中时,它只会水平显示它们。
例如,使用以下代码:
t1 <- mtcars[1:3,]
t2 <- mtcars[4:6,]
t3 <- mtcars[7:9,]
t4 <- mtcars[10:12,]
t5 <- mtcars[13:15,]
t6 <- mtcars[16:18,]
kable(list(t1,t2,t3,t4,t5,t6)) %>% kable_styling()
所有 6 个表都水平排列。有没有办法将这些 3 x 2 堆叠在一起?
答:
0赞
jasmine
10/5/2023
#1
我认为您可以使用软件包和功能的组合。kable
knitr
kableExtra
首先,对于每一行,我们可以使用 和 水平组合表。然后,我们垂直组合行。kable
pack_rows
这是我得到的一个例子:
library(knitr)
library(kableExtra)
t1 <- mtcars[1:3,]
t2 <- mtcars[4:6,]
t3 <- mtcars[7:9,]
t4 <- mtcars[10:12,]
t5 <- mtcars[13:15,]
t6 <- mtcars[16:18,]
# Create individual tables
k1 <- kable(t1, caption = "t1", format = "html") %>% kable_styling(bootstrap_options = c("striped"))
k2 <- kable(t2, caption = "t2", format = "html") %>% kable_styling(bootstrap_options = c("striped"))
k3 <- kable(t3, caption = "t3", format = "html") %>% kable_styling(bootstrap_options = c("striped"))
k4 <- kable(t4, caption = "t4", format = "html") %>% kable_styling(bootstrap_options = c("striped"))
k5 <- kable(t5, caption = "t5", format = "html") %>% kable_styling(bootstrap_options = c("striped"))
k6 <- kable(t6, caption = "t6", format = "html") %>% kable_styling(bootstrap_options = c("striped"))
# Combine horizontally for each row
row1 <- cbind(k1, k2) %>% kable_styling()
row2 <- cbind(k3, k4) %>% kable_styling()
row3 <- cbind(k5, k6) %>% kable_styling()
# Combine rows vertically
grid <- rbind(row1, row2, row3)
grid
此方法应将表排列在 3x2 网格中。样式和引导选项仅用于说明目的,您可以根据需要进行调整。
评论