Indentation of row labels is an effective way for establishing structure in a
table stub. The tab_stub_indent()
function allows for fine control over
row label indentation in the stub. We can use an explicit definition of an
indentation level, or, employ an indentation directive using keywords.
Arguments
- data
The gt table data object
obj:<gt_tbl>
// requiredThis is the gt table object that is commonly created through use of the
gt()
function.- rows
Rows to target
<row-targeting expression>
// requiredThe rows to consider for the indentation change. The default
everything()
results in all rows being targeted. Alternatively, we can supply a vector of row captions withinc()
, a vector of row indices, or a select helper function. Examples of select helper functions includestarts_with()
,ends_with()
,contains()
,matches()
,one_of()
,num_range()
, andeverything()
. We can also use expressions to filter down to the rows we need (e.g.,[colname_1] > 100 & [colname_2] < 50
).- indent
Indentation directive
scalar<character|numeric|integer>
// default:"increase"
An indentation directive either as a keyword describing the indentation change or as an explicit integer value for directly setting the indentation level. The keyword
"increase"
(the default) will increase the indentation level by one;"decrease"
will do the same in the reverse direction. The starting indentation level of0
means no indentation and this values serves as a lower bound. The upper bound for indentation is at level5
.
Compatibility of arguments with the from_column()
helper function
The from_column()
helper function can be used with the indent
argument
of tab_stub_indent()
to obtain varying parameter values from a specified
column within the table. This means that each row label could be indented a
little bit differently.
Please note that for this argument (indent
), a from_column()
call needs
to reference a column that has data of the numeric
or integer
type.
Additional columns for parameter values can be generated with the
cols_add()
function (if not already present). Columns that contain
parameter data can also be hidden from final display with cols_hide()
.
Examples
Let's use a summarized version of the pizzaplace
dataset to create a
gt table with row groups and row labels. With the summary_rows()
function, we'll generate summary rows at the top of each row group. With
tab_stub_indent()
we can add indentation to the row labels in the stub.
pizzaplace |>
dplyr::group_by(type, size) |>
dplyr::summarize(
sold = dplyr::n(),
income = sum(price),
.groups = "drop"
) |>
gt(rowname_col = "size", groupname_col = "type") |>
tab_header(title = "Pizzas Sold in 2015") |>
fmt_integer(columns = sold) |>
fmt_currency(columns = income) |>
summary_rows(
fns = list(label = "All Sizes", fn = "sum"),
side = "top",
fmt = list(
~ fmt_integer(., columns = sold),
~ fmt_currency(., columns = income)
)
) |>
tab_options(
summary_row.background.color = "gray95",
row_group.background.color = "#FFEFDB",
row_group.as_column = TRUE
) |>
tab_stub_indent(
rows = everything(),
indent = 2
)
See also
Other part creation/modification functions:
tab_caption()
,
tab_footnote()
,
tab_header()
,
tab_info()
,
tab_options()
,
tab_row_group()
,
tab_source_note()
,
tab_spanner_delim()
,
tab_spanner()
,
tab_stubhead()
,
tab_style_body()
,
tab_style()