kableExtra::collapse_rows
似乎没有正确放置行组名称
使用knitr::kable(longtable = T)
时在后续页面上显示。 collapse_rows()
行组名与可伸缩表头重叠,并且在后续页面上不再重复。将MWE转换为pdf时,请参见第2页和第4页的顶部。
MWE
library(dplyr)
library(knitr)
library(kableExtra)
# example data for a very long and wide table with nested rows groupings
set.seed(99112)
d <- merge(expand.grid(a = c("X", "Y"), b = c("a", "b"), y = 2011:2018),
data.frame(d1 = rnorm(5),
d2 = rnorm(5),
d3 = rnorm(5),
d4 = rnorm(5),
d5 = rnorm(5),
d6 = rnorm(5),
d7 = rnorm(5),
d8 = rnorm(5),
d9 = rnorm(5),
d10 = rnorm(5)))%>%
arrange(a, b, y)
# long header names that need to take two lines
names(d) <- c("Exches and\nWhyze", "Ehhze and\nBeeze", "Year", paste0("Deeze\nRnorm", 1:10))
# table
kable(d,
longtable = TRUE,
booktabs = TRUE,
linesep = '',
align = c(rep("l",3),rep("c", ncol(d)-3)),
caption = "Collapse rows group name overlaps with longtable headers and are not repeated on subsequent pages.",
escape = FALSE, format = 'latex', row.names = FALSE)%>%
add_header_above(header = c(rep(" ", 3), "Group 1" = 4, rep(" ", ncol(d) - 7)), bold = FALSE, line = TRUE, align = "c")%>%
add_header_above(header = c(rep(" ", ncol(d)- 5), "Group 2" = 3, "Group 3" = 2), bold = TRUE, line = TRUE, align = "c")%>%
add_header_above(header = c(rep(" ", 3), "Group 4" = ncol(d) - 3), bold = TRUE, line = TRUE, align = "c")%>%
kable_styling(position = "center", latex_options = c("HOLD_position", "repeat_header"), font_size = 6, full_width = FALSE)%>%
column_spec(1:2, bold = T, width = "1cm", border_right = T) %>%
column_spec(3:ncol(d), bold = F, width = "1cm") %>%
collapse_rows(columns = 1:2, valign = "top", latex_hline = 'major', custom_latex_hline = 1:2)%>%
landscape()