#------------------------------------------------------------------------------#
#
# /$$
# | $$
# /$$$$$$ /$$$$$$
# /$$__ $$|_ $$_/
# | $$ \ $$ | $$
# | $$ | $$ | $$ /$$
# | $$$$$$$ | $$$$/
# \____ $$ \___/
# /$$ \ $$
# | $$$$$$/
# \______/
#
# This file is part of the 'rstudio/gt' project.
#
# Copyright (c) 2018-2024 gt authors
#
# For full copyright and license information, please look at
# https://gt.rstudio.com/LICENSE.html
#
#------------------------------------------------------------------------------#
#' Format numeric values
#'
#' @description
#'
#' With numeric values in a **gt** table, we can perform number-based
#' formatting so that the targeted values are rendered with a higher
#' consideration for tabular presentation. Furthermore, there is finer control
#' over numeric formatting with the following options:
#'
#' - decimals: choice of the number of decimal places, option to drop
#' trailing zeros, and a choice of the decimal symbol
#' - digit grouping separators: options to enable/disable digit separators
#' and provide a choice of separator symbol
#' - scaling: we can choose to scale targeted values by a multiplier value
#' - large-number suffixing: larger figures (thousands, millions, etc.) can
#' be autoscaled and decorated with the appropriate suffixes
#' - pattern: option to use a text pattern for decoration of the formatted
#' values
#' - locale-based formatting: providing a locale ID will result in number
#' formatting specific to the chosen locale
#'
#' @param data *The gt table data object*
#'
#' `obj:<gt_tbl>` // **required**
#'
#' This is the **gt** table object that is commonly created through use of the
#' [gt()] function.
#'
#' @param columns *Columns to target*
#'
#' `<column-targeting expression>` // *default:* `everything()`
#'
#' Can either be a series of column names provided in `c()`, a vector of
#' column indices, or a select helper function (e.g. [starts_with()],
#' [ends_with()], [contains()], [matches()], [num_range()] and [everything()]).
#'
#' @param rows *Rows to target*
#'
#' `<row-targeting expression>` // *default:* `everything()`
#'
#' In conjunction with `columns`, we can specify which of their rows should
#' undergo formatting. The default [everything()] results in all rows in
#' `columns` being formatted. Alternatively, we can supply a vector of row
#' captions within `c()`, a vector of row indices, or a select helper
#' function (e.g. [starts_with()], [ends_with()], [contains()], [matches()],
#' [num_range()], and [everything()]). We can also use expressions to filter
#' down to the rows we need (e.g., `[colname_1] > 100 & [colname_2] < 50`).
#'
#' @param decimals *Number of decimal places*
#'
#' `scalar<numeric|integer>(val>=0)` // *default:* `2`
#'
#' This corresponds to the exact number of decimal places to use. A value
#' such as `2.34` can, for example, be formatted with `0` decimal places and
#' it would result in `"2"`. With `4` decimal places, the formatted value
#' becomes `"2.3400"`.
#'
#' @param n_sigfig *Number of significant figures*
#'
#' `scalar<numeric|integer>(val>=1)` // *default:* `NULL` (`optional`)
#'
#' A option to format numbers to *n* significant figures. By default, this is
#' `NULL` and thus number values will be formatted according to the number of
#' decimal places set via `decimals`. If opting to format according to the
#' rules of significant figures, `n_sigfig` must be a number greater than or
#' equal to `1`. Any values passed to the `decimals` and `drop_trailing_zeros`
#' arguments will be ignored.
#'
#' @param drop_trailing_zeros *Drop any trailing zeros*
#'
#' `scalar<logical>` // *default:* `FALSE`
#'
#' A logical value that allows for removal of trailing zeros (those redundant
#' zeros after the decimal mark).
#'
#' @param drop_trailing_dec_mark *Drop the trailing decimal mark*
#'
#' `scalar<logical>` // *default:* `TRUE`
#'
#' A logical value that determines whether decimal marks should always appear
#' even if there are no decimal digits to display after formatting (e.g., `23`
#' becomes `23.` if `FALSE`). By default trailing decimal marks are not shown.
#'
#' @param use_seps *Use digit group separators*
#'
#' `scalar<logical>` // *default:* `TRUE`
#'
#' An option to use digit group separators. The type of digit group separator
#' is set by `sep_mark` and overridden if a locale ID is provided to `locale`.
#' This setting is `TRUE` by default.
#'
#' @param accounting *Use accounting style*
#'
#' `scalar<logical>` // *default:* `FALSE`
#'
#' An option to use accounting style for values. Normally, negative values
#' will be shown with a minus sign but using accounting style will instead put
#' any negative values in parentheses.
#'
#' @param scale_by *Scale values by a fixed multiplier*
#'
#' `scalar<numeric|integer>` // *default:* `1`
#'
#' All numeric values will be multiplied by the `scale_by` value before
#' undergoing formatting. Since the `default` value is `1`, no values will be
#' changed unless a different multiplier value is supplied. This value will be
#' ignored if using any of the `suffixing` options (i.e., where `suffixing` is
#' not set to `FALSE`).
#'
#' @param suffixing *Specification for large-number suffixing*
#'
#' `scalar<logical>|vector<character>` // *default:* `FALSE`
#'
#' The `suffixing` option allows us to scale and apply suffixes to larger
#' numbers (e.g., `1924000` can be transformed to `1.92M`). This option can
#' accept a logical value, where `FALSE` (the default) will not perform this
#' transformation and `TRUE` will apply thousands (`"K"`), millions (`"M"`),
#' billions (`"B"`), and trillions (`"T"`) suffixes after automatic value
#' scaling.
#'
#' We can alternatively provide a character vector that serves as a
#' specification for which symbols are to used for each of the value ranges.
#' These preferred symbols will replace the defaults (e.g.,
#' `c("k", "Ml", "Bn", "Tr")` replaces `"K"`, `"M"`, `"B"`, and `"T"`).
#'
#' Including `NA` values in the vector will ensure that the particular range
#' will either not be included in the transformation (e.g.,
#' `c(NA, "M", "B", "T")` won't modify numbers at all in the thousands range)
#' or the range will inherit a previous suffix (e.g., with
#' `c("K", "M", NA, "T")`, all numbers in the range of millions and billions
#' will be in terms of millions).
#'
#' Any use of `suffixing` (where it is not set expressly as `FALSE`) means
#' that any value provided to `scale_by` will be ignored.
#'
#' If using `system = "ind"` then the default suffix set provided by
#' `suffixing = TRUE` will be the equivalent of `c(NA, "L", "Cr")`. This
#' doesn't apply suffixes to the thousands range, but does express values in
#' *lakhs* and *crores*.
#'
#' @param pattern *Specification of the formatting pattern*
#'
#' `scalar<character>` // *default:* `"{x}"`
#'
#' A formatting pattern that allows for decoration of the formatted value. The
#' formatted value is represented by the `{x}` (which can be used multiple
#' times, if needed) and all other characters will be interpreted as string
#' literals.
#'
#' @param sep_mark *Separator mark for digit grouping*
#'
#' `scalar<character>` // *default:* `","`
#'
#' The string to use as a separator between groups of digits. For example,
#' using `sep_mark = ","` with a value of `1000` would result in a formatted
#' value of `"1,000"`. This argument is ignored if a `locale` is supplied
#' (i.e., is not `NULL`).
#'
#' @param dec_mark *Decimal mark*
#'
#' `scalar<character>` // *default:* `"."`
#'
#' The string to be used as the decimal mark. For example, using
#' `dec_mark = ","` with the value `0.152` would result in a formatted value
#' of `"0,152"`). This argument is ignored if a `locale` is supplied (i.e., is
#' not `NULL`).
#'
#' @param force_sign *Forcing the display of a positive sign*
#'
#' `scalar<logical>` // *default:* `FALSE`
#'
#' Should the positive sign be shown for positive values (effectively showing
#' a sign for all values except zero)? If so, use `TRUE` for this option. The
#' default is `FALSE`, where only negative numbers will display a minus sign.
#' This option is disregarded when using accounting notation with
#' `accounting = TRUE`.
#'
#' @param system *Numbering system for grouping separators*
#'
#' `singl-kw:[intl|ind]` // *default:* `"intl"`
#'
#' The international numbering system (keyword: `"intl"`) is widely used and
#' its grouping separators (i.e., `sep_mark`) are always separated by three
#' digits. The alternative system, the Indian numbering system (keyword:
#' `"ind"`), uses grouping separators that correspond to thousand, lakh,
#' crore, and higher quantities.
#'
#' @param locale *Locale identifier*
#'
#' `scalar<character>` // *default:* `NULL` (`optional`)
#'
#' An optional locale identifier that can be used for formatting values
#' according the locale's rules. Examples include `"en"` for English (United
#' States) and `"fr"` for French (France). We can call [info_locales()] for a
#' useful reference for all of the locales that are supported. A locale ID can
#' be also set in the initial [gt()] function call (where it would be used
#' automatically by any function with a `locale` argument) but a `locale`
#' value provided here will override that global locale.
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_number()` is compatible with body cells that are of the `"numeric"` or
#' `"integer"` types. Any other types of body cells are ignored during
#' formatting. This is to say that cells of incompatible data types may be
#' targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_number()` to
#' obtain varying parameter values from a specified column within the table.
#' This means that each row could be formatted a little bit differently. These
#' arguments provide support for [from_column()]:
#'
#' - `decimals`
#' - `n_sigfig`
#' - `drop_trailing_zeros`
#' - `drop_trailing_dec_mark`
#' - `use_seps`
#' - `accounting`
#' - `scale_by`
#' - `suffixing`
#' - `pattern`
#' - `sep_mark`
#' - `dec_mark`
#' - `force_sign`
#' - `system`
#' - `locale`
#'
#' Please note that for all of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Adapting output to a specific `locale`:
#'
#' This formatting function can adapt outputs according to a provided `locale`
#' value. Examples include `"en"` for English (United States) and `"fr"` for
#' French (France). The use of a valid locale ID here means separator and
#' decimal marks will be correct for the given locale. Should any values be
#' provided in `sep_mark` or `dec_mark`, they will be overridden by the locale's
#' preferred values.
#'
#' Note that a `locale` value provided here will override any global locale
#' setting performed in [gt()]'s own `locale` argument (it is settable there as
#' a value received by all other functions that have a `locale` argument). As a
#' useful reference on which locales are supported, we can call [info_locales()]
#' to view an info table.
#'
#' @section Examples:
#'
#' Let's use the [`exibble`] dataset to create a **gt** table. With
#' `fmt_number()`, we'll format the `num` column to have three decimal
#' places (with `decimals = 3`) and omit the use of digit separators (with
#' `use_seps = FALSE`).
#'
#' ```r
#' exibble |>
#' gt() |>
#' fmt_number(
#' columns = num,
#' decimals = 3,
#' use_seps = FALSE
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_number_1.png")`
#' }}
#'
#' Use a modified version of the [`countrypops`] dataset to create a **gt**
#' table with row labels. Format all columns to use large-number suffixing
#' (e.g., where `"10,000,000"` becomes `"10M"`) with the `suffixing = TRUE`
#' option.
#'
#' ```r
#' countrypops |>
#' dplyr::select(country_code_3, year, population) |>
#' dplyr::filter(country_code_3 %in% c("CHN", "IND", "USA", "PAK", "IDN")) |>
#' dplyr::filter(year > 1975 & year %% 5 == 0) |>
#' tidyr::spread(year, population) |>
#' dplyr::arrange(desc(`2015`)) |>
#' gt(rowname_col = "country_code_3") |>
#' fmt_number(suffixing = TRUE)
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_number_2.png")`
#' }}
#'
#' In a variation of the previous table, we can combine large-number suffixing
#' with a declaration of the number of significant digits to use. With things
#' like population figures, `n_sigfig = 3` is a very good option.
#'
#' ```r
#' countrypops |>
#' dplyr::select(country_code_3, year, population) |>
#' dplyr::filter(country_code_3 %in% c("CHN", "IND", "USA", "PAK", "IDN")) |>
#' dplyr::filter(year > 1975 & year %% 5 == 0) |>
#' tidyr::spread(year, population) |>
#' dplyr::arrange(desc(`2015`)) |>
#' gt(rowname_col = "country_code_3") |>
#' fmt_number(suffixing = TRUE, n_sigfig = 3)
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_number_3.png")`
#' }}
#'
#' There can be cases where you want to show numbers to a large number of
#' decimal places but also drop the unnecessary trailing zeros for low-precision
#' values. Let's take a portion of the [`towny`] dataset and format the
#' `latitude` and `longitude` columns with `fmt_number()`. We'll have up to 5
#' digits displayed as decimal values, but we'll also unconditionally drop any
#' runs of trailing zeros in the decimal part with `drop_trailing_zeros = TRUE`.
#'
#' ```r
#' towny |>
#' dplyr::select(name, latitude, longitude) |>
#' dplyr::slice_head(n = 10) |>
#' gt() |>
#' fmt_number(decimals = 5, drop_trailing_zeros = TRUE) |>
#' cols_merge(columns = -name, pattern = "{1}, {2}") |>
#' cols_label(
#' name ~ "Municipality",
#' latitude = "Location"
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_number_4.png")`
#' }}
#'
#' Another strategy for dealing with precision of decimals is to have a separate
#' column of values that specify how many decimal digits to retain. Such a
#' column can be added via [cols_add()] or it can be part of the input table for
#' [gt()]. With that column available, it can be referenced in the `decimals`
#' argument with [from_column()]. This approach yields a display of coordinate
#' values that reflects the measurement precision of each value.
#'
#' ```r
#' towny |>
#' dplyr::select(name, latitude, longitude) |>
#' dplyr::slice_head(n = 10) |>
#' gt() |>
#' cols_add(dec_digits = c(1, 2, 2, 5, 5, 2, 3, 2, 3, 3)) |>
#' fmt_number(decimals = from_column(column = "dec_digits")) |>
#' cols_merge(columns = -name, pattern = "{1}, {2}") |>
#' cols_label(
#' name ~ "Municipality",
#' latitude = "Location"
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_number_5.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-1
#'
#' @section Function Introduced:
#' `v0.2.0.5` (March 31, 2020)
#'
#' @seealso
#' The integer-formatting function (format rounded values (i.e., no decimals shown and
#'input values are rounded as necessary): [fmt_integer()].
#'
#' The vector-formatting version of this function: [vec_fmt_number()]
#'
#' @export
fmt_number <- function(
data,
columns = everything(),
rows = everything(),
decimals = 2,
n_sigfig = NULL,
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = TRUE,
use_seps = TRUE,
accounting = FALSE,
scale_by = 1.0,
suffixing = FALSE,
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
force_sign = FALSE,
system = c("intl", "ind"),
locale = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
#
# Begin support for `from_column()` objects passed to compatible arguments
#
# Supports parameters:
#
# - decimals
# - n_sigfig
# - drop_trailing_zeros
# - drop_trailing_dec_mark
# - use_seps
# - accounting
# - scale_by
# - suffixing
# - pattern
# - sep_mark
# - dec_mark
# - force_sign
# - system
# - locale
arg_vals <-
mget(
get_arg_names(
function_name = "fmt_number",
all_args_except = c("data", "columns", "rows")
)
)
# Assume that if decimals = 0, function = fmt_integer, fmt_number otherwise
if (identical(decimals, 0)) {
fn_call <- call("fmt_integer")
} else {
fn_call <- call("fmt_number")
}
if (args_have_gt_column_obj(arg_vals = arg_vals)) {
# Resolve the row numbers using `resolve_vars()`
resolved_rows_idx <-
resolve_rows_i(
expr = {{ rows }},
data = data,
call = fn_call
)
param_tbl <-
generate_param_tbl(
data = data,
arg_vals = arg_vals,
resolved_rows_idx = resolved_rows_idx
)
for (i in seq_len(nrow(param_tbl))) {
p_i <- as.list(param_tbl[i, ])
data <-
fmt_number(
data = data,
columns = {{ columns }},
rows = resolved_rows_idx[i],
decimals = p_i$decimals %||% decimals,
n_sigfig = p_i$n_sigfig %||% n_sigfig,
drop_trailing_zeros = p_i$drop_trailing_zeros %||% drop_trailing_zeros,
drop_trailing_dec_mark = p_i$drop_trailing_dec_mark %||% drop_trailing_dec_mark,
use_seps = p_i$use_seps %||% use_seps,
accounting = p_i$accounting %||% accounting,
scale_by = p_i$scale_by %||% scale_by,
suffixing = p_i$suffixing %||% suffixing,
pattern = p_i$pattern %||% pattern,
sep_mark = p_i$sep_mark %||% sep_mark,
dec_mark = p_i$dec_mark %||% dec_mark,
force_sign = p_i$force_sign %||% force_sign,
system = p_i$system %||% system,
locale = p_i$locale %||% locale
)
}
return(data)
}
#
# End support for `from_column()` objects passed to compatible arguments
#
# Ensure that arguments are matched
system <- rlang::arg_match(system)
# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
validate_locale(locale = locale)
locale <- normalize_locale(locale = locale)
locale <- resolve_locale(data = data, locale = locale)
# Use locale-based marks if a locale ID is provided
sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps)
dec_mark <- get_locale_dec_mark(locale, dec_mark)
# Normalize the `suffixing` input to either return a character vector
# of suffix labels, or NULL (the case where `suffixing` is FALSE)
suffix_labels <- normalize_suffixing_inputs(suffixing, scale_by, system)
valid_class <- c("numeric", "integer")
check_columns_valid_if_strict(data, {{ columns }}, valid_class, call = fn_call)
# Set the `formatC_format` option according to whether number
# formatting with significant figures is to be performed
if (!is.null(n_sigfig) && !is.na(n_sigfig[1])) {
# Stop function if `n_sigfig` does not have a valid value
validate_n_sigfig(n_sigfig = n_sigfig)
formatC_format <- "fg"
} else {
formatC_format <- "f"
}
# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
fmt(
data = data,
columns = {{ columns }},
rows = {{ rows }},
compat = valid_class,
fns = num_fmt_factory_multi(
pattern = pattern,
format_fn = function(x, context) {
# Create the `suffix_df` object
suffix_df <-
create_suffix_df(
x,
decimals = decimals,
suffix_labels = suffix_labels,
scale_by = scale_by,
system = system
)
# Determine whether `scale_by` is supplied as a function; if it isn't
# the only other pathway involves getting the vector of values produced
# by the `create_suffix_df()` function
if (rlang::is_function(scale_by)) {
to_scale_by <- scale_by
} else {
to_scale_by <- suffix_df$scale_by
}
# Scale the `x` values using `to_scale_by` (either a function or
# a numeric vector)
x <- scale_x_values(x, scale_by = to_scale_by)
# Format numeric values to character-based numbers
x_str <-
format_num_to_str(
x,
context = context,
decimals = decimals,
n_sigfig = n_sigfig,
sep_mark = sep_mark,
dec_mark = dec_mark,
drop_trailing_zeros = drop_trailing_zeros,
drop_trailing_dec_mark = drop_trailing_dec_mark,
format = formatC_format,
system = system
)
# Paste the vector of suffixes to the right of the values
x_str <- paste_right(x_str, x_right = suffix_df$suffix)
# Format values in accounting notation (if `accounting = TRUE`)
x_str <-
format_as_accounting(
x_str,
x = x,
context = context,
accounting = accounting
)
# Force a positive sign on certain values if the option is taken
if (!accounting && force_sign) {
positive_x <- !is.na(x) & x > 0
x_str[positive_x] <- paste_left(x_str[positive_x], x_left = "+")
}
x_str
}
)
)
}
#' Format values as integers
#'
#' @description
#'
#' With numeric values in a **gt** table, we can perform number-based
#' formatting so that the targeted values are always rendered as integer values.
#' We can have fine control over integer formatting with the following options:
#'
#' - digit grouping separators: options to enable/disable digit separators
#' and provide a choice of separator symbol
#' - scaling: we can choose to scale targeted values by a multiplier value
#' - large-number suffixing: larger figures (thousands, millions, etc.) can
#' be autoscaled and decorated with the appropriate suffixes
#' - pattern: option to use a text pattern for decoration of the formatted
#' values
#' - locale-based formatting: providing a locale ID will result in number
#' formatting specific to the chosen locale
#'
#' @inheritParams fmt_number
#'
#' @param suffixing *Specification for large-number suffixing*
#'
#' `scalar<logical>|vector<character>` // *default:* `FALSE`
#'
#' The `suffixing` option allows us to scale and apply suffixes to larger
#' numbers (e.g., `1924000` can be transformed to `2M`). This option can
#' accept a logical value, where `FALSE` (the default) will not perform this
#' transformation and `TRUE` will apply thousands (`K`), millions (`M`),
#' billions (`B`), and trillions (`T`) suffixes after automatic value scaling.
#'
#' We can alternatively provide a character vector that serves as a
#' specification for which symbols are to used for each of the value ranges.
#' These preferred symbols will replace the defaults (e.g.,
#' `c("k", "Ml", "Bn", "Tr")` replaces `"K"`, `"M"`, `"B"`, and `"T"`).
#'
#' Including `NA` values in the vector will ensure that the particular range
#' will either not be included in the transformation (e.g.,
#' `c(NA, "M", "B", "T")` won't modify numbers at all in the thousands range)
#' or the range will inherit a previous suffix (e.g., with
#' `c("K", "M", NA, "T")`, all numbers in the range of millions and billions
#' will be in terms of millions).
#'
#' Any use of `suffixing` (where it is not set expressly as `FALSE`) means
#' that any value provided to `scale_by` will be ignored.
#'
#' If using `system = "ind"` then the default suffix set provided by
#' `suffixing = TRUE` will be the equivalent of `c(NA, "L", "Cr")`. This
#' doesn't apply suffixes to the thousands range, but does express values in
#' *lakhs* and *crores*.
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_integer()` is compatible with body cells that are of the `"numeric"` or
#' `"integer"` types. Any other types of body cells are ignored during
#' formatting. This is to say that cells of incompatible data types may be
#' targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_integer()` to
#' obtain varying parameter values from a specified column within the table.
#' This means that each row could be formatted a little bit differently. These
#' arguments provide support for [from_column()]:
#'
#' - `use_seps`
#' - `accounting`
#' - `scale_by`
#' - `suffixing`
#' - `pattern`
#' - `sep_mark`
#' - `force_sign`
#' - `system`
#' - `locale`
#'
#' Please note that for all of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Adapting output to a specific `locale`:
#'
#' This formatting function can adapt outputs according to a provided `locale`
#' value. Examples include `"en"` for English (United States) and `"fr"` for
#' French (France). The use of a valid locale ID here means separator marks will
#' be correct for the given locale. Should any value be provided in `sep_mark`,
#' it will be overridden by the locale's preferred value.
#'
#' Note that a `locale` value provided here will override any global locale
#' setting performed in [gt()]'s own `locale` argument (it is settable there as
#' a value received by all other functions that have a `locale` argument). As a
#' useful reference on which locales are supported, we can call [info_locales()]
#' to view an info table.
#'
#' @section Examples:
#'
#' For this example, we'll use two columns from the [`exibble`] dataset and
#' create a simple **gt** table. With `fmt_integer()`, we'll format the `num`
#' column as integer values having no digit separators (with the
#' `use_seps = FALSE` option).
#'
#' ```r
#' exibble |>
#' dplyr::select(num, char) |>
#' gt() |>
#' fmt_integer(use_seps = FALSE)
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_integer_1.png")`
#' }}
#'
#' Let's use a modified version of the [`countrypops`] dataset to create a
#' **gt** table with row labels. We will format all numeric columns with
#' `fmt_integer()` and scale all values by `1 / 1E6`, giving us integer values
#' representing millions of people. We can make clear what the values represent
#' with an informative spanner label via [tab_spanner()].
#'
#' ```r
#' countrypops |>
#' dplyr::select(country_code_3, year, population) |>
#' dplyr::filter(country_code_3 %in% c("CHN", "IND", "USA", "PAK", "IDN")) |>
#' dplyr::filter(year > 1975 & year %% 5 == 0) |>
#' tidyr::spread(year, population) |>
#' dplyr::arrange(desc(`2015`)) |>
#' gt(rowname_col = "country_code_3") |>
#' fmt_integer(scale_by = 1 / 1E6) |>
#' tab_spanner(label = "Millions of People", columns = everything())
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_integer_2.png")`
#' }}
#'
#' Using a subset of the [`towny`] dataset, we can do interesting things with
#' integer values. Through [cols_add()] we'll add the `difference` column (which
#' calculates the difference between 2021 and 2001 populations). All numeric
#' values will be formatted with a first pass of `fmt_integer()`; a second pass
#' of `fmt_integer()` focuses on the `difference` column and here we use the
#' `force_sign = TRUE` option to draw attention to positive and negative
#' difference values.
#'
#' ```r
#' towny |>
#' dplyr::select(name, population_2001, population_2021) |>
#' dplyr::slice_tail(n = 10) |>
#' gt() |>
#' cols_add(difference = population_2021 - population_2001) |>
#' fmt_integer() |>
#' fmt_integer(columns = difference, force_sign = TRUE) |>
#' cols_label_with(fn = function(x) gsub("population_", "", x)) |>
#' tab_style(
#' style = cell_fill(color = "gray90"),
#' locations = cells_body(columns = difference)
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_integer_3.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-2
#'
#' @section Function Introduced:
#' `v0.3.1` (August 9, 2021)
#'
#' @seealso
#' Format number with decimal values: [fmt_number()]
#'
#' The vector-formatting version of this function: [vec_fmt_integer()]
#'
#' @export
fmt_integer <- function(
data,
columns = everything(),
rows = everything(),
use_seps = TRUE,
accounting = FALSE,
scale_by = 1.0,
suffixing = FALSE,
pattern = "{x}",
sep_mark = ",",
force_sign = FALSE,
system = c("intl", "ind"),
locale = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
#
# Begin support for `from_column()` objects passed to compatible arguments
#
# Supports parameters:
#
# - use_seps
# - accounting
# - scale_by
# - suffixing
# - pattern
# - sep_mark
# - force_sign
# - system
# - locale
arg_vals <-
mget(
get_arg_names(
function_name = "fmt_integer",
all_args_except = c("data", "columns", "rows")
)
)
if (args_have_gt_column_obj(arg_vals = arg_vals)) {
# Resolve the row numbers using `resolve_vars()`
resolved_rows_idx <-
resolve_rows_i(
expr = {{ rows }},
data = data
)
param_tbl <-
generate_param_tbl(
data = data,
arg_vals = arg_vals,
resolved_rows_idx = resolved_rows_idx
)
for (i in seq_len(nrow(param_tbl))) {
p_i <- as.list(param_tbl[i, ])
data <-
fmt_integer(
data = data,
columns = {{ columns }},
rows = resolved_rows_idx[i],
use_seps = p_i$use_seps %||% use_seps,
accounting = p_i$accounting %||% accounting,
scale_by = p_i$scale_by %||% scale_by,
suffixing = p_i$suffixing %||% suffixing,
pattern = p_i$pattern %||% pattern,
sep_mark = p_i$sep_mark %||% sep_mark,
force_sign = p_i$force_sign %||% force_sign,
system = p_i$system %||% system,
locale = p_i$locale %||% locale
)
}
return(data)
}
#
# End support for `from_column()` objects passed to compatible arguments
#
fmt_number(
data = data,
columns = {{ columns }},
rows = {{ rows }},
decimals = 0,
n_sigfig = NULL,
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = TRUE,
use_seps = use_seps,
accounting = accounting,
scale_by = scale_by,
suffixing = suffixing,
pattern = pattern,
sep_mark = sep_mark,
dec_mark = "not used",
force_sign = force_sign,
system = system,
locale = locale
)
}
#' Format values to scientific notation
#'
#' @description
#'
#' With numeric values in a **gt** table, we can perform formatting so that the
#' targeted values are rendered in scientific notation, where extremely large or
#' very small numbers can be expressed in a more practical fashion. Here,
#' numbers are written in the form of a mantissa (`m`) and an exponent (`n`)
#' with the construction *m* x 10^*n* or *m*E*n*. The mantissa component is a
#' number between `1` and `10`. For instance, `2.5 x 10^9` can be used to
#' represent the value 2,500,000,000 in scientific notation. In a similar way,
#' 0.00000012 can be expressed as `1.2 x 10^-7`. Due to its ability to describe
#' numbers more succinctly and its ease of calculation, scientific notation is
#' widely employed in scientific and technical domains.
#'
#' We have fine control over the formatting task, with the following options:
#'
#' - decimals: choice of the number of decimal places, option to drop
#' trailing zeros, and a choice of the decimal symbol
#' - scaling: we can choose to scale targeted values by a multiplier value
#' - pattern: option to use a text pattern for decoration of the formatted
#' values
#' - locale-based formatting: providing a locale ID will result in
#' formatting specific to the chosen locale
#'
#' @inheritParams fmt_number
#'
#' @param scale_by *Scale values by a fixed multiplier*
#'
#' `scalar<numeric|integer>` // *default:* `1`
#'
#' All numeric values will be multiplied by the `scale_by` value before
#' undergoing formatting. Since the `default` value is `1`, no values will be
#' changed unless a different multiplier value is supplied.
#'
#' @param exp_style *Style declaration for exponent formatting*
#'
#' `scalar<character>` // *default:* `"x10n"`
#'
#' Style of formatting to use for the scientific notation formatting. By
#' default this is `"x10n"` but other options include using a single letter
#' (e.g., `"e"`, `"E"`, etc.), a letter followed by a `"1"` to signal a
#' minimum digit width of one, or `"low-ten"` for using a stylized `"10"`
#' marker.
#'
#' @param force_sign_m,force_sign_n *Forcing the display of a positive sign*
#'
#' `scalar<logical>` // *default:* `FALSE`
#'
#' Should the plus sign be shown for positive values of the mantissa (first
#' component, `force_sign_m`) or the exponent (`force_sign_n`)? This would
#' effectively show a sign for all values except zero on either of those
#' numeric components of the notation. If so, use `TRUE` for either one of
#' these options. The default for both is `FALSE`, where only negative numbers
#' will display a sign.
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_scientific()` is compatible with body cells that are of the `"numeric"`
#' or `"integer"` types. Any other types of body cells are ignored during
#' formatting. This is to say that cells of incompatible data types may be
#' targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_scientific()` to
#' obtain varying parameter values from a specified column within the table.
#' This means that each row could be formatted a little bit differently. These
#' arguments provide support for [from_column()]:
#'
#' - `decimals`
#' - `drop_trailing_zeros`
#' - `drop_trailing_dec_mark`
#' - `scale_by`
#' - `exp_style`
#' - `pattern`
#' - `sep_mark`
#' - `dec_mark`
#' - `force_sign_m`
#' - `force_sign_n`
#' - `locale`
#'
#' Please note that for all of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Adapting output to a specific `locale`:
#'
#' This formatting function can adapt outputs according to a provided `locale`
#' value. Examples include `"en"` for English (United States) and `"fr"` for
#' French (France). The use of a valid locale ID here means separator and
#' decimal marks will be correct for the given locale. Should any values be
#' provided in `sep_mark` or `dec_mark`, they will be overridden by the locale's
#' preferred values.
#'
#' Note that a `locale` value provided here will override any global locale
#' setting performed in [gt()]'s own `locale` argument (it is settable there as
#' a value received by all other functions that have a `locale` argument). As a
#' useful reference on which locales are supported, we can call [info_locales()]
#' to view an info table.
#'
#' @section Examples:
#'
#' Let's define a data frame that contains two columns of values (one `small`
#' and one `large`). After creating a simple **gt** table from `small_large_tbl`
#' we'll call `fmt_scientific()` on both columns.
#'
#' ```r
#' small_large_tbl <-
#' dplyr::tibble(
#' small = 10^(-12:-1),
#' large = 10^(1:12)
#' )
#'
#' small_large_tbl |>
#' gt() |>
#' fmt_scientific()
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_scientific_1.png")`
#' }}
#'
#' The default method of styling the notation uses the '*m* x 10^*n*'
#' construction but this can be changed to a '*m*E*n*' style via the `exp_style`
#' argument. We can supply any single letter here and optionally affix a `"1"`
#' to indicate there should not be any zero-padding of the *n* value. Two calls
#' of `fmt_scientific()` are used here to show different options for styling
#' in scientific notation.
#'
#' ```r
#' small_large_tbl |>
#' gt() |>
#' fmt_scientific(
#' columns = small,
#' exp_style = "E"
#' ) |>
#' fmt_scientific(
#' columns = large,
#' exp_style = "e1",
#' force_sign_n = TRUE
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_scientific_2.png")`
#' }}
#'
#' Taking a portion of the [`reactions`] dataset, we can create a **gt** table
#' that contains reaction rate constants that should be expressed in scientific
#' notation. All of the numeric values in the filtered table require that
#' type of formatting so `fmt_scientific()` can be called without requiring any
#' specification of column names in the `columns` argument. By default, the
#' number of decimal places is fixed to `2`, which is fine for this table.
#'
#' ```r
#' reactions |>
#' dplyr::filter(cmpd_type == "mercaptan") |>
#' dplyr::select(cmpd_name, cmpd_formula, OH_k298, Cl_k298, NO3_k298) |>
#' gt(rowname_col = "cmpd_name") |>
#' tab_header(title = "Gas-phase reactions of selected mercaptan compounds") |>
#' tab_spanner(
#' label = md("Reaction Rate Constant (298 K),<br>{{cm^3 molecules^-1 s^-1}}"),
#' columns = ends_with("k298")
#' ) |>
#' fmt_chem(columns = cmpd_formula) |>
#' fmt_scientific() |>
#' sub_missing() |>
#' cols_label(
#' cmpd_formula = "",
#' OH_k298 = "OH",
#' NO3_k298 = "{{%NO3%}}",
#' Cl_k298 = "Cl"
#' ) |>
#' opt_stylize() |>
#' opt_horizontal_padding(scale = 3) |>
#' opt_table_font(font = google_font("IBM Plex Sans")) |>
#' tab_options(stub.font.weight = "500")
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_scientific_3.png")`
#' }}
#'
#' The [`constants`] table contains a plethora of data on the fundamental
#' physical constants and values range from very small to very large, warranting
#' the use of figures in scientific notation. Because the values differ in the
#' degree of measurement precision, the dataset has columns (`sf_value` and
#' `sf_uncert`) that include the number of significant figures for each
#' measurement value and for the associated uncertainty. We can use the
#' `n_sigfig` argument of `fmt_scientific()` in conjunction with the
#' [from_column()] helper to format each value and its uncertainty to the proper
#' number of significant digits.
#'
#' ```r
#' constants |>
#' dplyr::filter(grepl("Planck", name)) |>
#' gt() |>
#' fmt_scientific(
#' columns = value,
#' n_sigfig = from_column(column = "sf_value")
#' ) |>
#' fmt_scientific(
#' columns = uncert,
#' n_sigfig = from_column(column = "sf_uncert")
#' ) |>
#' cols_hide(columns = starts_with("sf")) |>
#' fmt_units(columns = units) |>
#' sub_missing(missing_text = "")
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_scientific_4.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-3
#'
#' @section Function Introduced:
#' `v0.2.0.5` (March 31, 2020)
#'
#' @seealso The vector-formatting version of this function:
#' [vec_fmt_scientific()].
#'
#' @export
fmt_scientific <- function(
data,
columns = everything(),
rows = everything(),
decimals = 2,
n_sigfig = NULL,
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = TRUE,
scale_by = 1.0,
exp_style = "x10n",
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
force_sign_m = FALSE,
force_sign_n = FALSE,
locale = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
#
# Begin support for `from_column()` objects passed to compatible arguments
#
# Supports parameters:
#
# - decimals
# - n_sigfig
# - drop_trailing_zeros
# - drop_trailing_dec_mark
# - scale_by
# - exp_style
# - pattern
# - sep_mark
# - dec_mark
# - force_sign_m
# - force_sign_n
# - locale
arg_vals <-
mget(
get_arg_names(
function_name = "fmt_scientific",
all_args_except = c("data", "columns", "rows")
)
)
if (args_have_gt_column_obj(arg_vals = arg_vals)) {
# Resolve the row numbers using `resolve_vars()`
resolved_rows_idx <-
resolve_rows_i(
expr = {{ rows }},
data = data
)
param_tbl <-
generate_param_tbl(
data = data,
arg_vals = arg_vals,
resolved_rows_idx = resolved_rows_idx
)
for (i in seq_len(nrow(param_tbl))) {
p_i <- as.list(param_tbl[i, ])
data <-
fmt_scientific(
data = data,
columns = {{ columns }},
rows = resolved_rows_idx[i],
decimals = p_i$decimals %||% decimals,
n_sigfig = p_i$n_sigfig %||% n_sigfig,
drop_trailing_zeros = p_i$drop_trailing_zeros %||% drop_trailing_zeros,
drop_trailing_dec_mark = p_i$drop_trailing_dec_mark %||% drop_trailing_dec_mark,
scale_by = p_i$scale_by %||% scale_by,
exp_style = p_i$exp_style %||% exp_style,
pattern = p_i$pattern %||% pattern,
sep_mark = p_i$sep_mark %||% sep_mark,
dec_mark = p_i$dec_mark %||% dec_mark,
force_sign_m = p_i$force_sign_m %||% force_sign_m,
force_sign_n = p_i$force_sign_n %||% force_sign_n,
locale = p_i$locale %||% locale
)
}
return(data)
}
#
# End support for `from_column()` objects passed to compatible arguments
#
# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
validate_locale(locale = locale)
locale <- normalize_locale(locale = locale)
locale <- resolve_locale(data = data, locale = locale)
# Set default values
suffixing <- FALSE
use_seps <- TRUE
# Use locale-based marks if a locale ID is provided
sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps)
dec_mark <- get_locale_dec_mark(locale, dec_mark)
# Normalize the `suffixing` input to either return a character vector
# of suffix labels, or NULL (the case where `suffixing` is FALSE)
suffix_labels <- normalize_suffixing_inputs(suffixing, scale_by, system = "intl")
valid_class <- c("numeric", "integer")
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
# If `n_sigfig` is defined (and not `NA`) modify the number of
# decimal places and keep all trailing zeros
if (!is.null(n_sigfig) && !is.na(n_sigfig[1])) {
# Stop function if `n_sigfig` does not have a valid value
validate_n_sigfig(n_sigfig = n_sigfig)
decimals <- n_sigfig - 1
drop_trailing_zeros <- FALSE
}
# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
fmt(
data = data,
columns = {{ columns }},
rows = {{ rows }},
compat = valid_class,
fns = num_fmt_factory_multi(
pattern = pattern,
format_fn = function(x, context) {
# Define the marks by context
exp_marks <- context_exp_marks(context = context)
minus_mark <- context_minus_mark(context = context)
# Define the `replace_minus()` function
replace_minus <- function(x) {
gsub("-", minus_mark, x, fixed = TRUE)
}
# Create the `suffix_df` object
suffix_df <-
create_suffix_df(
x,
decimals = decimals,
suffix_labels = suffix_labels,
scale_by = scale_by,
system = "intl"
)
# Determine whether `scale_by` is supplied as a function; if it isn't
# the only other pathway involves getting the vector of values produced
# by the `create_suffix_df()` function
if (rlang::is_function(scale_by)) {
to_scale_by <- scale_by
} else {
to_scale_by <- suffix_df$scale_by
}
# Scale the `x` values using `to_scale_by` (either a function or
# a numeric vector)
x <- scale_x_values(x, scale_by = suffix_df$scale_by)
x_str <-
format_num_to_str(
x,
context = context,
decimals = decimals,
n_sigfig = NULL,
sep_mark = sep_mark,
dec_mark = dec_mark,
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = FALSE,
format = "e",
replace_minus_mark = FALSE
)
if (exp_style == "x10n") {
# Determine which values don't require the (x 10^n)
# for scientific formatting since their order would be zero
small_pos <- has_order_zero(x)
# For any numbers that shouldn't have an exponent, remove
# that portion from the character version
x_str[small_pos] <- replace_minus(gsub("(e|E).*", "", x_str[small_pos]))
# For any non-NA numbers that do have an exponent, format
# those according to the output context
sci_parts <- split_scientific_notn(x_str = x_str[!small_pos])
m_part <- sci_parts[["num"]]
n_part <- sci_parts[["exp"]]
if (force_sign_n) {
n_part <-
vapply(
n_part,
FUN.VALUE = character(1),
USE.NAMES = FALSE,
FUN = function(x) {
if (x > 0) gsub("^", "+", x) else as.character(x)
}
)
}
if (drop_trailing_zeros) {
m_part <- sub("0+$", "", m_part)
x_str[small_pos] <- sub("0+$", "", x_str[small_pos])
}
if (drop_trailing_dec_mark) {
m_part <- sub("\\.$", "", m_part)
x_str[small_pos] <- sub("\\.$", "", x_str[small_pos])
}
m_part <- replace_minus(m_part)
n_part <- replace_minus(n_part)
x_str[!small_pos] <-
paste0(m_part, exp_marks[1L], n_part, exp_marks[2L])
} else {
exp_str <- context_exp_str(exp_style = exp_style, context = context)
if (grepl("^[a-zA-Z]{1}1$", exp_style)) {
n_min_width <- 1
} else {
n_min_width <- 2
}
# The `n_part` will be extracted here and it must be padded to
# the defined minimum number of decimal places
n_part <-
vapply(
x_str,
FUN.VALUE = character(1L),
USE.NAMES = FALSE,
FUN = function(x) {
if (!grepl("e(\\+|-)[0-9]{2,}", x)) return("")
x <- unlist(strsplit(x, "e"))[2L]
if (grepl("-", x)) {
x <- gsub("-", "", x)
x <- formatC(as.numeric(x), width = n_min_width, flag = "0")
x <- paste0("-", x)
} else {
x <- formatC(as.numeric(x), width = n_min_width, flag = "0")
}
x
}
)
# Generate `x_str_left` using `x_str` here
x_str_left <-
vapply(
x_str,
FUN.VALUE = character(1L),
USE.NAMES = FALSE,
FUN = function(x) {
if (!grepl("e(\\+|-)[0-9]{2,}", x)) return("")
unlist(strsplit(x, "e"))[1]
}
)
if (force_sign_n) {
n_part <-
vapply(
seq_along(n_part),
FUN.VALUE = character(1L),
USE.NAMES = FALSE,
FUN = function(i) {
if (!grepl("-", n_part[i])) {
out <- gsub("^", "+", n_part[i])
} else {
out <- n_part[i]
}
out
}
)
}
x_str[!is.infinite(x)] <-
paste0(x_str_left[!is.infinite(x)], exp_str, replace_minus(n_part[!is.infinite(x)]))
x_str <- replace_minus(x_str)
}
# Force a positive sign on certain values if the option is taken
if (force_sign_m) {
positive_x <- !is.na(x) & x > 0
x_str[positive_x] <- paste_left(x_str[positive_x], x_left = "+")
}
x_str
}
)
)
}
#' Format values to engineering notation
#'
#' @description
#'
#' With numeric values in a **gt** table, we can perform formatting so that the
#' targeted values are rendered in engineering notation, where numbers are
#' written in the form of a mantissa (`m`) and an exponent (`n`). When combined
#' the construction is either of the form *m* x 10^*n* or *m*E*n*. The mantissa
#' is a number between `1` and `1000` and the exponent is a multiple of `3`. For
#' example, the number 0.0000345 can be written in engineering notation as
#' `34.50 x 10^-6`. This notation helps to simplify calculations and make it
#' easier to compare numbers that are on very different scales.
#'
#' We have fine control over the formatting task, with the following options:
#'
#' - decimals: choice of the number of decimal places, option to drop
#' trailing zeros, and a choice of the decimal symbol
#' - scaling: we can choose to scale targeted values by a multiplier value
#' - pattern: option to use a text pattern for decoration of the formatted
#' values
#' - locale-based formatting: providing a locale ID will result in
#' formatting specific to the chosen locale
#'
#' @inheritParams fmt_number
#'
#' @param scale_by *Scale values by a fixed multiplier*
#'
#' `scalar<numeric|integer>` // *default:* `1`
#'
#' All numeric values will be multiplied by the `scale_by` value before
#' undergoing formatting. Since the `default` value is `1`, no values will be
#' changed unless a different multiplier value is supplied.
#'
#' @param exp_style *Style declaration for exponent formatting*
#'
#' `scalar<character>` // *default:* `"x10n"`
#'
#' Style of formatting to use for the scientific notation formatting. By
#' default this is `"x10n"` but other options include using a single letter
#' (e.g., `"e"`, `"E"`, etc.), a letter followed by a `"1"` to signal a
#' minimum digit width of one, or `"low-ten"` for using a stylized `"10"`
#' marker.
#'
#' @param force_sign_m,force_sign_n *Forcing the display of a positive sign*
#'
#' `scalar<logical>` // *default:* `FALSE`
#'
#' Should the plus sign be shown for positive values of the mantissa (first
#' component, `force_sign_m`) or the exponent (`force_sign_n`)? This would
#' effectively show a sign for all values except zero on either of those
#' numeric components of the notation. If so, use `TRUE` for either one of
#' these options. The default for both is `FALSE`, where only negative numbers
#' will display a sign.
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_engineering()` is compatible with body cells that are of the
#' `"numeric"` or `"integer"` types. Any other types of body cells are ignored
#' during formatting. This is to say that cells of incompatible data types may
#' be targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_engineering()` to
#' obtain varying parameter values from a specified column within the table.
#' This means that each row could be formatted a little bit differently. These
#' arguments provide support for [from_column()]:
#'
#' - `decimals`
#' - `drop_trailing_zeros`
#' - `drop_trailing_dec_mark`
#' - `scale_by`
#' - `exp_style`
#' - `pattern`
#' - `sep_mark`
#' - `dec_mark`
#' - `force_sign_m`
#' - `force_sign_n`
#' - `locale`
#'
#' Please note that for all of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Adapting output to a specific `locale`:
#'
#' This formatting function can adapt outputs according to a provided `locale`
#' value. Examples include `"en"` for English (United States) and `"fr"` for
#' French (France). The use of a valid locale ID here means separator and
#' decimal marks will be correct for the given locale. Should any values be
#' provided in `sep_mark` or `dec_mark`, they will be overridden by the locale's
#' preferred values.
#'
#' Note that a `locale` value provided here will override any global locale
#' setting performed in [gt()]'s own `locale` argument (it is settable there as
#' a value received by all other functions that have a `locale` argument). As a
#' useful reference on which locales are supported, we can call [info_locales()]
#' to view an info table.
#'
#' @section Examples:
#'
#' Let's define a data frame that contains two columns of values (one `small`
#' and one `large`). After creating a simple **gt** table from `small_large_tbl`
#' we'll call `fmt_engineering()` on both columns.
#'
#' ```r
#' small_large_tbl <-
#' dplyr::tibble(
#' small = 10^(-12:-1),
#' large = 10^(1:12)
#' )
#'
#' small_large_tbl |>
#' gt() |>
#' fmt_engineering()
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_engineering_1.png")`
#' }}
#'
#' Notice that within the form of *m* x 10^*n*, the *n* values move in steps of
#' 3 (away from `0`), and *m* values can have 1-3 digits before the decimal.
#' Further to this, any values where *n* is `0` results in a display of only *m*
#' (the first two values in the `large` column demonstrates this).
#'
#' Engineering notation expresses values so that they are align to certain SI
#' prefixes. Here is a table that compares select SI prefixes and their symbols
#' to decimal and engineering-notation representations of the key numbers.
#'
#' ```r
#' prefixes_tbl <-
#' dplyr::tibble(
#' name = c(
#' "peta", "tera", "giga", "mega", "kilo",
#' NA,
#' "milli", "micro", "nano", "pico", "femto"
#' ),
#' symbol = c(
#' "P", "T", "G", "M", "k",
#' NA,
#' "m", ":micro:", "n", "p", "f"
#' ),
#' decimal = c(10^(seq(15, -15, -3))),
#' engineering = decimal
#' )
#'
#' prefixes_tbl |>
#' gt() |>
#' fmt_number(columns = decimal, n_sigfig = 1) |>
#' fmt_engineering(columns = engineering) |>
#' fmt_units(columns = symbol) |>
#' sub_missing()
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_engineering_2.png")`
#' }}
#'
#' The default method of styling the notation uses the '*m* x 10^*n*'
#' construction but this can be changed to a '*m*E*n*' style via the `exp_style`
#' argument. We can supply any single letter here and optionally affix a `"1"`
#' to indicate there should not be any zero-padding of the *n* value. Two calls
#' of `fmt_engineering()` are used here to show different options for styling
#' in engineering notation.
#'
#' ```r
#' small_large_tbl |>
#' gt() |>
#' fmt_engineering(
#' columns = small,
#' exp_style = "E"
#' ) |>
#' fmt_engineering(
#' columns = large,
#' exp_style = "e1",
#' force_sign_n = TRUE
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_engineering_3.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-4
#'
#' @section Function Introduced:
#' `v0.3.1` (August 9, 2021)
#'
#' @seealso The vector-formatting version of this function:
#' [vec_fmt_engineering()].
#'
#' @export
fmt_engineering <- function(
data,
columns = everything(),
rows = everything(),
decimals = 2,
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = TRUE,
scale_by = 1.0,
exp_style = "x10n",
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
force_sign_m = FALSE,
force_sign_n = FALSE,
locale = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
#
# Begin support for `from_column()` objects passed to compatible arguments
#
# Supports parameters:
#
# - decimals
# - drop_trailing_zeros
# - drop_trailing_dec_mark
# - scale_by
# - exp_style
# - pattern
# - sep_mark
# - dec_mark
# - force_sign_m
# - force_sign_n
# - locale
arg_vals <-
mget(
get_arg_names(
function_name = "fmt_engineering",
all_args_except = c("data", "columns", "rows")
)
)
if (args_have_gt_column_obj(arg_vals = arg_vals)) {
# Resolve the row numbers using `resolve_vars()`
resolved_rows_idx <-
resolve_rows_i(
expr = {{ rows }},
data = data
)
param_tbl <-
generate_param_tbl(
data = data,
arg_vals = arg_vals,
resolved_rows_idx = resolved_rows_idx
)
for (i in seq_len(nrow(param_tbl))) {
p_i <- as.list(param_tbl[i, ])
data <-
fmt_engineering(
data = data,
columns = {{ columns }},
rows = resolved_rows_idx[i],
decimals = p_i$decimals %||% decimals,
drop_trailing_zeros = p_i$drop_trailing_zeros %||% drop_trailing_zeros,
drop_trailing_dec_mark = p_i$drop_trailing_dec_mark %||% drop_trailing_dec_mark,
scale_by = p_i$scale_by %||% scale_by,
exp_style = p_i$exp_style %||% exp_style,
pattern = p_i$pattern %||% pattern,
sep_mark = p_i$sep_mark %||% sep_mark,
dec_mark = p_i$dec_mark %||% dec_mark,
force_sign_m = p_i$force_sign_m %||% force_sign_m,
force_sign_n = p_i$force_sign_n %||% force_sign_n,
locale = p_i$locale %||% locale
)
}
return(data)
}
#
# End support for `from_column()` objects passed to compatible arguments
#
# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
validate_locale(locale = locale)
locale <- normalize_locale(locale = locale)
locale <- resolve_locale(data = data, locale = locale)
# Set default values
suffixing <- FALSE
use_seps <- TRUE
# Use locale-based marks if a locale ID is provided
sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps)
dec_mark <- get_locale_dec_mark(locale, dec_mark)
# Normalize the `suffixing` input to either return a character vector
# of suffix labels, or NULL (the case where `suffixing` is FALSE)
suffix_labels <- normalize_suffixing_inputs(suffixing, scale_by, system = "intl")
valid_class <- c("numeric", "integer")
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
fmt(
data = data,
columns = {{ columns }},
rows = {{ rows }},
compat = valid_class,
fns = num_fmt_factory_multi(
pattern = pattern,
format_fn = function(x, context) {
# Define the marks by context
exp_marks <- context_exp_marks(context = context)
minus_mark <- context_minus_mark(context = context)
# Define the `replace_minus()` function
replace_minus <- function(x) {
gsub("-", minus_mark, x, fixed = TRUE)
}
# Create the `suffix_df` object
suffix_df <-
create_suffix_df(
x,
decimals = decimals,
suffix_labels = suffix_labels,
scale_by = scale_by,
system = "intl"
)
# Determine whether `scale_by` is supplied as a function; if it isn't
# the only other pathway involves getting the vector of values produced
# by the `create_suffix_df()` function
if (rlang::is_function(scale_by)) {
to_scale_by <- scale_by
} else {
to_scale_by <- suffix_df$scale_by
}
# Scale the `x` values using `to_scale_by` (either a function or
# a numeric vector)
x <- scale_x_values(x, suffix_df$scale_by)
zero_x <- x == 0
negative_x <- x < 0
x_str_left <- x_str_right <- x_str <- character(length = length(x))
# Powers in engineering notation always in steps of 3; this
# calculation gets, for every value, the effective power value
power_3 <- floor(log(abs(x), base = 1000)) * 3
# Any zero values will return Inf from the previous calculation
# so we must replace these with a `0`
power_3[is.infinite(power_3)] <- 0L
# The numbers on the LHS must be scaled to correspond to the
# RHS 10^`power_level` values (i.e., `<LHS> x 10^(n * 3)`)
x <- x / 10^(power_3)
# With the scaled values for the LHS, format these according
# to the options set by the user
x_str_left <-
format_num_to_str(
x,
context = context,
decimals = decimals,
n_sigfig = NULL,
sep_mark = sep_mark,
dec_mark = dec_mark,
drop_trailing_zeros = drop_trailing_zeros,
drop_trailing_dec_mark = drop_trailing_dec_mark,
format = "f",
replace_minus_mark = FALSE
)
x_str_left <- replace_minus(x_str_left)
n_part <-
vapply(
power_3,
FUN.VALUE = character(1L),
USE.NAMES = FALSE,
FUN = function(x) {
if (x > 0 && force_sign_n) {
out <- gsub("^", "+", x)
} else {
out <- as.character(x)
}
out
}
)
if (exp_style == "x10n") {
# Generate the RHS of the formatted value (i.e., the `x 10^(n * 3)`)
x_str_right <-
paste0(
exp_marks[1],
replace_minus(n_part),
exp_marks[2]
)
# Replace elements from `x_str_right` where exponent values
# are zero with empty strings
x_str_right[power_3 == 0] <- ""
# Paste the LHS and RHS components to generate the formatted values
x_str <- paste0(x_str_left, x_str_right)
} else {
exp_str <- context_exp_str(exp_style = exp_style, context = context)
if (grepl("^[a-zA-Z]{1}1$", exp_style)) {
n_min_width <- 1
} else {
n_min_width <- 2
}
# `power_3` must be padded to two decimal places
n_part <-
vapply(
power_3,
FUN.VALUE = character(1L),
USE.NAMES = FALSE,
FUN = function(x) {
if (grepl("-", x)) {
x <- gsub("-", "", x)
x <- formatC(as.numeric(x), width = n_min_width, flag = "0")
x <- paste0("-", x)
} else {
x <- formatC(as.numeric(x), width = n_min_width, flag = "0")
}
x
}
)
if (force_sign_n) {
n_part <-
vapply(
seq_along(n_part),
FUN.VALUE = character(1L),
USE.NAMES = FALSE,
FUN = function(i) {
if (power_3[i] >= 0) {
out <- gsub("^", "+", n_part[i])
} else {
out <- n_part[i]
}
out
}
)
}
x_str[!is.infinite(x)] <-
paste0(x_str_left[!is.infinite(x)], exp_str, replace_minus(n_part[!is.infinite(x)]))
x_str[is.infinite(x)] <- as.character(x[is.infinite(x)])
x_str <- replace_minus(x_str)
}
# Force a positive sign on certain values if the option is taken
if (force_sign_m) {
positive_x <- !is.na(x) & x > 0
x_str[positive_x] <- paste_left(x_str[positive_x], x_left = "+")
}
x_str
}
)
)
}
#' Format values to take a predefined symbol
#'
#' @inheritParams fmt_number
#' @inheritParams fmt_currency
#' @return An object of class `gt_tbl`.
#' @noRd
fmt_symbol <- function(
data,
columns,
rows,
symbol = "*",
accounting = FALSE,
decimals = NULL,
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = TRUE,
use_seps = TRUE,
scale_by = 1.0,
suffixing = FALSE,
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
force_sign = FALSE,
placement = "left",
incl_space = FALSE,
system = c("intl", "ind"),
locale = NULL
) {
# Ensure that arguments are matched
system <- rlang::arg_match(system)
# Use locale-based marks if a locale ID is provided
sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps)
dec_mark <- get_locale_dec_mark(locale, dec_mark)
# Normalize the `suffixing` input to either return a character vector
# of suffix labels, or NULL (the case where `suffixing` is FALSE)
suffix_labels <- normalize_suffixing_inputs(suffixing, scale_by, system = system)
# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
fmt(
data = data,
columns = {{ columns }},
rows = {{ rows }},
compat = c("numeric", "integer"),
fns = num_fmt_factory_multi(
pattern = pattern,
format_fn = function(x, context) {
# Create the `x_str` vector
x_str <- character(length(x))
# Create the `suffix_df` object
suffix_df <-
create_suffix_df(
x,
decimals = decimals,
suffix_labels = suffix_labels,
scale_by = scale_by,
system = system
)
# Determine whether `scale_by` is supplied as a function; if it isn't
# the only other pathway involves getting the vector of values produced
# by the `create_suffix_df()` function
if (rlang::is_function(scale_by)) {
to_scale_by <- scale_by
} else {
to_scale_by <- suffix_df$scale_by
}
# Scale the `x` values using `to_scale_by` (either a function or
# a numeric vector)
x <- scale_x_values(x, suffix_df$scale_by)
is_negative_x <- x < 0
is_not_negative_x <- !is_negative_x
if (any(is_not_negative_x)) {
# Format numeric values to character-based numbers
x_str[is_not_negative_x] <-
format_num_to_str_c(
x[is_not_negative_x],
context = context,
decimals = decimals,
n_sigfig = NULL,
sep_mark = sep_mark,
dec_mark = dec_mark,
drop_trailing_zeros = drop_trailing_zeros,
drop_trailing_dec_mark = drop_trailing_dec_mark,
system = system
)
}
x_abs_str <- x_str
if (any(is_negative_x)) {
# Format numeric values to character-based numbers
x_abs_str[is_negative_x] <-
format_num_to_str_c(
abs(x[is_negative_x]),
context = context,
decimals = decimals,
n_sigfig = NULL,
sep_mark = sep_mark,
dec_mark = dec_mark,
drop_trailing_zeros = drop_trailing_zeros,
drop_trailing_dec_mark = drop_trailing_dec_mark,
system = system
)
}
# If we supply a per mille or per myriad keyword as
# `symbol` (possible inputs in `fmt_partsper()`),
# get the contextually correct mark
if (is.character(symbol)) {
if (symbol == "per-mille") {
symbol <- I(context_permille_mark(context = context))
} else if (symbol == "per-myriad") {
symbol <- I(context_permyriad_mark(context = context))
}
}
# Format values with a symbol string
x_str <-
format_symbol_str(
x_abs_str = x_abs_str,
x = x,
context = context,
symbol = symbol,
incl_space = incl_space,
placement = placement
)
# Format values in accounting notation (if `accounting = TRUE`)
x_str <-
format_as_accounting(
x_str,
x = x,
context = context,
accounting = accounting
)
# Paste the vector of suffixes to the right of the values
x_str <- paste_right(x_str, x_right = suffix_df$suffix)
# Force a positive sign on certain values if the option is taken
if (!accounting && force_sign) {
positive_x <- !is.na(x) & x > 0
x_str[positive_x] <- paste_left(x_str[positive_x], x_left = "+")
}
x_str
}
)
)
}
#' Format values as a percentage
#'
#' @description
#'
#' With numeric values in a **gt** table, we can perform percentage-based
#' formatting. It is assumed the input numeric values are proportional values
#' and, in this case, the values will be automatically multiplied by `100`
#' before decorating with a percent sign (the other case is accommodated through
#' setting `scale_values = FALSE`). For more control over percentage
#' formatting, we can use the following options:
#'
#' - percent sign placement: the percent sign can be placed after or
#' before the values and a space can be inserted between the symbol and the
#' value.
#' - decimals: choice of the number of decimal places, option to drop
#' trailing zeros, and a choice of the decimal symbol
#' - digit grouping separators: options to enable/disable digit separators
#' and provide a choice of separator symbol
#' - value scaling toggle: choose to disable automatic value scaling in the
#' situation that values are already scaled coming in (and just require the
#' percent symbol)
#' - pattern: option to use a text pattern for decoration of the formatted
#' values
#' - locale-based formatting: providing a locale ID will result in number
#' formatting specific to the chosen locale
#'
#' @inheritParams fmt_number
#'
#' @param scale_values *Multiply input values by 100*
#'
#' `scalar<logical>` // *default:* `TRUE`
#'
#' Should the values be scaled through multiplication by 100? By default this
#' scaling is performed since the expectation is that incoming values are
#' usually proportional. Setting to `FALSE` signifies that the values are
#' already scaled and require only the percent sign when formatted.
#'
#' @param placement *Percent sign placement*
#'
#' `singl-kw:[right|left]` // *default:* `"right"`
#'
#' This option governs the placement of the percent sign. This can be either
#' be `"right"` (the default) or `"left"`.
#'
#' @param incl_space *Include a space between the value and the % sign*
#'
#' `scalar<logical>` // *default:* `FALSE`
#'
#' An option for whether to include a space between the value and the percent
#' sign. The default is to not introduce a space character.
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_percent()` is compatible with body cells that are of the `"numeric"` or
#' `"integer"` types. Any other types of body cells are ignored during
#' formatting. This is to say that cells of incompatible data types may be
#' targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_percent()` to
#' obtain varying parameter values from a specified column within the table.
#' This means that each row could be formatted a little bit differently. These
#' arguments provide support for [from_column()]:
#'
#' - `decimals`
#' - `drop_trailing_zeros`
#' - `drop_trailing_dec_mark`
#' - `scale_values`
#' - `use_seps`
#' - `accounting`
#' - `pattern`
#' - `sep_mark`
#' - `dec_mark`
#' - `force_sign`
#' - `incl_space`
#' - `placement`
#' - `system`
#' - `locale`
#'
#' Please note that for all of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Adapting output to a specific `locale`:
#'
#' This formatting function can adapt outputs according to a provided `locale`
#' value. Examples include `"en"` for English (United States) and `"fr"` for
#' French (France). The use of a valid locale ID here means separator and
#' decimal marks will be correct for the given locale. Should any values be
#' provided in `sep_mark` or `dec_mark`, they will be overridden by the locale's
#' preferred values.
#'
#' Note that a `locale` value provided here will override any global locale
#' setting performed in [gt()]'s own `locale` argument (it is settable there as
#' a value received by all other functions that have a `locale` argument). As a
#' useful reference on which locales are supported, we can call[info_locales()]
#' to view an info table.
#'
#' @section Examples:
#'
#' Use a summarized version of the [`pizzaplace`] dataset to create a **gt**
#' table. With `fmt_percent()`, we can format the `frac_of_quota` column to
#' display values as percentages (to one decimal place).
#'
#' ```r
#' pizzaplace |>
#' dplyr::mutate(month = as.numeric(substr(date, 6, 7))) |>
#' dplyr::group_by(month) |>
#' dplyr::summarize(pizzas_sold = dplyr::n()) |>
#' dplyr::ungroup() |>
#' dplyr::mutate(frac_of_quota = pizzas_sold / 4000) |>
#' gt(rowname_col = "month") |>
#' fmt_percent(
#' columns = frac_of_quota,
#' decimals = 1
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_percent_1.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-5
#'
#' @section Function Introduced:
#' `v0.2.0.5` (March 31, 2020)
#'
#' @seealso The vector-formatting version of this function: [vec_fmt_percent()].
#'
#' @export
fmt_percent <- function(
data,
columns = everything(),
rows = everything(),
decimals = 2,
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = TRUE,
scale_values = TRUE,
use_seps = TRUE,
accounting = FALSE,
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
force_sign = FALSE,
placement = "right",
incl_space = FALSE,
system = c("intl", "ind"),
locale = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
#
# Begin support for `from_column()` objects passed to compatible arguments
#
# Supports parameters:
#
# - decimals
# - drop_trailing_zeros
# - drop_trailing_dec_mark
# - scale_values
# - use_seps
# - accounting
# - pattern
# - sep_mark
# - dec_mark
# - force_sign
# - incl_space
# - placement
# - system
# - locale
arg_vals <-
mget(
get_arg_names(
function_name = "fmt_percent",
all_args_except = c("data", "columns", "rows")
)
)
if (args_have_gt_column_obj(arg_vals = arg_vals)) {
# Resolve the row numbers using `resolve_vars()`
resolved_rows_idx <-
resolve_rows_i(
expr = {{ rows }},
data = data
)
param_tbl <-
generate_param_tbl(
data = data,
arg_vals = arg_vals,
resolved_rows_idx = resolved_rows_idx
)
for (i in seq_len(nrow(param_tbl))) {
p_i <- as.list(param_tbl[i, ])
data <-
fmt_percent(
data = data,
columns = {{ columns }},
rows = resolved_rows_idx[i],
decimals = p_i$decimals %||% decimals,
drop_trailing_zeros = p_i$drop_trailing_zeros %||% drop_trailing_zeros,
drop_trailing_dec_mark = p_i$drop_trailing_dec_mark %||% drop_trailing_dec_mark,
scale_values = p_i$scale_values %||% scale_values,
use_seps = p_i$use_seps %||% use_seps,
accounting = p_i$accounting %||% accounting,
pattern = p_i$pattern %||% pattern,
sep_mark = p_i$sep_mark %||% sep_mark,
dec_mark = p_i$dec_mark %||% dec_mark,
force_sign = p_i$force_sign %||% force_sign,
incl_space = p_i$incl_space %||% incl_space,
placement = p_i$placement %||% placement,
system = p_i$system %||% system,
locale = p_i$locale %||% locale
)
}
return(data)
}
#
# End support for `from_column()` objects passed to compatible arguments
#
# Ensure that arguments are matched
system <- rlang::arg_match(system)
# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
validate_locale(locale = locale)
locale <- normalize_locale(locale = locale)
locale <- resolve_locale(data = data, locale = locale)
valid_class <- c("numeric", "integer")
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
if (scale_values) {
scale_by <- 100
} else {
scale_by <- 1.0
}
# Pass `data`, `columns`, `rows`, and other options to `fmt_symbol()`
fmt_symbol(
data = data,
columns = {{ columns }},
rows = {{ rows }},
symbol = "%",
accounting = accounting,
decimals = decimals,
drop_trailing_zeros = drop_trailing_zeros,
drop_trailing_dec_mark = drop_trailing_dec_mark,
use_seps = use_seps,
scale_by = scale_by,
suffixing = FALSE,
pattern = pattern,
sep_mark = sep_mark,
dec_mark = dec_mark,
force_sign = force_sign,
placement = placement,
incl_space = incl_space,
system = system,
locale = locale
)
}
#' Format values as parts-per quantities
#'
#' @description
#'
#' With numeric values in a **gt** table we can format the values so that they
#' are rendered as *per mille*, *ppm*, *ppb*, etc., quantities. The following
#' list of keywords (with associated naming and scaling factors) is available to
#' use within `fmt_partsper()`:
#'
#' - `"per-mille"`: Per mille, (1 part in `1,000`)
#' - `"per-myriad"`: Per myriad, (1 part in `10,000`)
#' - `"pcm"`: Per cent mille (1 part in `100,000`)
#' - `"ppm"`: Parts per million, (1 part in `1,000,000`)
#' - `"ppb"`: Parts per billion, (1 part in `1,000,000,000`)
#' - `"ppt"`: Parts per trillion, (1 part in `1,000,000,000,000`)
#' - `"ppq"`: Parts per quadrillion, (1 part in `1,000,000,000,000,000`)
#'
#' The function provides a lot of formatting control and we can use the
#' following options:
#'
#' - custom symbol/units: we can override the automatic symbol or units display
#' with our own choice as the situation warrants
#' - decimals: choice of the number of decimal places, option to drop
#' trailing zeros, and a choice of the decimal symbol
#' - digit grouping separators: options to enable/disable digit separators
#' and provide a choice of separator symbol
#' - value scaling toggle: choose to disable automatic value scaling in the
#' situation that values are already scaled coming in (and just require the
#' appropriate symbol or unit display)
#' - pattern: option to use a text pattern for decoration of the formatted
#' values
#' - locale-based formatting: providing a locale ID will result in number
#' formatting specific to the chosen locale
#'
#' @inheritParams fmt_number
#'
#' @param to_units *Output Quantity*
#'
#' `singl-kw:[per-mille|per-myriad|pcm|ppm|ppb|ppt|ppq]` // *default:* `"per-mille"`
#'
#' A keyword that signifies the desired output quantity. This can be any from
#' the following set: `"per-mille"`, `"per-myriad"`, `"pcm"`, `"ppm"`,
#' `"ppb"`, `"ppt"`, or `"ppq"`.
#'
#' @param symbol *Symbol or units to use in output display*
#'
#' `scalar<character>` // *default:* `"auto"`
#'
#' The symbol/units to use for the quantity. By default, this is set to
#' `"auto"` and **gt** will choose the appropriate symbol based on the
#' `to_units` keyword and the output context. However, this can be changed by
#' supplying a string (e.g, using `symbol = "ppbV"` when `to_units = "ppb"`).
#'
#' @param scale_values *Scale input values accordingly*
#'
#' `scalar<logical>` // *default:* `TRUE`
#'
#' Should the values be scaled through multiplication according to the keyword
#' set in `to_units`? By default this is `TRUE` since the expectation is that
#' normally values are proportions. Setting to `FALSE` signifies that the
#' values are already scaled and require only the appropriate symbol/units
#' when formatted.
#'
#' @param incl_space *Include a space between the value and the symbol/units*
#'
#' `scalar<character>|scalar<logical>` // *default:* `"auto"`
#'
#' An option for whether to include a space between the value and the
#' symbol/units. The default is `"auto"` which provides spacing dependent on
#' the mark itself. This can be directly controlled by using either `TRUE` or
#' `FALSE`.
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_partsper()` is compatible with body cells that are of the `"numeric"` or
#' `"integer"` types. Any other types of body cells are ignored during
#' formatting. This is to say that cells of incompatible data types may be
#' targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_partsper()` to
#' obtain varying parameter values from a specified column within the table.
#' This means that each row could be formatted a little bit differently. These
#' arguments provide support for [from_column()]:
#'
#' - `to_units`
#' - `symbol`
#' - `decimals`
#' - `drop_trailing_zeros`
#' - `drop_trailing_dec_mark`
#' - `scale_values`
#' - `use_seps`
#' - `pattern`
#' - `sep_mark`
#' - `dec_mark`
#' - `force_sign`
#' - `incl_space`
#' - `system`
#' - `locale`
#'
#' Please note that for all of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Adapting output to a specific `locale`:
#'
#' This formatting function can adapt outputs according to a provided `locale`
#' value. Examples include `"en"` for English (United States) and `"fr"` for
#' French (France). The use of a valid locale ID here means separator and
#' decimal marks will be correct for the given locale. Should any values be
#' provided in `sep_mark` or `dec_mark`, they will be overridden by the locale's
#' preferred values.
#'
#' Note that a `locale` value provided here will override any global locale
#' setting performed in [gt()]'s own `locale` argument (it is settable there as
#' a value received by all other functions that have a `locale` argument). As a
#' useful reference on which locales are supported, we can call [info_locales()]
#' to view an info table.
#'
#' @section Examples:
#'
#' Create a tibble of small numeric values and generate a **gt** table. Format
#' the `a` column to appear in scientific notation with [fmt_scientific()] and
#' format the `b` column as *per mille* values with `fmt_partsper()`.
#'
#' ```r
#' dplyr::tibble(x = 0:-5, a = 10^(0:-5), b = a) |>
#' gt(rowname_col = "x") |>
#' fmt_scientific(a, decimals = 0) |>
#' fmt_partsper(
#' columns = b,
#' to_units = "per-mille"
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_partsper_1.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-6
#'
#' @section Function Introduced:
#' `v0.6.0` (May 24, 2022)
#'
#' @seealso The vector-formatting version of this function:
#' [vec_fmt_partsper()].
#'
#' @export
fmt_partsper <- function(
data,
columns = everything(),
rows = everything(),
to_units = c("per-mille", "per-myriad", "pcm", "ppm", "ppb", "ppt", "ppq"),
symbol = "auto",
decimals = 2,
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = TRUE,
scale_values = TRUE,
use_seps = TRUE,
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
force_sign = FALSE,
incl_space = "auto",
system = c("intl", "ind"),
locale = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
#
# Begin support for `from_column()` objects passed to compatible arguments
#
# Supports parameters:
#
# - to_units
# - symbol
# - decimals
# - drop_trailing_zeros
# - drop_trailing_dec_mark
# - scale_values
# - use_seps
# - pattern
# - sep_mark
# - dec_mark
# - force_sign
# - incl_space
# - system
# - locale
arg_vals <-
mget(
get_arg_names(
function_name = "fmt_partsper",
all_args_except = c("data", "columns", "rows")
)
)
if (args_have_gt_column_obj(arg_vals = arg_vals)) {
# Resolve the row numbers using `resolve_vars()`
resolved_rows_idx <-
resolve_rows_i(
expr = {{ rows }},
data = data
)
param_tbl <-
generate_param_tbl(
data = data,
arg_vals = arg_vals,
resolved_rows_idx = resolved_rows_idx
)
for (i in seq_len(nrow(param_tbl))) {
p_i <- as.list(param_tbl[i, ])
data <-
fmt_partsper(
data = data,
columns = {{ columns }},
rows = resolved_rows_idx[i],
to_units = p_i$to_units %||% to_units,
symbol = p_i$symbol %||% symbol,
decimals = p_i$decimals %||% decimals,
drop_trailing_zeros = p_i$drop_trailing_zeros %||% drop_trailing_zeros,
drop_trailing_dec_mark = p_i$drop_trailing_dec_mark %||% drop_trailing_dec_mark,
scale_values = p_i$scale_values %||% scale_values,
use_seps = p_i$use_seps %||% use_seps,
pattern = p_i$pattern %||% pattern,
sep_mark = p_i$sep_mark %||% sep_mark,
dec_mark = p_i$dec_mark %||% dec_mark,
force_sign = p_i$force_sign %||% force_sign,
incl_space = p_i$incl_space %||% incl_space,
system = p_i$system %||% system,
locale = p_i$locale %||% locale
)
}
return(data)
}
#
# End support for `from_column()` objects passed to compatible arguments
#
# Ensure that arguments are matched
to_units <- rlang::arg_match(to_units)
system <- rlang::arg_match(system)
# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
validate_locale(locale = locale)
locale <- normalize_locale(locale = locale)
locale <- resolve_locale(data = data, locale = locale)
valid_class <- c("numeric", "integer")
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
# Scale values according to `to_units` value
if (scale_values) {
scale_by <-
switch(
to_units,
`per-mille` = 1E3,
`per-myriad` = 1E4,
pcm = 1E5,
ppm = 1E6,
ppb = 1E9,
ppt = 1E12,
ppq = 1E15,
)
} else {
scale_by <- 1.0
}
if (symbol == "auto") {
symbol <-
switch(
to_units,
`per-mille` = "per-mille",
`per-myriad` = "per-myriad",
pcm = "pcm",
ppm = "ppm",
ppb = "ppb",
ppt = "ppt",
ppq = "ppq"
)
}
if (incl_space == "auto") {
incl_space <-
switch(
to_units,
`per-mille` = ,
`per-myriad` = FALSE,
pcm = ,
ppm = ,
ppb = ,
ppt = ,
ppq = TRUE
)
}
# Pass `data`, `columns`, `rows`, and other options to `fmt_symbol()`
fmt_symbol(
data = data,
columns = {{ columns }},
rows = {{ rows }},
symbol = symbol,
accounting = FALSE,
decimals = decimals,
drop_trailing_zeros = drop_trailing_zeros,
drop_trailing_dec_mark = drop_trailing_dec_mark,
use_seps = use_seps,
scale_by = scale_by,
suffixing = FALSE,
pattern = pattern,
sep_mark = sep_mark,
dec_mark = dec_mark,
force_sign = force_sign,
placement = "right",
incl_space = incl_space,
system = system,
locale = locale
)
}
#' Format values as mixed fractions
#'
#' @description
#'
#' With numeric values in a **gt** table, we can perform mixed-fraction-based
#' formatting. There are several options for setting the accuracy of the
#' fractions. Furthermore, there is an option for choosing a layout (i.e.,
#' typesetting style) for the mixed-fraction output.
#'
#' The following options are available for controlling this type of formatting:
#'
#' - accuracy: how to express the fractional part of the mixed fractions; there
#' are three keyword options for this and an allowance for arbitrary denominator
#' settings
#' - simplification: an option to simplify fractions whenever possible
#' - layout: We can choose to output values with diagonal or inline fractions
#' - digit grouping separators: options to enable/disable digit separators
#' and provide a choice of separator symbol for the whole number portion
#' - pattern: option to use a text pattern for decoration of the formatted
#' mixed fractions
#' - locale-based formatting: providing a locale ID will result in number
#' formatting specific to the chosen locale
#'
#' @inheritParams fmt_number
#'
#' @param accuracy *Accuracy of fractions*
#'
#' `singl-kw:[low|med|high]|scalar<numeric|integer>(val>=1)` // *default:* `"low"`
#'
#' The type of fractions to generate. This can either be one of the keywords
#' `"low"`, `"med"`, or `"high"` (to generate fractions with denominators of
#' up to 1, 2, or 3 digits, respectively) or an integer value greater than
#' zero to obtain fractions with a fixed denominator (`2` yields halves, `3`
#' is for thirds, `4` is quarters, etc.). For the latter option, using
#' `simplify = TRUE` will simplify fractions where possible (e.g., `2/4` will
#' be simplified as `1/2`). By default, the `"low"` option is used.
#'
#' @param simplify *Simplify the fraction*
#'
#' `scalar<logical>` // *default:* `TRUE`
#'
#' If choosing to provide a numeric value for `accuracy`, the option to
#' simplify the fraction (where possible) can be taken with `TRUE` (the
#' default). With `FALSE`, denominators in fractions will be fixed to the
#' value provided in `accuracy`.
#'
#' @param layout *Layout of fractions in HTML output*
#'
#' `singl-kw:[inline|diagonal]` // *default:* `"inline"`
#'
#' For HTML output, the `"inline"` layout is the default. This layout places
#' the numerals of the fraction on the baseline and uses a standard slash
#' character. The `"diagonal"` layout will generate fractions that are typeset
#' with raised/lowered numerals and a virgule.
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_fraction()` is compatible with body cells that are of the `"numeric"` or
#' `"integer"` types. Any other types of body cells are ignored during
#' formatting. This is to say that cells of incompatible data types may be
#' targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_fraction()` to
#' obtain varying parameter values from a specified column within the table.
#' This means that each row could be formatted a little bit ifferently. These
#' arguments provide support for [from_column()]:
#'
#' - `accuracy`
#' - `simplify`
#' - `layout`
#' - `use_seps`
#' - `pattern`
#' - `sep_mark`
#' - `system`
#' - `locale`
#'
#' Please note that for all of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Adapting output to a specific `locale`:
#'
#' This formatting function can adapt outputs according to a provided `locale`
#' value. Examples include `"en"` for English (United States) and `"fr"` for
#' French (France). The use of a valid locale ID here means separator and
#' decimal marks will be correct for the given locale. Should any value be
#' provided in `sep_mark`, it will be overridden by the locale's preferred
#' values.
#'
#' Note that a `locale` value provided here will override any global locale
#' setting performed in [gt()]'s own `locale` argument (it is settable there as
#' a value received by all other functions that have a `locale` argument). As a
#' useful reference on which locales are supported, we can call [info_locales()]
#' to view an info table.
#'
#' @section Examples:
#'
#' Using a summarized version of the [`pizzaplace`] dataset, let's create a
#' **gt** table. With `fmt_fraction()` we can format the `f_sold` and `f_income`
#' columns to display fractions. As for how the fractions are represented, we
#' are electing to use `accuracy = 10`. This gives all fractions as tenths.
#' We won't simplify the fractions (by using `simplify = FALSE`) and this means
#' that a fraction like `5/10` won't become `1/2`. With `layout ="diagonal"`,
#' we get a diagonal display of all fractions.
#'
#' ```r
#' pizzaplace |>
#' dplyr::group_by(type, size) |>
#' dplyr::summarize(
#' sold = dplyr::n(),
#' income = sum(price),
#' .groups = "drop_last"
#' ) |>
#' dplyr::group_by(type) |>
#' dplyr::mutate(
#' f_sold = sold / sum(sold),
#' f_income = income / sum(income),
#' ) |>
#' dplyr::arrange(type, dplyr::desc(income)) |>
#' gt(rowname_col = "size") |>
#' tab_header(
#' title = "Pizzas Sold in 2015",
#' subtitle = "Fraction of Sell Count and Revenue by Size per Type"
#' ) |>
#' fmt_integer(columns = sold) |>
#' fmt_currency(columns = income) |>
#' fmt_fraction(
#' columns = starts_with("f_"),
#' accuracy = 10,
#' simplify = FALSE,
#' layout = "diagonal"
#' ) |>
#' sub_missing(missing_text = "") |>
#' tab_spanner(
#' label = "Sold",
#' columns = contains("sold")
#' ) |>
#' tab_spanner(
#' label = "Revenue",
#' columns = contains("income")
#' ) |>
#' text_transform(
#' locations = cells_body(),
#' fn = function(x) {
#' dplyr::case_when(
#' x == 0 ~ "<em>nil</em>",
#' x != 0 ~ x
#' )
#' }
#' ) |>
#' cols_label(
#' sold = "Amount",
#' income = "Amount",
#' f_sold = md("_f_"),
#' f_income = md("_f_")
#' ) |>
#' cols_align(align = "center", columns = starts_with("f")) |>
#' tab_options(
#' table.width = px(400),
#' row_group.as_column = TRUE
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_fraction_1.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-7
#'
#' @section Function Introduced:
#' `v0.4.0` (February 15, 2022)
#'
#' @seealso The vector-formatting version of this function:
#' [vec_fmt_fraction()].
#'
#' @export
fmt_fraction <- function(
data,
columns = everything(),
rows = everything(),
accuracy = NULL,
simplify = TRUE,
layout = c("inline", "diagonal"),
use_seps = TRUE,
pattern = "{x}",
sep_mark = ",",
system = c("intl", "ind"),
locale = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
#
# Begin support for `from_column()` objects passed to compatible arguments
#
# Supports parameters:
#
# - accuracy
# - simplify
# - layout
# - use_seps
# - pattern
# - sep_mark
# - system
# - locale
arg_vals <-
mget(
get_arg_names(
function_name = "fmt_fraction",
all_args_except = c("data", "columns", "rows")
)
)
if (args_have_gt_column_obj(arg_vals = arg_vals)) {
# Resolve the row numbers using `resolve_vars()`
resolved_rows_idx <-
resolve_rows_i(
expr = {{ rows }},
data = data
)
param_tbl <-
generate_param_tbl(
data = data,
arg_vals = arg_vals,
resolved_rows_idx = resolved_rows_idx
)
for (i in seq_len(nrow(param_tbl))) {
p_i <- as.list(param_tbl[i, ])
data <-
fmt_fraction(
data = data,
columns = {{ columns }},
rows = resolved_rows_idx[i],
accuracy = p_i$accuracy %||% accuracy,
simplify = p_i$simplify %||% simplify,
layout = p_i$layout %||% layout,
use_seps = p_i$use_seps %||% use_seps,
pattern = p_i$pattern %||% pattern,
sep_mark = p_i$sep_mark %||% sep_mark,
system = p_i$system %||% system,
locale = p_i$locale %||% locale
)
}
return(data)
}
#
# End support for `from_column()` objects passed to compatible arguments
#
# Ensure that arguments are matched
system <- rlang::arg_match(system)
layout <- rlang::arg_match(layout)
# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
validate_locale(locale = locale)
locale <- normalize_locale(locale = locale)
locale <- resolve_locale(data = data, locale = locale)
# Use "low" as default for accuracy
accuracy <- accuracy %||% "low"
if (is.character(accuracy)) {
rlang::arg_match0(accuracy, c("low", "med", "high"))
} else if (is.numeric(accuracy)) {
check_number_whole(accuracy, min = 1, allow_infinite = FALSE)
} else {
cli::cli_abort(c(
"The input for `accuracy` is invalid.",
"*" = "Must be a keyword \"low\", \"med\", or \"high\", or",
"*" = "Must be an integer value greater than zero."
))
}
valid_class <- c("numeric", "integer")
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
# Use locale-based `sep_mark` if a locale ID is provided
sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps)
# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
fmt(
data = data,
columns = {{ columns }},
rows = {{ rows }},
compat = valid_class,
fns = num_fmt_factory_multi(
pattern = pattern,
format_fn = function(x, context) {
# Get the correct minus mark based on the output context
minus_mark <- context_minus_mark(context = context)
# Generate an vector of empty strings that will eventually contain
# all of the fractional parts of the finalized numbers
fraction_x <- rep_len("", length(x))
# Round all values of x to 3 digits with the R-H-U method of
# rounding (for reproducibility purposes)
x <- round_gt(x, 3)
# Determine which of `x` are finite values
x_is_a_number <- is.finite(x)
# Divide the `x` values in 'big' and 'small' components; delay the
# formatting of `big_x` until it is appropriately rounded on the
# basis of the fractions obtained at the desired accuracy
big_x <- trunc(x)
small_x <- abs(x - big_x)
if (is.numeric(accuracy)) {
fraction_x[x_is_a_number] <-
make_frac(
x[x_is_a_number],
denom = accuracy,
simplify = simplify
)
} else {
# Format the 'small' portion of the numeric values
# to character-based numbers with exactly 3 decimal places
small_x_str <- as.character(small_x)
small_x_str[x_is_a_number] <-
format_num_to_str(
small_x[x_is_a_number],
context = context, decimals = 3, n_sigfig = NULL,
sep_mark = ",", dec_mark = ".",
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = TRUE,
format = "f"
)
# For every `small_x` value that corresponds to a number
# (i.e., not Inf), get the fractional part from the `fractions`
# lookup table
fraction_x[x_is_a_number] <-
fractions[(as.numeric(small_x_str[x_is_a_number]) * 1000) + 1, accuracy, drop = TRUE]
}
# Round up or down the `big_x` values when necessary; values
# of exactly "1" indicate a requirement for rounding and this
# is a two-pass operation to handle positive and then negative
# values of `big_x`
big_x[big_x >= 0 & fraction_x == "1"] <-
big_x[big_x >= 0 & fraction_x == "1"] + 1
big_x[big_x <= 0 & fraction_x == "1"] <-
big_x[big_x <= 0 & fraction_x == "1"] - 1
# Remove whole number values from `fraction_x`; they were only
# needed for rounding guidance and they signal the lack of a
# fractional part
fraction_x[fraction_x %in% c("0", "1")] <- ""
# Format the 'big' portion of the numeric values
# to character-based numbers
big_x <-
format_num_to_str(
big_x,
context = context,
decimals = 0,
n_sigfig = NULL,
sep_mark = sep_mark,
dec_mark = "",
drop_trailing_zeros = TRUE,
drop_trailing_dec_mark = TRUE,
format = "f",
system = system
)
# Initialize a vector that will contain the finalized strings
x_str <- character(length(x))
# Generate the mixed fractions by pasting `big_x` and `small_x`
# while ensuring there is a single space between these components
x_str[x_is_a_number] <-
paste(
big_x[x_is_a_number],
fraction_x[x_is_a_number],
sep = " "
)
# Trim any whitespace
x_str <- gsub("(^ | $)", "", x_str)
# Eliminate the display of leading zeros in mixed fractions
x_str <- gsub("^0\\s+?", "", x_str)
# There are situations where small fractions (not mixed) require
# a minus mark; these conditions are specific so we need to ascertain
# which values in `x_str` require this and then apply the mark to
# the targets
x_is_negative <- x < 0
x_is_zero <- x_str == "0"
x_has_minus_mark <- grepl(minus_mark, big_x)
x_needs_minus_mark <- x_is_negative & !x_is_zero & !x_has_minus_mark
x_str[x_needs_minus_mark] <- paste0(minus_mark, x_str[x_needs_minus_mark])
# Generate diagonal fractions if the `layout = "diagonal"` option was chosen
if (layout == "diagonal") {
has_a_fraction <- grepl("/", x_str)
non_fraction_part <- gsub("^(.*?)[0-9]*/[0-9]*", "\\1", x_str[has_a_fraction])
fraction_part <- gsub("^(.*?)([0-9]*/[0-9]*)", "\\2", x_str[has_a_fraction])
num_vec <- unlist(lapply(strsplit(fraction_part, "/"), `[[`, 1))
denom_vec <- unlist(lapply(strsplit(fraction_part, "/"), `[[`, 2))
if (context == "html") {
narrow_no_break_space_char <- "\U0202F"
slash_mark_char <- "\U02044"
num_vec <-
paste0(
"<span style=\"",
"font-size:0.6em;",
"line-height:0.6em;",
"vertical-align:0.45em;",
"\">",
num_vec,
"</span>"
)
denom_vec <-
paste0(
"<span style=\"",
"font-size:0.6em;",
"line-height:0.6em;",
"vertical-align:-0.05em;",
"\">",
denom_vec,
"</span>"
)
slash_mark <-
paste0(
"<span style=\"",
"font-size:0.7em;",
"line-height:0.7em;",
"vertical-align:0.15em;",
"\">",
slash_mark_char,
"</span>"
)
x_str[has_a_fraction] <-
paste0(
gsub(" ", narrow_no_break_space_char, non_fraction_part),
num_vec, slash_mark, denom_vec
)
} else if (context == "latex") {
x_str[has_a_fraction] <-
paste0(
gsub(" ", "", non_fraction_part),
paste0("\\textsuperscript{", num_vec, "}\\!/\\textsubscript{", denom_vec, "}")
)
} else if (context == "rtf") {
x_str[has_a_fraction] <-
paste0(
gsub(" ", "", non_fraction_part),
paste0("{\\super ", num_vec, "}/{\\sub ", denom_vec, "}")
)
}
}
# For the `layout = "inline"` option, LaTeX outputs in math mode
# disregard space characters so the `\ ` spacing command must used
if (layout == "inline" && context == "latex") {
x_str <- gsub(" ", "\\\\ ", x_str)
}
# In rare cases that Inf or -Inf appear, ensure that these
# special values are printed correctly
x_str[is.infinite(x)] <- x[is.infinite(x)]
x_str
}
)
)
}
gcd <- function(x,y) {
r <- x %% y
return(ifelse(r, gcd(y, r), y))
}
make_frac <- function(x, denom, simplify = TRUE) {
big_x <- trunc(x)
small_x <- abs(x - big_x)
numer <- round_gt(small_x * denom)
if (simplify) {
denom <- rep_len(denom, length(x))
factor <- gcd(numer, denom)
numer <- numer / factor
denom <- denom / factor
}
ifelse(
numer == denom, "1",
ifelse(
numer == 0, "0",
paste0(
format_num_to_str(
numer,
context = "plain",
decimals = 0,
n_sigfig = NULL,
sep_mark = "",
dec_mark = ".",
drop_trailing_zeros = TRUE,
drop_trailing_dec_mark = TRUE,
format = "f"
),
"/",
format_num_to_str(
denom,
context = "plain",
decimals = 0,
n_sigfig = NULL,
sep_mark = "",
dec_mark = ".",
drop_trailing_zeros = TRUE,
drop_trailing_dec_mark = TRUE,
format = "f"
)
)
)
)
}
# The `round_gt()` function is used in gt over `base::round()` for consistency
# in rounding across R versions; it uses the 'Round-Half-Up' (R-H-U) algorithm,
# which is *not* used in R >= 4.0
round_gt <- function(x, digits = 0) {
x_sign <- sign(x)
z <- abs(x) * 10^digits
z <- 0.5 + z + sqrt(.Machine$double.eps)
z <- trunc(z)
z <- z / 10^digits
z * x_sign
}
#' Format values as currencies
#'
#' @description
#'
#' With numeric values in a **gt** table, we can perform currency-based
#' formatting with `fmt_currency()`. The function supports both
#' automatic formatting with either a three-letter or a numeric currency code.
#' We can also specify a custom currency that is formatted according to one or
#' more output contexts with the [currency()] helper function. We have fine
#' control over the conversion from numeric values to currency values, where we
#' could take advantage of the following options:
#'
#' - the currency: providing a currency code or common currency name will
#' procure the correct currency symbol and number of currency subunits; we could
#' also use the [currency()] helper function to specify a custom currency
#' - currency symbol placement: the currency symbol can be placed before
#' or after the values
#' - decimals/subunits: choice of the number of decimal places, and a
#' choice of the decimal symbol, and an option on whether to include or exclude
#' the currency subunits (the decimal portion)
#' - negative values: choice of a negative sign or parentheses for values
#' less than zero
#' - digit grouping separators: options to enable/disable digit separators
#' and provide a choice of separator symbol
#' - scaling: we can choose to scale targeted values by a multiplier value
#' - large-number suffixing: larger figures (thousands, millions, etc.) can
#' be autoscaled and decorated with the appropriate suffixes
#' - pattern: option to use a text pattern for decoration of the formatted
#' currency values
#' - locale-based formatting: providing a locale ID will result in currency
#' formatting specific to the chosen locale; it will also retrieve the locale's
#' currency if none is explicitly given
#'
#' We can call [info_currencies()] for a useful reference on all of the valid
#' inputs to the `currency` argument.
#'
#' @inheritParams fmt_number
#'
#' @param currency *Currency to use*
#'
#' `scalar<character>|obj:<gt_currency>` // *default:* `NULL` (`optional`)
#'
#' The currency to use for the numeric value. This input can be
#' supplied as a 3-letter currency code (e.g., `"USD"` for U.S. Dollars,
#' `"EUR"` for the Euro currency). Use [info_currencies()] to get an
#' information table with all of the valid currency codes and examples of
#' each. Alternatively, we can provide a general currency type (e.g.,
#' `"dollar"`, `"pound"`, `"yen"`, etc.) to simplify the process. Use
#' [info_currencies()] with the `type == "symbol"` option to view an
#' information table with all of the supported currency symbol names along
#' with examples.
#'
#' We can also use the [currency()] helper function to specify a custom
#' currency, where the string could vary across output contexts. For example,
#' using `currency(html = "ƒ", default = "f")` would give us a suitable
#' glyph for the Dutch guilder in an HTML output table, and it would simply be
#' the letter "f" in all other output contexts). Please note that `decimals`
#' will default to `2` when using the [currency()] helper function.
#'
#' If nothing is provided here but a `locale` value has been set (either in
#' this function call or as part of the initial [gt()] call), the currency
#' will be obtained from that locale. Virtually all locales are linked to a
#' territory that is a country (use [info_locales()] for details on all
#' locales used in this package), so, the in-use (or *de facto*) currency will
#' be obtained. As the default locale is `"en"`, the `"USD"` currency will be
#' used if neither a `locale` nor a `currency` value is given.
#'
#' @param use_subunits *Show or hide currency subunits*
#'
#' `scalar<logical>` // *default:* `TRUE`
#'
#' An option for whether the subunits portion of a currency value should be
#' displayed. For example, with an input value of `273.81`, the default
#' formatting will produce `"$273.81"`. Removing the subunits (with
#' `use_subunits = FALSE`) will give us `"$273"`.
#'
#' @param decimals *Number of decimal places*
#'
#' `scalar<numeric|integer>(val>=0)` // *default:* `NULL` (`optional`)
#'
#' The `decimals` values corresponds to the exact number of decimal places to
#' use. This value is optional as a currency has an intrinsic number of
#' decimal places (i.e., the subunits). A value such as `2.34` can, for
#' example, be formatted with `0` decimal places and if the currency used is
#' `"USD"` it would result in `"$2"`. With `4` decimal places, the formatted
#' value becomes `"$2.3400"`.
#'
#' @param drop_trailing_dec_mark *Drop the trailing decimal mark*
#'
#' `scalar<logical>` // *default:* `TRUE`
#'
#' A logical value that determines whether decimal marks should always appear
#' even if there are no decimal digits to display after formatting. For
#' example, when `use_subunits = FALSE` or `decimals = 0` a formatted value
#' such as `"$23"` can be fashioned as `"$23."` by setting
#' `drop_trailing_dec_mark = FALSE`.
#'
#' @param placement *Currency symbol placement*
#'
#' `singl-kw:[left|right]` // *default:* `"left"`
#'
#' The placement of the currency symbol. This can be either be `"left"` (as
#' in `"$450"`) or `"right"` (which yields `"450$"`).
#'
#' @param incl_space *Include a space between the value and the currency symbol*
#'
#' `scalar<logical>` // *default:* `FALSE`
#'
#' An option for whether to include a space between the value and the currency
#' symbol. The default is to not introduce a space character.
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_currency()` is compatible with body cells that are of the `"numeric"` or
#' `"integer"` types. Any other types of body cells are ignored during
#' formatting. This is to say that cells of incompatible data types may be
#' targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_currency()` to
#' obtain varying parameter values from a specified column within the table.
#' This means that each row could be formatted a little bit differently. These
#' arguments provide support for [from_column()]:
#'
#' - `currency`
#' - `use_subunits`
#' - `decimals`
#' - `drop_trailing_dec_mark`
#' - `use_seps`
#' - `accounting`
#' - `scale_by`
#' - `suffixing`
#' - `pattern`
#' - `sep_mark`
#' - `dec_mark`
#' - `force_sign`
#' - `placement`
#' - `incl_space`
#' - `system`
#' - `locale`
#'
#' Please note that for all of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Adapting output to a specific `locale`:
#'
#' This formatting function can adapt outputs according to a provided `locale`
#' value. Examples include `"en"` for English (United States) and `"fr"` for
#' French (France). The use of a locale ID here means separator and
#' decimal marks will be correct for the given locale. Should any values be
#' provided in `sep_mark` or `dec_mark`, they will be overridden by the locale's
#' preferred values. In addition to number formatting, providing a `locale`
#' value and not providing a `currency` allows **gt** to obtain the currency
#' code from the locale's territory.
#'
#' Note that a `locale` value provided here will override any global locale
#' setting performed in [gt()]'s own `locale` argument (it is settable there as
#' a value received by all other functions that have a `locale` argument). As a
#' useful reference on which locales are supported, we can call [info_locales()]
#' to view an info table.
#'
#' @section Examples:
#'
#' Let's make a simple **gt** table from the [`exibble`] dataset. We'll keep
#' only the `num` and `currency`, columns, then, format those columns using
#' `fmt_currency()` (with the `"JPY"` and `"GBP"` currencies).
#'
#' ```r
#' exibble |>
#' dplyr::select(num, currency) |>
#' gt() |>
#' fmt_currency(
#' columns = num,
#' currency = "JPY"
#' ) |>
#' fmt_currency(
#' columns = currency,
#' currency = "GBP"
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_currency_1.png")`
#' }}
#'
#' Let's take a single column from [`exibble`] (`currency`) and format it with a
#' currency name (this differs from the 3-letter currency code). In this case,
#' we'll use the `"euro"` currency and set the placement of the symbol to the
#' right of any value. Additionally, the currency symbol will separated from the
#' value with a single space character (using `incl_space = TRUE`).
#'
#' ```r
#' exibble |>
#' dplyr::select(currency) |>
#' gt() |>
#' fmt_currency(
#' currency = "euro",
#' placement = "right",
#' incl_space = TRUE
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_currency_2.png")`
#' }}
#'
#' With the [`pizzaplace`] dataset, let's make a summary table that gets the
#' number of `"hawaiian"` pizzas sold (and revenue generated) by month. In the
#' **gt** table, we'll format only the `revenue` column. The `currency` value is
#' automatically U.S. Dollars when don't supply either a currency code or a
#' locale. We'll also create a grand summary with [grand_summary_rows()]. Within
#' that summary row, the total revenue needs to be formatted with
#' `fmt_currency()` and we can do that within the `fmt` argument.
#'
#' ```r
#' pizzaplace |>
#' dplyr::filter(name == "hawaiian") |>
#' dplyr::mutate(month = lubridate::month(date, label = TRUE, abbr = TRUE)) |>
#' dplyr::select(month, price) |>
#' dplyr::group_by(month) |>
#' dplyr::summarize(
#' `number sold` = dplyr::n(),
#' revenue = sum(price)
#' ) |>
#' gt(rowname_col = "month") |>
#' tab_header(title = "Summary of Hawaiian Pizzas Sold by Month") |>
#' fmt_currency(columns = revenue) |>
#' grand_summary_rows(
#' fns = list(label = "Totals:", id = "totals", fn = "sum"),
#' fmt = ~ fmt_currency(., columns = revenue),
#' ) |>
#' opt_all_caps()
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_currency_3.png")`
#' }}
#'
#' If supplying a `locale` value to `fmt_currency()`, we can opt use the
#' locale's assumed currency and not have to supply a `currency` value (doing so
#' would override the locale's default currency). With a column of locale
#' values, we can format currency values on a row-by-row basis through the use
#' of [from_column()]. Here, we'll reference the `locale` column in the argument
#' of the same name.
#'
#' ```r
#' dplyr::tibble(
#' amount = rep(50.84, 5),
#' currency = c("JPY", "USD", "GHS", "KRW", "CNY"),
#' locale = c("ja", "en", "ee", "ko", "zh"),
#' ) |>
#' gt() |>
#' fmt_currency(
#' columns = amount,
#' locale = from_column(column = "locale")
#' ) |>
#' cols_hide(columns = locale)
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_currency_4.png")`
#' }}
#'
#' We can similarly use [from_column()] to reference a column that has currency
#' code values. Here's an example of how to create a simple currency conversion
#' table. The `curr` column contains the 3-letter currency codes, and that
#' column is referenced via [from_column()] in the `currency` argument of
#' `fmt_currency()`.
#'
#' ```r
#' dplyr::tibble(
#' flag = c("EU", "GB", "CA", "AU", "JP", "IN"),
#' curr = c("EUR", "GBP", "CAD", "AUD", "JPY", "INR"),
#' conv = c(
#' 0.912952, 0.787687, 1.34411,
#' 1.53927, 144.751, 82.9551
#' )
#' ) |>
#' gt() |>
#' fmt_currency(
#' columns = conv,
#' currency = from_column(column = "curr")
#' ) |>
#' fmt_flag(columns = flag) |>
#' cols_merge(columns = c(flag, curr)) |>
#' cols_label(
#' flag = "Currency",
#' conv = "Amount"
#' ) |>
#' tab_header(
#' title = "Conversion of 1 USD to Six Other Currencies",
#' subtitle = md("Conversion rates obtained on **Aug 13, 2023**")
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_currency_5.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-8
#'
#' @section Function Introduced:
#' `v0.2.0.5` (March 31, 2020)
#'
#' @seealso The vector-formatting version of this function:
#' [vec_fmt_currency()].
#'
#' @export
fmt_currency <- function(
data,
columns = everything(),
rows = everything(),
currency = NULL,
use_subunits = TRUE,
decimals = NULL,
drop_trailing_dec_mark = TRUE,
use_seps = TRUE,
accounting = FALSE,
scale_by = 1.0,
suffixing = FALSE,
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
force_sign = FALSE,
placement = "left",
incl_space = FALSE,
system = c("intl", "ind"),
locale = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
#
# Begin support for `from_column()` objects passed to compatible arguments
#
# Supports parameters:
#
# - currency
# - use_subunits
# - decimals
# - drop_trailing_dec_mark
# - use_seps
# - accounting
# - scale_by
# - suffixing
# - pattern
# - sep_mark
# - dec_mark
# - force_sign
# - placement
# - incl_space
# - system
# - locale
arg_vals <-
mget(
get_arg_names(
function_name = "fmt_currency",
all_args_except = c("data", "columns", "rows")
)
)
if (args_have_gt_column_obj(arg_vals = arg_vals)) {
# Resolve the row numbers using `resolve_vars()`
resolved_rows_idx <-
resolve_rows_i(
expr = {{ rows }},
data = data
)
param_tbl <-
generate_param_tbl(
data = data,
arg_vals = arg_vals,
resolved_rows_idx = resolved_rows_idx
)
for (i in seq_len(nrow(param_tbl))) {
p_i <- as.list(param_tbl[i, ])
data <-
fmt_currency(
data = data,
columns = {{ columns }},
rows = resolved_rows_idx[i],
currency = p_i$currency %||% currency,
use_subunits = p_i$use_subunits %||% use_subunits,
decimals = p_i$decimals %||% decimals,
drop_trailing_dec_mark = p_i$drop_trailing_dec_mark %||% drop_trailing_dec_mark,
use_seps = p_i$use_seps %||% use_seps,
accounting = p_i$accounting %||% accounting,
scale_by = p_i$scale_by %||% scale_by,
suffixing = p_i$suffixing %||% suffixing,
pattern = p_i$pattern %||% pattern,
sep_mark = p_i$sep_mark %||% sep_mark,
dec_mark = p_i$dec_mark %||% dec_mark,
force_sign = p_i$force_sign %||% force_sign,
placement = p_i$placement %||% placement,
incl_space = p_i$incl_space %||% incl_space,
system = p_i$system %||% system,
locale = p_i$locale %||% locale
)
}
return(data)
}
#
# End support for `from_column()` objects passed to compatible arguments
#
# Ensure that arguments are matched
system <- rlang::arg_match(system)
# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
validate_locale(locale = locale)
locale <- normalize_locale(locale = locale)
locale <- resolve_locale(data = data, locale = locale)
valid_class <- c("numeric", "integer")
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
# Resolve the currency either from direct input in `currency` or
# through a locale
currency <- currency %||% get_locale_currency_code(locale = locale)
# Stop function if `currency` does not have a valid value
validate_currency(currency = currency)
# Get the number of decimal places
decimals <-
get_currency_decimals(
currency = currency,
decimals = decimals,
use_subunits = use_subunits
)
# Pass `data`, `columns`, `rows`, and other options to `fmt_symbol()`
fmt_symbol(
data = data,
columns = {{ columns }},
rows = {{ rows }},
symbol = currency,
accounting = accounting,
decimals = decimals,
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = drop_trailing_dec_mark,
use_seps = use_seps,
scale_by = scale_by,
suffixing = suffixing,
pattern = pattern,
sep_mark = sep_mark,
dec_mark = dec_mark,
force_sign = force_sign,
placement = placement,
incl_space = incl_space,
system = system,
locale = locale
)
}
#' Format values as Roman numerals
#'
#' @description
#'
#' With numeric values in a **gt** table we can transform those to Roman
#' numerals, rounding values as necessary.
#'
#' @inheritParams fmt_number
#'
#' @param case *Use uppercase or lowercase letters*
#'
#' `singl-kw:[upper|lower]` // *default:* `"upper"`
#'
#' Should Roman numerals should be rendered as uppercase (`"upper"`) or
#' lowercase (`"lower"`) letters? By default, this is set to `"upper"`.
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_roman()` is compatible with body cells that are of the `"numeric"` or
#' `"integer"` types. Any other types of body cells are ignored during
#' formatting. This is to say that cells of incompatible data types may be
#' targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_roman()` to obtain
#' varying parameter values from a specified column within the table. This means
#' that each row could be formatted a little bit differently. These arguments
#' provide support for [from_column()]:
#'
#' - `case`
#' - `pattern`
#'
#' Please note that for both of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Examples:
#'
#' Create a tibble of small numeric values and generate a **gt** table. Format
#' the `roman` column to appear as Roman numerals with `fmt_roman()`.
#'
#' ```r
#' dplyr::tibble(arabic = c(1, 8, 24, 85), roman = arabic) |>
#' gt(rowname_col = "arabic") |>
#' fmt_roman(columns = roman)
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_roman_1.png")`
#' }}
#'
#' Formatting values to Roman numerals can be very useful when combining such
#' output with row labels (usually through [cols_merge()]). Here's an example
#' where we take a portion of the [`illness`] dataset and generate some row
#' labels that combine (1) a row number (in lowercase Roman numerals), (2) the
#' name of the test, and (3) the measurement units for the test (nicely
#' formatted by way of [fmt_units()]):
#'
#' ```r
#' illness |>
#' dplyr::slice_head(n = 6) |>
#' gt(rowname_col = "test") |>
#' fmt_units(columns = units) |>
#' cols_hide(columns = starts_with("day")) |>
#' sub_missing(missing_text = "") |>
#' cols_merge_range(col_begin = norm_l, col_end = norm_u) |>
#' cols_add(i = 1:6) |>
#' fmt_roman(columns = i, case = "lower", pattern = "{x}.") |>
#' cols_merge(columns = c(test, i, units), pattern = "{2} {1} ({3})") |>
#' cols_label(norm_l = "Normal Range") |>
#' tab_stubhead(label = "Test")
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_roman_2.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-9
#'
#' @section Function Introduced:
#' `v0.8.0` (November 16, 2022)
#'
#' @seealso The vector-formatting version of this function: [vec_fmt_roman()].
#'
#' @export
fmt_roman <- function(
data,
columns = everything(),
rows = everything(),
case = c("upper", "lower"),
pattern = "{x}"
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
#
# Begin support for `from_column()` objects passed to compatible arguments
#
# Supports parameters:
#
# - case
# - pattern
arg_vals <-
mget(
get_arg_names(
function_name = "fmt_roman",
all_args_except = c("data", "columns", "rows")
)
)
if (args_have_gt_column_obj(arg_vals = arg_vals)) {
# Resolve the row numbers using `resolve_vars()`
resolved_rows_idx <-
resolve_rows_i(
expr = {{ rows }},
data = data
)
param_tbl <-
generate_param_tbl(
data = data,
arg_vals = arg_vals,
resolved_rows_idx = resolved_rows_idx
)
for (i in seq_len(nrow(param_tbl))) {
p_i <- as.list(param_tbl[i, ])
data <-
fmt_roman(
data = data,
columns = {{ columns }},
rows = resolved_rows_idx[i],
case = p_i$case %||% case,
pattern = p_i$pattern %||% pattern
)
}
return(data)
}
#
# End support for `from_column()` objects passed to compatible arguments
#
# Ensure that arguments are matched
case <- rlang::arg_match(case)
valid_class <- c("numeric", "integer")
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
fmt(
data = data,
columns = {{ columns }},
rows = {{ rows }},
compat = valid_class,
fns = num_fmt_factory_multi(
pattern = pattern,
use_latex_math_mode = FALSE,
format_fn = function(x, context) {
# Generate an vector of empty strings that will eventually contain
# all of the roman numerals
x_str <- character(length(x))
# Round all values of x to 3 digits with the R-H-U method of
# rounding (for reproducibility purposes)
x <- round_gt(x, 0)
# Determine which of `x` are finite values
x_is_a_number <- is.finite(x)
x[x_is_a_number] <- abs(x[x_is_a_number])
x_is_in_range <- x > 0 & x < 3900
x_str[x_is_a_number & x_is_in_range] <-
as.character(utils::as.roman(x[x_is_a_number & x_is_in_range]))
x_str[x_is_a_number & x == 0] <- if (case == "lower") "n" else "N"
if (case == "lower") {
x_str[x_is_in_range] <- tolower(x_str[x_is_in_range])
}
# In rare cases that Inf or -Inf appear, ensure that these
# special values are printed correctly
x_str[is.infinite(x)] <- x[is.infinite(x)]
x_str[x_is_a_number & x != 0 & !x_is_in_range] <- "ex terminis"
x_str
}
)
)
}
#' Format values to indexed characters
#'
#' @description
#'
#' With numeric values in a **gt** table we can transform those to index values,
#' usually based on letters. These characters can be derived from a specified
#' locale and they are intended for ordering (often leaving out characters with
#' diacritical marks).
#'
#' @inheritParams fmt_number
#'
#' @param case *Use uppercase or lowercase letters*
#'
#' `singl-kw:[upper|lower]` // *default:* `"upper"`
#'
#' Should the resulting index characters be rendered as uppercase (`"upper"`)
#' or lowercase (`"lower"`) letters? By default, this is set to `"upper"`.
#'
#' @param index_algo *Indexing algorithm*
#'
#' `singl-kw:[repeat|excel]` // *default:* `"repeat"`
#'
#' The indexing algorithm handles the recycling of the index character set. By
#' default, the `"repeat"` option is used where characters are doubled,
#' tripled, and so on, when moving past the character set limit. The
#' alternative is the `"excel"` option, where Excel-based column naming is
#' adapted and used here (e.g., `[..., Y, Z, AA, AB, ...]`).
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_index()` is compatible with body cells that are of the `"numeric"` or
#' `"integer"` types. Any other types of body cells are ignored during
#' formatting. This is to say that cells of incompatible data types may be
#' targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_index()` to obtain
#' varying parameter values from a specified column within the table. This means
#' that each row could be formatted a little bit differently. These arguments
#' provide support for [from_column()]:
#'
#' - `case`
#' - `index_algo`
#' - `pattern`
#' - `locale`
#'
#' Please note that for all of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Examples:
#'
#' Using a summarized version of the [`towny`] dataset, let's create a **gt**
#' table. Here, `fmt_index()` is used to transform incremental
#' integer values into capitalized letters (in the `ranking` column). With
#' [cols_merge()] that formatted column of `"A"` to `"E"` values is merged with
#' the `census_div` column to create an indexed listing of census subdivisions,
#' here ordered by increasing total municipal population.
#'
#' ```r
#' towny |>
#' dplyr::select(name, csd_type, census_div, population_2021) |>
#' dplyr::group_by(census_div) |>
#' dplyr::summarize(
#' population = sum(population_2021),
#' .groups = "drop_last"
#' ) |>
#' dplyr::slice_min(population, n = 5) |>
#' dplyr::mutate(ranking = dplyr::row_number(), .before = 0) |>
#' gt() |>
#' fmt_integer() |>
#' fmt_index(columns = ranking, pattern = "{x}.") |>
#' cols_merge(columns = c(ranking, census_div)) |>
#' cols_align(align = "left", columns = ranking) |>
#' cols_label(
#' ranking = md("Census \nSubdivision"),
#' population = md("Population \nin 2021")
#' ) |>
#' tab_header(title = md("The smallest \ncensus subdivisions")) |>
#' tab_options(table.width = px(325))
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_index_1.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-10
#'
#' @section Function Introduced:
#' `v0.9.0` (Mar 31, 2023)
#'
#' @seealso The vector-formatting version of this function: [vec_fmt_index()].
#'
#' @export
fmt_index <- function(
data,
columns = everything(),
rows = everything(),
case = c("upper", "lower"),
index_algo = c("repeat", "excel"),
pattern = "{x}",
locale = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
#
# Begin support for `from_column()` objects passed to compatible arguments
#
# Supports parameters:
#
# - case
# - index_algo
# - pattern
# - locale
arg_vals <-
mget(
get_arg_names(
function_name = "fmt_index",
all_args_except = c("data", "columns", "rows")
)
)
if (args_have_gt_column_obj(arg_vals = arg_vals)) {
# Resolve the row numbers using `resolve_vars()`
resolved_rows_idx <-
resolve_rows_i(
expr = {{ rows }},
data = data
)
param_tbl <-
generate_param_tbl(
data = data,
arg_vals = arg_vals,
resolved_rows_idx = resolved_rows_idx
)
for (i in seq_len(nrow(param_tbl))) {
p_i <- as.list(param_tbl[i, ])
data <-
fmt_index(
data = data,
columns = {{ columns }},
rows = resolved_rows_idx[i],
case = p_i$case %||% case,
index_algo = p_i$index_algo %||% index_algo,
pattern = p_i$pattern %||% pattern,
locale = p_i$locale %||% locale
)
}
return(data)
}
#
# End support for `from_column()` objects passed to compatible arguments
#
# Ensure that arguments are matched
case <- rlang::arg_match(case)
index_algo <- rlang::arg_match(index_algo)
# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
validate_locale(locale = locale)
locale <- normalize_locale(locale = locale)
locale <- resolve_locale(data = data, locale = locale)
# Use locale-based `idx_set` if a locale ID is provided
idx_set <- get_locale_idx_set(locale)
valid_class <- c("numeric", "integer")
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
fmt(
data = data,
columns = {{ columns }},
rows = {{ rows }},
compat = valid_class,
fns = num_fmt_factory_multi(
pattern = pattern,
use_latex_math_mode = FALSE,
format_fn = function(x, context) {
# Generate an vector of empty strings that will eventually contain
# all of the roman numerals
x_str <- character(length(x))
# Round all values of x to 3 digits with the R-H-U method of
# rounding (for reproducibility purposes)
x <- round_gt(x, 0)
# Determine which of `x` are finite values
x_is_a_number <- is.finite(x)
x[x_is_a_number] <- abs(x[x_is_a_number])
# Select the correct indexing function
if (index_algo == "repeat") {
index_fn <- index_repeat
} else {
index_fn <- index_excel
}
x_str[x_is_a_number] <-
vapply(
x[x_is_a_number],
FUN.VALUE = character(1L),
USE.NAMES = FALSE,
FUN = function(x) index_fn(x, set = idx_set)
)
x_str[x_is_a_number & x == 0] <- ""
if (case == "lower") {
x_str <- tolower(x_str)
}
# In rare cases that Inf or -Inf appear, ensure that these
# special values are printed correctly
x_str[is.infinite(x)] <- x[is.infinite(x)]
x_str
}
)
)
}
index_repeat <- function(x, set) {
marks_rep <- floor((x - 1) / length(set)) + 1
marks_val <- set[(x - 1) %% length(set) + 1]
unname(
mapply(
marks_val, marks_rep,
FUN = function(val_i, rep_i) {
paste(rep(val_i, rep_i), collapse = "")}
)
)
}
index_excel <- function(num, set) {
result <-
vapply(
num,
FUN.VALUE = character(1L),
USE.NAMES = FALSE,
FUN = function(x) {
get_letters_from_div(x, set = set)
}
)
ifelse(result == "", NA_character_, result)
}
get_letters_from_div <- function(x, set) {
if (is.na(x)) {
return(NA_character_)
}
result <- integer()
while (x > 0) {
remainder <- ((x - 1) %% 26) + 1
result <- c(remainder, result)
x <- (x - remainder) %/% 26
}
paste(set[result], collapse = "")
}
#' Format values to spelled-out numbers
#'
#' @description
#'
#' With numeric values in a **gt** table we can transform those to numbers that
#' are spelled out with `fmt_spelled_num()`. Any values from `0` to `100` can be
#' spelled out so, for example, the value `23` will be formatted as `"twenty-three"`.
#' Providing a locale ID will result in the number spelled out in the locale's
#' language rules. For example, should a Swedish locale (`"sv"`) be provided,
#' the value `23` will yield `"tjugotre"`. In addition to this, we can
#' optionally use the `pattern` argument for decoration of the formatted values.
#'
#' @inheritParams fmt_number
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_spelled_num()` is compatible with body cells that are of the `"numeric"`
#' or `"integer"` types. Any other types of body cells are ignored during
#' formatting. This is to say that cells of incompatible data types may be
#' targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_spelled_num()` to
#' obtain varying parameter values from a specified column within the table.
#' This means that each row could be formatted a little bit differently. These
#' arguments provide support for [from_column()]:
#'
#' - `pattern`
#' - `locale`
#'
#' Please note that for both of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Supported locales:
#'
#' The following 80 locales are supported in the `locale` argument of
#' `fmt_spelled_num()`: `"af"` (Afrikaans), `"ak"` (Akan), `"am"` (Amharic),
#' `"ar"` (Arabic), `"az"` (Azerbaijani), `"be"` (Belarusian), `"bg"`
#' (Bulgarian), `"bs"` (Bosnian), `"ca"` (Catalan), `"ccp"` (Chakma), `"chr"`
#' (Cherokee), `"cs"` (Czech), `"cy"` (Welsh), `"da"` (Danish), `"de"` (German),
#' `"de-CH"` (German (Switzerland)), `"ee"` (Ewe), `"el"` (Greek), `"en"`
#' (English), `"eo"` (Esperanto), `"es"` (Spanish), `"et"` (Estonian), `"fa"`
#' (Persian), `"ff"` (Fulah), `"fi"` (Finnish), `"fil"` (Filipino), `"fo"`
#' (Faroese), `"fr"` (French), `"fr-BE"` (French (Belgium)), `"fr-CH"` (French
#' (Switzerland)), `"ga"` (Irish), `"he"` (Hebrew), `"hi"` (Hindi), `"hr"`
#' (Croatian), `"hu"` (Hungarian), `"hy"` (Armenian), `"id"` (Indonesian),
#' `"is"` (Icelandic), `"it"` (Italian), `"ja"` (Japanese), `"ka"` (Georgian),
#' `"kk"` (Kazakh), `"kl"` (Kalaallisut), `"km"` (Khmer), `"ko"` (Korean),
#' `"ky"` (Kyrgyz), `"lb"` (Luxembourgish), `"lo"` (Lao), `"lrc"` (Northern
#' Luri), `"lt"` (Lithuanian), `"lv"` (Latvian), `"mk"` (Macedonian), `"ms"`
#' (Malay), `"mt"` (Maltese), `"my"` (Burmese), `"ne"` (Nepali), `"nl"` (Dutch),
#' `"nn"` (Norwegian Nynorsk), `"no"` (Norwegian), `"pl"` (Polish), `"pt"`
#' (Portuguese), `"qu"` (Quechua), `"ro"` (Romanian), `"ru"` (Russian), `"se"`
#' (Northern Sami), `"sk"` (Slovak), `"sl"` (Slovenian), `"sq"` (Albanian),
#' `"sr"` (Serbian), `"sr-Latn"` (Serbian (Latin)), `"su"` (Sundanese), `"sv"`
#' (Swedish), `"sw"` (Swahili), `"ta"` (Tamil), `"th"` (Thai), `"tr"` (Turkish),
#' `"uk"` (Ukrainian), `"vi"` (Vietnamese), `"yue"` (Cantonese), and `"zh"`
#' (Chinese).
#'
#' @section Examples:
#'
#' Let's use a summarized version of the [`gtcars`] dataset to create a
#' **gt** table. `fmt_spelled_num()` is used to transform
#' integer values into spelled-out numbering (in the `n` column). That formatted
#' column of numbers-as-words is given cell background colors via [data_color()]
#' (the underlying numerical values are always available).
#'
#' ```r
#' gtcars |>
#' dplyr::count(mfr, ctry_origin) |>
#' dplyr::arrange(ctry_origin) |>
#' gt(rowname_col = "mfr", groupname_col = "ctry_origin") |>
#' cols_label(n = "No. of Entries") |>
#' fmt_spelled_num() |>
#' tab_stub_indent(rows = everything(), indent = 2) |>
#' data_color(
#' columns = n,
#' method = "numeric",
#' palette = "viridis",
#' alpha = 0.8
#' ) |>
#' opt_all_caps() |>
#' opt_vertical_padding(scale = 0.5) |>
#' cols_align(align = "center", columns = n)
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_spelled_num_1.png")`
#' }}
#'
#' With a considerable amount of **dplyr** and **tidyr** work done to the
#' [`pizzaplace`] dataset, we can create a new **gt** table. `fmt_spelled_num()`
#' will be used here to transform the integer values in the `rank` column.
#' We'll do so with a special `pattern` that puts the word 'Number' in front of
#' every spelled-out number.
#'
#' ```r
#' pizzaplace |>
#' dplyr::mutate(month = lubridate::month(date, label = TRUE)) |>
#' dplyr::filter(month %in% month.abb[1:6]) |>
#' dplyr::group_by(name, month) |>
#' dplyr::summarize(sum = sum(price), .groups = "drop") |>
#' dplyr::arrange(month, desc(sum)) |>
#' dplyr::group_by(month) |>
#' dplyr::slice_head(n = 5) |>
#' dplyr::mutate(rank = dplyr::row_number()) |>
#' dplyr::ungroup() |>
#' dplyr::select(-sum) |>
#' tidyr::pivot_wider(names_from = month, values_from = c(name)) |>
#' gt() |>
#' fmt_spelled_num(columns = rank, pattern = "Number {x}") |>
#' opt_all_caps() |>
#' cols_align(columns = -rank, align = "center") |>
#' cols_width(
#' rank ~ px(120),
#' everything() ~ px(100)
#' ) |>
#' opt_table_font(stack = "rounded-sans") |>
#' tab_options(table.font.size = px(14))
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_spelled_num_2.png")`
#' }}
#'
#' Let's make a table that compares how the numbers from `1` to `10` are spelled
#' across a small selection of languages. Here we use `fmt_spelled_num()` with
#' each column, ensuring that the `locale` value matches that of the column
#' name.
#'
#' ```r
#' dplyr::tibble(
#' num = 1:10,
#' en = num,
#' fr = num,
#' de = num,
#' es = num,
#' pl = num,
#' bg = num,
#' ko = num,
#' zh = num
#' ) |>
#' gt(rowname_col = "num") |>
#' fmt_spelled_num(columns = en, locale = "en") |>
#' fmt_spelled_num(columns = fr, locale = "fr") |>
#' fmt_spelled_num(columns = de, locale = "de") |>
#' fmt_spelled_num(columns = es, locale = "es") |>
#' fmt_spelled_num(columns = pl, locale = "pl") |>
#' fmt_spelled_num(columns = bg, locale = "bg") |>
#' fmt_spelled_num(columns = ko, locale = "ko") |>
#' fmt_spelled_num(columns = zh, locale = "zh") |>
#' cols_label_with(fn = function(x) md(paste0("`", x, "`"))) |>
#' tab_spanner(
#' label = "Numbers in the specified locale",
#' columns = everything()
#' ) |>
#' cols_align(align = "left", columns = everything()) |>
#' cols_width(
#' c(en, fr, de, es, pl, bg) ~ px(100),
#' c(ko, zh) ~ px(50)
#' ) |>
#' opt_horizontal_padding(scale = 2) |>
#' opt_vertical_padding(scale = 0.5)
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_spelled_num_3.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-11
#'
#' @section Function Introduced:
#' `v0.9.0` (Mar 31, 2023)
#'
#' @seealso The vector-formatting version of this function:
#' [vec_fmt_spelled_num()].
#'
#' @export
fmt_spelled_num <- function(
data,
columns = everything(),
rows = everything(),
pattern = "{x}",
locale = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
#
# Begin support for `from_column()` objects passed to compatible arguments
#
# Supports parameters:
#
# - pattern
# - locale
arg_vals <-
mget(
get_arg_names(
function_name = "fmt_spelled_num",
all_args_except = c("data", "columns", "rows")
)
)
if (args_have_gt_column_obj(arg_vals = arg_vals)) {
# Resolve the row numbers using `resolve_vars()`
resolved_rows_idx <-
resolve_rows_i(
expr = {{ rows }},
data = data
)
param_tbl <-
generate_param_tbl(
data = data,
arg_vals = arg_vals,
resolved_rows_idx = resolved_rows_idx
)
for (i in seq_len(nrow(param_tbl))) {
p_i <- as.list(param_tbl[i, ])
data <-
fmt_spelled_num(
data = data,
columns = {{ columns }},
rows = resolved_rows_idx[i],
pattern = p_i$pattern %||% pattern,
locale = p_i$locale %||% locale
)
}
return(data)
}
#
# End support for `from_column()` objects passed to compatible arguments
#
# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
validate_locale(locale = locale)
locale <- normalize_locale(locale = locale)
locale <- resolve_locale(data = data, locale = locale)
# Obtain a locale-based `num_spellout_set` vector
num_spellout_set <- get_locale_num_spellout(locale = locale)
valid_class <- c("numeric", "integer")
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
fmt(
data = data,
columns = {{ columns }},
rows = {{ rows }},
compat = valid_class,
fns = num_fmt_factory_multi(
pattern = pattern,
use_latex_math_mode = FALSE,
format_fn = function(x, context) {
# Generate an vector of empty strings that will eventually contain
# all of the roman numerals
x_str <- character(length(x))
# Round all values of x to 3 digits with the R-H-U method of
# rounding (for reproducibility purposes)
x <- floor(x)
# Determine which of `x` are finite values
x_is_a_number <- is.finite(x)
# x[x_is_a_number] <- abs(x[x_is_a_number])
# The allowed range of numbers that can be spelled out
# is `0` to `100`
x_is_in_range <- x >= 0 & x <= 100
# The `num_spellout_set` vector should always contain 101
# elements; it contains zero then the numbers from 1 to 100
x_str[x_is_a_number & x_is_in_range] <-
num_spellout_set[x[x_is_a_number & x_is_in_range] + 1]
# Ensure that numbers not in range are included as
# floored numeric values
x_str[x_is_a_number & !x_is_in_range] <-
x[x_is_a_number & !x_is_in_range]
# In rare cases that Inf or -Inf appear, ensure that these
# special values are printed correctly
x_str[is.infinite(x)] <- x[is.infinite(x)]
x_str
}
)
)
}
#' Format values as bytes
#'
#' @description
#'
#' With numeric values in a **gt** table, we can transform those to values of
#' bytes with human readable units. `fmt_bytes()` allows for the
#' formatting of byte sizes to either of two common representations: (1) with
#' decimal units (powers of 1000, examples being `"kB"` and `"MB"`), and (2)
#' with binary units (powers of 1024, examples being `"KiB"` and `"MiB"`).
#'
#' It is assumed the input numeric values represent the number of bytes and
#' automatic truncation of values will occur. The numeric values will be scaled
#' to be in the range of 1 to <1000 and then decorated with the correct unit
#' symbol according to the standard chosen. For more control over the formatting
#' of byte sizes, we can use the following options:
#'
#' - decimals: choice of the number of decimal places, option to drop
#' trailing zeros, and a choice of the decimal symbol
#' - digit grouping separators: options to enable/disable digit separators
#' and provide a choice of separator symbol
#' - pattern: option to use a text pattern for decoration of the formatted
#' values
#' - locale-based formatting: providing a locale ID will result in number
#' formatting specific to the chosen locale
#'
#' @inheritParams fmt_number
#'
#' @param standard *Standard used to express byte sizes*
#'
#' `singl-kw:[decimal|binary]` // *default:* `"decimal"`
#'
#' The form of expressing large byte sizes is divided between: (1) decimal
#' units (powers of 1000; e.g., `"kB"` and `"MB"`), and (2) binary units
#' (powers of 1024; e.g., `"KiB"` and `"MiB"`).
#'
#' @param decimals *Number of decimal places*
#'
#' `scalar<numeric|integer>(val>=0)` // *default:* `1`
#'
#' This corresponds to the exact number of decimal places to use. A value
#' such as `2.34` can, for example, be formatted with `0` decimal places and
#' it would result in `"2"`. With `4` decimal places, the formatted value
#' becomes `"2.3400"`. The trailing zeros can be removed with
#' `drop_trailing_zeros = TRUE`.
#'
#' @param force_sign *Forcing the display of a positive sign*
#'
#' `scalar<logical>` // *default:* `FALSE`
#'
#' Should the positive sign be shown for positive numbers (effectively showing
#' a sign for all numbers except zero)? If so, use `TRUE` for this option. The
#' default is `FALSE`, where only negative numbers will display a minus sign.
#'
#' @param incl_space *Include a space between the value and the units*
#'
#' `scalar<logical>` // *default:* `TRUE`
#'
#' An option for whether to include a space between the value and the units.
#' The default is to use a space character for separation.
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_bytes()` is compatible with body cells that are of the `"numeric"` or
#' `"integer"` types. Any other types of body cells are ignored during
#' formatting. This is to say that cells of incompatible data types may be
#' targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_bytes()` to obtain
#' varying parameter values from a specified column within the table. This means
#' that each row could be formatted a little bit differently. These arguments
#' provide support for [from_column()]:
#'
#' - `standard`
#' - `decimals`
#' - `n_sigfig`
#' - `drop_trailing_zeros`
#' - `drop_trailing_dec_mark`
#' - `use_seps`
#' - `pattern`
#' - `sep_mark`
#' - `dec_mark`
#' - `force_sign`
#' - `incl_space`
#' - `locale`
#'
#' Please note that for each of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Adapting output to a specific `locale`:
#'
#' This formatting function can adapt outputs according to a provided `locale`
#' value. Examples include `"en"` for English (United States) and `"fr"` for
#' French (France). The use of a valid locale ID here means separator and
#' decimal marks will be correct for the given locale. Should any values be
#' provided in `sep_mark` or `dec_mark`, they will be overridden by the locale's
#' preferred values.
#'
#' Note that a `locale` value provided here will override any global locale
#' setting performed in [gt()]'s own `locale` argument (it is settable there as
#' a value received by all other functions that have a `locale` argument). As a
#' useful reference on which locales are supported, we can call [info_locales()]
#' to view an info table.
#'
#' @section Examples:
#'
#' Use a single column from the [`exibble`] dataset and create a simple **gt**
#' table. We'll use `fmt_bytes()` to format the `num` column to display as byte
#' sizes in the decimal standard.
#'
#' ```r
#' exibble |>
#' dplyr::select(num) |>
#' gt() |>
#' fmt_bytes()
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_bytes_1.png")`
#' }}
#'
#' Let's create an analogous table again by using `fmt_bytes()`,
#' this time showing byte sizes as binary values by using `standard = "binary"`.
#'
#' ```r
#' exibble |>
#' dplyr::select(num) |>
#' gt() |>
#' fmt_bytes(standard = "binary")
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_bytes_2.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-12
#'
#' @section Function Introduced:
#' `v0.3.0` (May 12, 2021)
#'
#' @seealso The vector-formatting version of this function: [vec_fmt_bytes()].
#'
#' @export
fmt_bytes <- function(
data,
columns = everything(),
rows = everything(),
standard = c("decimal", "binary"),
decimals = 1,
n_sigfig = NULL,
drop_trailing_zeros = TRUE,
drop_trailing_dec_mark = TRUE,
use_seps = TRUE,
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
force_sign = FALSE,
incl_space = TRUE,
locale = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
#
# Begin support for `from_column()` objects passed to compatible arguments
#
# Supports parameters:
#
# - standard
# - decimals
# - n_sigfig
# - drop_trailing_zeros
# - drop_trailing_dec_mark
# - use_seps
# - pattern
# - sep_mark
# - dec_mark
# - force_sign
# - incl_space
# - locale
arg_vals <-
mget(
get_arg_names(
function_name = "fmt_bytes",
all_args_except = c("data", "columns", "rows")
)
)
if (args_have_gt_column_obj(arg_vals = arg_vals)) {
# Resolve the row numbers using `resolve_vars()`
resolved_rows_idx <-
resolve_rows_i(
expr = {{ rows }},
data = data
)
param_tbl <-
generate_param_tbl(
data = data,
arg_vals = arg_vals,
resolved_rows_idx = resolved_rows_idx
)
for (i in seq_len(nrow(param_tbl))) {
p_i <- as.list(param_tbl[i, ])
data <-
fmt_bytes(
data = data,
columns = {{ columns }},
rows = resolved_rows_idx[i],
standard = p_i$standard %||% standard,
decimals = p_i$decimals %||% decimals,
n_sigfig = p_i$n_sigfig %||% n_sigfig,
drop_trailing_zeros = p_i$drop_trailing_zeros %||% drop_trailing_zeros,
drop_trailing_dec_mark = p_i$drop_trailing_dec_mark %||% drop_trailing_dec_mark,
use_seps = p_i$use_seps %||% use_seps,
pattern = p_i$pattern %||% pattern,
sep_mark = p_i$sep_mark %||% sep_mark,
dec_mark = p_i$dec_mark %||% dec_mark,
force_sign = p_i$force_sign %||% force_sign,
incl_space = p_i$incl_space %||% incl_space,
locale = p_i$locale %||% locale
)
}
return(data)
}
#
# End support for `from_column()` objects passed to compatible arguments
#
# Ensure that arguments are matched
standard <- rlang::arg_match(standard)
# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
validate_locale(locale = locale)
locale <- normalize_locale(locale = locale)
locale <- resolve_locale(data = data, locale = locale)
valid_class <- c("numeric", "integer")
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
# Use locale-based marks if a locale ID is provided
sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps)
dec_mark <- get_locale_dec_mark(locale, dec_mark)
# Set the `formatC_format` option according to whether number
# formatting with significant figures is to be performed
if (!is.null(n_sigfig)) {
# Stop function if `n_sigfig` does not have a valid value
validate_n_sigfig(n_sigfig = n_sigfig)
formatC_format <- "fg"
} else {
formatC_format <- "f"
}
if (standard == "decimal") {
base <- 1000
byte_units <- c("B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
} else {
base <- 1024
byte_units <- c("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB")
}
# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
fmt(
data = data,
columns = {{ columns }},
rows = {{ rows }},
compat = valid_class,
fns = num_fmt_factory_multi(
pattern = pattern,
format_fn = function(x, context) {
# Truncate all byte values
x <- trunc(x)
num_power_idx <- floor(log(abs(x), base = base)) + 1
num_power_idx <- pmax(1, pmin(length(byte_units), num_power_idx))
units_str <- byte_units[num_power_idx]
x <- x / base^(num_power_idx - 1)
# Format numeric values to character-based numbers
x_str <-
format_num_to_str(
x,
context = context,
decimals = decimals,
n_sigfig = n_sigfig,
sep_mark = sep_mark,
dec_mark = dec_mark,
drop_trailing_zeros = drop_trailing_zeros,
drop_trailing_dec_mark = drop_trailing_dec_mark,
format = formatC_format
)
x_str <-
paste_right(x_str, x_right = paste0(if (incl_space) " ", units_str))
# Force a positive sign on certain values if the option is taken
if (force_sign) {
positive_x <- !is.na(x) & x > 0
x_str[positive_x] <- paste_left(x_str[positive_x], x_left = "+")
}
x_str
}
)
)
}
#' Format values as dates
#'
#' @description
#'
#' Format input values to time values using one of 41 preset date styles. Input
#' can be in the form of `POSIXt` (i.e., datetimes), the `Date` type, or
#' `character` (must be in the ISO 8601 form of `YYYY-MM-DD HH:MM:SS` or
#' `YYYY-MM-DD`).
#'
#' @inheritParams fmt_number
#'
#' @param date_style *Predefined style for dates*
#'
#' `scalar<character>|scalar<numeric|integer>(1<=val<=41)` // *default:* `"iso"`
#'
#' The date style to use. By default this is the short name `"iso"` which
#' corresponds to ISO 8601 date formatting. There are 41 date styles in total
#' and their short names can be viewed using [info_date_style()].
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_date()` is compatible with body cells that are of the `"Date"`,
#' `"POSIXt"` or `"character"` types. Any other types of body cells are ignored
#' during formatting. This is to say that cells of incompatible data types may
#' be targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_date()` to obtain
#' varying parameter values from a specified column within the table. This means
#' that each row could be formatted a little bit differently. These arguments
#' provide support for [from_column()]:
#'
#' - `date_style`
#' - `pattern`
#' - `locale`
#'
#' Please note that for each of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Formatting with the `date_style` argument:
#'
#' We need to supply a preset date style to the `date_style` argument. The date
#' styles are numerous and can handle localization to any supported locale. A
#' large segment of date styles are termed flexible date formats and this means
#' that their output will adapt to any `locale` provided. That feature makes the
#' flexible date formats a better option for locales other than `"en"` (the
#' default locale).
#'
#' The following table provides a listing of all date styles and their output
#' values (corresponding to an input date of `2000-02-29`).
#'
#' | | Date Style | Output | Notes |
#' |----|-----------------------|-------------------------|---------------|
#' | 1 | `"iso"` | `"2000-02-29"` | ISO 8601 |
#' | 2 | `"wday_month_day_year"`| `"Tuesday, February 29, 2000"` | |
#' | 3 | `"wd_m_day_year"` | `"Tue, Feb 29, 2000"` | |
#' | 4 | `"wday_day_month_year"`| `"Tuesday 29 February 2000"` | |
#' | 5 | `"month_day_year"` | `"February 29, 2000"` | |
#' | 6 | `"m_day_year"` | `"Feb 29, 2000"` | |
#' | 7 | `"day_m_year"` | `"29 Feb 2000"` | |
#' | 8 | `"day_month_year"` | `"29 February 2000"` | |
#' | 9 | `"day_month"` | `"29 February"` | |
#' | 10 | `"day_m"` | `"29 Feb"` | |
#' | 11 | `"year"` | `"2000"` | |
#' | 12 | `"month"` | `"February"` | |
#' | 13 | `"day"` | `"29"` | |
#' | 14 | `"year.mn.day"` | `"2000/02/29"` | |
#' | 15 | `"y.mn.day"` | `"00/02/29"` | |
#' | 16 | `"year_week"` | `"2000-W09"` | |
#' | 17 | `"year_quarter"` | `"2000-Q1"` | |
#' | 18 | `"yMd"` | `"2/29/2000"` | flexible |
#' | 19 | `"yMEd"` | `"Tue, 2/29/2000"` | flexible |
#' | 20 | `"yMMM"` | `"Feb 2000"` | flexible |
#' | 21 | `"yMMMM"` | `"February 2000"` | flexible |
#' | 22 | `"yMMMd"` | `"Feb 29, 2000"` | flexible |
#' | 23 | `"yMMMEd"` | `"Tue, Feb 29, 2000"` | flexible |
#' | 24 | `"GyMd"` | `"2/29/2000 A"` | flexible |
#' | 25 | `"GyMMMd"` | `"Feb 29, 2000 AD"` | flexible |
#' | 26 | `"GyMMMEd"` | `"Tue, Feb 29, 2000 AD"`| flexible |
#' | 27 | `"yM"` | `"2/2000"` | flexible |
#' | 28 | `"Md"` | `"2/29"` | flexible |
#' | 29 | `"MEd"` | `"Tue, 2/29"` | flexible |
#' | 30 | `"MMMd"` | `"Feb 29"` | flexible |
#' | 31 | `"MMMEd"` | `"Tue, Feb 29"` | flexible |
#' | 32 | `"MMMMd"` | `"February 29"` | flexible |
#' | 33 | `"GyMMM"` | `"Feb 2000 AD"` | flexible |
#' | 34 | `"yQQQ"` | `"Q1 2000"` | flexible |
#' | 35 | `"yQQQQ"` | `"1st quarter 2000"` | flexible |
#' | 36 | `"Gy"` | `"2000 AD"` | flexible |
#' | 37 | `"y"` | `"2000"` | flexible |
#' | 38 | `"M"` | `"2"` | flexible |
#' | 39 | `"MMM"` | `"Feb"` | flexible |
#' | 40 | `"d"` | `"29"` | flexible |
#' | 41 | `"Ed"` | `"29 Tue"` | flexible |
#'
#' We can call [info_date_style()] in the console to view a similar table
#' of date styles with example output.
#'
#' @section Adapting output to a specific `locale`:
#'
#' This formatting function can adapt outputs according to a provided `locale`
#' value. Examples include `"en"` for English (United States) and `"fr"` for
#' French (France). Note that a `locale` value provided here will override any
#' global locale setting performed in [gt()]'s own `locale` argument (it is
#' settable there as a value received by all other functions that have a
#' `locale` argument). As a useful reference on which locales are supported, we
#' can call [info_locales()] to view an info table.
#'
#' @section Examples:
#'
#' Let's use the [`exibble`] dataset to create a simple, two-column **gt** table
#' (keeping only the `date` and `time` columns). With `fmt_date()`, we'll format
#' the `date` column to display dates formatted with the `"month_day_year"`
#' date style.
#'
#' ```r
#' exibble |>
#' dplyr::select(date, time) |>
#' gt() |>
#' fmt_date(
#' columns = date,
#' date_style = "month_day_year"
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_date_1.png")`
#' }}
#'
#' Again using the [`exibble`] dataset, let's format the `date` column to have
#' mixed date formats, where dates after April 1st will be different than the
#' others because of the expressions used in the `rows` argument. This will
#' involve two calls of `fmt_date()` with different statements provided for
#' `rows`. In the first call (dates after the 1st of April) the date style
#' `"m_day_year"` is used; for the second call, `"day_m_year"` is the named
#' date style supplied to `date_style`.
#'
#' ```r
#' exibble |>
#' dplyr::select(date, time) |>
#' gt() |>
#' fmt_date(
#' columns = date,
#' rows = as.Date(date) > as.Date("2015-04-01"),
#' date_style = "m_day_year"
#' ) |>
#' fmt_date(
#' columns = date,
#' rows = as.Date(date) <= as.Date("2015-04-01"),
#' date_style = "day_m_year"
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_date_2.png")`
#' }}
#'
#' Use the [`exibble`] dataset to create a single-column **gt** table (with only
#' the `date` column). Format the date values using the `"yMMMEd"` date style
#' (which is one of the 'flexible' styles). Also, we'll set the locale to `"nl"`
#' to get the dates in Dutch.
#'
#' ```r
#' exibble |>
#' dplyr::select(date) |>
#' gt() |>
#' fmt_date(
#' date_style = "yMMMEd",
#' locale = "nl"
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_date_3.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-13
#'
#' @section Function Introduced:
#' `v0.2.0.5` (March 31, 2020)
#'
#' @seealso The vector-formatting version of this function: [vec_fmt_date()].
#'
#' @export
fmt_date <- function(
data,
columns = everything(),
rows = everything(),
date_style = "iso",
pattern = "{x}",
locale = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
#
# Begin support for `from_column()` objects passed to compatible arguments
#
# Supports parameters:
#
# - date_style
# - pattern
# - locale
arg_vals <-
mget(
get_arg_names(
function_name = "fmt_date",
all_args_except = c("data", "columns", "rows")
)
)
if (args_have_gt_column_obj(arg_vals = arg_vals)) {
# Resolve the row numbers using `resolve_vars()`
resolved_rows_idx <-
resolve_rows_i(
expr = {{ rows }},
data = data
)
param_tbl <-
generate_param_tbl(
data = data,
arg_vals = arg_vals,
resolved_rows_idx = resolved_rows_idx
)
for (i in seq_len(nrow(param_tbl))) {
p_i <- as.list(param_tbl[i, ])
data <-
fmt_date(
data = data,
columns = {{ columns }},
rows = resolved_rows_idx[i],
date_style = p_i$date_style %||% date_style,
pattern = p_i$pattern %||% pattern,
locale = p_i$locale %||% locale
)
}
return(data)
}
#
# End support for `from_column()` objects passed to compatible arguments
#
# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
validate_locale(locale = locale)
locale <- normalize_locale(locale = locale)
locale <- resolve_locale(data = data, locale = locale)
# Transform `date_style` to `date_format_str`
date_format_str <- withCallingHandlers(
get_date_format(date_style = date_style),
error = function(e) {
cli::cli_abort("Invalid date style. See {.run gt::info_date_style()}.",
parent = e)
})
valid_class <- c("Date", "POSIXt", "character")
extra_msg <- c(i = "If character data is supplied, it should be ISO-8601 formatted dates.")
check_columns_valid_if_strict(data, {{ columns }}, valid_class, extra_msg = extra_msg)
# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
fmt(
data = data,
columns = {{ columns }},
rows = {{ rows }},
compat = valid_class,
fns = num_fmt_factory_multi(
pattern = pattern,
use_latex_math_mode = FALSE,
format_fn = function(x, context) {
# Convert incoming values to POSIXlt but provide a friendly error
# if the values cannot be parsed by `as.POSIXlt()`
date <-
withCallingHandlers(
as.POSIXlt(x, tz = "GMT"),
error = function(e) {
cli::cli_abort(
"One or more of the provided date/datetime values are invalid.",
call = call("fmt_date"),
parent = e
)
}
)
# Format the date string using `fdt()`
bigD::fdt(
input = as.character(date),
format = date_format_str,
locale = locale
)
}
)
)
}
#' Format values as times
#'
#' @description
#'
#' Format input values to time values using one of 25 preset time styles. Input
#' can be in the form of `POSIXt` (i.e., datetimes), `character` (must be in the
#' ISO 8601 forms of `HH:MM:SS` or `YYYY-MM-DD HH:MM:SS`), or `Date` (which
#' always results in the formatting of `00:00:00`).
#'
#' @inheritParams fmt_number
#'
#' @param time_style *Predefined style for times*
#'
#' `scalar<character>|scalar<numeric|integer>(1<=val<=25)` // *default:* `"iso"`
#'
#' The time style to use. By default this is the short name `"iso"` which
#' corresponds to how times are formatted within ISO 8601 datetime values.
#' There are 25 time styles in total and their short names can be viewed using
#' [info_time_style()].
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_time()` is compatible with body cells that are of the `"Date"`,
#' `"POSIXt"` or `"character"` types. Any other types of body cells are ignored
#' during formatting. This is to say that cells of incompatible data types may
#' be targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_time()` to obtain
#' varying parameter values from a specified column within the table. This means
#' that each row could be formatted a little bit differently. These arguments
#' provide support for [from_column()]:
#'
#' - `time_style`
#' - `pattern`
#' - `locale`
#'
#' Please note that for each of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Formatting with the `time_style` argument:
#'
#' We need to supply a preset time style to the `time_style` argument. There are
#' many time styles and all of them can handle localization to any supported
#' locale. Many of the time styles are termed flexible time formats and this
#' means that their output will adapt to any `locale` provided. That feature
#' makes the flexible time formats a better option for locales other than `"en"`
#' (the default locale).
#'
#' The following table provides a listing of all time styles and their output
#' values (corresponding to an input time of `14:35:00`). It is noted which of
#' these represent 12- or 24-hour time.
#'
#' | | Time Style | Output | Notes |
#' |----|---------------|---------------------------------|---------------|
#' | 1 | `"iso"` | `"14:35:00"` | ISO 8601, 24h |
#' | 2 | `"iso-short"` | `"14:35"` | ISO 8601, 24h |
#' | 3 | `"h_m_s_p"` | `"2:35:00 PM"` | 12h |
#' | 4 | `"h_m_p"` | `"2:35 PM"` | 12h |
#' | 5 | `"h_p"` | `"2 PM"` | 12h |
#' | 6 | `"Hms"` | `"14:35:00"` | flexible, 24h |
#' | 7 | `"Hm"` | `"14:35"` | flexible, 24h |
#' | 8 | `"H"` | `"14"` | flexible, 24h |
#' | 9 | `"EHm"` | `"Thu 14:35"` | flexible, 24h |
#' | 10 | `"EHms"` | `"Thu 14:35:00"` | flexible, 24h |
#' | 11 | `"Hmsv"` | `"14:35:00 GMT+00:00"` | flexible, 24h |
#' | 12 | `"Hmv"` | `"14:35 GMT+00:00"` | flexible, 24h |
#' | 13 | `"hms"` | `"2:35:00 PM"` | flexible, 12h |
#' | 14 | `"hm"` | `"2:35 PM"` | flexible, 12h |
#' | 15 | `"h"` | `"2 PM"` | flexible, 12h |
#' | 16 | `"Ehm"` | `"Thu 2:35 PM"` | flexible, 12h |
#' | 17 | `"Ehms"` | `"Thu 2:35:00 PM"` | flexible, 12h |
#' | 18 | `"EBhms"` | `"Thu 2:35:00 in the afternoon"` | flexible, 12h |
#' | 19 | `"Bhms"` | `"2:35:00 in the afternoon"` | flexible, 12h |
#' | 20 | `"EBhm"` | `"Thu 2:35 in the afternoon"` | flexible, 12h |
#' | 21 | `"Bhm"` | `"2:35 in the afternoon"` | flexible, 12h |
#' | 22 | `"Bh"` | `"2 in the afternoon"` | flexible, 12h |
#' | 23 | `"hmsv"` | `"2:35:00 PM GMT+00:00"` | flexible, 12h |
#' | 24 | `"hmv"` | `"2:35 PM GMT+00:00"` | flexible, 12h |
#' | 25 | `"ms"` | `"35:00"` | flexible |
#'
#' We can call [info_time_style()] in the console to view a similar table of
#' time styles with example output.
#'
#' @section Adapting output to a specific `locale`:
#'
#' This formatting function can adapt outputs according to a provided `locale`
#' value. Examples include `"en"` for English (United States) and `"fr"` for
#' French (France). Note that a `locale` value provided here will override any
#' global locale setting performed in [gt()]'s own `locale` argument (it is
#' settable there as a value received by all other functions that have a
#' `locale` argument). As a useful reference on which locales are supported, we
#' can use [info_locales()] to view an info table.
#'
#' @section Examples:
#'
#' Let's use the [`exibble`] dataset to create a simple, two-column **gt** table
#' (keeping only the `date` and `time` columns). Format the `time` column with
#' `fmt_time()` to display times formatted with the `"h_m_s_p"` time style.
#'
#' ```r
#' exibble |>
#' dplyr::select(date, time) |>
#' gt() |>
#' fmt_time(
#' columns = time,
#' time_style = "h_m_s_p"
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_time_1.png")`
#' }}
#'
#' Again using the [`exibble`] dataset, let's format the `time` column to have
#' mixed time formats, where times after 16:00 will be different than the others
#' because of the expressions used in the `rows` argument. This will involve two
#' calls of `fmt_time()` with different statements provided for `rows`. In the
#' first call (times after 16:00) the time style `"h_m_s_p"` is used; for the
#' second call, `"h_m_p"` is the named time style supplied to `time_style`.
#'
#' ```r
#' exibble |>
#' dplyr::select(date, time) |>
#' gt() |>
#' fmt_time(
#' columns = time,
#' rows = time > "16:00",
#' time_style = "h_m_s_p"
#' ) |>
#' fmt_time(
#' columns = time,
#' rows = time <= "16:00",
#' time_style = "h_m_p"
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_time_2.png")`
#' }}
#'
#' Use the [`exibble`] dataset to create a single-column **gt** table (with only
#' the `time` column). Format the time values using the `"EBhms"` time style
#' (which is one of the 'flexible' styles). Also, we'll set the locale to `"sv"`
#' to get the times in Swedish.
#'
#' ```r
#' exibble |>
#' dplyr::select(time) |>
#' gt() |>
#' fmt_time(
#' columns = time,
#' time_style = "EBhms",
#' locale = "sv"
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_time_3.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-14
#'
#' @section Function Introduced:
#' `v0.2.0.5` (March 31, 2020)
#'
#' @seealso The vector-formatting version of this function: [vec_fmt_time()].
#'
#' @export
fmt_time <- function(
data,
columns = everything(),
rows = everything(),
time_style = "iso",
pattern = "{x}",
locale = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
#
# Begin support for `from_column()` objects passed to compatible arguments
#
# Supports parameters:
#
# - time_style
# - pattern
# - locale
arg_vals <-
mget(
get_arg_names(
function_name = "fmt_time",
all_args_except = c("data", "columns", "rows")
)
)
if (args_have_gt_column_obj(arg_vals = arg_vals)) {
# Resolve the row numbers using `resolve_vars()`
resolved_rows_idx <-
resolve_rows_i(
expr = {{ rows }},
data = data
)
param_tbl <-
generate_param_tbl(
data = data,
arg_vals = arg_vals,
resolved_rows_idx = resolved_rows_idx
)
for (i in seq_len(nrow(param_tbl))) {
p_i <- as.list(param_tbl[i, ])
data <-
fmt_time(
data = data,
columns = {{ columns }},
rows = resolved_rows_idx[i],
time_style = p_i$time_style %||% time_style,
pattern = p_i$pattern %||% pattern,
locale = p_i$locale %||% locale
)
}
return(data)
}
#
# End support for `from_column()` objects passed to compatible arguments
#
# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
validate_locale(locale = locale)
locale <- normalize_locale(locale = locale)
locale <- resolve_locale(data = data, locale = locale)
# Transform `time_style` to `time_format_str`
time_format_str <- withCallingHandlers(
get_time_format(time_style = time_style),
error = function(e) {
cli::cli_abort("Invalid time style. See {.run gt::info_time_style()}.",
parent = e)
})
valid_class <- c("Date", "POSIXt", "character")
msg <- c(i = "Allowed types are `Date`, `POSIXt`, and `character` (in
`HH:MM:SS` format).")
check_columns_valid_if_strict(data, {{ columns }}, valid_class, msg)
# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
fmt(
data = data,
columns = {{ columns }},
rows = {{ rows }},
compat = valid_class,
fns = num_fmt_factory_multi(
pattern = pattern,
use_latex_math_mode = FALSE,
format_fn = function(x, context) {
# If the incoming values are strings that adequately represent time
# values, then prepend with the `1970-01-01` dummy date to create an
# input that will works with `strftime()`
if (all(is_string_time(x))) {
x <- paste("1970-01-01", x)
}
# Convert incoming values to POSIXlt but provide a friendly error
# if the values cannot be parsed by `as.POSIXlt()`
time <-
withCallingHandlers(
as.POSIXlt(x, tz = "GMT"),
error = function(e) {
cli::cli_abort(
"One or more of the provided date/time/datetime values are invalid.",
call = call("fmt_time"),
parent = e
)
}
)
# Format the time string using `fdt()`
bigD::fdt(
input = as.character(time),
format = time_format_str,
locale = locale
)
}
)
)
}
#' Format values as datetimes
#'
#' @description
#'
#' Format input values to datetime values using either presets for the date and
#' time components or a formatting directive (this can either use a *CLDR*
#' datetime pattern or `strptime` formatting). The input values can be in the
#' form of `POSIXct` (i.e., datetimes), the `Date` type, or `character` (must be
#' in the ISO 8601 form of `YYYY-MM-DD HH:MM:SS` or `YYYY-MM-DD`).
#'
#' @inheritParams fmt_number
#'
#' @inheritParams fmt_date
#'
#' @inheritParams fmt_time
#'
#' @param sep *Separator between date and time components*
#'
#' `scalar<character>` // *default:* `" "`
#'
#' The separator string to use between the date and time components. By
#' default, this is a single space character (`" "`). Only used when not
#' specifying a `format` code.
#'
#' @param format *Date/time formatting string*
#'
#' `scalar<character>` // *default:* `NULL` (`optional`)
#'
#' An optional formatting string used for generating custom dates/times. If
#' used then the arguments governing preset styles (`date_style` and
#' `time_style`) will be ignored in favor of formatting via the `format`
#' string.
#'
#' @param tz *Time zone*
#'
#' `scalar<character>` // *default:* `NULL` (`optional`)
#'
#' The time zone for printing dates/times (i.e., the output). The
#' default of `NULL` will preserve the time zone of the input data in the
#' output. If providing a time zone, it must be one that is recognized by the
#' user's operating system (a vector of all valid `tz` values can be produced
#' with [OlsonNames()]).
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_datetime()` is compatible with body cells that are of the `"Date"`,
#' `"POSIXct"` or `"character"` types. Any other types of body cells are ignored
#' during formatting. This is to say that cells of incompatible data types may
#' be targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_datetime()` to
#' obtain varying parameter values from a specified column within the table.
#' This means that each row could be formatted a little bit differently. These
#' arguments provide support for [from_column()]:
#'
#' - `date_style`
#' - `time_style`
#' - `sep`
#' - `format`
#' - `tz`
#' - `pattern`
#' - `locale`
#'
#' Please note that for each of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Formatting with the `date_style` argument:
#'
#' We can supply a preset date style to the `date_style` argument to separately
#' handle the date portion of the output. The date styles are numerous and can
#' handle localization to any supported locale. A large segment of date styles
#' are termed flexible date formats and this means that their output will adapt
#' to any `locale` provided. That feature makes the flexible date formats a
#' better option for locales other than `"en"` (the default locale).
#'
#' The following table provides a listing of all date styles and their output
#' values (corresponding to an input date of `2000-02-29`).
#'
#' | | Date Style | Output | Notes |
#' |----|-----------------------|-------------------------|---------------|
#' | 1 | `"iso"` | `"2000-02-29"` | ISO 8601 |
#' | 2 | `"wday_month_day_year"`| `"Tuesday, February 29, 2000"` | |
#' | 3 | `"wd_m_day_year"` | `"Tue, Feb 29, 2000"` | |
#' | 4 | `"wday_day_month_year"`| `"Tuesday 29 February 2000"` | |
#' | 5 | `"month_day_year"` | `"February 29, 2000"` | |
#' | 6 | `"m_day_year"` | `"Feb 29, 2000"` | |
#' | 7 | `"day_m_year"` | `"29 Feb 2000"` | |
#' | 8 | `"day_month_year"` | `"29 February 2000"` | |
#' | 9 | `"day_month"` | `"29 February"` | |
#' | 10 | `"day_m"` | `"29 Feb"` | |
#' | 11 | `"year"` | `"2000"` | |
#' | 12 | `"month"` | `"February"` | |
#' | 13 | `"day"` | `"29"` | |
#' | 14 | `"year.mn.day"` | `"2000/02/29"` | |
#' | 15 | `"y.mn.day"` | `"00/02/29"` | |
#' | 16 | `"year_week"` | `"2000-W09"` | |
#' | 17 | `"year_quarter"` | `"2000-Q1"` | |
#' | 18 | `"yMd"` | `"2/29/2000"` | flexible |
#' | 19 | `"yMEd"` | `"Tue, 2/29/2000"` | flexible |
#' | 20 | `"yMMM"` | `"Feb 2000"` | flexible |
#' | 21 | `"yMMMM"` | `"February 2000"` | flexible |
#' | 22 | `"yMMMd"` | `"Feb 29, 2000"` | flexible |
#' | 23 | `"yMMMEd"` | `"Tue, Feb 29, 2000"` | flexible |
#' | 24 | `"GyMd"` | `"2/29/2000 A"` | flexible |
#' | 25 | `"GyMMMd"` | `"Feb 29, 2000 AD"` | flexible |
#' | 26 | `"GyMMMEd"` | `"Tue, Feb 29, 2000 AD"`| flexible |
#' | 27 | `"yM"` | `"2/2000"` | flexible |
#' | 28 | `"Md"` | `"2/29"` | flexible |
#' | 29 | `"MEd"` | `"Tue, 2/29"` | flexible |
#' | 30 | `"MMMd"` | `"Feb 29"` | flexible |
#' | 31 | `"MMMEd"` | `"Tue, Feb 29"` | flexible |
#' | 32 | `"MMMMd"` | `"February 29"` | flexible |
#' | 33 | `"GyMMM"` | `"Feb 2000 AD"` | flexible |
#' | 34 | `"yQQQ"` | `"Q1 2000"` | flexible |
#' | 35 | `"yQQQQ"` | `"1st quarter 2000"` | flexible |
#' | 36 | `"Gy"` | `"2000 AD"` | flexible |
#' | 37 | `"y"` | `"2000"` | flexible |
#' | 38 | `"M"` | `"2"` | flexible |
#' | 39 | `"MMM"` | `"Feb"` | flexible |
#' | 40 | `"d"` | `"29"` | flexible |
#' | 41 | `"Ed"` | `"29 Tue"` | flexible |
#'
#' We can call [info_date_style()] in the console to view a similar table of
#' date styles with example output.
#'
#' @section Formatting with the `time_style` argument:
#'
#' We can supply a preset time style to the `time_style` argument to separately
#' handle the time portion of the output. There are many time styles and all of
#' them can handle localization to any supported locale. Many of the time styles
#' are termed flexible time formats and this means that their output will adapt
#' to any `locale` provided. That feature makes the flexible time formats a
#' better option for locales other than `"en"` (the default locale).
#'
#' The following table provides a listing of all time styles and their output
#' values (corresponding to an input time of `14:35:00`). It is noted which of
#' these represent 12- or 24-hour time. Some of the flexible formats (those
#' that begin with `"E"`) include the day of the week. Keep this in mind
#' when pairing such `time_style` values with a `date_style` so as to avoid
#' redundant or repeating information.
#'
#' | | Time Style | Output | Notes |
#' |----|---------------|---------------------------------|---------------|
#' | 1 | `"iso"` | `"14:35:00"` | ISO 8601, 24h |
#' | 2 | `"iso-short"` | `"14:35"` | ISO 8601, 24h |
#' | 3 | `"h_m_s_p"` | `"2:35:00 PM"` | 12h |
#' | 4 | `"h_m_p"` | `"2:35 PM"` | 12h |
#' | 5 | `"h_p"` | `"2 PM"` | 12h |
#' | 6 | `"Hms"` | `"14:35:00"` | flexible, 24h |
#' | 7 | `"Hm"` | `"14:35"` | flexible, 24h |
#' | 8 | `"H"` | `"14"` | flexible, 24h |
#' | 9 | `"EHm"` | `"Thu 14:35"` | flexible, 24h |
#' | 10 | `"EHms"` | `"Thu 14:35:00"` | flexible, 24h |
#' | 11 | `"Hmsv"` | `"14:35:00 GMT+00:00"` | flexible, 24h |
#' | 12 | `"Hmv"` | `"14:35 GMT+00:00"` | flexible, 24h |
#' | 13 | `"hms"` | `"2:35:00 PM"` | flexible, 12h |
#' | 14 | `"hm"` | `"2:35 PM"` | flexible, 12h |
#' | 15 | `"h"` | `"2 PM"` | flexible, 12h |
#' | 16 | `"Ehm"` | `"Thu 2:35 PM"` | flexible, 12h |
#' | 17 | `"Ehms"` | `"Thu 2:35:00 PM"` | flexible, 12h |
#' | 18 | `"EBhms"` | `"Thu 2:35:00 in the afternoon"` | flexible, 12h |
#' | 19 | `"Bhms"` | `"2:35:00 in the afternoon"` | flexible, 12h |
#' | 20 | `"EBhm"` | `"Thu 2:35 in the afternoon"` | flexible, 12h |
#' | 21 | `"Bhm"` | `"2:35 in the afternoon"` | flexible, 12h |
#' | 22 | `"Bh"` | `"2 in the afternoon"` | flexible, 12h |
#' | 23 | `"hmsv"` | `"2:35:00 PM GMT+00:00"` | flexible, 12h |
#' | 24 | `"hmv"` | `"2:35 PM GMT+00:00"` | flexible, 12h |
#' | 25 | `"ms"` | `"35:00"` | flexible |
#'
#' We can call [info_time_style()] in the console to view a similar table of
#' time styles with example output.
#'
#' @section Formatting with a *CLDR* datetime pattern:
#'
#' We can use a *CLDR* datetime pattern with the `format` argument to create
#' a highly customized and locale-aware output. This is a character string that
#' consists of two types of elements:
#'
#' - Pattern fields, which repeat a specific pattern character one or more
#' times. These fields are replaced with date and time data when formatting.
#' The character sets of `A`-`Z` and `a`-`z` are reserved for use as pattern
#' characters.
#' - Literal text, which is output verbatim when formatting. This can include:
#' - Any characters outside the reserved character sets, including
#' spaces and punctuation.
#' - Any text between single vertical quotes (e.g., `'text'`).
#' - Two adjacent single vertical quotes (''), which represent a literal
#' single quote, either inside or outside quoted text.
#'
#' The number of pattern fields is quite sizable so let's first look at how some
#' *CLDR* datetime patterns work. We'll use the datetime string
#' `"2018-07-04T22:05:09.2358(America/Vancouver)"` for all of the examples that
#' follow.
#'
#' - `"mm/dd/y"` -> `"05/04/2018"`
#' - `"EEEE, MMMM d, y"` -> `"Wednesday, July 4, 2018"`
#' - `"MMM d E"` -> `"Jul 4 Wed"`
#' - `"HH:mm"` -> `"22:05"`
#' - `"h:mm a"` -> `"10:05 PM"`
#' - `"EEEE, MMMM d, y 'at' h:mm a"` -> `"Wednesday, July 4, 2018 at 10:05 PM"`
#'
#' Here are the individual pattern fields:
#'
#' ## Year
#'
#' ### Calendar Year
#'
#' This yields the calendar year, which is always numeric. In most cases the
#' length of the `"y"` field specifies the minimum number of digits to display,
#' zero-padded as necessary. More digits will be displayed if needed to show the
#' full year. There is an exception: `"yy"` gives use just the two low-order
#' digits of the year, zero-padded as necessary. For most use cases, `"y"` or
#' `"yy"` should be good enough.
#'
#' | Field Patterns | Output |
#' |------------------------------- |----------------------------------------|
#' | `"y"` | `"2018"` |
#' | `"yy"` | `"18"` |
#' | `"yyy"` to `"yyyyyyyyy"` | `"2018"` to `"000002018"` |
#'
#' ### Year in the Week in Year Calendar
#'
#' This is the year in 'Week of Year' based calendars in which the year
#' transition occurs on a week boundary. This may differ from calendar year
#' `"y"` near a year transition. This numeric year designation is used in
#' conjunction with pattern character `"w"` in the ISO year-week calendar as
#' defined by ISO 8601.
#'
#' | Field Patterns | Output |
#' |--------------------------------|----------------------------------------|
#' | `"Y"` | `"2018"` |
#' | `"YY"` | `"18"` |
#' | `"YYY"` to `"YYYYYYYYY"` | `"2018"` to `"000002018"` |
#'
#' ## Quarter
#'
#' ### Quarter of the Year: formatting and standalone versions
#'
#' The quarter names are identified numerically, starting at `1` and ending at
#' `4`. Quarter names may vary along two axes: the width and the context. The
#' context is either 'formatting' (taken as a default), which the form used
#' within a complete date format string, or, 'standalone', the form for date
#' elements used independently (such as in calendar headers). The standalone
#' form may be used in any other date format that shares the same form of the
#' name. Here, the formatting form for quarters of the year consists of some run
#' of `"Q"` values whereas the standalone form uses `"q"`.
#'
#' | Field Patterns | Output | Notes |
#' |-------------------|-----------------|-----------------------------------|
#' | `"Q"`/`"q"` | `"3"` | Numeric, one digit |
#' | `"QQ"`/`"qq"` | `"03"` | Numeric, two digits (zero padded) |
#' | `"QQQ"`/`"qqq"` | `"Q3"` | Abbreviated |
#' | `"QQQQ"`/`"qqqq"` | `"3rd quarter"` | Wide |
#' | `"QQQQQ"`/`"qqqqq"` | `"3"` | Narrow |
#'
#' ## Month
#'
#' ### Month: formatting and standalone versions
#'
#' The month names are identified numerically, starting at `1` and ending at
#' `12`. Month names may vary along two axes: the width and the context. The
#' context is either 'formatting' (taken as a default), which the form used
#' within a complete date format string, or, 'standalone', the form for date
#' elements used independently (such as in calendar headers). The standalone
#' form may be used in any other date format that shares the same form of the
#' name. Here, the formatting form for months consists of some run of `"M"`
#' values whereas the standalone form uses `"L"`.
#'
#' | Field Patterns | Output | Notes |
#' |-------------------|-----------------|-----------------------------------|
#' | `"M"`/`"L"` | `"7"` | Numeric, minimum digits |
#' | `"MM"`/`"LL"` | `"07"` | Numeric, two digits (zero padded) |
#' | `"MMM"`/`"LLL"` | `"Jul"` | Abbreviated |
#' | `"MMMM"`/`"LLLL"` | `"July"` | Wide |
#' | `"MMMMM"`/`"LLLLL"` | `"J"` | Narrow |
#'
#' ## Week
#'
#' ### Week of Year
#'
#' Values calculated for the week of year range from `1` to `53`. Week `1` for a
#' year is the first week that contains at least the specified minimum number of
#' days from that year. Weeks between week `1` of one year and week `1` of the
#' following year are numbered sequentially from `2` to `52` or `53` (if
#' needed).
#'
#' There are two available field lengths. Both will display the week of year
#' value but the `"ww"` width will always show two digits (where weeks `1` to
#' `9` are zero padded).
#'
#' | Field Patterns | Output | Notes |
#' |------------------|-----------|------------------------------------------|
#' | `"w"` | `"27"` | Minimum digits |
#' | `"ww"` | `"27"` | Two digits (zero padded) |
#'
#' ### Week of Month
#'
#' The week of a month can range from `1` to `5`. The first day of every month
#' always begins at week `1` and with every transition into the beginning of a
#' week, the week of month value is incremented by `1`.
#'
#' | Field Pattern | Output |
#' |------------------|------------------------------------------------------|
#' | `"W"` | `"1"` |
#'
#' ## Day
#'
#' ### Day of Month
#'
#' The day of month value is always numeric and there are two available field
#' length choices in its formatting. Both will display the day of month value
#' but the `"dd"` formatting will always show two digits (where days `1` to `9`
#' are zero padded).
#'
#' | Field Patterns | Output | Notes |
#' |----------------|-----------|--------------------------------------------|
#' | `"d"` | `"4"` | Minimum digits |
#' | `"dd"` | `"04"` | Two digits, zero padded |
#'
#' ### Day of Year
#'
#' The day of year value ranges from `1` (January 1) to either `365` or `366`
#' (December 31), where the higher value of the range indicates that the year is
#' a leap year (29 days in February, instead of 28). The field length specifies
#' the minimum number of digits, with zero-padding as necessary.
#'
#' | Field Patterns | Output | Notes |
#' |-----------------|----------|--------------------------------------------|
#' | `"D"` | `"185"` | |
#' | `"DD"` | `"185"` | Zero padded to minimum width of 2 |
#' | `"DDD"` | `"185"` | Zero padded to minimum width of 3 |
#'
#' ### Day of Week in Month
#'
#' The day of week in month returns a numerical value indicating the number of
#' times a given weekday had occurred in the month (e.g., '2nd Monday in
#' March'). This conveniently resolves to predicable case structure where ranges
#' of day of the month values return predictable day of week in month values:
#'
#' - days `1` - `7` -> `1`
#' - days `8` - `14` -> `2`
#' - days `15` - `21` -> `3`
#' - days `22` - `28` -> `4`
#' - days `29` - `31` -> `5`
#'
#' | Field Pattern | Output |
#' |--------------------------------|----------------------------------------|
#' | `"F"` | `"1"` |
#'
#' ### Modified Julian Date
#'
#' The modified version of the Julian date is obtained by subtracting
#' 2,400,000.5 days from the Julian date (the number of days since January 1,
#' 4713 BC). This essentially results in the number of days since midnight
#' November 17, 1858. There is a half day offset (unlike the Julian date, the
#' modified Julian date is referenced to midnight instead of noon).
#'
#' | Field Patterns | Output |
#' |--------------------------------|----------------------------------------|
#' | `"g"` to `"ggggggggg"` | `"58303"` -> `"000058303"` |
#'
#' ## Weekday
#'
#' ### Day of Week Name
#'
#' The name of the day of week is offered in four different widths.
#'
#' | Field Patterns | Output | Notes |
#' |----------------------------|----------------|---------------------------|
#' | `"E"`, `"EE"`, or `"EEE"` | `"Wed"` | Abbreviated |
#' | `"EEEE"` | `"Wednesday"` | Wide |
#' | `"EEEEE"` | `"W"` | Narrow |
#' | `"EEEEEE"` | `"We"` | Short |
#'
#' ## Periods
#'
#' ### AM/PM Period of Day
#'
#' This denotes before noon and after noon time periods. May be upper or
#' lowercase depending on the locale and other options. The wide form may be
#' the same as the short form if the 'real' long form (e.g. 'ante meridiem') is
#' not customarily used. The narrow form must be unique, unlike some other
#' fields.
#'
#' | Field Patterns | Output | Notes |
#' |--------------------------------|----------|-----------------------------|
#' | `"a"`, `"aa"`, or `"aaa"` | `"PM"` | Abbreviated |
#' | `"aaaa"` | `"PM"` | Wide |
#' | `"aaaaa"` | `"p"` | Narrow |
#'
#' ### AM/PM Period of Day Plus Noon and Midnight
#'
#' Provide AM and PM as well as phrases for exactly noon and midnight. May be
#' upper or lowercase depending on the locale and other options. If the locale
#' doesn't have the notion of a unique 'noon' (i.e., 12:00), then the PM form
#' may be substituted. A similar behavior can occur for 'midnight' (00:00) and
#' the AM form. The narrow form must be unique, unlike some other fields.
#'
#' (a) `input_midnight`: `"2020-05-05T00:00:00"`
#' (b) `input_noon`: `"2020-05-05T12:00:00"`
#'
#' | Field Patterns | Output | Notes |
#' |--------------------------------|--------------------|-------------------|
#' | `"b"`, `"bb"`, or `"bbb"` | (a) `"midnight"` | Abbreviated |
#' | | (b) `"noon"` | |
#' | `"bbbb"` | (a) `"midnight"` | Wide |
#' | | (b) `"noon"` | |
#' | `"bbbbb"` | (a) `"mi"` | Narrow |
#' | | (b) `"n"` | |
#'
#' ### Flexible Day Periods
#'
#' Flexible day periods denotes things like 'in the afternoon', 'in the
#' evening', etc., and the flexibility comes from a locale's language and
#' script. Each locale has an associated rule set that specifies when the day
#' periods start and end for that locale.
#'
#' (a) `input_morning`: `"2020-05-05T00:08:30"`
#' (b) `input_afternoon`: `"2020-05-05T14:00:00"`
#'
#' | Field Patterns | Output | Notes |
#' |----------------------------|--------------------------|-----------------|
#' | `"B"`, `"BB"`, or `"BBB"` | (a) `"in the morning"` | Abbreviated |
#' | | (b) `"in the afternoon"` | |
#' | `"BBBB"` | (a) `"in the morning"` | Wide |
#' | | (b) `"in the afternoon"` | |
#' | `"BBBBB"` | (a) `"in the morning"` | Narrow |
#' | | (b) `"in the afternoon"` | |
#'
#' ## Hours, Minutes, and Seconds
#'
#' ### Hour 0-23
#'
#' Hours from `0` to `23` are for a standard 24-hour clock cycle (midnight plus
#' 1 minute is `00:01`) when using `"HH"` (which is the more common width that
#' indicates zero-padding to 2 digits).
#'
#' Using `"2015-08-01T08:35:09"`:
#'
#' | Field Patterns | Output | Notes |
#' |------------------------|---------|--------------------------------------|
#' | `"H"` | `"8"` | Numeric, minimum digits |
#' | `"HH"` | `"08"` | Numeric, 2 digits (zero padded) |
#'
#' ### Hour 1-12
#'
#' Hours from `1` to `12` are for a standard 12-hour clock cycle (midnight plus
#' 1 minute is `12:01`) when using `"hh"` (which is the more common width that
#' indicates zero-padding to 2 digits).
#'
#' Using `"2015-08-01T08:35:09"`:
#'
#' | Field Patterns | Output | Notes |
#' |------------------------|---------|--------------------------------------|
#' | `"h"` | `"8"` | Numeric, minimum digits |
#' | `"hh"` | `"08"` | Numeric, 2 digits (zero padded) |
#'
#' ### Hour 1-24
#'
#' Using hours from `1` to `24` is a less common way to express a 24-hour clock
#' cycle (midnight plus 1 minute is `24:01`) when using `"kk"` (which is the
#' more common width that indicates zero-padding to 2 digits).
#'
#' Using `"2015-08-01T08:35:09"`:
#'
#' | Field Patterns | Output | Notes |
#' |------------------------|---------|--------------------------------------|
#' | `"k"` | `"9"` | Numeric, minimum digits |
#' | `"kk"` | `"09"` | Numeric, 2 digits (zero padded) |
#'
#' ### Hour 0-11
#'
#' Using hours from `0` to `11` is a less common way to express a 12-hour clock
#' cycle (midnight plus 1 minute is `00:01`) when using `"KK"` (which is the
#' more common width that indicates zero-padding to 2 digits).
#'
#' Using `"2015-08-01T08:35:09"`:
#'
#' | Field Patterns | Output | Notes |
#' |------------------------|---------|--------------------------------------|
#' | `"K"` | `"7"` | Numeric, minimum digits |
#' | `"KK"` | `"07"` | Numeric, 2 digits (zero padded) |
#'
#' ### Minute
#'
#' The minute of the hour which can be any number from `0` to `59`. Use `"m"` to
#' show the minimum number of digits, or `"mm"` to always show two digits
#' (zero-padding, if necessary).
#'
#' | Field Patterns | Output | Notes |
#' |------------------------|---------|--------------------------------------|
#' | `"m"` | `"5"` | Numeric, minimum digits |
#' | `"mm"` | `"06"` | Numeric, 2 digits (zero padded) |
#'
#' ### Seconds
#'
#' The second of the minute which can be any number from `0` to `59`. Use `"s"`
#' to show the minimum number of digits, or `"ss"` to always show two digits
#' (zero-padding, if necessary).
#'
#' | Field Patterns | Output | Notes |
#' |------------------------|---------|--------------------------------------|
#' | `"s"` | `"9"` | Numeric, minimum digits |
#' | `"ss"` | `"09"` | Numeric, 2 digits (zero padded) |
#'
#' ### Fractional Second
#'
#' The fractional second truncates (like other time fields) to the width
#' requested (i.e., count of letters). So using pattern `"SSSS"` will display
#' four digits past the decimal (which, incidentally, needs to be added manually
#' to the pattern).
#'
#' | Field Patterns | Output |
#' |--------------------------------|----------------------------------------|
#' | `"S"` to `"SSSSSSSSS"` | `"2"` -> `"235000000"` |
#'
#' ### Milliseconds Elapsed in Day
#'
#' There are 86,400,000 milliseconds in a day and the `"A"` pattern will provide
#' the whole number. The width can go up to nine digits with `"AAAAAAAAA"` and
#' these higher field widths will result in zero padding if necessary.
#'
#' Using `"2011-07-27T00:07:19.7223"`:
#'
#' | Field Patterns | Output |
#' |--------------------------------|----------------------------------------|
#' | `"A"` to `"AAAAAAAAA"` | `"439722"` -> `"000439722"` |
#'
#' ## Era
#'
#' ### The Era Designator
#'
#' This provides the era name for the given date. The Gregorian calendar has two
#' eras: AD and BC. In the AD year numbering system, AD 1 is immediately
#' preceded by 1 BC, with nothing in between them (there was no year zero).
#'
#' | Field Patterns | Output | Notes |
#' |--------------------------------|-----------------|----------------------|
#' | `"G"`, `"GG"`, or `"GGG"` | `"AD"` | Abbreviated |
#' | `"GGGG"` | `"Anno Domini"` | Wide |
#' | `"GGGGG"` | `"A"` | Narrow |
#'
#' ## Time Zones
#'
#' ### TZ // Short and Long Specific non-Location Format
#'
#' The short and long specific non-location formats for time zones are suggested
#' for displaying a time with a user friendly time zone name. Where the short
#' specific format is unavailable, it will fall back to the short localized GMT
#' format (`"O"`). Where the long specific format is unavailable, it will fall
#' back to the long localized GMT format (`"OOOO"`).
#'
#' | Field Patterns | Output | Notes |
#' |----------------------------|---------------------------|----------------|
#' | `"z"`, `"zz"`, or `"zzz"` | `"PDT"` | Short Specific |
#' | `"zzzz"` | `"Pacific Daylight Time"` | Long Specific |
#'
#' ### TZ // Common UTC Offset Formats
#'
#' The ISO8601 basic format with hours, minutes and optional seconds fields is
#' represented by `"Z"`, `"ZZ"`, or `"ZZZ"`. The format is equivalent to RFC 822
#' zone format (when the optional seconds field is absent). This is equivalent
#' to the `"xxxx"` specifier. The field pattern `"ZZZZ"` represents the long
#' localized GMT format. This is equivalent to the `"OOOO"` specifier. Finally,
#' `"ZZZZZ"` pattern yields the ISO8601 extended format with hours, minutes and
#' optional seconds fields. The ISO8601 UTC indicator `Z` is used when local
#' time offset is `0`. This is equivalent to the `"XXXXX"` specifier.
#'
#' | Field Patterns | Output | Notes |
#' |----------------------------|--------------|-----------------------------|
#' | `"Z"`, `"ZZ"`, or `"ZZZ"` | `"-0700"` | ISO 8601 basic format |
#' | `"ZZZZ"` | `"GMT-7:00"` | Long localized GMT format |
#' | `"ZZZZZ"` | `"-07:00"` | ISO 8601 extended format |
#'
#' ### TZ // Short and Long Localized GMT Formats
#'
#' The localized GMT formats come in two widths `"O"` (which removes the minutes
#' field if it's `0`) and `"OOOO"` (which always contains the minutes field).
#' The use of the `GMT` indicator changes according to the locale.
#'
#' | Field Patterns | Output | Notes |
#' |-------------------------|---------------|-------------------------------|
#' | `"O"` | `"GMT-7"` | Short localized GMT format |
#' | `"OOOO"` | `"GMT-07:00"` | Long localized GMT format |
#'
#' ### TZ // Short and Long Generic non-Location Formats
#'
#' The generic non-location formats are useful for displaying a recurring wall
#' time (e.g., events, meetings) or anywhere people do not want to be overly
#' specific. Where either of these is unavailable, there is a fallback to the
#' generic location format (`"VVVV"`), then the short localized GMT format as
#' the final fallback.
#'
#' | Field Patterns | Output | Notes |
#' |-----------------|------------------|------------------------------------|
#' | `"v"` | `"PT"` | Short generic non-location format |
#' | `"vvvv"` | `"Pacific Time"` | Long generic non-location format |
#'
#' ### TZ // Short Time Zone IDs and Exemplar City Formats
#'
#' These formats provide variations of the time zone ID and often include the
#' exemplar city. The widest of these formats, `"VVVV"`, is useful for
#' populating a choice list for time zones, because it supports 1-to-1 name/zone
#' ID mapping and is more uniform than other text formats.
#'
#' | Field Patterns | Output | Notes |
#' |--------------------|-----------------------|----------------------------|
#' | `"V"` | `"cavan"` | Short time zone ID |
#' | `"VV"` | `"America/Vancouver"` | Long time zone ID |
#' | `"VVV"` | `"Vancouver"` | The tz exemplar city |
#' | `"VVVV"` | `"Vancouver Time"` | Generic location format |
#'
#' ### TZ // ISO 8601 Formats with Z for +0000
#'
#' The `"X"`-`"XXX"` field patterns represent valid ISO 8601 patterns for time
#' zone offsets in datetimes. The final two widths, `"XXXX"` and `"XXXXX"` allow
#' for optional seconds fields. The seconds field is *not* supported by the ISO
#' 8601 specification. For all of these, the ISO 8601 UTC indicator `Z` is used
#' when the local time offset is `0`.
#'
#' | Field Patterns | Output | Notes |
#' |----------------|------------|-------------------------------------------|
#' | `"X"` | `"-07"` | ISO 8601 basic format (h, optional m) |
#' | `"XX"` | `"-0700"` | ISO 8601 basic format (h & m) |
#' | `"XXX"` | `"-07:00"` | ISO 8601 extended format (h & m) |
#' | `"XXXX"` | `"-0700"` | ISO 8601 basic format (h & m, optional s) |
#' | `"XXXXX"` | `"-07:00"` | ISO 8601 extended format (h & m, optional s) |
#'
#' ### TZ // ISO 8601 Formats (no use of Z for +0000)
#'
#' The `"x"`-`"xxxxx"` field patterns represent valid ISO 8601 patterns for time
#' zone offsets in datetimes. They are similar to the `"X"`-`"XXXXX"` field
#' patterns except that the ISO 8601 UTC indicator `Z` *will not* be used when
#' the local time offset is `0`.
#'
#' | Field Patterns | Output | Notes |
#' |----------------|------------|-------------------------------------------|
#' | `"x"` | `"-07"` | ISO 8601 basic format (h, optional m) |
#' | `"xx"` | `"-0700"` | ISO 8601 basic format (h & m) |
#' | `"xxx"` | `"-07:00"` | ISO 8601 extended format (h & m) |
#' | `"xxxx"` | `"-0700"` | ISO 8601 basic format (h & m, optional s) |
#' | `"xxxxx"` | `"-07:00"` | ISO 8601 extended format (h & m, optional s) |
#'
#' @section Formatting with a `strptime` format code:
#'
#' Performing custom date/time formatting with the `format` argument can also
#' occur with a `strptime` format code. This works by constructing a string of
#' individual format codes representing formatted date and time elements. These
#' are all indicated with a leading `%`, literal characters are interpreted as
#' any characters not starting with a `%` character.
#'
#' First off, let's look at a few format code combinations that work well
#' together as a `strptime` format. This will give us an intuition on how these
#' generally work. We'll use the datetime `"2015-06-08 23:05:37.48"` for all of
#' the examples that follow.
#'
#' - `"%m/%d/%Y"` -> `"06/08/2015"`
#' - `"%A, %B %e, %Y"` -> `"Monday, June 8, 2015"`
#' - `"%b %e %a"` -> `"Jun 8 Mon"`
#' - `"%H:%M"` -> `"23:05"`
#' - `"%I:%M %p"` -> `"11:05 pm"`
#' - `"%A, %B %e, %Y at %I:%M %p"` -> `"Monday, June 8, 2015 at 11:05 pm"`
#'
#' Here are the individual format codes for the date components:
#'
#' - `"%a"` -> `"Mon"` (abbreviated day of week name)
#' - `"%A"` -> `"Monday"` (full day of week name)
#' - `"%w"` -> `"1"` (day of week number in `0..6`; Sunday is `0`)
#' - `"%u"` -> `"1"` (day of week number in `1..7`; Monday is `1`, Sunday `7`)
#' - `"%y"` -> `"15"` (abbreviated year, using the final two digits)
#' - `"%Y"` -> `"2015"` (full year)
#' - `"%b"` -> `"Jun"` (abbreviated month name)
#' - `"%B"` -> `"June"` (full month name)
#' - `"%m"` -> `"06"` (month number)
#' - `"%d"` -> `"08"` (day number, zero-padded)
#' - `"%e"` -> `"8"` (day number without zero padding)
#' - `"%j"` -> `"159"` (day of the year, always zero-padded)
#' - `"%W"` -> `"23"` (week number for the year, always zero-padded)
#' - `"%V"` -> `"24"` (week number for the year, following the ISO 8601
#' standard)
#' - `"%C"` -> `"20"` (the century number)
#'
#' Here are the individual format codes for the time components:
#'
#' - `"%H"` -> `"23"` (24h hour)
#' - `"%I"` -> `"11"` (12h hour)
#' - `"%M"` -> `"05"` (minute)
#' - `"%S"` -> `"37"` (second)
#' - `"%OS3"` -> `"37.480"` (seconds with decimals; `3` decimal places here)
#' - `%p` -> `"pm"` (AM or PM indicator)
#'
#' Here are some extra formats that you may find useful:
#'
#' - `"%z"` -> `"+0000"` (signed time zone offset, here using UTC)
#' - `"%F"` -> `"2015-06-08"` (the date in the ISO 8601 date format)
#' - `"%%"` -> `"%"` (the literal "`%`" character, in case you need it)
#'
#' @section Adapting output to a specific `locale`:
#'
#' This formatting function can adapt outputs according to a provided `locale`
#' value. Examples include `"en"` for English (United States) and `"fr"` for
#' French (France). Note that a `locale` value provided here will override any
#' global locale setting performed in [gt()]'s own `locale` argument (it is
#' settable there as a value received by all other functions that have a
#' `locale` argument). As a useful reference on which locales are supported, we
#' can call [info_locales()] to view an info table.
#'
#' @section Examples:
#'
#' Use the [`exibble`] dataset to create a single-column **gt** table (with only
#' the `datetime` column). With `fmt_datetime()` we'll format the `datetime`
#' column to have dates formatted with the `"month_day_year"` style and times
#' with the `"h_m_s_p"` 12-hour time style.
#'
#' ```r
#' exibble |>
#' dplyr::select(datetime) |>
#' gt() |>
#' fmt_datetime(
#' date_style = "month_day_year",
#' time_style = "h_m_s_p"
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_datetime_1.png")`
#' }}
#'
#' Using the same input table, we can use `fmt_datetime()` with flexible date
#' and time styles. Two that work well together are `"MMMEd"` and `"Hms"`. These
#' date and time styles will, being flexible, create outputs that conform to the
#' locale value given to the `locale` argument. Let's use two calls of
#' `fmt_datetime()`: the first will format all rows in `datetime` to the Danish
#' locale (with `locale = "da"`) and the second call will target the first three
#' rows with the same formatting, but in the default locale (which is `"en"`).
#'
#' ```r
#' exibble |>
#' dplyr::select(datetime) |>
#' gt() |>
#' fmt_datetime(
#' date_style = "MMMEd",
#' time_style = "Hms",
#' locale = "da"
#' ) |>
#' fmt_datetime(
#' rows = 1:3,
#' date_style = "MMMEd",
#' time_style = "Hms"
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_datetime_2.png")`
#' }}
#'
#' It's possible to use the `format` argument and write our own formatting
#' specification. Using the CLDR datetime pattern
#' `"EEEE, MMMM d, y 'at' h:mm a (zzzz)"` gives us datetime outputs with time
#' zone formatting. Let's provide a time zone ID (`"America/Vancouver"`) to the
#' `tz` argument.
#'
#' ```r
#' exibble |>
#' dplyr::select(datetime) |>
#' gt() |>
#' fmt_datetime(
#' format = "EEEE, MMMM d, y 'at' h:mm a (zzzz)",
#' tz = "America/Vancouver"
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_datetime_3.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-15
#'
#' @section Function Introduced:
#' `v0.2.0.5` (March 31, 2020)
#'
#' @seealso The vector-formatting version of this function:
#' [vec_fmt_datetime()].
#'
#' @export
fmt_datetime <- function(
data,
columns = everything(),
rows = everything(),
date_style = "iso",
time_style = "iso",
sep = " ",
format = NULL,
tz = NULL,
pattern = "{x}",
locale = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
#
# Begin support for `from_column()` objects passed to compatible arguments
#
# Supports parameters:
#
# - date_style
# - time_style
# - sep
# - format
# - tz
# - pattern
# - locale
arg_vals <-
mget(
get_arg_names(
function_name = "fmt_datetime",
all_args_except = c("data", "columns", "rows")
)
)
if (args_have_gt_column_obj(arg_vals = arg_vals)) {
# Resolve the row numbers using `resolve_vars()`
resolved_rows_idx <-
resolve_rows_i(
expr = {{ rows }},
data = data
)
param_tbl <-
generate_param_tbl(
data = data,
arg_vals = arg_vals,
resolved_rows_idx = resolved_rows_idx
)
for (i in seq_len(nrow(param_tbl))) {
p_i <- as.list(param_tbl[i, ])
data <-
fmt_datetime(
data = data,
columns = {{ columns }},
rows = resolved_rows_idx[i],
date_style = p_i$date_style %||% date_style,
time_style = p_i$time_style %||% time_style,
sep = p_i$sep %||% sep,
format = p_i$format %||% format,
tz = p_i$tz %||% tz,
pattern = p_i$pattern %||% pattern,
locale = p_i$locale %||% locale
)
}
return(data)
}
#
# End support for `from_column()` objects passed to compatible arguments
#
# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
validate_locale(locale = locale)
locale <- normalize_locale(locale = locale)
locale <- resolve_locale(data = data, locale = locale)
if (!is.null(format)) {
# Ensure that the format code meets some basic validation requirements
check_string(format)
} else {
# Transform `date_style` to `date_format_str`
date_format_str <- withCallingHandlers(
get_date_format(date_style = date_style),
error = function(e) {
cli::cli_abort("Invalid date style. See {.run gt::info_date_style()} for valid inputs.",
parent = e)
})
# Transform `time_style` to `time_format_str`
time_format_str <- withCallingHandlers(
get_time_format(time_style = time_style),
error = function(e) {
cli::cli_abort("Invalid time style. See {.run gt::info_time_style()} for valid inputs.",
parent = e)
})
}
valid_class <- c("Date", "POSIXct", "character")
msg <- c(i = "Allowed types are `Date`, `POSIXct`, and `character` (with
ISO-8601 formatted dates)")
check_columns_valid_if_strict(data, {{ columns }}, valid_class, msg)
# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
fmt(
data = data,
columns = {{ columns }},
rows = {{ rows }},
compat = valid_class,
fns = num_fmt_factory_multi(
pattern = pattern,
use_latex_math_mode = FALSE,
format_fn = function(x, context) {
# If a format string is provided then use that to generate the
# formatted date/time string
if (!is.null(format)) {
# If the incoming values are strings that adequately represent time
# values, then prepend with the `1970-01-01` dummy date to create an
# input that will works with `strftime()`
if (all(is_string_time(x))) {
x <- paste("1970-01-01", x)
}
if (grepl("%", format)) {
if (is.character(x)) {
tz <- tz %||% "GMT"
datetime <-
withCallingHandlers(
as.POSIXlt(x),
error = function(e) {
cli::cli_abort(
"One or more of the provided date/datetime values are invalid.",
call = call("fmt_datetime"),
parent = e
)
}
)
attr(datetime, which = "tzone") <- tz
datetime <- as.POSIXct(datetime)
return(strftime(datetime, format = format, tz = tz))
}
# Format the datetime values using `strftime()`
return(strftime(x, format = format, tz = tz))
} else {
tz <- tz %||% "UTC"
dt_str <- strftime(x, format = "%Y-%m-%dT%H:%M:%S%z", tz = tz)
if ("tzone" %in% names(attributes(x))) {
tzone <- attr(x, which = "tzone", exact = TRUE)
dt_str <- paste0(dt_str, "(", tzone, ")")
}
# Format the datetime values using `fdt()`
return(
bigD::fdt(
input = dt_str,
format = format,
locale = locale,
use_tz = tz
)
)
}
}
#
# Format the date and time portions of the datetime value
#
# Convert incoming values to POSIXlt but provide a friendly error
# if the values cannot be parsed by `as.POSIXlt()`
datetime <-
withCallingHandlers(
as.POSIXlt(x),
error = function(e) {
cli::cli_abort(
# possibly Error in `fmt()` caused by error in `as.POSIXlt`
"One or more of the provided date/datetime values are invalid.",
call = call("fmt_datetime"),
parent = e
)
}
)
#
# Separately format the date and time portions using `fdt()`
#
date_str <-
bigD::fdt(
input = as.character(datetime),
format = date_format_str,
locale = locale
)
time_str <-
bigD::fdt(
input = as.character(datetime),
format = time_format_str,
locale = locale
)
paste0(date_str, sep, time_str)
}
)
)
}
#' Format numeric or duration values as styled time duration strings
#'
#' @description
#'
#' Format input values to time duration values whether those input values are
#' numbers or of the `difftime` class. We can specify which time units any
#' numeric input values have (as weeks, days, hours, minutes, or seconds) and
#' the output can be customized with a duration style (corresponding to narrow,
#' wide, colon-separated, and ISO forms) and a choice of output units ranging
#' from weeks to seconds.
#'
#' @section Output units for the colon-separated duration style:
#'
#' The colon-separated duration style (enabled when
#' `duration_style = "colon-sep"`) is essentially a clock-based output format
#' which uses the display logic of chronograph watch functionality. It will, by
#' default, display duration values in the `(D/)HH:MM:SS` format. Any duration
#' values greater than or equal to 24 hours will have the number of days
#' prepended with an adjoining slash mark. While this output format is
#' versatile, it can be changed somewhat with the `output_units` option. The
#' following combinations of output units are permitted:
#'
#' - `c("minutes", "seconds")` -> `MM:SS`
#' - `c("hours", "minutes")` -> `HH:MM`
#' - `c("hours", "minutes", "seconds")` -> `HH:MM:SS`
#' - `c("days", "hours", "minutes")` -> `(D/)HH:MM`
#'
#' Any other specialized combinations will result in the default set being used,
#' which is `c("days", "hours", "minutes", "seconds")`
#'
#' @inheritParams fmt_number
#'
#' @param input_units *Declaration of duration units for numerical values*
#'
#' `scalar<character>` // *default:* `NULL` (`optional`)
#'
#' If one or more selected columns contains numeric values (not `difftime`
#' values, which contain the duration units), a keyword must be provided for
#' `input_units` for **gt** to determine how those values are to be
#' interpreted in terms of duration. The accepted units are: `"seconds"`,
#' `"minutes"`, `"hours"`, `"days"`, and `"weeks"`.
#'
#' @param output_units *Choice of output units*
#'
#' `mult-kw:[weeks|days|hours|minutes|seconds]` // *default:* `NULL` (`optional`)
#'
#' Controls the output time units. The default, `NULL`, means that **gt** will
#' automatically choose time units based on the input duration value. To
#' control which time units are to be considered for output (before trimming
#' with `trim_zero_units`) we can specify a vector of one or more of the
#' following keywords: `"weeks"`, `"days"`, `"hours"`, `"minutes"`, or
#' `"seconds"`.
#'
#' @param duration_style *Style for representing duration values*
#'
#' `singl-kw:[narrow|wide|colon-sep|iso]` // *default:* `"narrow"`
#'
#' A choice of four formatting styles for the output duration values. With
#' `"narrow"` (the default style), duration values will be formatted with
#' single letter time-part units (e.g., 1.35 days will be styled as
#' `"1d 8h 24m"`). With `"wide"`, this example value will be expanded to
#' `"1 day 8 hours 24 minutes"` after formatting. The `"colon-sep"` style will
#' put days, hours, minutes, and seconds in the `"([D]/)[HH]:[MM]:[SS]"`
#' format. The `"iso"` style will produce a value that conforms to the ISO
#' 8601 rules for duration values (e.g., 1.35 days will become `"P1DT8H24M"`).
#'
#' @param trim_zero_units *Trimming of zero values*
#'
#' `scalar<logical>|mult-kw:[leading|trailing|internal]` // *default:* `TRUE`
#'
#' Provides methods to remove output time units that have zero values. By
#' default this is `TRUE` and duration values that might otherwise be
#' formatted as `"0w 1d 0h 4m 19s"` with `trim_zero_units = FALSE` are instead
#' displayed as `"1d 4m 19s"`. Aside from using `TRUE`/`FALSE` we could
#' provide a vector of keywords for more precise control. These keywords are:
#' (1) `"leading"`, to omit all leading zero-value time units (e.g., `"0w 1d"`
#' -> `"1d"`), (2) `"trailing"`, to omit all trailing zero-value time units
#' (e.g., `"3d 5h 0s"` -> `"3d 5h"`), and `"internal"`, which removes all
#' internal zero-value time units (e.g., `"5d 0h 33m"` -> `"5d 33m"`).
#'
#' @param max_output_units *Maximum number of time units to display*
#'
#' `scalar<numeric|integer>(val>=1)` // *default:* `NULL` (`optional`)
#'
#' If `output_units` is `NULL`, where the output time units are unspecified
#' and left to **gt** to handle, a numeric value provided for
#' `max_output_units` will be taken as the maximum number of time units to
#' display in all output time duration values. By default, this is `NULL` and
#' all possible time units will be displayed. This option has no effect when
#' `duration_style = "colon-sep"` (only `output_units` can be used to
#' customize that type of duration output).
#'
#' @param force_sign *Forcing the display of a positive sign*
#'
#' `scalar<logical>` // *default:* `FALSE`
#'
#' Should the positive sign be shown for positive values (effectively showing
#' a sign for all values except zero)? If so, use `TRUE` for this option. By
#' default only negative values will display a minus sign.
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_duration()` is compatible with body cells that are of the `"numeric"`,
#' `"integer"`, or `"difftime"` types. Any other types of body cells are ignored
#' during formatting. This is to say that cells of incompatible data types may
#' be targeted, but there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Adapting output to a specific `locale`:
#'
#' This formatting function can adapt outputs according to a provided `locale`
#' value. Examples include `"en"` for English (United States) and `"fr"` for
#' French (France). The use of a valid locale ID here means separator and
#' decimal marks will be correct for the given locale. Should any value be
#' provided in `sep_mark`, it will be overridden by the locale's preferred
#' values.
#'
#' Note that a `locale` value provided here will override any global locale
#' setting performed in [gt()]'s own `locale` argument (it is settable there as
#' a value received by all other functions that have a `locale` argument). As a
#' useful reference on which locales are supported, we can call [info_locales()]
#' to view an info table.
#'
#' @section Examples:
#'
#' Use part of the `sp500` table to create a **gt** table. Create a
#' `difftime`-based column and format the duration values to be displayed as the
#' number of days since March 30, 2020.
#'
#' ```r
#' sp500 |>
#' dplyr::slice_head(n = 10) |>
#' dplyr::mutate(
#' time_point = lubridate::ymd("2020-03-30"),
#' time_passed = difftime(time_point, date)
#' ) |>
#' dplyr::select(time_passed, open, close) |>
#' gt(rowname_col = "month") |>
#' fmt_duration(
#' columns = time_passed,
#' output_units = "days",
#' duration_style = "wide"
#' ) |>
#' fmt_currency(columns = c(open, close))
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_duration_1.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-16
#'
#' @section Function Introduced:
#' `v0.7.0` (Aug 25, 2022)
#'
#' @seealso The vector-formatting version of this function:
#' [vec_fmt_duration()].
#'
#' @export
fmt_duration <- function(
data,
columns = everything(),
rows = everything(),
input_units = NULL,
output_units = NULL,
duration_style = c("narrow", "wide", "colon-sep", "iso"),
trim_zero_units = TRUE,
max_output_units = NULL,
pattern = "{x}",
use_seps = TRUE,
sep_mark = ",",
force_sign = FALSE,
system = c("intl", "ind"),
locale = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
# Ensure that arguments are matched
duration_style <- rlang::arg_match(duration_style)
system <- rlang::arg_match(system)
check_chr_has_length(output_units, allow_null = TRUE, allow_0 = FALSE)
check_chr_has_length(input_units, allow_null = TRUE, allow_0 = FALSE)
# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
validate_locale(locale = locale)
locale <- normalize_locale(locale = locale)
locale <- resolve_locale(data = data, locale = locale)
# Duration values will never have decimal marks
dec_mark <- "unused"
# Use locale-based marks if a locale ID is provided
sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps)
if (isTRUE(trim_zero_units)) {
trim_zero_units <- c("leading", "trailing", "internal")
} else if (isFALSE(trim_zero_units)) {
trim_zero_units <- NULL
} else if (is.character(trim_zero_units) && length(trim_zero_units) > 0) {
# Validate that `trim_zero_units` contains only the allowed keywords
rlang::arg_match(trim_zero_units, c("leading", "trailing", "internal"), multiple = TRUE)
} else {
cli::cli_abort(c(
"The value provided for `trim_zero_units` is invalid. Either use:",
"*" = "`TRUE` or `FALSE`, or",
"*" = "A vector with any of the keywords \"leading\", \"trailing\", or \"internal\"."
))
}
check_number_whole(max_output_units, min = 1, allow_null = TRUE, allow_infinite = TRUE)
valid_class <- c("numeric", "integer", "difftime")
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
# Stop function if any columns have numeric data and `input_units` is NULL
if (
is.null(input_units) &&
!column_classes_are_valid(
data = data,
columns = {{ columns }},
valid_classes = "difftime"
)
) {
cli::cli_abort(c(
"{.arg input_units} must be supplied when there are numeric columns to format.",
"i" = "Use one of \"seconds\", \"minutes\", \"hours\", \"days\", or \"weeks\"."
))
}
# Initialize `colon_sep_params` list
colon_sep_params <- list()
# Resolve input units
if (!is.null(input_units)) {
# Stop function if `input_units` isn't a character vector, isn't of
# the right length (1 or greater), and does not contain valid values
validate_duration_input_units(input_units = input_units)
# Normalize the valid set of provided `input_units`
input_units <- normalize_duration_input_units(input_units = input_units)
}
# Resolve output units
output_units <- output_units %||% c("weeks", "days", "hours", "minutes", "seconds")
# Stop function if `output_units` isn't a character vector, isn't of
# the right length (1 or greater), and does not contain valid values
output_units <- rlang::arg_match(
output_units,
values = c("weeks", "days", "hours", "mins", "minutes", "secs", "seconds"),
# Error message will naturally improve as r-lib/rlang#1682 is fixed
multiple = TRUE
)
# Normalize the valid set of provided `output_units`
output_units <- normalize_duration_output_units(output_units = output_units)
# If `duration_style` is of the "iso" or "colon-sep" types, then
# some options need to be overridden
if (duration_style == "iso") {
output_units <- c("days", "hours", "minutes", "seconds")
max_output_units <- NULL
trim_zero_units <- c("leading", "trailing")
}
if (duration_style == "colon-sep") {
if (
any(
identical(output_units, c("minutes", "seconds")),
identical(output_units, c("hours", "minutes")),
identical(output_units, c("hours", "minutes", "seconds")),
identical(output_units, c("days", "hours", "minutes"))
)
) {
colon_sep_output_units <- output_units
} else {
colon_sep_output_units <- c("days", "hours", "minutes", "seconds")
}
output_units <- c("days", "hours", "minutes", "seconds")
if (identical(trim_zero_units, "leading")) {
colon_sep_trim_zero_units <- "leading"
} else {
colon_sep_trim_zero_units <- FALSE
}
colon_sep_params <-
list(
output_units = colon_sep_output_units,
trim_zero_units = colon_sep_trim_zero_units
)
trim_zero_units <- FALSE
max_output_units <- NULL
}
# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
fmt(
data = data,
columns = {{ columns }},
rows = {{ rows }},
compat = valid_class,
fns = num_fmt_factory_multi(
pattern = pattern,
use_latex_math_mode = FALSE,
format_fn = function(x, context) {
if (duration_style %in% c("narrow", "wide")) {
patterns <-
get_localized_duration_patterns(
value = x,
type = duration_style,
locale = locale
)
} else {
patterns <- NULL
}
x_str <-
values_to_durations(
x,
in_units = input_units,
out_units = output_units,
out_style = duration_style,
trim_zero_units = trim_zero_units,
max_output_units = max_output_units,
colon_sep_params = colon_sep_params,
sep_mark = sep_mark,
dec_mark = dec_mark,
system = system,
locale = locale,
patterns = patterns
)
#
# Prefix with plus and minus signs where necessary
#
x_str[x < 0 & !is.infinite(x)] <-
paste0(
context_minus_mark(context = context),
x_str[x < 0 & !is.infinite(x)]
)
if (force_sign) {
x_str[x > 0 & !is.infinite(x)] <-
paste0("+", x_str[x > 0 & !is.infinite(x)])
}
x_str
}
)
)
}
validate_duration_input_units <- function(input_units, call = rlang::caller_env()) {
if (is.null(input_units)) {
return(NULL)
}
time_parts_vec <- c("weeks", "days", "hours", "mins", "minutes", "secs", "seconds")
rlang::arg_match0(
input_units,
time_parts_vec,
error_call = call
)
}
normalize_duration_input_units <- function(input_units) {
# Ensure that key transforms occur
input_units <- sub("secs", "seconds", input_units, fixed = TRUE)
input_units <- sub("mins", "minutes", input_units, fixed = TRUE)
input_units
}
normalize_duration_output_units <- function(output_units) {
# Ensure that key transforms occur and that the output units are a unique set
output_units <- sub("secs", "seconds", output_units, fixed = TRUE)
output_units <- sub("mins", "minutes", output_units, fixed = TRUE)
output_units <- unique(output_units)
# Ensure that the order of output units is from greatest to smallest
time_parts <- c("weeks", "days", "hours", "minutes", "seconds")
output_units[order(match(output_units, time_parts))]
}
values_to_durations <- function(
x,
in_units,
out_units,
out_style,
trim_zero_units,
max_output_units,
colon_sep_params,
sep_mark,
dec_mark,
system,
locale,
patterns
) {
# Obtain the units of `x` if it is of the difftime class (and
# drop difftime attrs with `as.numeric()`)
if (inherits(x, "difftime")) {
in_units <- units(x)
x <- as.numeric(x)
}
if (inherits(x, "integer")) {
x <- as.numeric(x)
}
if (in_units == "mins") {
in_units <- "minutes"
}
if (in_units == "secs") {
in_units <- "seconds"
}
second_conversion_factor <-
c(
weeks = 604800L,
days = 86400L,
hours = 3600L,
minutes = 60L,
seconds = 1L
)
# Should `in_units` be anything other than seconds then
# convert all `x` values to seconds
if (in_units != "seconds") {
x <- x * second_conversion_factor[[in_units]]
}
x_str <- character(length(x))
for (i in seq_along(x)) {
x_df_i <-
dplyr::tibble(
value = NA_integer_,
time_part = out_units,
formatted = NA_character_
)
x_rem_i <- abs(x[[i]])
for (j in seq_along(out_units)) {
factor <- second_conversion_factor[[out_units[[j]]]]
x_df_i$value[[j]] <- floor(x_rem_i / factor)
x_rem_i <- x_rem_i %% factor
}
# Remove time parts according to keywords in `trim_zero_units`
total_time_units <- nrow(x_df_i)
first_non_zero_unit_idx <- utils::head(which(x_df_i$value != 0), 1)
last_non_zero_unit_idx <- utils::tail(which(x_df_i$value != 0), 1)
remove_idx <- c()
# Possibly add leading zero time parts to `remove_idx`
if (
"leading" %in% trim_zero_units &&
length(first_non_zero_unit_idx) > 0 &&
first_non_zero_unit_idx > 1
) {
remove_idx <- c(remove_idx, seq(1, first_non_zero_unit_idx - 1))
}
# Possibly add trailing zero time parts to `remove_idx`
if (
"trailing" %in% trim_zero_units &&
length(last_non_zero_unit_idx) > 0 &&
last_non_zero_unit_idx < total_time_units
) {
remove_idx <- c(remove_idx, seq(last_non_zero_unit_idx + 1, total_time_units))
}
# Possibly add internal zero time parts to `remove_idx`
if (
"internal" %in% trim_zero_units &&
length(first_non_zero_unit_idx) > 0
) {
internal_idx <- first_non_zero_unit_idx:last_non_zero_unit_idx
remove_idx <- c(remove_idx, base::intersect(internal_idx, which(x_df_i$value == 0)))
}
# Remove rows from `x_df_i`
if (length(remove_idx) > 0) {
x_df_i <- x_df_i[-remove_idx, ]
}
if (all(x_df_i$value == 0) && length(trim_zero_units) > 0) {
# Remove all but the final row
x_df_i <- utils::tail(x_df_i, n = 1)
}
# Remove units that exceed a maximum number according to `max_output_units`
if (!is.null(max_output_units) && nrow(x_df_i) > max_output_units) {
x_df_i <- x_df_i[seq_len(max_output_units), ]
}
for (j in seq_len(nrow(x_df_i))) {
pattern <-
extract_duration_pattern(
value = x_df_i$value[j],
time_p = x_df_i$time_part[j],
patterns = patterns
)
x_df_i[j, "formatted"] <-
format_time_part(
x = x_df_i$value[j],
time_part = x_df_i$time_part[j],
out_style = out_style,
sep_mark = sep_mark,
dec_mark = dec_mark,
locale = locale,
system = system,
pattern = pattern
)
}
# Handle edge cases where duration is smaller
# than the smallest unit in `out_units`
if (all(x_df_i$value == 0)) {
# Obtain the smallest time unit; `normalize_duration_output_units()`
# ensures that `out_units` is sorted from largest to smallest so the
# last component will always be needed here
time_p <- out_units[length(out_units)]
# If the time duration is zero then use `0` as the value,
# otherwise, use `1` and indicate that the value is less than that
pattern <-
extract_duration_pattern(
value = if (x_rem_i == 0) 0 else 1,
time_p = time_p,
patterns = patterns
)
x_df_i[nrow(x_df_i), "formatted"] <-
format_time_part(
x = if (x_rem_i == 0) 0 else 1,
time_part = time_p,
out_style = out_style,
sep_mark = sep_mark,
dec_mark = dec_mark,
locale = locale,
system = system,
pattern = pattern
)
if (x_rem_i != 0) {
x_df_i[1, "formatted"] <- paste0("<", x_df_i[1, "formatted"])
}
}
if (out_style == "colon-sep") {
colon_sep_output_units <- colon_sep_params$output_units
colon_sep_trim_zero_units <- colon_sep_params$trim_zero_units
# Filter to only the output units needed
x_df_i <- dplyr::filter(x_df_i, time_part %in% colon_sep_output_units)
# If days has a zero value, remove that entry unconditionally
if ("days" %in% x_df_i$time_part && x_df_i[[1, "value"]] == 0) {
x_df_i <- dplyr::filter(x_df_i, time_part != "days")
}
if (colon_sep_trim_zero_units == "leading") {
if (
identical(x_df_i$time_part, c("hours", "minutes", "seconds")) &&
x_df_i[[1, "value"]] == 0
) {
x_df_i <- dplyr::filter(x_df_i, time_part != "hours")
}
}
# Assemble the remaining time parts
hms_part <-
x_df_i[x_df_i$time_part %in% c("hours", "minutes", "seconds"), "formatted", drop = TRUE]
hms_part <- paste(hms_part, collapse = ":")
d_part <-
ifelse("days" %in% x_df_i$time_part, paste0(x_df_i$formatted[1], "/"), "")
x_str[i] <- paste0(d_part, hms_part)
} else if (out_style == "iso") {
x_str[i] <- paste0("P", paste0(x_df_i$formatted, collapse = ""))
x_str[i] <- sub("D", "DT", x_str[i], fixed = TRUE)
} else {
x_str[i] <- paste0(x_df_i$formatted, collapse = " ")
}
}
x_str
}
format_time_part <- function(
x,
time_part,
out_style,
sep_mark,
dec_mark,
locale,
system,
pattern
) {
x_val <-
format_num_to_str(
x,
context = "plain",
decimals = 0,
n_sigfig = NULL,
sep_mark = if (out_style != "iso") sep_mark else "",
dec_mark = dec_mark,
drop_trailing_zeros = TRUE,
drop_trailing_dec_mark = TRUE,
format = "f",
system = system
)
if (out_style %in% c("narrow", "wide")) {
out <- gsub("{0}", x_val, pattern, fixed = TRUE)
} else if (out_style == "iso") {
out <- paste0(x_val, toupper(substr(time_part, 1, 1)))
} else {
if (time_part %in% c("hours", "minutes", "seconds") && x < 10) {
out <- paste0("0", x_val)
} else {
out <- as.character(x_val)
}
}
out
}
get_localized_duration_patterns <- function(
value,
type,
locale
) {
locale <- locale %||% "en"
if (type == "wide") type <- "long"
pattern_tbl <-
durations[
durations$locale == locale,
grepl(
"^duration-(week|day|hour|minute|second).unitPattern-count-(zero|one|other)$",
colnames(durations)
) |
grepl("type", colnames(durations), fixed = TRUE)
]
pattern_tbl <- pattern_tbl[pattern_tbl$type == type, , drop = FALSE]
pattern_tbl$type <- NULL
colnames(pattern_tbl) <- gsub("(duration|-|unitPattern-count)", "", colnames(pattern_tbl))
as.list(pattern_tbl)
}
extract_duration_pattern <- function(
value,
time_p,
patterns
) {
x_val_i_type <-
dplyr::case_when(
value == 1 ~ "one",
value == 0 ~ "zero",
TRUE ~ "other"
)
pattern <- patterns[grepl(paste0(gsub("s$", "", time_p), ".*?.", x_val_i_type), names(patterns))][[1]]
if (!is.null(pattern) && is.na(pattern)) {
pattern <- patterns[grepl(paste0(gsub("s$", "", time_p), ".*?.other"), names(patterns))][[1]]
}
pattern
}
#' Format column data containing bin/interval information
#'
#' @description
#'
#' When using `cut()` (or other functions that use it in some way)
#' you get bins that can look like this: `"(0,10]"`, `"(10,15]"`, `"(15,20]"`,
#' `"(20,40]"`. This interval notation expresses the lower and upper limits of
#' each range. The square or round brackets define whether each of the endpoints
#' are included in the range (`[`/`]` for inclusion, `(`/`)` for exclusion).
#' Should bins of this sort be present in a table, the `fmt_bins()` function can
#' be used to format that syntax to a form that presents better in a display
#' table. It's possible to format the values of the intervals with the `fmt`
#' argument, and, the separator can be modified with the `sep` argument.
#'
#' @inheritParams fmt_number
#'
#' @param sep *Separator between values*
#'
#' `scalar<character>` // *default:* `"--"`
#'
#' The separator text that indicates the values are ranged. The default value
#' of `"--"` indicates that an en dash will be used for the range separator.
#' Using `"---"` will be taken to mean that an em dash should be used. Should
#' you want these special symbols to be taken literally, they can be supplied
#' within [base::I()].
#'
#' @param fmt *Formatting expressions*
#'
#' `<single expression>` // *default:* `NULL` (`optional`)
#'
#' An optional formatting expression in formula form. If used, the RHS of `~`
#' should contain a formatting call (e.g.,
#' `~ fmt_number(., decimals = 3, use_seps = FALSE`).
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_bins()` is compatible with body cells that are of the `"character"` or
#' `"factor"` types. Any other types of body cells are ignored during formatting.
#' This is to say that cells of incompatible data types may be targeted, but
#' there will be no attempt to format them.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Formatting expressions for `fmt`:
#'
#' We can supply a one-sided (RHS only) expression to `fmt`, and, several can be
#' provided in a list. The expression uses a formatting function (e.g.,
#' [fmt_number()], [fmt_currency()], etc.) and it must contain an initial `.`
#' that stands for the data object. If performing numeric formatting it might
#' look something like this:
#'
#' `fmt = ~ fmt_number(., decimals = 1, use_seps = FALSE)`
#'
#' @section Examples:
#'
#' Use the [`countrypops`] dataset to create a **gt** table. Before even getting
#' to the [gt()] call, we use `cut()` in conjunction with [scales::breaks_log()]
#' to create some highly customized bins. Consequently each country's population
#' in the 2021 year is assigned to a bin. These bins have a characteristic type
#' of formatting that can be used as input to `fmt_bins()`, and using that
#' formatting function allows us to customize the presentation of those ranges.
#' For instance, here we are formatting the left and right values of the ranges
#' with [fmt_integer()] (using formula syntax).
#'
#' ```r
#' countrypops |>
#' dplyr::filter(year == 2021) |>
#' dplyr::select(country_code_2, population) |>
#' dplyr::mutate(population_class = cut(
#' population,
#' breaks = scales::breaks_log(n = 20)(population)
#' )
#' ) |>
#' dplyr::group_by(population_class) |>
#' dplyr::summarize(
#' count = dplyr::n(),
#' countries = paste0(country_code_2, collapse = ",")
#' ) |>
#' dplyr::arrange(desc(population_class)) |>
#' gt() |>
#' fmt_flag(columns = countries) |>
#' fmt_bins(
#' columns = population_class,
#' fmt = ~ fmt_integer(., suffixing = TRUE)
#' ) |>
#' cols_label(
#' population_class = "Population Range",
#' count = "",
#' countries = "Countries"
#' ) |>
#' cols_width(
#' population_class ~ px(150),
#' count ~ px(50)
#' ) |>
#' tab_style(
#' style = cell_text(style = "italic"),
#' locations = cells_body(columns = count)
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_bins_1.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-17
#'
#' @section Function Introduced:
#' `v0.9.0` (Mar 31, 2023)
#'
#' @export
fmt_bins <- function(
data,
columns = everything(),
rows = everything(),
sep = "--",
fmt = NULL
) {
# Perform input object validation
stop_if_not_gt_tbl(data = data)
valid_class <- c("character", "factor")
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
fmt(
data = data,
columns = {{ columns }},
rows = {{ rows }},
fns = list(
html = function(x) {
format_bins_by_context(x, sep = sep, fmt = fmt, context = "html")
},
latex = function(x) {
format_bins_by_context(x, sep = sep, fmt = fmt, context = "latex")
},
rtf = function(x) {
format_bins_by_context(x, sep = sep, fmt = fmt, context = "rtf")
},
word = function(x) {
format_bins_by_context(x, sep = sep, fmt = fmt, context = "word")
},
default = function(x) {
format_bins_by_context(x, sep = sep, fmt = fmt, context = "plain")
}
)
)
}
format_bins_by_context <- function(x, sep, fmt, context) {
# Format `sep` for output context
if (context != "plain") {
sep <- context_dash_mark(sep, context = context)
}
# Generate an vector of empty strings that will eventually
# contain all of the ranged value text
x_str <- character(length(x))
x_str_non_missing <- x[!is.na(x)]
x_str_non_missing <- as.character(x_str_non_missing)
x_str_is_bin <-
grepl("^(\\(|\\[]).*?,.*?(\\)|\\])$", x_str_non_missing)
x_str_lhs <-
gsub(
"^(\\(|\\[])(.*?),(.*?)(\\)|\\])$",
"\\2",
x_str_non_missing[x_str_is_bin]
)
x_str_rhs <-
gsub(
"^(\\(|\\[])(.*?),(.*?)(\\)|\\])$",
"\\3",
x_str_non_missing[x_str_is_bin]
)
if (!is.null(fmt)) {
# Format the LHS and RHS values
val_tbl <-
dplyr::tibble(
left = as.numeric(x_str_lhs),
right = as.numeric(x_str_rhs)
)
val_tbl_gt <- gt(val_tbl)
# Ensure that the expression (a RHS formula) is made a closure
format_fn <- rlang::as_closure(fmt)
# Perform the formatting on this gt table with closure
val_tbl_gt <- format_fn(val_tbl_gt)
#
# Extract the columns of formatted data
#
x_val_lhs_fmt <-
extract_cells(val_tbl_gt, columns = "left", output = context)
x_val_rhs_fmt <-
extract_cells(val_tbl_gt, columns = "right", output = context)
} else {
x_val_lhs_fmt <- x_str_lhs
x_val_rhs_fmt <- x_str_rhs
}
x_str_non_missing[x_str_is_bin] <-
paste0(x_val_lhs_fmt, sep, x_val_rhs_fmt)
x_str[!is.na(x)] <- x_str_non_missing
x_str[is.na(x)] <- NA_character_
x_str
}
#' Format `TRUE` and `FALSE` values
#'
#' @description
#'
#' There can be times where logical values are useful in a **gt** table. You
#' might want to express a 'yes' or 'no', a 'true' or 'false', or, perhaps use
#' pairings of complementary symbols that make sense in a table. The `fmt_tf()`
#' function has a set of `tf_style` presets that can be used to quickly map
#' `TRUE`/`FALSE` values to strings (which are automatically translated
#' according to a given `locale` value), or, symbols like up/down or left/right
#' arrows and open/closed shapes.
#'
#' While the presets are nice, you can provide your own mappings through the
#' `true_val` and `false_val` arguments. With those you could provide text
#' (perhaps a Unicode symbol?) or even a **fontawesome** icon by using
#' `fontawesome::fa("<icon name>")`. The function will automatically handle
#' alignment when `auto_align = TRUE` and try to give you the best look
#' depending on the options chosen. For extra customization, you can also apply
#' color to the individual `TRUE`, `FALSE`, and `NA` mappings. Just supply
#' a vector of colors (up to a length of 3) to the `colors` argument.
#'
#' @inheritParams fmt_number
#'
#' @param tf_style *Predefined style for `TRUE`/`FALSE` formatting*
#'
#' `scalar<character>|scalar<numeric|integer>(1<=val<=10)` // *default:* `"true-false"`
#'
#' The `TRUE`/`FALSE` mapping style to use. By default this is the short name
#' `"true-false"` which corresponds to the words 'true' and 'false'. Two other
#' `tf_style` values produce words: `"yes-no"` and `"up-down"`. All three of
#' these options for `tf_style` are locale-aware through the `locale` option,
#' so, a `"yes"` value will instead be `"ja"` when `locale = "de"`. Options
#' 4 through to 10 involve pairs of symbols (e.g., `"check-mark"` displays
#' a check mark for `TRUE` and an X symbol for `FALSE`).
#'
#' @param true_val *Text to use for `TRUE` values*
#'
#' `scalar<character>` // *default:* `NULL` (`optional`)
#'
#' While the choice of a `tf_style` will typically supply the `true_val` and
#' `false_val` text, we could override this and supply text for any `TRUE`
#' values. This doesn't need to be used in conjunction with `false_val`.
#'
#' @param false_val *Text to use for `FALSE` values*
#'
#' `scalar<character>` // *default:* `NULL` (`optional`)
#'
#' While the choice of a `tf_style` will typically supply the `true_val` and
#' `false_val` text, we could override this and supply text for any `FALSE`
#' values. This doesn't need to be used in conjunction with `true_val`.
#'
#' @param na_val *Text to use for `NA` values*
#'
#' `scalar<character>` // *default:* `NULL` (`optional`)
#'
#' None of the `tf_style` presets will replace any missing values encountered
#' in the targeted cells. While we always have the option to use
#' [sub_missing()] for `NA` replacement, we have the opportunity to do that
#' here with the `na_val` option. This is useful because we also have the
#' means to add color to the `na_val` text or symbol and doing that requires
#' that a replacement value for `NA`s is specified here.
#'
#' @param colors *Colors to use for the resulting strings or symbols*
#'
#' `vector<character>` // *default:* `NULL` (`optional`)
#'
#' Providing a vector of color values to `colors` will progressively add color
#' to the formatted result depending on the number of colors provided. With a
#' single color, all formatted values will be in that color. Giving two colors
#' results in `TRUE` values being the first color, and `FALSE` values
#' receiving the second. With the three color option, the final color will be
#' given to any `NA` values replaced through `na_val`.
#'
#' @param auto_align *Automatic alignment of the formatted column*
#'
#' `scalar<logical>` // *default:* `TRUE`
#'
#' The input values may have resulted in an alignment that is not as suitable
#' once formatting has occurred. With `auto_align = TRUE`, the formatted
#' values will be inspected and this may result in a favorable change in
#' alignment. Typically, symbols will be center aligned whereas words will
#' receive a left alignment (for words in LTR languages).
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Compatibility of formatting function with data values:
#'
#' `fmt_tf()` is compatible with body cells that are of the `"logical"`
#' (preferred) or `"numeric"` types. Any other types of body cells are ignored
#' during formatting. This is to say that cells of incompatible data types may
#' be targeted, but there will be no attempt to format them.
#'
#' There is a special caveat when attempting to format numerical values: the
#' values must either be exactly `1` (the analogue for `TRUE`) or exactly `0`
#' (the analogue for `FALSE`). Any other numerical values will be disregarded
#' and left as is. Because of these restrictions, it is recommended that only
#' logical values undergo formatting.
#'
#' @section Targeting cells with `columns` and `rows`:
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). The
#' `columns` argument allows us to target a subset of cells contained in the
#' resolved columns. We say resolved because aside from declaring column names
#' in `c()` (with bare column names or names in quotes) we can use
#' **tidyselect**-style expressions. This can be as basic as supplying a select
#' helper like `starts_with()`, or, providing a more complex incantation like
#'
#' `where(~ is.numeric(.x) && max(.x, na.rm = TRUE) > 1E6)`
#'
#' which targets numeric columns that have a maximum value greater than
#' 1,000,000 (excluding any `NA`s from consideration).
#'
#' By default all columns and rows are selected (with the `everything()`
#' defaults). Cell values that are incompatible with a given formatting function
#' will be skipped over, like `character` values and numeric `fmt_*()`
#' functions. So it's safe to select all columns with a particular formatting
#' function (only those values that can be formatted will be formatted), but,
#' you may not want that. One strategy is to format the bulk of cell values with
#' one formatting function and then constrain the columns for later passes with
#' other types of formatting (the last formatting done to a cell is what you get
#' in the final output).
#'
#' Once the columns are targeted, we may also target the `rows` within those
#' columns. This can be done in a variety of ways. If a stub is present, then we
#' potentially have row identifiers. Those can be used much like column names in
#' the `columns`-targeting scenario. We can use simpler **tidyselect**-style
#' expressions (the select helpers should work well here) and we can use quoted
#' row identifiers in `c()`. It's also possible to use row indices (e.g.,
#' `c(3, 5, 6)`) though these index values must correspond to the row numbers of
#' the input data (the indices won't necessarily match those of rearranged rows
#' if row groups are present). One more type of expression is possible, an
#' expression that takes column values (can involve any of the available columns
#' in the table) and returns a logical vector. This is nice if you want to base
#' formatting on values in the column or another column, or, you'd like to use a
#' more complex predicate expression.
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_tf()` to obtain
#' varying parameter values from a specified column within the table. This means
#' that each row could be formatted a little bit differently. These arguments
#' provide support for [from_column()]:
#'
#' - `tf_style`
#' - `pattern`
#' - `true_val`
#' - `false_val`
#' - `na_val`
#' - `locale`
#'
#' Please note that for each of the aforementioned arguments, a [from_column()]
#' call needs to reference a column that has data of the correct type (this is
#' different for each argument). Additional columns for parameter values can be
#' generated with [cols_add()] (if not already present). Columns that contain
#' parameter data can also be hidden from final display with [cols_hide()].
#' Finally, there is no limitation to how many arguments the [from_column()]
#' helper is applied so long as the arguments belong to this closed set.
#'
#' @section Formatting with the `tf_style` argument:
#'
#' We can supply a preset `TRUE`/`FALSE` style to the `tf_style` argument to
#' handle the formatting of logical values. There are several such styles and
#' the first three of them can handle localization to any supported locale
#' (i.e., the pairs of words for each style will be translated to the language
#' of the `locale`) value.
#'
#' The following table provides a listing of all valid `tf_style` values and a
#' description of their output values. The output from styles `4` to `10` are
#' described in terms of the Unicode character names used for the `TRUE` and
#' `FALSE` values.
#'
#' | | TF Style | Output (for `TRUE` and `FALSE`) |
#' |----|---------------|-------------------------------------------------|
#' | 1 | `"true-false"`| `"true"`, `"false"` (`locale`-aware) |
#' | 2 | `"yes-no"` | `"yes"`, `"no"` (`locale`-aware) |
#' | 3 | `"up-down"` | `"up"`, `"down"` (`locale`-aware) |
#' | 4 | `"check-mark"`| `<Heavy Check Mark>`, `<Heavy Ballot X>` |
#' | 5 | `"circles"` | `<Black Circle>`, `<Heavy Circle>` |
#' | 6 | `"squares"` | `<Black Square>`, `<White Square>` |
#' | 7 | `"diamonds"` | `<Black Diamond>`, `<White Diamond>` |
#' | 8 | `"arrows"` | `<Upwards Arrow>`, `<Downwards Arrow>` |
#' | 9 | `"triangles"` | `<Black Up-Pointing Triangle>`, `<Black Down-Pointing Triangle>`|
#' | 10 | `"triangles-lr"`| `<Heavy Check Mark>`, `<Heavy Ballot X>` |
#'
#' @section Adapting output to a specific `locale`:
#'
#' This formatting function can adapt outputs according to a provided `locale`
#' value. Examples include `"en"` for English (United States) and `"fr"` for
#' French (France). Note that a `locale` value provided here will override any
#' global locale setting performed in [gt()]'s own `locale` argument (it is
#' settable there as a value received by all other functions that have a
#' `locale` argument). As a useful reference on which locales are supported, we
#' can call [info_locales()] to view an info table.
#'
#' @section Examples:
#'
#' Let's use a subset of the [`sp500`] dataset to create a small **gt** table
#' containing opening and closing price data for a week in 2013. We can add
#' a logical column (`dir`) with [cols_add()]; the expression used determines
#' whether the `close` value is greater than the `open` value. That new column
#' is inserted between `open` and `close`. Then, we use `fmt_tf()` to generate
#' up and down arrows in the `dir` column. We elect to use green upward arrows
#' and red downward arrows (through the `colors` option). With a little numeric
#' formatting and changes to the column labels, the table becomes more presentable.
#'
#' ```r
#' sp500 |>
#' dplyr::filter(date >= "2013-01-07" & date <= "2013-01-12") |>
#' dplyr::arrange(date) |>
#' dplyr::select(-c(adj_close, volume, high, low)) |>
#' gt(rowname_col = "date") |>
#' cols_add(dir = close > open, .after = open) |>
#' fmt_tf(
#' columns = dir,
#' tf_style = "arrows",
#' colors = c("green", "red")
#' ) |>
#' fmt_currency(columns = c(open, close)) |>
#' cols_label(
#' open = "Opening",
#' close = "Closing",
#' dir = ""
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_tf_1.png")`
#' }}
#'
#' The [`reactions`] dataset contains chemical kinetic information on a wide
#' variety of atmospherically-relevant compounds. It might be interesting to get
#' a summary (for a small subset of compounds) for which rate constants are
#' available for the selected compounds. We first start by selecting the
#' relevant rows and columns. Then we generate logical columns for each of the
#' reaction types (i.e., if a value is `NA` then there's no measurement, so
#' that's `FALSE`). Once the **gt** table has been created, we can use
#' `fmt_tf()` to provide open and filled circles to indicate whether a
#' particular reaction has been measured and presented in the literature.
#'
#' ```r
#' reactions |>
#' dplyr::filter(cmpd_type %in% c("carboxylic acid", "alkyne", "allene")) |>
#' dplyr::select(cmpd_name, cmpd_type, ends_with("k298")) |>
#' dplyr::mutate(across(ends_with("k298"), is.na)) |>
#' gt(rowname_col = "cmpd_name", groupname_col = "cmpd_type") |>
#' tab_spanner(
#' label = "Has a measured rate constant",
#' columns = ends_with("k298")
#' ) |>
#' tab_stub_indent(
#' rows = everything(),
#' indent = 2
#' ) |>
#' fmt_tf(
#' columns = ends_with("k298"),
#' tf_style = "circles"
#' ) |>
#' cols_label(
#' OH_k298 = "OH",
#' O3_k298 = "Ozone",
#' NO3_k298 = "Nitrate",
#' Cl_k298 = "Chlorine"
#' ) |>
#' cols_width(
#' stub() ~ px(200),
#' ends_with("k298") ~ px(80)
#' ) |>
#' opt_vertical_padding(scale = 0.35)
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_tf_2.png")`
#' }}
#'
#' There are census-based population values in the [`towny`] dataset and quite a
#' few small towns within it. Let's look at the ten smallest towns (according
#' to the 2021 figures) and work out whether their populations have increased or
#' declined since 1996. Also, let's determine which of these towns even have a
#' website. After that data preparation, the data is made into a **gt** table
#' and `fmt_tf()` can be used in the `website` and `pop_dir` columns (which both
#' have `TRUE`/`FALSE` values). Each of these `fmt_tf()` calls will either
#' produce `"yes"`/`"no"` or `"up"`/`"down"` strings (set via the `tf_style`
#' option).
#'
#' ```r
#' towny |>
#' dplyr::arrange(population_2021) |>
#' dplyr::mutate(website = !is.na(website)) |>
#' dplyr::mutate(pop_dir = population_2021 > population_1996) |>
#' dplyr::select(name, website, population_1996, population_2021, pop_dir) |>
#' dplyr::slice_head(n = 10) |>
#' gt(rowname_col = "name") |>
#' tab_spanner(
#' label = "Population",
#' columns = starts_with("pop")
#' ) |>
#' tab_stubhead(label = "Town") |>
#' fmt_tf(
#' columns = website,
#' tf_style = "yes-no",
#' auto_align = FALSE
#' ) |>
#' fmt_tf(
#' columns = pop_dir,
#' tf_style = "up-down",
#' pattern = "It's {x}."
#' ) |>
#' cols_label_with(
#' columns = starts_with("population"),
#' fn = function(x) sub("population_", "", x)
#' ) |>
#' cols_label(
#' website = md("Has a \n website?"),
#' pop_dir = "Pop. direction?"
#' ) |>
#' opt_horizontal_padding(scale = 2)
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_tf_3.png")`
#' }}
#'
#' If formatting to words instead of symbols (with the hyphenated `tf_style`
#' keywords), the words themselves can be translated to different languages
#' if providing a `locale` value. In this next example, we're manually creating
#' a tibble with locale codes and their associated languages. The `yes` and `up`
#' columns all receive `TRUE` whereas `no` and `down` will all be `FALSE`.
#' With two calls of `fmt_tf()` for each of these pairings, we get the columns'
#' namesake words. To have these words translated, the `locale` argument is
#' pointed toward values in the `code` column by using [from_column()].
#'
#' ```r
#' dplyr::tibble(
#' code = c("de", "fr", "is", "tr", "ka", "lt", "ca", "bg", "lv"),
#' lang = c(
#' "German", "French", "Icelandic", "Turkish", "Georgian",
#' "Lithuanian", "Catalan", "Bulgarian", "Latvian"
#' ),
#' yes = TRUE,
#' no = FALSE,
#' up = TRUE,
#' down = FALSE
#' ) |>
#' gt(rowname_col = "lang") |>
#' tab_header(title = "Common words in a few languages") |>
#' fmt_tf(
#' columns = c(yes, no),
#' tf_style = "yes-no",
#' locale = from_column("code")
#' ) |>
#' fmt_tf(
#' columns = c(up, down),
#' tf_style = "up-down",
#' locale = from_column("code")
#' ) |>
#' cols_merge(
#' columns = c(lang, code),
#' pattern = "{1} ({2})"
#' ) |>
#' cols_width(
#' stub() ~ px(150),
#' everything() ~ px(80)
#' )
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_tf_4.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-18
#'
#' @section Function Introduced:
#' `v0.11.0`
#'
#' @export
fmt_tf <- function(
data,
columns = everything(),
rows = everything(),
tf_style = "true-false",