Add summary rows to one or more row groups by using the table data and any
suitable aggregation functions. You choose how to format the values in the
resulting summary cells by use of a
formatter function (e.g,
etc.) and any relevant options.
summary_rows( data, groups = NULL, columns = everything(), fns, missing_text = "---", formatter = fmt_number, ... )
A table object that is created using the
The groups to consider for generation of groupwise summary
rows. By default this is set to
NULL, which results in the formation of
grand summary rows (a grand summary operates on all table data). Providing
the names of row groups in
c() will create a groupwise summary and
generate summary rows for the specified groups. Setting this to
indicates that all available groups will receive groupwise summary rows.
The columns for which the summaries should be calculated.
Functions used for aggregations. This can include base functions
sum or any other
user-defined aggregation function. The function(s) should be supplied
list(). Within that list, we can specify the functions by use of
function names in quotes (e.g.,
"sum"), as bare functions (e.g.,
or as one-sided R formulas using a leading
~. In the formula
. serves as the data to be summarized (e.g.,
sum(., na.rm = TRUE)). The use of named arguments is recommended as the names
will serve as summary row labels for the corresponding summary rows data
(the labels can derived from the function names but only when not providing
bare function names).
The text to be used in place of
NA values in summary
cells with no data outputs.
A formatter function name. These can be any of the
functions available in the package (e.g.,
etc.), or a custom function using
fmt(). The default function is
fmt_number() and its options can be accessed through
Values passed to the
formatter function, where the provided
values are to be in the form of named vectors. For example, when using the
fmt_number(), options such as
locale can be used.
An object of class
Should we need to obtain the summary data for external purposes, the
extract_summary() function can be used with a
gt_tbl object where summary
rows were added via
Other Add Rows:
# Use `sp500` to create a gt table with # row groups; create summary rows (`min`, # `max`, `avg`) by row group, where each # each row group is a week number tab_1 <- sp500 %>% dplyr::filter( date >= "2015-01-05" & date <="2015-01-16" ) %>% dplyr::arrange(date) %>% dplyr::mutate( week = paste0( "W", strftime(date, format = "%V")) ) %>% dplyr::select(-adj_close, -volume) %>% gt( rowname_col = "date", groupname_col = "week" ) %>% summary_rows( groups = TRUE, columns = c(open, high, low, close), fns = list( min = ~min(.), max = ~max(.), avg = ~mean(.)), formatter = fmt_number, use_seps = FALSE )