I'm trying to create a relatively complex table with kable in rmarkdown, where I need to pack_rows()
with collapse_rows()
and add_header_above()
.
When I use pack_rows()
, kable creates an indentation in the middle of the table:
library(tidyverse)
library(kableExtra)
Catch_Ratio_Meshsize <- structure(list(Lake_Code = c("RK_20", "RK_20", "RK_20", "RK_20", "RO_21", "RO_21", "SC_18", "SC_18", "SC_18", "SC_18"),
Zone = c("Benthic", "Benthic", "Pelagic", "Pelagic", "Benthic", "Benthic", "Benthic", "Benthic", "Pelagic", "Pelagic"),
Net_Type = c("DIN", "MOD", "DIN", "MOD", "DIN", "MOD", "DIN", "MOD", "DIN", "MOD"),
`5` = c(0.0227848101265823, 0, 0, 0, 0.0113285272914521, 0, 0, 0, 0, 0),
`6.3` = c(0.164556962025316, 0.0506329113924051, 0, 0, 0.043254376930999, 0.0350154479917611, 0.0221843003412969, 0.00170648464163823, 0.0050251256281407, 0),
`8` = c(0.154430379746835, 0.0506329113924051, 0, 0, 0.0154479917610711, 0.0185375901132853, 0.0793515358361775, 0.0290102389078498, 0.0301507537688442, 0.0201005025125628),
`10` = c(0.126582278481013, 0.0607594936708861, 0.2, 1.6, 0.200823892893924, 0.0514933058702369, 0.175767918088737, 0.0614334470989761, 0.0402010050251256, 0),
`12.5` = c(0.134177215189873, 0.126582278481013, 0.2, 0.9, 0.199794026776519, 0.0844490216271885, 0.34641638225256, 0.133105802047782, 0.391959798994975, 0.195979899497487),
`15.5` = c(0.151898734177215, 0.134177215189873, 0.4, 0.9, 0.208032955715757, 0.185375901132853, 0.215017064846416, 0.199658703071672, 0.381909547738693, 0.417085427135678),
`19.5` = c(0.126582278481013, 0.0936708860759494, 0, 0, 0.167868177136972, 0.189495365602472, 0.0998293515358362, 0.083617747440273, 0.125628140703518, 0.0251256281407035),
`24` = c(0.0860759493670886, 0.0329113924050633, 0.1, 0.2, 0.0978372811534501, 0.0865087538619979, 0.0324232081911263, 0.0409556313993174, 0.0251256281407035, 0.0703517587939699),
`29` = c(0.010126582278481, 0.0278481012658228, 0.1, 0, 0.0247167868177137, 0.0638516992790937, 0.0110921501706485, 0.0119453924914676, 0, 0),
`35` = c(0.0177215189873418, 0.020253164556962, 0, 0, 0.0185375901132853, 0.03913491246138, 0.0119453924914676, 0.0426621160409556, 0, 0),
`43` = c(0.00506329113924051, 0.00253164556962025, 0, 0, 0.0113285272914521, 0.0164778578784758, 0.00255972696245734, 0.0273037542662116, 0, 0),
`55` = c(0, 0, 0, 0, 0.00102986611740474, 0, 0.00341296928327645, 0, 0, 0),
total = c(1, 0.6, 1, 3.6, 1, 0.770339855818744, 1, 0.631399317406143, 1, 0.728643216080402),
Lake_Year = c("Rohrköpfle - 2020", "Rohrköpfle - 2020", "Rohrköpfle - 2020", "Rohrköpfle - 2020", "Rohrsee - 2021", "Rohrsee - 2021", "Schluchsee - 2018", "Schluchsee - 2018", "Schluchsee - 2018", "Schluchsee - 2018" )), class = "data.frame", row.names = c(NA, -10L))
Catch_Ratio_Meshsize %>%
select(-c(Lake_Code, Lake_Year)) %>%
rename("\U00A0" = "Zone") %>%
kbl(digits = 3) %>%
kable_classic("striped", full_width = T) %>%
column_spec(15, bold = Catch_Ratio_Meshsize$total > 1) %>%
collapse_rows(columns = 1, valign = "top") %>%
pack_rows(index = table(Catch_Ratio_Meshsize$Lake_Year), indent = F, label_row_css = "border-bottom: 1px solid;") %>%
add_header_above(c(" " = 2, "Mesh size" = 12, " " = 1), line = TRUE,
extra_css = "border-bottom: 1px solid;")
Any thoughts would be greatly appreciated!