Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

collapse_rows

Collapse repeated rows to multirow cell


Description

Collapse same values in columns into multirow cells. This feature does similar things with group_rows. However, unlike group_rows, it analyzes existing columns, finds out rows that can be grouped together, and make them multirow cells. Note that if you want to use column_spec to specify column styles, you should use column_spec before collapse_rows.

Usage

collapse_rows(
  kable_input,
  columns = NULL,
  valign = c("middle", "top", "bottom"),
  latex_hline = c("full", "major", "none", "custom", "linespace"),
  row_group_label_position = c("identity", "stack"),
  custom_latex_hline = NULL,
  row_group_label_fonts = NULL,
  headers_to_remove = NULL,
  target = NULL,
  col_names = TRUE,
  longtable_clean_cut = TRUE
)

Arguments

kable_input

Output of knitr::kable() with format specified

columns

A numeric value or vector indicating in which column(s) rows need to be collapsed.

valign

Select from "top", "middle"(default), "bottom". The reason why "top" is not default is that the multirow package on CRAN win-builder is not up to date.

latex_hline

Option controlling the behavior of adding hlines to table. Choose from full, major, none, custom and linespace.

row_group_label_position

Option controlling positions of row group labels. Choose from identity, stack.

custom_latex_hline

Numeric column positions whose collapsed rows will be separated by hlines.

row_group_label_fonts

A list of arguments that can be supplied to group_rows function to format the row group label when row_group_label_position is stack

headers_to_remove

Numeric column positions where headers should be removed when they are stacked.

target

If multiple columns are selected to do collapsing and a target column is specified, this target column will be used to collapse other columns based on the groups of this target column.

col_names

T/F. A LaTeX specific option. If you set col.names be NULL in your kable call, you need to set this option false to let everything work properly.

longtable_clean_cut

T/F with default T. Multirow cell sometimes are displayed incorrectly around pagebreak. This option forces groups to cut before the end of a page. If you have a group that is longer than 1 page, you need to turn off this option.

Examples

## Not run: 
dt <- data.frame(a = c(1, 1, 2, 2), b = c("a", "a", "a", "b"))
x <- knitr::kable(dt, "html")
collapse_rows(x)

## End(Not run)

kableExtra

Construct Complex Table with 'kable' and Pipe Syntax

v1.3.4
MIT + file LICENSE
Authors
Hao Zhu [aut, cre] (<https://orcid.org/0000-0002-3386-6076>), Thomas Travison [ctb], Timothy Tsai [ctb], Will Beasley [ctb], Yihui Xie [ctb], GuangChuang Yu [ctb], Stéphane Laurent [ctb], Rob Shepherd [ctb], Yoni Sidi [ctb], Brian Salzer [ctb], George Gui [ctb], Yeliang Fan [ctb], Duncan Murdoch [ctb], Bill Evans [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.