我希望我的 kable 桌子上有一致的边框
这是我的数据框:
df1 <- data.frame(inst = c("A", "B", "C", "D", "E", "F", "G"),
year_1_type1 = c(1,5,3,8,7,9,4),
year_2_type1 = c(5,7,8,3,5,2,6),
year_1_type2 = c(1,5,3,8,7,9,4),
year_2_type2 = c(5,7,8,3,5,2,6))
这是我的桌子:
这是代码:
df1 %>%
kbl(caption = '<b>Table 5</b>-Comparison of Types and Years', align='l',
col.names = c("inst", "year 1", "year 2", "year 1", "year 2")) %>%
kable_classic(full_width = T, html_font = "Arial Narrow") %>%
kable_styling() %>%
add_header_above(c(" " = 1, "Type 1" = 2, "Type 2"=2), align = "l", extra_css ="border-left: 1px solid;")
我绝对希望边框位于
Type 1
和 Type 2
的左侧,但如果它扩展到 year 1
的左侧,那就太好了我尝试在 col.names 函数之后添加 extra_css ="border-left: 1px solid;"
但那不起作用。有谁知道如何添加这些边框?
您可以使用
column_spec(c(1:5),border_left = "1px solid")
来扩展边框。但是,这不会为列标题创建边框。要向标题添加边框,请使用 extra_css: row_spec(0, extra_css ="border-left: 1px solid;")
。这是最终的代码:
df1 %>%
kbl(caption = '<b>Table 5</b>-Comparison of Types and Years', align='l',
col.names = c("inst", "year 1", "year 2", "year 1", "year 2")) %>%
kable_classic(full_width = T, html_font = "Arial Narrow") %>%
kable_styling() %>%
column_spec(c(1:5),border_left = "1px solid") %>%
row_spec(0, extra_css ="border-left: 1px solid;") %>%
add_header_above(c(" " = 1, "Type 1" = 2, "Type 2"=2), align = "l", extra_css ="border-left: 1px solid;")