The goal of gtExtras is to provide some additional helper functions to
assist in creating beautiful tables with {gt}
You can install the dev version of gtExtras from GitHub with:
remotes::install_github("jthomasmock/gtExtras")
This function allows you to format your columns only on the first row, where remaining rows in that column have whitespace added to the end to maintain proper alignment.
library(gtExtras)
library(gt)
gtcars %>%
head() %>%
dplyr::select(mfr, year, bdy_style, mpg_h, hp) %>%
dplyr::mutate(mpg_h = rnorm(n = dplyr::n(), mean = 22, sd = 1)) %>%
gt::gt() %>%
gt::opt_table_lines() %>%
fmt_symbol_first(column = mfr, symbol = "$", suffix = " ", last_row_n = 6) %>%
fmt_symbol_first(column = year, symbol = NULL, suffix = "%", last_row_n = 6) %>%
fmt_symbol_first(column = mpg_h, symbol = "%", suffix = NULL, last_row_n = 6, decimals = 1) %>%
fmt_symbol_first(column = hp, symbol = "°", suffix = "F", last_row_n = 6, decimals = NULL, symbol_first = TRUE)
You can use pad_fn()
with gt::fmt()
to pad specific columns that
contain numeric values. You will use it when you want to “decimal align”
the values in the column, but not require printing extra trailing
zeroes.
data.frame(x = c(1.2345, 12.345, 123.45, 1234.5, 12345)) %>%
gt() %>%
fmt(fns = function(x){pad_fn(x, nsmall = 4)}) %>%
tab_style(
# MUST USE A MONO-SPACED FONT
style = cell_text(font = google_font("Fira Mono")),
locations = cells_body(columns = x)
)
The package includes two different themes, the gt_theme_538()
styled
after FiveThirtyEight style tables, and gt_theme_espn()
styled after
ESPN style tables.
head(mtcars) %>%
gt() %>%
gt_theme_538()
head(mtcars) %>%
gt() %>%
gt_theme_538()
This is an opinionated diverging color palette. It diverges from low to high as purple to green. It is a good alternative to a red-green diverging palette as a color-blind friendly palette. The specific colors come from colorbrewer2.
Basic usage below, where a specific column is passed.
# basic use
head(mtcars) %>%
gt::gt() %>%
gt_hulk_color(mpg)
Trim provides a tighter range of purple/green so the colors are less pronounced.
head(mtcars) %>%
gt::gt() %>%
# trim gives smaller range of colors
# so the green and purples are not as dark
gt_hulk_color(mpg:disp, trim = TRUE)
Reverse makes higher values represented by purple and lower by green. The default is to have high = green, low = purple.
# option to reverse the color palette
# so that purple is higher
head(mtcars) %>%
gt::gt() %>%
# reverse = green for low, purple for high
gt_hulk_color(mpg:disp, reverse = FALSE)