From 3c70565882a0e52e4aba2dce37081777fb0a05d0 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Wed, 9 Jun 2021 13:43:51 -0400 Subject: [PATCH] Add the `force_sign` arg to several numeric formatters (#793) * Add the `force_sign` arg and its implementation * Update help files using roxygen * Add several testthat tests --- R/format_data.R | 238 ++++++++++++++++++------ man/fmt_bytes.Rd | 6 + man/fmt_currency.Rd | 7 + man/fmt_engineering.Rd | 6 + man/fmt_integer.Rd | 7 + man/fmt_number.Rd | 7 + man/fmt_percent.Rd | 7 + man/fmt_scientific.Rd | 6 + tests/testthat/test-fmt_bytes.R | 31 +++ tests/testthat/test-fmt_engineering.R | 65 +++++++ tests/testthat/test-fmt_integer.R | 32 ++++ tests/testthat/test-fmt_number.R | 35 ++++ tests/testthat/test-fmt_percent.R | 47 +++++ tests/testthat/test-fmt_scientific.R | 32 ++++ tests/testthat/test-l_fmt_engineering.R | 43 +++++ tests/testthat/test-l_fmt_integer.R | 35 ++++ tests/testthat/test-l_fmt_number.R | 38 ++++ tests/testthat/test-l_fmt_percent.R | 46 +++++ tests/testthat/test-l_fmt_scientific.R | 27 +++ 19 files changed, 657 insertions(+), 58 deletions(-) diff --git a/R/format_data.R b/R/format_data.R index a40f7cd77..2be5a2790 100644 --- a/R/format_data.R +++ b/R/format_data.R @@ -87,6 +87,11 @@ #' of `1,000`). #' @param dec_mark The character to use as a decimal mark (e.g., using `dec_mark #' = ","` with `0.152` would result in a formatted value of `0,152`). +#' @param force_sign 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 locale An optional locale ID that can be used for formatting the value #' according the locale's rules. Examples include `"en_US"` for English #' (United States) and `"fr_FR"` for French (France). The use of a valid @@ -157,6 +162,7 @@ fmt_number <- function(data, pattern = "{x}", sep_mark = ",", dec_mark = ".", + force_sign = FALSE, locale = NULL) { # Perform input object validation @@ -211,30 +217,50 @@ fmt_number <- function(data, format_fn = function(x, context) { # Create the `suffix_df` object - suffix_df <- create_suffix_df(x, decimals, suffix_labels, scale_by) + suffix_df <- + create_suffix_df( + x, + decimals = decimals, + suffix_labels = suffix_labels, + scale_by = scale_by + ) + + # Scale the `x` values by the `scale_by` values in `suffix_df` + x <- scale_x_values(x, scale_by = suffix_df$scale_by) + # Format numeric values to character-based numbers x_str <- - x %>% - # Scale the `x_vals` by the `scale_by` values - scale_x_values(suffix_df$scale_by) %>% - # Format numeric values to character-based numbers format_num_to_str( - context = context, decimals = decimals, n_sigfig = n_sigfig, - sep_mark = sep_mark, dec_mark = dec_mark, + 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 - ) %>% - # With large-number suffixing support, we paste the - # vector of suffixes to the right of the values - paste_right(suffix_df$suffix) + ) + + # 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 <- - x_str %>% format_as_accounting( - x = x, context = context, accounting = 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 } ) @@ -316,6 +342,7 @@ fmt_integer <- function(data, suffixing = FALSE, pattern = "{x}", sep_mark = ",", + force_sign = FALSE, locale = NULL) { fmt_number( @@ -333,6 +360,7 @@ fmt_integer <- function(data, pattern = pattern, sep_mark = sep_mark, dec_mark = "not used", + force_sign = force_sign, locale = locale ) } @@ -361,6 +389,10 @@ fmt_integer <- function(data, #' @inheritParams fmt_number #' @param scale_by A value to scale the input. The default is `1.0`. All numeric #' values will be multiplied by this value first before undergoing formatting. +#' @param force_sign 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. #' #' @return An object of class `gt_tbl`. #' @@ -403,6 +435,7 @@ fmt_scientific <- function(data, pattern = "{x}", sep_mark = ",", dec_mark = ".", + force_sign = FALSE, locale = NULL) { # Perform input object validation @@ -417,7 +450,7 @@ fmt_scientific <- function(data, dec_mark <- get_locale_dec_mark(locale, dec_mark) # Stop function if `locale` does not have a valid value - validate_locale(locale) + validate_locale(locale = locale) # Normalize the `suffixing` input to either return a character vector # of suffix labels, or NULL (the case where `suffixing` is FALSE) @@ -450,16 +483,25 @@ fmt_scientific <- function(data, } # Create the `suffix_df` object - suffix_df <- create_suffix_df(x, decimals, suffix_labels, scale_by) + suffix_df <- + create_suffix_df( + x, + decimals = decimals, + suffix_labels = suffix_labels, + scale_by = scale_by + ) - # Scale the `x_vals` by the `scale_by` values - x <- x %>% scale_x_values(suffix_df$scale_by) + # Scale the `x` values by the `scale_by` values in `suffix_df` + x <- scale_x_values(x, scale_by = suffix_df$scale_by) x_str <- - x %>% format_num_to_str( - context = context, decimals = decimals, n_sigfig = NULL, - sep_mark = sep_mark, dec_mark = dec_mark, + 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 = FALSE, format = "e", @@ -488,6 +530,13 @@ fmt_scientific <- function(data, exp_marks[2] ) + # 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 } ) @@ -522,6 +571,10 @@ fmt_scientific <- function(data, #' @inheritParams fmt_number #' @param scale_by A value to scale the input. The default is `1.0`. All numeric #' values will be multiplied by this value first before undergoing formatting. +#' @param force_sign 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. #' #' @return An object of class `gt_tbl`. #' @@ -551,6 +604,7 @@ fmt_engineering <- function(data, pattern = "{x}", sep_mark = ",", dec_mark = ".", + force_sign = FALSE, locale = NULL) { # Perform input object validation @@ -565,7 +619,7 @@ fmt_engineering <- function(data, dec_mark <- get_locale_dec_mark(locale, dec_mark) # Stop function if `locale` does not have a valid value - validate_locale(locale) + validate_locale(locale = locale) # Normalize the `suffixing` input to either return a character vector # of suffix labels, or NULL (the case where `suffixing` is FALSE) @@ -597,10 +651,16 @@ fmt_engineering <- function(data, } # Create the `suffix_df` object - suffix_df <- create_suffix_df(x, decimals, suffix_labels, scale_by) + suffix_df <- + create_suffix_df( + x, + decimals = decimals, + suffix_labels = suffix_labels, + scale_by = scale_by + ) # Scale the `x_vals` by the `scale_by` values - x <- x %>% scale_x_values(suffix_df$scale_by) + x <- scale_x_values(x, suffix_df$scale_by) zero_x <- x == 0 negative_x <- x < 0 @@ -621,10 +681,13 @@ fmt_engineering <- function(data, # With the scaled values for the LHS, format these according # to the options set by the user x_str_left <- - x %>% format_num_to_str( - context = context, decimals = decimals, n_sigfig = NULL, - sep_mark = sep_mark, dec_mark = dec_mark, + 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 = FALSE, format = "f", @@ -647,6 +710,13 @@ fmt_engineering <- function(data, # Paste the LHS and RHS components to generate the formatted values x_str <- paste0(x_str_left, x_str_right) + # 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 } ) @@ -673,6 +743,7 @@ fmt_symbol <- function(data, pattern = "{x}", sep_mark = ",", dec_mark = ".", + force_sign = FALSE, placement = "left", incl_space = FALSE, locale = NULL) { @@ -682,7 +753,7 @@ fmt_symbol <- function(data, dec_mark <- get_locale_dec_mark(locale, dec_mark) # Stop function if `locale` does not have a valid value - validate_locale(locale) + validate_locale(locale = locale) # Normalize the `suffixing` input to either return a character vector # of suffix labels, or NULL (the case where `suffixing` is FALSE) @@ -702,22 +773,31 @@ fmt_symbol <- function(data, x_str <- character(length(x)) # Create the `suffix_df` object - suffix_df <- create_suffix_df(x, decimals, suffix_labels, scale_by) + suffix_df <- + create_suffix_df( + x, + decimals = decimals, + suffix_labels = suffix_labels, + scale_by = scale_by + ) # Scale the `x_vals` by the `scale_by` value - x <- x %>% scale_x_values(suffix_df$scale_by) + 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] <- - x[is_not_negative_x] %>% - # Format numeric values to character-based numbers format_num_to_str_c( - context = context, decimals = decimals, sep_mark = sep_mark, - dec_mark = dec_mark, drop_trailing_zeros = drop_trailing_zeros, + x[is_not_negative_x], + context = context, + decimals = decimals, + sep_mark = sep_mark, + dec_mark = dec_mark, + drop_trailing_zeros = drop_trailing_zeros, drop_trailing_dec_mark = drop_trailing_dec_mark ) } @@ -726,31 +806,48 @@ fmt_symbol <- function(data, if (any(is_negative_x)) { + # Format numeric values to character-based numbers x_abs_str[is_negative_x] <- - x[is_negative_x] %>% - abs() %>% - # Format numeric values to character-based numbers format_num_to_str_c( - context = context, decimals = decimals, sep_mark = sep_mark, - dec_mark = dec_mark, drop_trailing_zeros = drop_trailing_zeros, + abs(x[is_negative_x]), + context = context, + decimals = decimals, + sep_mark = sep_mark, + dec_mark = dec_mark, + drop_trailing_zeros = drop_trailing_zeros, drop_trailing_dec_mark = drop_trailing_dec_mark ) } + # Format values with a symbol string x_str <- - # Format values with a symbol string format_symbol_str( - context = context, x_abs_str = x_abs_str, x = x, - symbol = symbol, incl_space = incl_space, + x_abs_str = x_abs_str, + x = x, + context = context, + symbol = symbol, + incl_space = incl_space, placement = placement - ) %>% - # Format values in accounting style + ) + + # Format values in accounting notation (if `accounting = TRUE`) + x_str <- format_as_accounting( - x = x, context = context, accounting = accounting - ) %>% - # With large-number suffixing support, we paste the - # vector of suffixes to the right of the values - paste_right(suffix_df$suffix) + 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 } @@ -836,6 +933,7 @@ fmt_percent <- function(data, pattern = "{x}", sep_mark = ",", dec_mark = ".", + force_sign = FALSE, incl_space = FALSE, placement = "right", locale = NULL) { @@ -871,6 +969,7 @@ fmt_percent <- function(data, pattern = pattern, sep_mark = sep_mark, dec_mark = dec_mark, + force_sign = force_sign, placement = placement, incl_space = incl_space, locale = locale @@ -1000,6 +1099,7 @@ fmt_currency <- function(data, pattern = "{x}", sep_mark = ",", dec_mark = ".", + force_sign = FALSE, placement = "left", incl_space = FALSE, locale = NULL) { @@ -1040,6 +1140,7 @@ fmt_currency <- function(data, pattern = pattern, sep_mark = sep_mark, dec_mark = dec_mark, + force_sign = force_sign, placement = placement, incl_space = incl_space, locale = locale @@ -1082,6 +1183,10 @@ fmt_currency <- function(data, #' use. The default number of decimal places is `1`. #' @param incl_space An option for whether to include a space between the value #' and the units. The default of `TRUE` uses a space character for separation. +#' @param force_sign 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. #' #' @return An object of class `gt_tbl`. #' @@ -1125,6 +1230,7 @@ fmt_bytes <- function(data, pattern = "{x}", sep_mark = ",", dec_mark = ".", + force_sign = FALSE, incl_space = TRUE, locale = NULL) { @@ -1142,21 +1248,23 @@ fmt_bytes <- function(data, if (!is.null(n_sigfig)) { # Stop function if `n_sigfig` does not have a valid value - validate_n_sigfig(n_sigfig) + 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") + 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") + byte_units <- c("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB") } fmt( @@ -1176,16 +1284,29 @@ fmt_bytes <- function(data, units_str <- byte_units[num_power_idx] x <- x / base^(num_power_idx-1) - x %>% - # Format numeric values to character-based numbers + # Format numeric values to character-based numbers + x_str <- format_num_to_str( - context = context, decimals = decimals, n_sigfig = n_sigfig, - sep_mark = sep_mark, dec_mark = dec_mark, + 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 ) %>% - paste_right(paste0(if (incl_space) " ", units_str)) + paste_right(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 } ) ) @@ -1926,6 +2047,7 @@ fmt_missing <- function(data, ifelse(is.na(x), missing_text, NA_character_) }, rtf = function(x) { + missing_text <- context_missing_text( missing_text = missing_text, diff --git a/man/fmt_bytes.Rd b/man/fmt_bytes.Rd index d07843960..7182cbcf8 100644 --- a/man/fmt_bytes.Rd +++ b/man/fmt_bytes.Rd @@ -17,6 +17,7 @@ fmt_bytes( pattern = "{x}", sep_mark = ",", dec_mark = ".", + force_sign = FALSE, incl_space = TRUE, locale = NULL ) @@ -73,6 +74,11 @@ of \verb{1,000}).} \item{dec_mark}{The character to use as a decimal mark (e.g., using \code{dec_mark = ","} with \code{0.152} would result in a formatted value of \verb{0,152}).} +\item{force_sign}{Should the positive sign be shown for positive numbers +(effectively showing a sign for all numbers except zero)? If so, use \code{TRUE} +for this option. The default is \code{FALSE}, where only negative numbers will +display a minus sign.} + \item{incl_space}{An option for whether to include a space between the value and the units. The default of \code{TRUE} uses a space character for separation.} diff --git a/man/fmt_currency.Rd b/man/fmt_currency.Rd index 201b427e8..3b3a99854 100644 --- a/man/fmt_currency.Rd +++ b/man/fmt_currency.Rd @@ -19,6 +19,7 @@ fmt_currency( pattern = "{x}", sep_mark = ",", dec_mark = ".", + force_sign = FALSE, placement = "left", incl_space = FALSE, locale = NULL @@ -113,6 +114,12 @@ of \verb{1,000}).} \item{dec_mark}{The character to use as a decimal mark (e.g., using \code{dec_mark = ","} with \code{0.152} would result in a formatted value of \verb{0,152}).} +\item{force_sign}{Should the positive sign be shown for positive values +(effectively showing a sign for all values except zero)? If so, use \code{TRUE} +for this option. The default is \code{FALSE}, where only negative numbers will +display a minus sign. This option is disregarded when using accounting +notation with \code{accounting = TRUE}.} + \item{placement}{The placement of the currency symbol. This can be either be \code{left} (the default) or \code{right}.} diff --git a/man/fmt_engineering.Rd b/man/fmt_engineering.Rd index b95315224..d2c7a4e5d 100644 --- a/man/fmt_engineering.Rd +++ b/man/fmt_engineering.Rd @@ -14,6 +14,7 @@ fmt_engineering( pattern = "{x}", sep_mark = ",", dec_mark = ".", + force_sign = FALSE, locale = NULL ) } @@ -54,6 +55,11 @@ of \verb{1,000}).} \item{dec_mark}{The character to use as a decimal mark (e.g., using \code{dec_mark = ","} with \code{0.152} would result in a formatted value of \verb{0,152}).} +\item{force_sign}{Should the positive sign be shown for positive values +(effectively showing a sign for all values except zero)? If so, use \code{TRUE} +for this option. The default is \code{FALSE}, where only negative numbers will +display a minus sign.} + \item{locale}{An optional locale ID that can be used for formatting the value according the locale's rules. Examples include \code{"en_US"} for English (United States) and \code{"fr_FR"} for French (France). The use of a valid diff --git a/man/fmt_integer.Rd b/man/fmt_integer.Rd index f1ee40122..19137838c 100644 --- a/man/fmt_integer.Rd +++ b/man/fmt_integer.Rd @@ -14,6 +14,7 @@ fmt_integer( suffixing = FALSE, pattern = "{x}", sep_mark = ",", + force_sign = FALSE, locale = NULL ) } @@ -73,6 +74,12 @@ characters are taken to be string literals.} (e.g., using \code{sep_mark = ","} with \code{1000} would result in a formatted value of \verb{1,000}).} +\item{force_sign}{Should the positive sign be shown for positive values +(effectively showing a sign for all values except zero)? If so, use \code{TRUE} +for this option. The default is \code{FALSE}, where only negative numbers will +display a minus sign. This option is disregarded when using accounting +notation with \code{accounting = TRUE}.} + \item{locale}{An optional locale ID that can be used for formatting the value according the locale's rules. Examples include \code{"en_US"} for English (United States) and \code{"fr_FR"} for French (France). The use of a valid diff --git a/man/fmt_number.Rd b/man/fmt_number.Rd index 4ff268279..5c1ea7af3 100644 --- a/man/fmt_number.Rd +++ b/man/fmt_number.Rd @@ -19,6 +19,7 @@ fmt_number( pattern = "{x}", sep_mark = ",", dec_mark = ".", + force_sign = FALSE, locale = NULL ) } @@ -98,6 +99,12 @@ of \verb{1,000}).} \item{dec_mark}{The character to use as a decimal mark (e.g., using \code{dec_mark = ","} with \code{0.152} would result in a formatted value of \verb{0,152}).} +\item{force_sign}{Should the positive sign be shown for positive values +(effectively showing a sign for all values except zero)? If so, use \code{TRUE} +for this option. The default is \code{FALSE}, where only negative numbers will +display a minus sign. This option is disregarded when using accounting +notation with \code{accounting = TRUE}.} + \item{locale}{An optional locale ID that can be used for formatting the value according the locale's rules. Examples include \code{"en_US"} for English (United States) and \code{"fr_FR"} for French (France). The use of a valid diff --git a/man/fmt_percent.Rd b/man/fmt_percent.Rd index 5d12d7073..9a0197752 100644 --- a/man/fmt_percent.Rd +++ b/man/fmt_percent.Rd @@ -17,6 +17,7 @@ fmt_percent( pattern = "{x}", sep_mark = ",", dec_mark = ".", + force_sign = FALSE, incl_space = FALSE, placement = "right", locale = NULL @@ -74,6 +75,12 @@ of \verb{1,000}).} \item{dec_mark}{The character to use as a decimal mark (e.g., using \code{dec_mark = ","} with \code{0.152} would result in a formatted value of \verb{0,152}).} +\item{force_sign}{Should the positive sign be shown for positive values +(effectively showing a sign for all values except zero)? If so, use \code{TRUE} +for this option. The default is \code{FALSE}, where only negative numbers will +display a minus sign. This option is disregarded when using accounting +notation with \code{accounting = TRUE}.} + \item{incl_space}{An option for whether to include a space between the value and the percent sign. The default is to not introduce a space character.} diff --git a/man/fmt_scientific.Rd b/man/fmt_scientific.Rd index d0a3d7a8c..e830fa5e5 100644 --- a/man/fmt_scientific.Rd +++ b/man/fmt_scientific.Rd @@ -14,6 +14,7 @@ fmt_scientific( pattern = "{x}", sep_mark = ",", dec_mark = ".", + force_sign = FALSE, locale = NULL ) } @@ -54,6 +55,11 @@ of \verb{1,000}).} \item{dec_mark}{The character to use as a decimal mark (e.g., using \code{dec_mark = ","} with \code{0.152} would result in a formatted value of \verb{0,152}).} +\item{force_sign}{Should the positive sign be shown for positive values +(effectively showing a sign for all values except zero)? If so, use \code{TRUE} +for this option. The default is \code{FALSE}, where only negative numbers will +display a minus sign.} + \item{locale}{An optional locale ID that can be used for formatting the value according the locale's rules. Examples include \code{"en_US"} for English (United States) and \code{"fr_FR"} for French (France). The use of a valid diff --git a/tests/testthat/test-fmt_bytes.R b/tests/testthat/test-fmt_bytes.R index 96d60faa7..acba23596 100644 --- a/tests/testthat/test-fmt_bytes.R +++ b/tests/testthat/test-fmt_bytes.R @@ -202,6 +202,37 @@ test_that("the `fmt_bytes()` function works correctly", { ) ) + # Format the `num` column to 3 decimal places, force the sign + expect_equal( + (tab %>% + fmt_bytes( + columns = num, decimals = 3, force_sign = TRUE) %>% + render_formats_test("html"))[["num"]], + c( + "−500 B", "0 B", "0 B", "0 B", "+500 B", "+1.023 kB", + "+1.001 kB", "+1.024 kB", "+1.049 MB", "+1.074 GB", "+1.1 TB", + "+1.126 PB", "+1.153 EB", "+1.181 ZB", "+1.209 YB", "+1 kB", + "+1 MB", "+1 GB", "+1 TB", "+1 PB", "+1 EB", "+1 ZB", "+1 YB", + "+15 YB", "+150 YB", "+1,500 YB", "+15,000 YB", "NA" + ) + ) + + # Format the `num` column, force the sign and + # define a pattern for decorating values + expect_equal( + (tab %>% + fmt_bytes( + columns = num, pattern = "*{x}*", force_sign = TRUE) %>% + render_formats_test("html"))[["num"]], + c( + "*−500 B*", "*0 B*", "*0 B*", "*0 B*", "*+500 B*", "*+1 kB*", + "*+1 kB*", "*+1 kB*", "*+1 MB*", "*+1.1 GB*", "*+1.1 TB*", "*+1.1 PB*", + "*+1.2 EB*", "*+1.2 ZB*", "*+1.2 YB*", "*+1 kB*", "*+1 MB*", + "*+1 GB*", "*+1 TB*", "*+1 PB*", "*+1 EB*", "*+1 ZB*", "*+1 YB*", + "*+15 YB*", "*+150 YB*", "*+1,500 YB*", "*+15,000 YB*", "NA" + ) + ) + # Format the `num` column to 2 decimal places, apply the `en_US` # locale and use all other defaults; extract `output_df` and compare # to expected values diff --git a/tests/testthat/test-fmt_engineering.R b/tests/testthat/test-fmt_engineering.R index bd71c1ef6..e1c9f446c 100644 --- a/tests/testthat/test-fmt_engineering.R +++ b/tests/testthat/test-fmt_engineering.R @@ -242,6 +242,71 @@ test_that("the `fmt_engineering()` function works correctly", { ) ) + # Format the `num` column to 2 decimal places, force the sign + expect_equal( + (tab %>% + fmt_engineering( + columns = "num", decimals = 3, force_sign = TRUE) %>% + render_formats_test("html"))[["num"]], + c( + "+82.030 × 1030", + "+829.300 × 1018", + "+492.032 × 109", + "+84.930 × 109", + "+5.043 × 109", + "+203.821 × 106", + "+84.729 × 106", + "+2.323 × 106", + "+230.323 × 103", + "+50.000 × 103", + "+1.000 × 103", "+10.000", + "+12.345 × 103", + "+1.234 × 103", "+123.450", "+1.234", + "+123.450 × 10−3", + "+12.346 × 10−6", + "−50.000 × 103", + "−1.000 × 103", "−10.000", + "−12.345 × 103", + "−1.234 × 103", + "−123.450", "−1.234", + "−123.450 × 10−3", + "−12.346 × 10−6" + ) + ) + + # Format the `num` column to 2 decimal places, force the sign and + # define a pattern for decorating values + expect_equal( + (tab %>% + fmt_engineering( + columns = "num", pattern = "*{x}*", force_sign = TRUE) %>% + render_formats_test("html"))[["num"]], + c( + "*+82.03 × 1030*", + "*+829.30 × 1018*", + "*+492.03 × 109*", + "*+84.93 × 109*", + "*+5.04 × 109*", + "*+203.82 × 106*", + "*+84.73 × 106*", + "*+2.32 × 106*", + "*+230.32 × 103*", + "*+50.00 × 103*", + "*+1.00 × 103*", "*+10.00*", + "*+12.35 × 103*", + "*+1.23 × 103*", "*+123.45*", "*+1.23*", + "*+123.45 × 10−3*", + "*+12.35 × 10−6*", + "*−50.00 × 103*", + "*−1.00 × 103*", "*−10.00*", + "*−12.35 × 103*", + "*−1.23 × 103*", + "*−123.45*", "*−1.23*", + "*−123.45 × 10−3*", + "*−12.35 × 10−6*" + ) + ) + # Format the `num` column to 2 decimal places, apply the `en_US` # locale and use all other defaults expect_equal( diff --git a/tests/testthat/test-fmt_integer.R b/tests/testthat/test-fmt_integer.R index 9d7f72805..406d6f564 100644 --- a/tests/testthat/test-fmt_integer.R +++ b/tests/testthat/test-fmt_integer.R @@ -129,6 +129,38 @@ test_that("the `fmt_integer()` function works correctly in the HTML context", { c("a1,836b", "a2,763b", "a937b", "a643b", "a212b", "a0b", "a(23)b") ) + # Format the `num_1` column to 2 decimal places, force the sign + expect_equal( + (tab %>% + fmt_integer( + columns = num_1, force_sign = TRUE) %>% + render_formats_test("html"))[["num_1"]], + c("+1,836", "+2,763", "+937", "+643", "+212", "0", "−23") + ) + + # Expect that using `force_sign = TRUE` with `accounting = TRUE` + # will render values in accounting format + expect_equal( + (tab %>% + fmt_integer( + columns = num_1, accounting = TRUE, force_sign = TRUE) %>% + render_formats_test("html"))[["num_1"]], + (tab %>% + fmt_integer( + columns = num_1, accounting = TRUE) %>% + render_formats_test("html"))[["num_1"]] + ) + + # Format the `num_1` column to 2 decimal places, force the sign and + # define a pattern for decorating values + expect_equal( + (tab %>% + fmt_integer( + columns = num_1, pattern = "*{x}*", force_sign = TRUE) %>% + render_formats_test("html"))[["num_1"]], + c("*+1,836*", "*+2,763*", "*+937*", "*+643*", "*+212*", "*0*", "*−23*") + ) + # Format the `num_1`, apply the `en_US` # locale and use all other defaults expect_equal( diff --git a/tests/testthat/test-fmt_number.R b/tests/testthat/test-fmt_number.R index aecfc9879..6f6e00d85 100644 --- a/tests/testthat/test-fmt_number.R +++ b/tests/testthat/test-fmt_number.R @@ -185,6 +185,41 @@ test_that("the `fmt_number()` function works correctly in the HTML context", { c("1,836.23", "2,763.39", "937.29", "643", "212.232", "0", "(23.24)") ) + # Format the `num_1` column to 2 decimal places, force the sign + expect_equal( + (tab %>% + fmt_number( + columns = num_1, decimals = 3, force_sign = TRUE) %>% + render_formats_test("html"))[["num_1"]], + c("+1,836.230", "+2,763.390", "+937.290", "+643.000", "+212.232", "0.000", "−23.240") + ) + + # Expect that using `force_sign = TRUE` with `accounting = TRUE` + # will render values in accounting format + expect_equal( + (tab %>% + fmt_number( + columns = num_1, decimals = 3, accounting = TRUE, force_sign = TRUE) %>% + render_formats_test("html"))[["num_1"]], + (tab %>% + fmt_number( + columns = num_1, decimals = 3, accounting = TRUE) %>% + render_formats_test("html"))[["num_1"]] + ) + + # Format the `num_1` column to 2 decimal places, force the sign and + # define a pattern for decorating values + expect_equal( + (tab %>% + fmt_number( + columns = num_1, pattern = "*{x}*", force_sign = TRUE) %>% + render_formats_test("html"))[["num_1"]], + c( + "*+1,836.23*", "*+2,763.39*", "*+937.29*", "*+643.00*", "*+212.23*", + "*0.00*", "*−23.24*" + ) + ) + # Format the `num_1` column to 2 decimal places, apply the `en_US` # locale and use all other defaults expect_equal( diff --git a/tests/testthat/test-fmt_percent.R b/tests/testthat/test-fmt_percent.R index d6181552b..937e6ebd1 100644 --- a/tests/testthat/test-fmt_percent.R +++ b/tests/testthat/test-fmt_percent.R @@ -202,6 +202,53 @@ test_that("the `fmt_percent()` function works correctly in the HTML context", { ) ) + # Format the `num_1` column to 2 decimal places, force the sign + expect_equal( + (tab %>% + fmt_percent( + columns = num_1, decimals = 2, drop_trailing_zeros = TRUE, + scale_values = FALSE, force_sign = TRUE + ) %>% + render_formats_test("html"))[["num_1"]], + c( + "+1,836.23%", "+2,763.39%", "+937.29%", + "+643%", "+212.23%", "0%", "−23.24%" + ) + ) + + # Expect that using `force_sign = TRUE` with `accounting = TRUE` + # will render values in accounting format + expect_equal( + (tab %>% + fmt_percent( + columns = num_1, decimals = 2, drop_trailing_zeros = TRUE, + scale_values = FALSE, accounting = TRUE, force_sign = TRUE + ) %>% + render_formats_test("html"))[["num_1"]], + (tab %>% + fmt_percent( + columns = num_1, decimals = 2, drop_trailing_zeros = TRUE, + scale_values = FALSE, accounting = TRUE + ) %>% + render_formats_test("html"))[["num_1"]] + ) + + # Format the `num_1` column to 2 decimal places, force the sign and + # define a pattern for decorating values + expect_equal( + (tab %>% + fmt_percent( + columns = num_1, decimals = 2, drop_trailing_zeros = TRUE, + pattern = "*{x}*", force_sign = TRUE + ) %>% + render_formats_test("html"))[["num_1"]], + c( + "*+183,623%*", "*+276,339%*", "*+93,729%*", + "*+64,300%*", "*+21,223.2%*", "*0%*", + "*−2,324%*" + ) + ) + # Format the `num_1` column to 2 decimal places, apply the `en_US` # locale and use all other defaults expect_equal( diff --git a/tests/testthat/test-fmt_scientific.R b/tests/testthat/test-fmt_scientific.R index a2b8b5ff9..d79f69574 100644 --- a/tests/testthat/test-fmt_scientific.R +++ b/tests/testthat/test-fmt_scientific.R @@ -226,6 +226,38 @@ test_that("the `fmt_scientific()` function works correctly", { ) ) + # Format the `num_1` column to 2 decimal places, force the sign + expect_equal( + (tab %>% + fmt_scientific( + columns = num_1, decimals = 3, force_sign = TRUE) %>% + render_formats_test("html"))[["num_1"]], + c( + "+1.836 × 103", + "+2.763 × 103", + "+9.373 × 102", + "+6.430 × 102", + "+2.232", "0.000", "−2.324 × 101" + ) + ) + + # Format the `num_1` column to 2 decimal places, force the sign and + # define a pattern for decorating values + expect_equal( + (tab %>% + fmt_scientific( + columns = num_1, pattern = "*{x}*", force_sign = TRUE) %>% + render_formats_test("html"))[["num_1"]], + c( + "*+1.84 × 103*", + "*+2.76 × 103*", + "*+9.37 × 102*", + "*+6.43 × 102*", + "*+2.23*", "*0.00*", + "*−2.32 × 101*" + ) + ) + # Format the `num_1` column to 2 decimal places, apply the `en_US` # locale and use all other defaults; extract `output_df` in the HTML # context and compare to expected values diff --git a/tests/testthat/test-l_fmt_engineering.R b/tests/testthat/test-l_fmt_engineering.R index b4b8e7741..70403f887 100644 --- a/tests/testthat/test-l_fmt_engineering.R +++ b/tests/testthat/test-l_fmt_engineering.R @@ -165,6 +165,49 @@ test_that("the `fmt_engineering()` function works correctly in the LaTeX context ) ) + # Format the `num` column to 2 decimal places, force the sign + expect_equal( + (tab %>% + fmt_engineering( + columns = "num", decimals = 3, force_sign = TRUE) %>% + render_formats_test("latex"))[["num"]], + c( + "$+82.030 \\times 10^{30}$", "$+829.300 \\times 10^{18}$", + "$+492.032 \\times 10^{9}$", "$+84.930 \\times 10^{9}$", + "$+5.043 \\times 10^{9}$", "$+203.821 \\times 10^{6}$", + "$+84.729 \\times 10^{6}$", "$+2.323 \\times 10^{6}$", + "$+230.323 \\times 10^{3}$", "$+50.000 \\times 10^{3}$", + "$+1.000 \\times 10^{3}$", "$+10.000$", "$+12.345 \\times 10^{3}$", + "$+1.234 \\times 10^{3}$", "$+123.450$", "$+1.234$", + "$+123.450 \\times 10^{-3}$", "$+12.346 \\times 10^{-6}$", + "$-50.000 \\times 10^{3}$", "$-1.000 \\times 10^{3}$", "$-10.000$", + "$-12.345 \\times 10^{3}$", "$-1.234 \\times 10^{3}$", "$-123.450$", + "$-1.234$", "$-123.450 \\times 10^{-3}$", "$-12.346 \\times 10^{-6}$" + ) + ) + + # Format the `num` column to 2 decimal places, force the sign and + # define a pattern for decorating values + expect_equal( + (tab %>% + fmt_engineering( + columns = "num", pattern = "*{x}*", force_sign = TRUE) %>% + render_formats_test("latex"))[["num"]], + c( + "*$+82.03 \\times 10^{30}$*", "*$+829.30 \\times 10^{18}$*", + "*$+492.03 \\times 10^{9}$*", "*$+84.93 \\times 10^{9}$*", + "*$+5.04 \\times 10^{9}$*", "*$+203.82 \\times 10^{6}$*", + "*$+84.73 \\times 10^{6}$*", "*$+2.32 \\times 10^{6}$*", + "*$+230.32 \\times 10^{3}$*", "*$+50.00 \\times 10^{3}$*", + "*$+1.00 \\times 10^{3}$*", "*$+10.00$*", "*$+12.35 \\times 10^{3}$*", + "*$+1.23 \\times 10^{3}$*", "*$+123.45$*", "*$+1.23$*", + "*$+123.45 \\times 10^{-3}$*", "*$+12.35 \\times 10^{-6}$*", + "*$-50.00 \\times 10^{3}$*", "*$-1.00 \\times 10^{3}$*", "*$-10.00$*", + "*$-12.35 \\times 10^{3}$*", "*$-1.23 \\times 10^{3}$*", "*$-123.45$*", + "*$-1.23$*", "*$-123.45 \\times 10^{-3}$*", "*$-12.35 \\times 10^{-6}$*" + ) + ) + # Format the `num` column to 2 decimal places, apply the `en_US` # locale and use all other defaults expect_equal( diff --git a/tests/testthat/test-l_fmt_integer.R b/tests/testthat/test-l_fmt_integer.R index 393e93969..66b66c3db 100644 --- a/tests/testthat/test-l_fmt_integer.R +++ b/tests/testthat/test-l_fmt_integer.R @@ -130,6 +130,41 @@ test_that("the `fmt_integer()` function works correctly in the LaTeX context", { "a$0$b", "a$(23)$b") ) + # Format the `num_1` column to 2 decimal places, force the sign + expect_equal( + (tab %>% + fmt_integer( + columns = num_1, force_sign = TRUE) %>% + render_formats_test("latex"))[["num_1"]], + c("$+1,836$", "$+2,763$", "$+937$", "$+643$", "$+212$", "$0$", "$-23$") + ) + + # Expect that using `force_sign = TRUE` with `accounting = TRUE` + # will render values in accounting format + expect_equal( + (tab %>% + fmt_integer( + columns = num_1, accounting = TRUE, force_sign = TRUE) %>% + render_formats_test("latex"))[["num_1"]], + (tab %>% + fmt_integer( + columns = num_1, accounting = TRUE) %>% + render_formats_test("latex"))[["num_1"]] + ) + + # Format the `num_1` column to 2 decimal places, force the sign and + # define a pattern for decorating values + expect_equal( + (tab %>% + fmt_integer( + columns = num_1, pattern = "*{x}*", force_sign = TRUE) %>% + render_formats_test("latex"))[["num_1"]], + c( + "*$+1,836$*", "*$+2,763$*", "*$+937$*", "*$+643$*", "*$+212$*", + "*$0$*", "*$-23$*" + ) + ) + # Format the `num_1`, apply the `en_US` # locale and use all other defaults expect_equal( diff --git a/tests/testthat/test-l_fmt_number.R b/tests/testthat/test-l_fmt_number.R index 807048673..145d75395 100644 --- a/tests/testthat/test-l_fmt_number.R +++ b/tests/testthat/test-l_fmt_number.R @@ -182,6 +182,44 @@ test_that("the `fmt_number()` function works correctly in the LaTeX context", { ) ) + # Format the `num_1` column to 2 decimal places, force the sign + expect_equal( + (tbl_latex %>% + fmt_number( + columns = num_1, decimals = 3, force_sign = TRUE) %>% + render_formats_test("latex"))[["num_1"]], + c( + "$+1,836.230$", "$+2,763.390$", "$+937.290$", "$+643.000$", + "$+212.232$", "$0.000$", "$-23.240$" + ) + ) + + # Expect that using `force_sign = TRUE` with `accounting = TRUE` + # will render values in accounting format + expect_equal( + (tbl_latex %>% + fmt_number( + columns = num_1, decimals = 3, accounting = TRUE, force_sign = TRUE) %>% + render_formats_test("latex"))[["num_1"]], + (tbl_latex %>% + fmt_number( + columns = num_1, decimals = 3, accounting = TRUE) %>% + render_formats_test("latex"))[["num_1"]] + ) + + # Format the `num_1` column to 2 decimal places, force the sign and + # define a pattern for decorating values + expect_equal( + (tbl_latex %>% + fmt_number( + columns = num_1, pattern = "*{x}*", force_sign = TRUE) %>% + render_formats_test("latex"))[["num_1"]], + c( + "*$+1,836.23$*", "*$+2,763.39$*", "*$+937.29$*", "*$+643.00$*", + "*$+212.23$*", "*$0.00$*", "*$-23.24$*" + ) + ) + # Format the `num_1` column to 2 decimal places, apply the `en_US` # locale and use all other defaults expect_equal( diff --git a/tests/testthat/test-l_fmt_percent.R b/tests/testthat/test-l_fmt_percent.R index 23258d698..f1a37493c 100644 --- a/tests/testthat/test-l_fmt_percent.R +++ b/tests/testthat/test-l_fmt_percent.R @@ -196,6 +196,52 @@ test_that("the `fmt_percent()` function works correctly in the LaTeX context", { ) ) + # Format the `num_1` column to 2 decimal places, force the sign + expect_equal( + (tbl_latex %>% + fmt_percent( + columns = num_1, decimals = 2, drop_trailing_zeros = TRUE, + scale_values = FALSE, force_sign = TRUE + ) %>% + render_formats_test("latex"))[["num_1"]], + c( + "$+1,836.23\\%$", "$+2,763.39\\%$", "$+937.29\\%$", "$+643\\%$", + "$+212.23\\%$", "$0\\%$", "$-23.24\\%$" + ) + ) + + # Expect that using `force_sign = TRUE` with `accounting = TRUE` + # will render values in accounting format + expect_equal( + (tbl_latex %>% + fmt_percent( + columns = num_1, decimals = 2, drop_trailing_zeros = TRUE, + scale_values = FALSE, accounting = TRUE, force_sign = TRUE + ) %>% + render_formats_test("latex"))[["num_1"]], + (tbl_latex %>% + fmt_percent( + columns = num_1, decimals = 2, drop_trailing_zeros = TRUE, + scale_values = FALSE, accounting = TRUE + ) %>% + render_formats_test("latex"))[["num_1"]] + ) + + # Format the `num_1` column to 2 decimal places, force the sign and + # define a pattern for decorating values + expect_equal( + (tbl_latex %>% + fmt_percent( + columns = num_1, decimals = 2, drop_trailing_zeros = TRUE, + pattern = "*{x}*", force_sign = TRUE + ) %>% + render_formats_test("latex"))[["num_1"]], + c( + "*$+183,623\\%$*", "*$+276,339\\%$*", "*$+93,729\\%$*", + "*$+64,300\\%$*", "*$+21,223.2\\%$*", "*$0\\%$*", "*$-2,324\\%$*" + ) + ) + # Format the `num_1` column to 2 decimal places, apply the `en_US` # locale and use all other defaults; extract `output_df` and compare # to expected values diff --git a/tests/testthat/test-l_fmt_scientific.R b/tests/testthat/test-l_fmt_scientific.R index de8b210c9..c94a9595a 100644 --- a/tests/testthat/test-l_fmt_scientific.R +++ b/tests/testthat/test-l_fmt_scientific.R @@ -88,6 +88,33 @@ test_that("the `fmt_scientific()` function works correctly", { "a $2.23$ b", "a $0.00$ b", "a $-2.32 \\times 10^{1}$ b") ) + # Format the `num_1` column to 2 decimal places, force the sign + expect_equal( + (tbl_latex %>% + fmt_scientific( + columns = num_1, decimals = 3, force_sign = TRUE) %>% + render_formats_test("latex"))[["num_1"]], + c( + "$+1.836 \\times 10^{3}$", "$+2.763 \\times 10^{3}$", + "$+9.373 \\times 10^{2}$", "$+6.430 \\times 10^{2}$", + "$+2.232$", "$0.000$", "$-2.324 \\times 10^{1}$" + ) + ) + + # Format the `num_1` column to 2 decimal places, force the sign and + # define a pattern for decorating values + expect_equal( + (tbl_latex %>% + fmt_scientific( + columns = num_1, pattern = "*{x}*", force_sign = TRUE) %>% + render_formats_test("latex"))[["num_1"]], + c( + "*$+1.84 \\times 10^{3}$*", "*$+2.76 \\times 10^{3}$*", + "*$+9.37 \\times 10^{2}$*", "*$+6.43 \\times 10^{2}$*", + "*$+2.23$*", "*$0.00$*", "*$-2.32 \\times 10^{1}$*" + ) + ) + # Format the `num_1` column to 2 decimal places, apply the `en_US` # locale and use all other defaults; extract `output_df` in the HTML # context and compare to expected values