Learn R Programming

sumExtras (version 0.3.0)

get_group_rows: Get row numbers of variable group headers for gt styling

Description

Extracts the row indices of variable group headers from a gtsummary table. This is useful for applying background colors or other gt-specific styling after converting a gtsummary table to gt with as_gt().

Usage

get_group_rows(tbl)

Value

An integer vector of row numbers where variable_group headers are located

Arguments

tbl

A gtsummary table object with variable group headers created by gtsummary::add_variable_group_header()

Details

Variable group headers are identified by row_type == 'variable_group' in the table body. The returned row numbers can be used with gt::tab_style() to apply styling like background colors after converting to a gt table.

This function should be called BEFORE converting the table with as_gt(), as the row type information is only available in gtsummary table objects.

See Also

  • add_group_styling() for applying text formatting to group headers

  • gtsummary::add_variable_group_header() for creating variable groups

  • gt::tab_style() for applying gt-specific styling

Examples

Run this code
# \donttest{
# Create table with variable groups
my_tbl <- gtsummary::trial |>
  gtsummary::tbl_summary(by = trt, include = c(age, marker, grade, stage)) |>
  gtsummary::add_variable_group_header(
    header = "Demographics",
    variables = age
  ) |>
  gtsummary::add_variable_group_header(
    header = "Clinical",
    variables = marker:stage
  ) |>
  add_group_styling()

# Get group row numbers before conversion
group_rows <- get_group_rows(my_tbl)

# Convert to gt and apply gray background
my_tbl |>
  gtsummary::as_gt() |>
  gt::tab_style(
    style = gt::cell_fill(color = "#E8E8E8"),
    locations = gt::cells_body(rows = group_rows)
  )
# }

Run the code above in your browser using DataLab