kableExtra可以添加标题行,其中标题项跨越表中使用kable生成的表中的多列
library(tidyverse)
library(knitr)
library(kableExtra)
mat <- matrix(1:4,2,dimnames=list(c("a","b"),c("x","y")))
mat %>% kable("html") %>% add_header_above(c(" " = 1,"row header" = 2))
我也想有一个列标题,就像这样:
能做到吗?
答案 0 :(得分:0)
我认为您无法做到,但我找到了解决您问题的方法
mat <- data.frame(a = c("row header","row header"),b = c("a","b"),c = c("x","y"))
mat %>% kable("html") %>% add_header_above(c(" " = 1,"row header" = 2))
kable(mat, align = "c",col.names = c("","","")) %>%
kable_styling(bootstrap_options = "striped", full_width = F, position = "left",font_size = 12)%>%
column_spec(1, bold = T) %>%
collapse_rows(columns = 1, valign = "middle")%>%
add_header_above(c(" " = 1, "row header" = 2))
我希望这对您有帮助
答案 1 :(得分:0)
基于您告诉我的信息,我能够完全解决我的问题:
mat <- data.frame(a = c("column header","column header"),b = c("a","b"),c = c("x","y"))
kable(mat, align = "c",col.names = c("","","")) %>%
kable_styling(bootstrap_options = "striped", full_width = F, position = "left",font_size = 12)%>%
column_spec(1, bold = T,width="3em",extra_css="transform: rotate(-90deg);") %>%
collapse_rows(columns = 1, valign = "middle")%>%
add_header_above(c(" " = 1, "row header" = 2))