kableExtra
kableExtra copied to clipboard
collapse_rows () has problem with columns
Describe the bug The collapse_rows can no longer be applied to columns in version 1.4.0. In previous versions this worked without any problems.
To Reproduce
library(knitr)
library(kableExtra)
df <- data.frame(
items = rep(c("Text A", "Text B", "Text C", "Text D", "Text E", "Text F", "Text G", "Text H", "Text I"), each = 2),
char = rep(c("bla", "blub"), times = 9),
mw = c(4.4, 4, 4.4, 4.4, 4.3, 4.3, 3.9, 4.4, 4.1, 4.4, 4.3, 4.5, 4, 4.3, 4.2, 4.2, 3.5, 2.8),
sa = c(0.7, 1.1, 0.6, 0.9, 0.7, 0.9, 0.9, 0.9, 0.8, 0.9, 0.8, 0.8, 0.9, 0.9, 0.8, 1, 1.2, 1.5),
n = rep(c("834", "838", "842", "839", "841", "844", "843", "840", "841"), each = 2),
diff = c(-0.4, -0.4, rep(0, 14), -0.7, -0.7)
)
titletxt <- c("Title: xxx" = 6)
skalatxt <- c("Text" = 6)
sourcetxt <- "Source: xxx"
kbl(
df,
col.names = c(" ", " ", "AAA", "BBB", "CCC", "DDDD"),
booktabs = TRUE,
align = c("l", "r", "r", "r", "r", "r"),
linesep = '',
) %>%
add_header_above(skalatxt, escape = FALSE, italic = TRUE) %>%
add_header_above(titletxt, escape = FALSE) %>%
collapse_rows(c(1:2, 5:6), row_group_label_position = 'identity') %>%
footnote(general = sourcetxt, general_title = "") %>%
kable_styling(
latex_options = c("HOLD_position", "scale_down"),
bootstrap_options = c("hover", "condensed", "responsive")
)
Error
Error in `[.data.frame`(kable_dt, , i) : undefined columns selected
I have found a workaround! The error is triggered because two column names consist only of blanks. If the Unicode character \u200B
is used instead, the code is executed without error. To Reproduce:
library(knitr)
library(kableExtra)
df <- data.frame(
items = rep(c("Text A", "Text B", "Text C", "Text D", "Text E", "Text F", "Text G", "Text H", "Text I"), each = 2),
char = rep(c("bla", "blub"), times = 9),
mw = c(4.4, 4, 4.4, 4.4, 4.3, 4.3, 3.9, 4.4, 4.1, 4.4, 4.3, 4.5, 4, 4.3, 4.2, 4.2, 3.5, 2.8),
sa = c(0.7, 1.1, 0.6, 0.9, 0.7, 0.9, 0.9, 0.9, 0.8, 0.9, 0.8, 0.8, 0.9, 0.9, 0.8, 1, 1.2, 1.5),
n = rep(c("834", "838", "842", "839", "841", "844", "843", "840", "841"), each = 2),
diff = c(-0.4, -0.4, rep(0, 14), -0.7, -0.7)
)
titletxt <- c("Title: xxx" = 6)
skalatxt <- c("Text" = 6)
sourcetxt <- "Source: xxx"
kbl(
df,
col.names = c("\u200B", "\u200B", "AAA", "BBB", "CCC", "DDDD"),
booktabs = TRUE,
align = c("l", "r", "r", "r", "r", "r"),
linesep = '',
) %>%
add_header_above(skalatxt, escape = FALSE, italic = TRUE) %>%
add_header_above(titletxt, escape = FALSE) %>%
collapse_rows(c(1:2, 5:6), row_group_label_position = 'identity') %>%
footnote(general = sourcetxt, general_title = "") %>%
kable_styling(
latex_options = c("HOLD_position", "scale_down"),
bootstrap_options = c("hover", "condensed", "responsive")
)