Learn R Programming

gt (version 0.7.0)

tab_row_group: Add a row group to a gt table

Description

Create a row group with a collection of rows. This requires specification of the rows to be included, either by supplying row labels, row indices, or through use of a select helper function like starts_with(). To modify the order of row groups, use the row_group_order() function.

To set a default row group label for any rows not formally placed in a row group, we can use a separate call to tab_options(row_group.default_label = <label>). If this is not done and there are rows that haven't been placed into a row group (where one or more row groups already exist), those rows will be automatically placed into a row group without a label. To restore labels for row groups not explicitly assigned a group, tab_options(row_group.default_label = "") can be used.

Usage

tab_row_group(data, label, rows, id = label, others_label = NULL, group = NULL)

Value

An object of class gt_tbl.

Arguments

data

A table object that is created using the gt() function.

label

The text to use for the row group label.

rows

The rows to be made components of the row group. Can either be a vector of row captions provided in c(), a vector of row indices, or a helper function focused on selections. The select helper functions are: starts_with(), ends_with(), contains(), matches(), one_of(), and everything().

id

The ID for the row group. When accessing a row group through cells_row_groups() (when using tab_style() or tab_footnote()) the id value is used as the reference (and not the label). If an id is not explicitly provided here, it will be taken from the label value. It is advisable to set an explicit id value if you plan to access this cell in a later function call and the label text is complicated (e.g., contains markup, is lengthy, or both). Finally, when providing an id value you must ensure that it is unique across all ID values set for row groups (the function will stop if id isn't unique).

others_label

This argument is deprecated. Instead use tab_options(row_group.default_label = <label>).

group

This argument is deprecated. Instead use label.

Examples

Use gtcars to create a gt table and use tab_row_group() to add two row groups with the labels: numbered and NA. The row group with the NA label ends up being rendered without a label at all.

gtcars %>%
  dplyr::select(model, year, hp, trq) %>%
  dplyr::slice(1:8) %>%
  gt(rowname_col = "model") %>%
  tab_row_group(
    label = "numbered",
    rows = matches("^[0-9]")
  )

This image of a table was generated from the first code example in the `tab_row_group()` help file.

Use gtcars to create a gt table. Add two row groups with the labels powerful and super powerful. The distinction between the groups is whether hp is lesser or greater than 600 (governed by the expressions provided to the rows argument).

gtcars %>%
  dplyr::select(model, year, hp, trq) %>%
  dplyr::slice(1:8) %>%
  gt(rowname_col = "model") %>%
  tab_row_group(
    label = "powerful",
    rows = hp <= 600
  ) %>%
  tab_row_group(
    label = "super powerful",
    rows = hp > 600
  )

This image of a table was generated from the second code example in the `tab_row_group()` help file.

Function ID

2-4

See Also

Other part creation/modification functions: tab_footnote(), tab_header(), tab_options(), tab_source_note(), tab_spanner_delim(), tab_spanner(), tab_stub_indent(), tab_stubhead(), tab_style()