在 kableextra table 中的每个折叠行之后创建水平线

creating horizontal lines after each collapsed row in kableextra table

我有一个 table,第 1 列中有折叠的行。我想用实水平线分隔第 1 列中的分组。

我当前的代码可以使用 row_spec() 画线,但我必须手动输入每一行。问题是如果更新数据,行数可能会发生变化。此外,第一列中的线条不是实线。有没有办法创建这些线?

代码如下:

数据

 baseline_tbl <- structure(list(var = c("Total", "Age at initial diagnosis", "Age at initial diagnosis", 
"Age at initial diagnosis", "Age at initial diagnosis", "Year of inital diagnosis", 
"Year of inital diagnosis", "Year of inital diagnosis", "Year of inital diagnosis", 
"Year of inital diagnosis", "Year of inital diagnosis", "Year of inital diagnosis", 
"Is advanced NSCLC", "Is advanced NSCLC", "Is advanced NSCLC", 
"Age at advanced diagnosis", "Age at advanced diagnosis", "Age at advanced diagnosis", 
"Age at advanced diagnosis"), level = c("", "<65", ">=65", "Missing", 
"Median", "Prior 2015", "2015", "2016", "2017", "2018", "2019", 
"2020", "Yes", "No", "Missing", "<65", ">=65", "Missing", "Median"
), nsclc = c(1320L, 695L, 620L, 5L, 64L, 305L, 320L, 403L, 253L, 
24L, 0L, 0L, 1026L, 294L, 0L, 553L, 467L, 300L, 64L), nsclc_pct = c(NA, 
0.526515151515151, 0.46969696969697, 0.00378787878787879, NA, 
0.231060606060606, 0.242424242424242, 0.30530303030303, 0.191666666666667, 
0.0181818181818182, 0, 0, 0.777272727272727, 0.222727272727273, 
0, 0.418939393939394, 0.353787878787879, 0.227272727272727, NA
), adv_nsclc = c(1026, 575, 446, 5, 63, 275, 260, 295, 170, 16, 
0, 0, 1026, 0, 0, 553, 467, 6, 63.7), adv_pct = c(NA, 0.560428849902534, 
0.434697855750487, 0.00487329434697856, NA, 0.268031189083821, 
0.253411306042885, 0.287524366471735, 0.165692007797271, 0.0155945419103314, 
0, 0, 1, 0, 0, 0.538986354775828, 0.455165692007797, 0.00584795321637427, 
NA), early_nsclc = c(656, 319, 334, 3, 65, 164, 151, 199, 121, 
10, 0, 0, 364, 292, 0, 178, 183, 295, 65.21), early_pct = c(NA, 
0.486280487804878, 0.509146341463415, 0.00457317073170732, NA, 
0.25, 0.230182926829268, 0.303353658536585, 0.184451219512195, 
0.0152439024390244, 0, 0, 0.554878048780488, 0.445121951219512, 
0, 0.271341463414634, 0.278963414634146, 0.44969512195122, NA
)), row.names = c(NA, -19L), class = c("tbl_df", "tbl", "data.frame"
))

代码

baseline_tbl %>% 
mutate_at(c(3,5,7), ~sub("..00+$", "", as.character(.))) %>% 
mutate_at(c(4,6,8), ~scales::percent(., accuracy = 0.01)) %>% 
kbl(align = "llcccccc", escape=T, col.names = c("Variable", "Level", "N", "%", "N", "%", "N", "%")) %>%
kable_paper(full_width = F) %>%
column_spec(1, bold = T, background = "#91D1C233") %>%
column_spec(2,
            width = "10em", border_right = T) %>% 
column_spec(c(3,5,7), width = "3em") %>% 
column_spec(c(4,6,8), italic = T,border_right = T,width = "3em") %>% 
row_spec(0, bold = T, font_size = 18, extra_css = "border-bottom: 1px solid") %>% 
row_spec(1, underline = T, extra_css = "border-bottom: 1px solid") %>% 

collapse_rows(columns = 1, valign = "top") %>% 
add_header_above(c(" " = 2, "NSCLC" = 2, "Advanced NSCLC" = 2, "Early NSCLC" = 2),
                 bold = T,extra_css ="border-bottom: 1px solid") %>% 
row_spec(c(5,12,15,19), extra_css = "border-bottom: 1px solid;") %>% 
kable_classic_2()

我为第 1 列的实线添加了 extra_css 边框。我仍然必须为每个组手动添加水平线。

baseline_tbl %>% 
    kbl(align = "llcccccc", escape=T, col.names = c("Variable", "Level", "N", "%", "N", "%", "N", "%")) %>%
    kable_paper(c("striped"),full_width = F) %>%
    column_spec(1, bold = T, background = "#91D1C233",extra_css = "border-bottom: 1px solid;") %>%
    column_spec(2,
                width = "10em", border_right = T) %>% 
    column_spec(c(3,5,7), width = "3em") %>% 
    column_spec(c(4,6,8), italic = T,border_right = T,width = "5em") %>% 
    row_spec(0, bold = T, font_size = 18, extra_css = "border-bottom: 1px solid;") %>% 
    row_spec(1, underline = T, extra_css = "border-bottom: 1px solid;") %>% 
    row_spec(c(5,12,15,19,22,29,34,37,40,46,50,51), extra_css = "border-bottom: 1px solid;") %>% 
    collapse_rows(columns = 1, valign = "top") %>% 
    add_header_above(c(" " = 2, "NSCLC" = 2, "Advanced NSCLC" = 2, "Early NSCLC" = 2),
                     bold = T,extra_css ="border-bottom: 1px solid;") %>% 
    kable_classic_2()