I'd love to have consistent borders on my kable table
Here's my data frame:
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))
And here is my table:
And here is the code:
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;")
I definitely want the border to the left of Type 1
and Type 2
but it would be nice if it extended to the left of year 1
I tried adding extra_css ="border-left: 1px solid;"
after the col.names function but that didn't work. Does anyone know how to add these borders?
You can extend the border by using column_spec(c(1:5),border_left = "1px solid")
. However, this will not create border for the column headers. To add border to the headers, use extra_css: row_spec(0, extra_css ="border-left: 1px solid;")
. Here is the final code:
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;")