Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collapse_rows () has problem with columns #843

Open
Dierforth opened this issue Apr 24, 2024 · 1 comment
Open

collapse_rows () has problem with columns #843

Dierforth opened this issue Apr 24, 2024 · 1 comment

Comments

@Dierforth
Copy link

Dierforth commented Apr 24, 2024

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
@Dierforth Dierforth changed the title collapse_rows has problem with columns collapse_rows () has problem with columns Apr 24, 2024
@Dierforth
Copy link
Author

Dierforth commented Jul 12, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant