Search code examples
rdplyrkablekableextra

creating horizontal lines after each collapsed row in kableextra table


I have a table with collapsed rows in column 1. I want to separate the grouping in column 1 with a solid horizontal line.

My current code is able to draw line using row_spec() but I am having to enter each line manually. The problem is there might be changes in the number of rows if the data is updated. Also the line is not solid in column one. Is there a way to create these lines?

Here is the code:

DATA

 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"
))

Code

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()

enter image description here


Solution

  • I added extra_css border for column 1 for the solid line. I still had to manually add horizontal line for each group.

    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() 
    

    enter image description here