kableExtra icon indicating copy to clipboard operation
kableExtra copied to clipboard

Cannot stack a single row in collapse

Open shabbybanks opened this issue 6 years ago • 3 comments

Using row_group_label_position='stack' with a single column throws an error. Here is a MWE:

suppressMessages({
  require(dplyr)
  require(knitr)
  require(kableExtra)
  require(tibble)
})
tribble(~modl, ~metric,  ~value,
        'AAA',  'med',    0.19,
        'AAA',  'min',   -1.30,
        'BBB',  'med',    0.44,
        'BBB',  'min',   -0.93) %>%
  kableExtra::kable('latex',longtable=TRUE,booktabs=TRUE) %>%
  kableExtra::kable_styling(latex_options = c("repeat_header")) %>%
  kableExtra::collapse_rows(columns=1, row_group_label_position = 'stack')

This throws the error:

Error in group_row_index_list[[i]] : subscript out of bounds

Enter a frame number, or 0 to exit

 1: tribble(~modl, ~metric, ~value, "AAA", "med", 0.19, "AAA", "min", -1.3, "BB
 2: withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
 3: eval(quote(`_fseq`(`_lhs`)), env, env)
 4: eval(expr, envir, enclos)
 5: `_fseq`(`_lhs`)
 6: freduce(value, `_function_list`)
 7: withVisible(function_list[[k]](value))
 8: function_list[[k]](value)
 9: kableExtra::collapse_rows(., columns = 1, row_group_label_position = "stack
10: collapse_rows_latex(kable_input, columns, latex_hline, valign, row_group_la
11: collapse_rows_latex_stack(out, group_row_index_list, row_group_label_fonts)
12: merge_lists(list(kable_input = out, index = group_row_index_list[[i]]), gro

Setting columns=1:2 or row_group_label_position='identity' in the last call avoids the error.

It would seem that for a single column, the stack option should be automagically changed to identity.

shabbybanks avatar Nov 09 '18 18:11 shabbybanks

Ooops, forgot my session info (yes, it's an old version of R):

R version 3.2.2 (2015-08-14)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux Server release 6.10 (Santiago)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] tibble_1.4.2     kableExtra_0.9.0 knitr_1.20       dplyr_0.7.7
[5] fortunes_1.5-4   drat_0.1.2

shabbybanks avatar Nov 09 '18 18:11 shabbybanks

+1 this error just happend to me as well. Any ideas, what could cause this issue and how to fix it?

kapsner avatar Jun 27 '22 14:06 kapsner

A workaround is to use kableExtra::pack_rows, which gives similar results:

dat <- tribble(~modl, ~metric,  ~value,
        'AAA',  'med',    0.19,
        'AAA',  'min',   -1.30,
        'BBB',  'med',    0.44,
        'BBB',  'min',   -0.93)

dat %>%
  kableExtra::kable(format = 'latex',longtable=TRUE,booktabs=TRUE) %>%
  kableExtra::kable_styling(latex_options = c("repeat_header")) %>%
  #kableExtra::collapse_rows(columns=1, row_group_label_position = 'stack')
  kableExtra::pack_rows(index = table(dat$modl))

kapsner avatar Jun 27 '22 20:06 kapsner