I am trying to add an additional header row in a long kable. Below is an example of what my current table looks like:
Here is an example of what I am trying to achieve:
I have not been able to find any description of this. If anyone has any ideas how I might accomplish this it would be much appreciated.
Code for kable table:
library(kableExtra)
my_data <- mtcars[1:8, 1:6]
kable(my_data, booktabs = TRUE, align = "c", table.attr = "style='width:100%;'") %>%
kable_styling(bootstrap_options = c("striped"), position = "center", full_width = FALSE) %>%
group_rows("Set1", 1, 4) %>%
group_rows("Set2", 5, 8)
It's not very important that the final table looks exactly like the image, if the header row is another full row or something like that, that is totally fine. Thanks in advance.
I don't there's a "kable" way of doing it. Here I mimicked your output by adding the column names as a row in my_data
, then use row_spec
to decorate that row.
library(kableExtra)
my_data <- mtcars[1:8, 1:6]
my_data <- rbind(my_data[1:4,],
as.data.frame(rbind(colnames(my_data)), row.names = "Set2") |>
setNames(colnames(my_data)),
my_data[5:nrow(my_data),])
kable(my_data, booktabs = TRUE, align = "c", table.attr = "style='width:100%;'") %>%
kable_styling(bootstrap_options = c("striped"), position = "center", full_width = FALSE) %>%
group_rows("Set1", 1, 4) %>%
add_indent(6:9) %>%
row_spec(5, bold = T, extra_css = "border-bottom: 1px solid")