View source: R/summary_columns.R
| summary_columns | R Documentation |
The summary_columns() function adds one or more computed columns to a table
by aggregating values across selected columns within each row. This is the
horizontal counterpart to summary_rows(), which aggregates down columns to
create summary rows. Common use cases include calculating row totals, row
averages, or other row-wise statistics.
Summary columns are computed immediately when the function is called and are
added to the table's data as regular columns. This means they can be
referenced in subsequent gt operations like cols_move(), fmt_number(),
tab_style(), or even in other computed columns via cols_add().
You can create a single summary column or multiple columns at once by
providing a list of aggregation functions. The functions can be any R
expression that takes a vector of values and returns a single value (like
sum(), mean(), max(), min(), etc.). Column names and labels can be
auto-generated from function names or explicitly specified.
summary_columns(
data,
columns = everything(),
fns = NULL,
new_col_names = NULL,
new_col_labels = NULL,
fmt = NULL,
side = c("right", "left"),
missing_text = "---"
)
data |
The gt table data object
This is the gt table object that is commonly created through use of the
|
columns |
Columns to aggregate
The columns to aggregate across for each row. Can either be a series of
column names provided in |
fns |
Aggregation Expression(s)
One or more functions used for aggregation. For a single summary column,
provide a single function as a formula (e.g., |
new_col_names |
Names for new summary columns
Optional names for the new summary columns. Can provide one name for a
single summary column, or multiple names (must match the length of |
new_col_labels |
Labels for new summary columns
Optional labels for the new summary column headers. Can provide one label
for a single column, or multiple labels (must match the length of |
fmt |
Formatting expression
An optional formatting expression in formula form. The RHS of |
side |
Side used for placement of summary column
Should the summary column be placed on the |
missing_text |
Replacement text for
The text to be used in place of |
An object of class gt_tbl.
Use a subset of the countrypops dataset to create a gt table. Add a
summary column that computes the average annual change using
summary_columns().
countrypops |>
arrange(country_name, year) |>
group_by(country_name, country_code_2) |>
summarize(
pop_1960 = first(population),
pop_2024 = last(population),
years = n(),
.groups = "drop"
) |>
filter(!is.na(pop_1960), !is.na(pop_2024)) |>
mutate(total_change = pop_2024 - pop_1960) |>
slice_max(total_change, n = 5) |>
select(country_name, country_code_2, pop_1960, pop_2024, years) |>
gt() |>
summary_columns(
columns = c(pop_1960, pop_2024, years),
fns = ~ (.[2] - .[1]) / (.[3] - 1),
new_col_names = "avg_annual_change",
new_col_labels = md("**Avg. Annual Change**") # No list() needed!
) |>
cols_hide(columns = c(pop_1960, pop_2024, years)) |>
fmt_integer() |>
cols_label(
country_name = "Country",
country_code_2 = "Code"
) |>
tab_header(
title = "Top 5 Countries by Population Growth",
subtitle = "Average annual population change (1960-2024)"
)
6-6
v1.2.0 (December 16, 2025)
Other row addition/modification functions:
grand_summary_rows(),
row_group_order(),
row_order(),
rows_add(),
summary_rows()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.