diff --git a/R/format_data.R b/R/format_data.R index f96ed04233..1d6b566b36 100644 --- a/R/format_data.R +++ b/R/format_data.R @@ -142,115 +142,102 @@ fmt_number <- function(data, locale = NULL) { # Use locale-based marks if a locale ID is provided - if (!is.null(locale) && locale %in% locales$base_locale_id) { - sep_mark <- get_locale_sep_mark(locale = locale) - dec_mark <- get_locale_dec_mark(locale = locale) - } else if (!is.null(locale) && !(locale %in% locales$base_locale_id)) { - stop("The supplied `locale` is not available in the list of supported locales.", - call. = FALSE) - } + sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps) + dec_mark <- get_locale_dec_mark(locale, dec_mark) - # Provide an empty string for `sep_mark` if we choose - # to not use digit group separators - if (!use_seps) { - sep_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) - # 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) + # Create a function factory for the `fmt_number()` function + fmt_number_factory <- function(context = "html") { - # If choosing to perform large-number suffixing - # of numeric values, force `scale_by` to be 1.0 - if (!is.null(suffix_labels)) { + function(x) { - if (!missing(scale_by) && !identical(scale_by, 1.0)) { - warning("The value for `scale_by` can't be changed if `suffixing` is ", - "anything other than `FALSE`. The value provided to `scale_by` ", - "will be ignored.", - call. = FALSE) - } + # Define the marks by context + minus_mark <- context_minus_mark(context) + parens_marks <- context_parens_marks_number(context) - scale_by <- 1.0 - } + # Determine which of `x` are not NA + non_na_x <- !is.na(x) - # Capture expression in `rows` and `columns` - rows <- rlang::enquo(rows) - columns <- rlang::enquo(columns) + # Create a possibly shorter vector of non-NA `x` values + x_vals <- x[non_na_x] - # Pass `data`, `columns`, `rows`, and the formatting - # functions as a function list to `fmt()` - fmt(data = data, - columns = !!columns, - rows = !!rows, - fns = list( - default = function(x) { + # Create a tibble with scaled values for `x[non_na_x]` + # and the suffix labels to use for character formatting + suffix_df <- + num_suffix( + round(x_vals, decimals), + suffixes = suffix_labels, + scale_by = scale_by + ) - # Determine which of `x` are not NA - non_na_x <- !is.na(x) + # Scale the `x_vals` by the `scale_by` value + x_vals <- scale_x_values(x_vals, scale_by = suffix_df$scale_by) - # Create a tibble with scaled values for - # `x[non_na_x]` and the suffix labels to - # use for character formatting - suffix_df <- - num_suffix( - x = round(x[non_na_x], decimals), - suffixes = suffix_labels - ) + # Format all non-NA x values + x_str_vals <- + format_num_to_str( + x_vals, decimals, sep_mark, dec_mark, drop_trailing_zeros + ) - # If choosing to perform large-number suffixing - # of numeric values, replace `scale_by` with - # a vector of scaling values (of equal length - # with `x[non_na_x]`) - if (!is.null(suffix_labels)) { - scale_by <- suffix_df$scale_by[non_na_x] - } + # Paste vector of suffixes to the right of the `x_str_vals` + x_str_vals <- paste_right(x_str_vals, suffix_df$suffix) - # Create `x_str` with same length as `x` - x_str <- rep(NA_character_, length(x)) + # Perform negative value formatting + if (any(x_vals < 0)) { - # Format all non-NA x values - x_str[non_na_x] <- - formatC( - x = x[non_na_x] * scale_by, - digits = decimals, - mode = "double", - big.mark = sep_mark, - decimal.mark = dec_mark, - format = "f", - drop0trailing = drop_trailing_zeros) - - # Apply large-number suffixes to scaled and - # formatted values if that option is taken - if (!is.null(suffix_labels)) { - - # Apply vector of suffixes - x_str[non_na_x] <- - paste0(x_str[non_na_x], suffix_df$suffix[non_na_x]) - } + # Handle replacement of the minus mark + x_str_vals <- + x_str_vals %>% + tidy_gsub("-", minus_mark, fixed = TRUE) - # Handle negative values - if (negative_val == "parens") { + # Handle case where negative values are to be placed within parentheses + if (negative_val == "parens") { - # Determine which of `x` are not NA and also negative - negative_x <- x < 0 & !is.na(x) + # Selectively remove minus sign and paste between parentheses + x_str_vals[x_vals < 0] <- + paste_between( + x = gsub(paste0("^", minus_mark), "", x_str_vals[x_vals < 0]), + x_2 = parens_marks + ) + } + } - # Apply parentheses to the formatted value and remove - # the minus sign - x_str[negative_x] <- paste0("(", gsub("^-", "", x_str[negative_x]), ")") - } + # If in a LaTeX context, remove any double negative + # signs in the exponent + if (context == "latex") { + x_str_vals <- to_latex_math_mode(x_str_vals) + } - # Handle formatting of pattern - x_str[non_na_x] <- - apply_pattern_fmt_x( - pattern, - values = x_str[non_na_x] - ) + # Handle formatting of pattern + x_str_vals <- apply_pattern_fmt_x(pattern, x_str_vals) - x_str - } - )) + # Create `x_str` with the same length as `x`; place the + # `x_str_vals` into `str` (at the non-NA indices) + x_str <- rep(NA_character_, length(x)) + x_str[non_na_x] <- x_str_vals + x_str + } + } + + # Capture expression in `rows` and `columns` + rows <- rlang::enquo(rows) + columns <- rlang::enquo(columns) + + # Pass `data`, `columns`, `rows`, and the formatting + # functions as a function list to `fmt()` + fmt( + data = data, + columns = !!columns, + rows = !!rows, + fns = list( + html = fmt_number_factory(context = "html"), + latex = fmt_number_factory(context = "latex"), + default = fmt_number_factory(context = "default") + ) + ) } #' Format values to scientific notation @@ -316,93 +303,82 @@ fmt_scientific <- function(data, locale = NULL) { # Use locale-based marks if a locale ID is provided - if (!is.null(locale) && locale %in% locales$base_locale_id) { - sep_mark <- get_locale_sep_mark(locale = locale) - dec_mark <- get_locale_dec_mark(locale = locale) - } else if (!is.null(locale) && !(locale %in% locales$base_locale_id)) { - stop("The supplied `locale` is not available in the list of supported locales.", - call. = FALSE) - } + sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps = TRUE) + dec_mark <- get_locale_dec_mark(locale, dec_mark) - format_fcn_sci_notn_factory <- function(exp_start_str, exp_end_str) { + # Create a function factory for the `fmt_scientific()` function + fmt_scientific_factory <- function(context = "html") { function(x) { + # Define the marks by context + minus_mark <- context_minus_mark(context) + exp_marks <- context_exp_marks(context) + # Determine which of `x` are not NA non_na_x <- !is.na(x) - # Determine which of `x` don't require scientific notation - small_pos <- - ((x >= 1 & x < 10) | - (x <= -1 & x > -10) | - x == 0) & !is.na(x) + # Create a possibly shorter vector of non-NA `x` values + x_vals <- x[non_na_x] - # Create `x_str` with same length as `x` - x_str <- rep(NA_character_, length(x)) + # Scale the `x_vals` by the `scale_by` value + x_vals <- scale_x_values(x_vals, scale_by) - # Format the number component as a character vector - x_str[non_na_x] <- - formatC( - x = x[non_na_x] * scale_by, - digits = decimals, - mode = "double", - big.mark = sep_mark, - decimal.mark = dec_mark, - format = "e", - drop0trailing = drop_trailing_zeros) + # Determine which of `x` don't require the (x 10^n) + # since their order would be zero + small_pos <- has_order_zero(x_vals) + + # Format all non-NA x values + x_str_vals <- + format_num_to_str_e( + x_vals, decimals, sep_mark, dec_mark, + drop_trailing_zeros + ) # For any numbers that shouldn't have an exponent, remove # that portion from the character version if (any(small_pos)) { - x_str[small_pos] <- - split_scientific_notn(x_str[small_pos])$num + + x_str_vals[small_pos] <- + split_scientific_notn(x_str_vals[small_pos])$num } # For any non-NA numbers that do have an exponent, format # those according to the output context if (any(!small_pos)) { - sci_parts <- split_scientific_notn(x_str[non_na_x & !small_pos]) + sci_parts <- split_scientific_notn(x_str_vals[!small_pos]) - x_str[non_na_x & !small_pos] <- + x_str_vals[!small_pos] <- paste0( - sci_parts$num, exp_start_str, - sci_parts$exp, exp_end_str + sci_parts$num, exp_marks[1], + sci_parts$exp, exp_marks[2] ) } + # Handle replacement of the minus mark in number + # and exponent parts + x_str_vals <- + x_str_vals %>% + tidy_gsub("-", minus_mark, fixed = TRUE) + + # If in a LaTeX context, put formatted numbers + # in math mode + if (context == "latex") { + x_str_vals <- to_latex_math_mode(x_str_vals) + } + # Handle formatting of pattern - x_str[non_na_x] <- - apply_pattern_fmt_x( - pattern, - values = x_str[non_na_x] - ) + x_str_vals <- apply_pattern_fmt_x(pattern, x_str_vals) + # Create `x_str` with the same length as `x`; place the + # `x_str_vals` into `str` (at the non-NA indices) + x_str <- rep(NA_character_, length(x)) + x_str[non_na_x] <- x_str_vals x_str } } - # Create the default formatting function for scientific notation - format_fcn_sci_notn_default <- - format_fcn_sci_notn_factory( - exp_start_str = " x 10(", - exp_end_str = ")" - ) - - # Create the HTML formatting function for scientific notation - format_fcn_sci_notn_html <- - format_fcn_sci_notn_factory( - exp_start_str = " × 10", - exp_end_str = "" - ) - - # Create the LaTeX formatting function for scientific notation - format_fcn_sci_notn_latex <- - format_fcn_sci_notn_factory( - exp_start_str = "$ \\times 10^{", - exp_end_str = "}$" - ) - # Capture expression in `rows` and `columns` rows <- rlang::enquo(rows) columns <- rlang::enquo(columns) @@ -414,9 +390,10 @@ fmt_scientific <- function(data, columns = !!columns, rows = !!rows, fns = list( - html = format_fcn_sci_notn_html, - default = format_fcn_sci_notn_default, - latex = format_fcn_sci_notn_latex) + html = fmt_scientific_factory(context = "html"), + latex = fmt_scientific_factory(context = "latex"), + default = fmt_scientific_factory(context = "default") + ) ) } @@ -492,143 +469,103 @@ fmt_percent <- function(data, locale = NULL) { # Use locale-based marks if a locale ID is provided - if (!is.null(locale) && locale %in% locales$base_locale_id) { - sep_mark <- get_locale_sep_mark(locale = locale) - dec_mark <- get_locale_dec_mark(locale = locale) - } else if (!is.null(locale) && !(locale %in% locales$base_locale_id)) { - stop("The supplied `locale` is not available in the list of supported locales.", - call. = FALSE) - } - - # Provide an empty string for `sep_mark` if we choose - # to not use digit group separators - if (!use_seps) { - sep_mark <- "" - } + sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps) + dec_mark <- get_locale_dec_mark(locale, dec_mark) - # Capture expression in `rows` and `columns` - rows <- rlang::enquo(rows) - columns <- rlang::enquo(columns) + # Create a function factory for the `fmt_percent()` function + fmt_percent_factory <- function(context = "html") { - # Pass `data`, `columns`, `rows`, and the formatting - # functions as a function list to `fmt()` - fmt(data = data, - columns = !!columns, - rows = !!rows, - fns = list( - latex = function(x) { - - # Determine which of `x` are not NA - non_na_x <- !is.na(x) + function(x) { - # Create `x_str` with same length as `x` - x_str <- rep(NA_character_, length(x)) + # Define the marks by context + minus_mark <- context_minus_mark(context) + percent_mark <- context_percent_mark(context) + parens_marks <- context_parens_marks(context) - # Format all non-NA x values - x_str[non_na_x] <- - formatC( - x = x[non_na_x] * 100.0, - digits = decimals, - mode = "double", - big.mark = sep_mark, - decimal.mark = dec_mark, - format = "f", - drop0trailing = drop_trailing_zeros) - - if (placement == "right") { - - x_str[non_na_x] <- - paste0( - x_str[non_na_x], - ifelse(incl_space, " \\%", "\\%") - ) - - } else { - - x_str[non_na_x] <- - paste0( - ifelse(incl_space, "\\% ", "\\%"), - x_str[non_na_x] - ) - } + # Determine which of `x` are not NA + non_na_x <- !is.na(x) - # Handle negative values - if (negative_val == "parens") { + # Create a possibly shorter vector of non-NA `x` values + x_vals <- x[non_na_x] - # Determine which of `x` are not NA and also negative - negative_x <- x < 0 & !is.na(x) + # Scale the `x_vals` by the `scale_by` value + x_vals <- scale_x_values(x_vals, scale_by = 100) - # Apply parentheses to the formatted value and remove - # the minus sign - x_str[negative_x] <- paste0("(", gsub("^-", "", x_str[negative_x]), ")") - } + # Format all non-NA x values + x_str_vals <- + format_num_to_str( + x_vals, decimals, sep_mark, dec_mark, drop_trailing_zeros + ) - # Handle formatting of pattern - x_str[non_na_x] <- - apply_pattern_fmt_x( - pattern, - values = x_str[non_na_x] - ) + # Handle placement of the percent symbol + x_str_vals <- + x_str_vals %>% + paste_on_side( + x_side = ifelse(incl_space, " ", ""), + direction = placement + ) %>% + paste_on_side( + x_side = percent_mark, + direction = placement + ) %>% + swap_adjacent_text_groups( + pattern_1 = percent_mark, + pattern_2 = "-" + ) - x_str - }, - default = function(x) { + # Perform negative value formatting + if (any(x_vals < 0)) { - # Determine which of `x` are not NA - non_na_x <- !is.na(x) + # Handle replacement of the minus mark + x_str_vals <- + x_str_vals %>% + tidy_gsub("-", minus_mark, fixed = TRUE) - # Create `x_str` with same length as `x` - x_str <- rep(NA_character_, length(x)) + # Handle case where negative values are to be placed within parentheses + if (negative_val == "parens") { - # Format all non-NA x values - x_str[non_na_x] <- - formatC( - x = x[non_na_x] * 100.0, - digits = decimals, - mode = "double", - big.mark = sep_mark, - decimal.mark = dec_mark, - format = "f", - drop0trailing = drop_trailing_zeros) - - if (placement == "right") { - - x_str[non_na_x] <- - paste0( - x_str[non_na_x], - ifelse(incl_space, " %", "%") - ) - - } else { - - x_str[non_na_x] <- - paste0( - ifelse(incl_space, "% ", "%"), - x_str[non_na_x] - ) - } + # Selectively remove minus sign and paste between parentheses + x_str_vals[x_vals < 0] <- + paste_between( + x = gsub(paste0("^", minus_mark), "", x_str_vals[x_vals < 0]), + x_2 = parens_marks + ) + } + } - # Handle negative values - if (negative_val == "parens") { + # If in a LaTeX context, remove any double negative + # signs in the exponent + if (context == "latex") { + x_str_vals <- to_latex_math_mode(x_str_vals) + } - # Determine which of `x` are not NA and also negative - negative_x <- x < 0 & !is.na(x) + # Handle formatting of pattern + x_str_vals <- apply_pattern_fmt_x(pattern, x_str_vals) - # Apply parentheses to the formatted value and remove - # the minus sign - x_str[negative_x] <- paste0("(", gsub("^-", "", x_str[negative_x]), ")") - } + # Create `x_str` with the same length as `x`; place the + # `x_str_vals` into `str` (at the non-NA indices) + x_str <- rep(NA_character_, length(x)) + x_str[non_na_x] <- x_str_vals + x_str + } + } - # Handle formatting of pattern - x_str[non_na_x] <- - apply_pattern_fmt_x( - pattern, - values = x_str[non_na_x] - ) + # Capture expression in `rows` and `columns` + rows <- rlang::enquo(rows) + columns <- rlang::enquo(columns) - x_str - } - )) + # Pass `data`, `columns`, `rows`, and the formatting + # functions as a function list to `fmt()` + fmt( + data = data, + columns = !!columns, + rows = !!rows, + fns = list( + html = fmt_percent_factory(context = "html"), + latex = fmt_percent_factory(context = "latex"), + default = fmt_percent_factory(context = "default") + ) + ) } #' Format values as currencies @@ -742,325 +679,118 @@ fmt_currency <- function(data, locale = NULL) { # Use locale-based marks if a locale ID is provided - if (!is.null(locale) && locale %in% locales$base_locale_id) { - sep_mark <- get_locale_sep_mark(locale = locale) - dec_mark <- get_locale_dec_mark(locale = locale) - } else if (!is.null(locale) && !(locale %in% locales$base_locale_id)) { - stop("The supplied `locale` is not available in the list of supported locales.", - call. = FALSE) - } + sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps) + dec_mark <- get_locale_dec_mark(locale, dec_mark) # Stop function if `currency` does not have a valid value - if (!is_currency_valid(currency)) { - stop("The supplied `currency` is not available in the list of supported currencies.", - call. = FALSE) - } - - # Get the currency string for the HTML context - currency_str_html <- get_currency_str(currency) - - # Get the currency string for the non-HTML context - currency_str <- get_currency_str(currency, fallback_to_code = TRUE) + validate_currency(currency) # Get the number of decimal places - if (is.null(decimals) & use_subunits) { - - # Get decimal places using `get_currency_exponent()` fcn - if (currency %in% currency_symbols$curr_symbol) { - decimals <- 2 - } else { - decimals <- get_currency_exponent(currency = currency) - } - - } else if (is.null(decimals) & use_subunits == FALSE) { - decimals <- 0 - } - - # Provide an empty string for `sep_mark` if we choose - # to not use digit group separators - if (!use_seps) { - sep_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) - - # If choosing to perform large-number suffixing - # of numeric values, force `scale_by` to be 1.0 - if (!is.null(suffix_labels)) { - - if (!missing(scale_by) && !identical(scale_by, 1.0)) { - warning("The value for `scale_by` can't be changed if `suffixing` is ", - "anything other than `FALSE`. The value provided to `scale_by` ", - "will be ignored.", - call. = FALSE) - } - - scale_by <- 1.0 - } - - # Capture expression in `rows` and `columns` - rows <- rlang::enquo(rows) - columns <- rlang::enquo(columns) - - # Pass `data`, `columns`, `rows`, and the formatting - # functions as a function list to `fmt()` - fmt(data = data, - columns = !!columns, - rows = !!rows, - fns = list( - default = function(x) { - - # Determine which of `x` are not NA - non_na_x <- !is.na(x) - - # Determine which of `x` are not NA and also negative - negative_x <- x < 0 & !is.na(x) - - # Create `x_str` with same length as `x` - x_str <- rep(NA_character_, length(x)) - - # Create a tibble with scaled values for - # `x[non_na_x]` and the suffix labels to - # use for character formatting - suffix_df <- - num_suffix( - x = round(x[non_na_x], decimals), - suffixes = suffix_labels - ) - - # If choosing to perform large-number suffixing - # of numeric values, replace `scale_by` with - # a vector of scaling values (of equal length - # with `x[non_na_x]`) - if (!is.null(suffix_labels)) { - scale_by <- suffix_df$scale_by[non_na_x] - } - - # Format all non-NA x values - x_str[non_na_x] <- - formatC( - x = x[non_na_x] * scale_by, - digits = decimals, - mode = "double", - big.mark = sep_mark, - decimal.mark = dec_mark, - format = "f", - drop0trailing = FALSE) - - # Apply large-number suffixes to scaled and - # formatted values if that option is taken - if (!is.null(suffix_labels)) { - - # Apply vector of suffixes - x_str[non_na_x] <- - paste0(x_str[non_na_x], suffix_df$suffix[non_na_x]) - } - - # Handle placement of the currency symbol - if (placement == "left") { - - x_str[non_na_x] <- - paste0( - currency_str, - ifelse(incl_space, " ", ""), x_str[non_na_x] - ) - - } else { - - x_str[non_na_x] <- - paste0( - x_str[non_na_x], - ifelse(incl_space, " ", ""), currency_str - ) - } - - # Handle negative values - if (negative_val == "parens") { - - # Apply parentheses to the formatted value and remove - # the minus sign - x_str[negative_x] <- paste0("(", gsub("-", "", x_str[negative_x]), ")") - } - - # Handle formatting of pattern - x_str[non_na_x] <- - apply_pattern_fmt_x( - pattern, - values = x_str[non_na_x] - ) + decimals <- get_currency_decimals(currency, decimals, use_subunits) - x_str - }, - html = function(x) { - - # Determine which of `x` are not NA - non_na_x <- !is.na(x) - - # Determine which of `x` are not NA and also negative - negative_x <- x < 0 & !is.na(x) - - # Create `x_str` with same length as `x` - x_str <- rep(NA_character_, length(x)) + # 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) - # Create a tibble with scaled values for - # `x[non_na_x]` and the suffix labels to - # use for character formatting - suffix_df <- - num_suffix( - x = round(x[non_na_x], decimals), - suffixes = suffix_labels - ) - - # If choosing to perform large-number suffixing - # of numeric values, replace `scale_by` with - # a vector of scaling values (of equal length - # with `x[non_na_x]`) - if (!is.null(suffix_labels)) { - scale_by <- suffix_df$scale_by[non_na_x] - } + # Create a function factory for the `fmt_currency()` function + fmt_currency_factory <- function(context = "html") { - # Format all non-NA x values - x_str[non_na_x] <- - formatC( - x = x[non_na_x] * scale_by, - digits = decimals, - mode = "double", - big.mark = sep_mark, - decimal.mark = dec_mark, - format = "f", - drop0trailing = FALSE) - - # Apply large-number suffixes to scaled and - # formatted values if that option is taken - if (!is.null(suffix_labels)) { - - # Apply vector of suffixes - x_str[non_na_x] <- - paste0(x_str[non_na_x], suffix_df$suffix[non_na_x]) - } + function(x) { - # Handle placement of the currency symbol - if (placement == "left") { + # Define the marks by context + negative_currency_mark <- context_negative_currency_mark(context) + currency_str <- context_currency_str(context, currency) + currency_str_regex <- context_currency_str_regex(context) + parens_marks <- context_parens_marks(context) - x_str[non_na_x] <- - paste0( - currency_str_html, - ifelse(incl_space, " ", ""), x_str[non_na_x] - ) + # Determine which of `x` are not NA + non_na_x <- !is.na(x) - } else { + # Create a possibly shorter vector of non-NA `x` values + x_vals <- x[non_na_x] - x_str[non_na_x] <- - paste0( - x_str[non_na_x], - ifelse(incl_space, " ", ""), currency_str_html - ) - } + # Create a tibble with scaled values for `x[non_na_x]` + # and the suffix labels to use for character formatting + suffix_df <- + num_suffix( + round(x_vals, decimals), + suffixes = suffix_labels, + scale_by = scale_by + ) - # Handle negative values - if (negative_val == "parens") { + # Scale the `x_vals` by the `scale_by` value + x_vals <- scale_x_values(x_vals, scale_by = suffix_df$scale_by) - # Apply parentheses to the formatted value and remove - # the minus sign - x_str[negative_x] <- paste0("(", gsub("-", "", x_str[negative_x]), ")") - } + # Format all non-NA x values + x_str_vals <- format_num_to_str_c(x_vals, decimals, sep_mark, dec_mark) - # Handle formatting of pattern - x_str[non_na_x] <- - apply_pattern_fmt_x( - pattern, - values = x_str[non_na_x] - ) + # Paste vector of suffixes to the right of the `x_str_vals` + x_str_vals <- paste_right(x_str_vals, suffix_df$suffix) - x_str - }, - latex = function(x) { + # Handle placement of the currency symbol + x_str_vals <- + x_str_vals %>% + paste_currency_str(currency_str, incl_space, placement) - # Determine which of `x` are not NA - non_na_x <- !is.na(x) + # Perform negative value formatting + if (any(x_vals < 0)) { - # Determine which of `x` are not NA and also negative - negative_x <- x < 0 & !is.na(x) + # Handle replacement of the minus mark + x_str_vals <- + x_str_vals %>% + tidy_gsub("-", negative_currency_mark, fixed = TRUE) - # Create `x_str` with same length as `x` - x_str <- rep(NA_character_, length(x)) + # Handle case where negative values are to be placed within parentheses + if (negative_val == "parens") { - # Create a tibble with scaled values for - # `x[non_na_x]` and the suffix labels to - # use for character formatting - suffix_df <- - num_suffix( - x = round(x[non_na_x], decimals), - suffixes = suffix_labels + # Selectively remove minus sign and paste between parentheses + x_str_vals[x_vals < 0] <- + paste_between( + x = x_str_vals[x_vals < 0] %>% + tidy_gsub( + negative_currency_mark, "", + fixed = TRUE + ), + x_2 = parens_marks ) + } + } - # If choosing to perform large-number suffixing - # of numeric values, replace `scale_by` with - # a vector of scaling values (of equal length - # with `x[non_na_x]`) - if (!is.null(suffix_labels)) { - scale_by <- suffix_df$scale_by[non_na_x] - } - - # Format all non-NA x values - x_str[non_na_x] <- - formatC( - x = x[non_na_x] * scale_by, - digits = decimals, - mode = "double", - big.mark = sep_mark, - decimal.mark = dec_mark, - format = "f", - drop0trailing = FALSE) - - # Apply large-number suffixes to scaled and - # formatted values if that option is taken - if (!is.null(suffix_labels)) { - - # Apply vector of suffixes - x_str[non_na_x] <- - paste0(x_str[non_na_x], suffix_df$suffix[non_na_x]) - } - - # Handle placement of the currency symbol - if (placement == "left") { - - x_str[non_na_x] <- - paste0( - markdown_to_latex(currency_str), - ifelse(incl_space, " ", ""), x_str[non_na_x] - ) - - } else { + # If in a LaTeX context, wrap values in math mode + if (context == "latex") { - x_str[non_na_x] <- - paste0( - x_str[non_na_x], ifelse(incl_space, " ", ""), - markdown_to_latex(currency_str) - ) - } + x_str_vals <- + x_str_vals %>% + to_latex_math_mode() + } - # Handle negative values - if (negative_val == "parens") { + # Handle formatting of pattern + x_str_vals <- apply_pattern_fmt_x(pattern, x_str_vals) - # Apply parentheses to the formatted value and remove - # the minus sign - x_str[negative_x] <- paste0("(", gsub("-", "", x_str[negative_x]), ")") - } + # Create `x_str` with the same length as `x`; place the + # `x_str_vals` into `str` (at the non-NA indices) + x_str <- rep(NA_character_, length(x)) + x_str[non_na_x] <- x_str_vals + x_str + } + } - # Handle formatting of pattern - x_str[non_na_x] <- - apply_pattern_fmt_x( - pattern, - values = x_str[non_na_x] - ) + # Capture expression in `rows` and `columns` + rows <- rlang::enquo(rows) + columns <- rlang::enquo(columns) - x_str - } - )) + # Pass `data`, `columns`, `rows`, and the formatting + # functions as a function list to `fmt()` + fmt( + data = data, + columns = !!columns, + rows = !!rows, + fns = list( + html = fmt_currency_factory(context = "html"), + latex = fmt_currency_factory(context = "latex"), + default = fmt_currency_factory(context = "default") + ) + ) } #' Format values as dates diff --git a/R/tab_style.R b/R/tab_style.R index 8105147a8e..fba6e83efd 100644 --- a/R/tab_style.R +++ b/R/tab_style.R @@ -80,7 +80,6 @@ #' date <= "2015-12-15" #' ) %>% #' dplyr::select(-c(adj_close, volume)) %>% -#' dplyr::mutate(date = as.character(date)) %>% #' gt() %>% #' tab_style( #' style = cells_styles( diff --git a/R/utils.R b/R/utils.R index b858bca870..5ac18e838e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,4 +1,5 @@ -# Create a tibble containing date formats +#' Create a tibble containing date formats +#' #' @importFrom dplyr tribble #' @noRd date_formats <- function() { @@ -21,7 +22,8 @@ date_formats <- function() { "14", "y.mn.day", "%y/%m/%d") } -# Create a tibble containing time formats +#' Create a tibble containing time formats +#' #' @importFrom dplyr tribble #' @noRd time_formats <- function() { @@ -35,7 +37,8 @@ time_formats <- function() { "5", "h_p", "%I %P") } -# Transform `date_style` to `date_format` +#' Transform a `date_style` to a `date_format` +#' #' @importFrom dplyr filter pull #' @noRd get_date_format <- function(date_style) { @@ -59,7 +62,8 @@ get_date_format <- function(date_style) { } } -# Transform `time_style` to `time_format` +#' Transform a `time_style` to a `time_format` +#' #' @importFrom dplyr filter pull #' @noRd get_time_format <- function(time_style) { @@ -83,18 +87,8 @@ get_time_format <- function(time_style) { } } -# Determine if a provided `currency` type is valid -#' @noRd -is_currency_valid <- function(currency) { - - ifelse( - as.character(currency) %in% currency_symbols$curr_symbol | - as.character(currency) %in% currencies$curr_code | - as.character(currency) %in% currencies$curr_number, - TRUE, FALSE) -} - -# Transform `currency` to currency string +#' Transform a `currency` code to a currency string +#' #' @importFrom dplyr filter pull #' @noRd get_currency_str <- function(currency, @@ -149,7 +143,8 @@ get_currency_str <- function(currency, } } -# Transform `currency` to a currency exponent +#' Get a currency exponent from a currency code +#' #' @importFrom dplyr filter pull #' @noRd get_currency_exponent <- function(currency) { @@ -179,39 +174,13 @@ get_currency_exponent <- function(currency) { } } -# Get the `sep_mark` value from a locale -#' @importFrom dplyr filter pull -#' @noRd -get_locale_sep_mark <- function(locale) { - - sep_mark <- - locales %>% - dplyr::filter(base_locale_id == locale) %>% - dplyr::pull(group_sep) - - sep_mark <- ifelse(sep_mark == "", " ", sep_mark) - - sep_mark -} - -# Get the `dec_mark` value from a locale -#' @importFrom dplyr filter pull -#' @noRd -get_locale_dec_mark <- function(locale) { - - dec_mark <- - locales %>% - dplyr::filter(base_locale_id == locale) %>% - dplyr::pull(dec_sep) - - dec_mark -} - -# This function processes input text based on the class; if incoming text has -# the class `from_markdown` (applied by the `md()` helper function), then the -# text will be sanitized and transformed to HTML from Markdown. If the incoming -# text has the class `html` (applied by `html()` helper function), then -# the text will be seen as HTML and it won't undergo sanitization +#' Process text based on rendering context any applied classes +#' +#' If the incoming text has the class `from_markdown` (applied by the `md()` +#' helper function), then the text will be sanitized and transformed to HTML +#' from Markdown. If the incoming text has the class `html` (applied by `html()` +#' helper function), then the text will be seen as HTML and it won't undergo +#' sanitization #' @importFrom stringr str_replace_all #' @importFrom htmltools htmlEscape #' @importFrom commonmark markdown_html @@ -305,8 +274,11 @@ process_text <- function(text, } } -# Find common HTML entities resulting from HTML escaping and -# restore them back to ascii characters +#' Reverse HTML escaping +#' +#' Find common HTML entities resulting from HTML escaping and restore them back +#' to ASCII characters +#' @noRd unescape_html <- function(text) { text %>% @@ -315,7 +287,8 @@ unescape_html <- function(text) { tidy_gsub("&", "&") } -#' Transform Markdown text to HTML; also performs HTML escaping +#' Transform Markdown text to HTML and also perform HTML escaping +#' #' @importFrom commonmark markdown_html #' @noRd md_to_html <- function(x) { @@ -331,8 +304,11 @@ md_to_html <- function(x) { x } -# Transform Markdown text to LaTeX; also escapes ASCII -# characters with special meaning in LaTeX +#' Transform Markdown text to LaTeX +#' +#' In addition to the Markdown-to-LaTeX text transformation, +#' `markdown_to_latex()` also escapes ASCII characters with special meaning in +#' LaTeX. #' @importFrom commonmark markdown_latex #' @noRd markdown_to_latex <- function(text) { @@ -360,7 +336,8 @@ markdown_to_latex <- function(text) { unname() } -# Transform Markdown text to plain text +#' Transform Markdown text to plain text +#' #' @importFrom commonmark markdown_text #' @noRd markdown_to_text <- function(text) { @@ -411,9 +388,13 @@ apply_pattern_fmt_x <- function(pattern, ) } +#' Get a vector of indices for large-number suffixing +#' #' @importFrom utils head #' @noRd -non_na_index <- function(values, index, default_value = NA) { +non_na_index <- function(values, + index, + default_value = NA) { if (is.logical(index)) { index <- is.integer(index) @@ -467,16 +448,17 @@ non_na_index <- function(values, index, default_value = NA) { positions[index] } -# This function operates on a vector of numerical -# values and returns a tibble where each row -# represents a scaled values for `x` and the -# correct suffix to use during x's character-based -# formatting +#' Get a tibble of scaling values and suffixes +#' +#' The `num_suffix()` function operates on a vector of numerical values and +#' returns a tibble where each row represents a scaled value for `x` and the +#' correct suffix to use during `x`'s character-based formatting #' @importFrom dplyr tibble #' @noRd num_suffix <- function(x, suffixes = c("K", "M", "B", "T"), - base = 1000) { + base = 1000, + scale_by) { # If `suffixes` is a zero-length vector, we # provide a tibble that will ultimately not @@ -485,7 +467,7 @@ num_suffix <- function(x, return( dplyr::tibble( - scale_by = rep_len(1, length(x)), + scale_by = rep_len(scale_by, length(x)), suffix = rep_len("", length(x)) ) ) @@ -542,19 +524,28 @@ num_suffix <- function(x, ) } -# Create an `isFALSE`-based helper function that -# works with earlier versions of R (the `isFALSE()` -# function was introduced in R 3.5.0) +#' An `isFALSE`-based helper function +#' +#' The `is_false()` function is similar to the `isFALSE()` function that was +#' introduced in R 3.5.0 except that this implementation works with earlier +#' versions of R. +#' @param x The single value to test for whether it is `FALSE`. +#' @noRd is_false = function(x) { is.logical(x) && length(x) == 1L && !is.na(x) && !x } -# This function normalizes the `suffixing` input to a -# character vector which is later appended to scaled -# numerical values; the input can either be a single -# logical value or a character vector -normalize_suffixing_inputs <- function(suffixing) { +#' Normalize all suffixing input values +#' +#' This function normalizes the `suffixing` input to a character vector which is +#' later appended to scaled numerical values; the input can either be a single +#' logical value or a character vector +#' @param suffixing,scale_by The `suffixing` and `scale_by` options in some +#' `fmt_*()` functions. +#' @noRd +normalize_suffixing_inputs <- function(suffixing, + scale_by) { if (is_false(suffixing)) { @@ -565,16 +556,21 @@ normalize_suffixing_inputs <- function(suffixing) { } else if (isTRUE(suffixing)) { + # Issue a warning if `scale_by` is not 1.0 (the default) + warn_on_scale_by_input(scale_by) + # If `suffixing` is TRUE, return the default # set of suffixes return(c("K", "M", "B", "T")) } else if (is.character(suffixing)) { + # Issue a warning if `scale_by` is not 1.0 (the default) + warn_on_scale_by_input(scale_by) + # In the case that a character vector is provided # to `suffixing`, we first want to check if there # are any names provided - # TODO: found that the conditional below seems # better than other solutions to determine whether # the vector is even partially named @@ -599,7 +595,22 @@ normalize_suffixing_inputs <- function(suffixing) { } } -# Derive a label based on a formula or a function name +#' If performing large-number suffixing, warn on `scale_by` != 1 +#' +#' @param scale_by The `scale_by` option in some `fmt_*()` functions. +#' @noRd +warn_on_scale_by_input <- function(scale_by) { + + if (scale_by != 1) { + warning("The value for `scale_by` cannot be changed if `suffixing` is ", + "anything other than `FALSE`. The value provided to `scale_by` ", + "will be ignored.", + call. = FALSE) + } +} + +#' Derive a label based on a formula or a function name +#' #' @import rlang #' @noRd derive_summary_label <- function(fn) { @@ -615,21 +626,25 @@ derive_summary_label <- function(fn) { } #nocov start - -# This function is a conveient wrapper for `system.file()` where the `package` -# refers to this package +#' A `system.file()` replacement specific to this package +#' +#' This is a conveient wrapper for `system.file()` where the `package` refers to +#' this package. +#' @noRd system_file <- function(file) { system.file(file, package = "gt") } - #nocov end -# This function removes entire HTML tags from input text +#' Remove all HTML tags from input text +#' +#' @noRd remove_html <- function(text) { gsub("<.+?>", "", text) } -# This function transforms a CSS stylesheet to a tibble representation +#' Transform a CSS stylesheet to a tibble representation +#' #' @importFrom dplyr bind_rows tibble filter mutate case_when select pull #' @importFrom stringr str_remove str_extract str_trim str_detect #' @noRd @@ -686,7 +701,8 @@ get_css_tbl <- function(data) { css_tbl } -# Create an inlined style block from a CSS tibble +#' Create an inlined style block from a CSS tibble +#' #' @importFrom dplyr filter select distinct mutate pull #' @importFrom stringr str_split #' @noRd @@ -712,7 +728,8 @@ create_inline_styles <- function(class_names, "\"") } -# Transform HTML to inlined HTML using a CSS tibble +#' Transform HTML to inlined HTML using a CSS tibble +#' #' @importFrom stringr str_extract str_replace str_match #' @noRd inline_html_styles <- function(html, css_tbl) { @@ -779,6 +796,11 @@ inline_html_styles <- function(html, css_tbl) { html } +#' Split any strings that are values in scientific notation +#' +#' @param x_str The input character vector of values formatted in scientific +#' notation. +#' @noRd split_scientific_notn <- function(x_str) { exp_parts <- strsplit(x_str, "e|E") @@ -788,14 +810,25 @@ split_scientific_notn <- function(x_str) { list(num = num_part, exp = exp_part) } -# This function is wrapper for `gsub()` that uses default argument values and -# rearranges first three arguments for better pipelining +#' Wrapper for `gsub()` where `x` is the first argument +#' +#' This function is wrapper for `gsub()` that uses default argument values and +#' rearranges first three arguments for better pipelining +#' @param x,pattern,replacement,fixed Select arguments from the `gsub()` +#' function. +#' @noRd tidy_gsub <- function(x, pattern, replacement, fixed = FALSE) { gsub(pattern, replacement, x, fixed = fixed) } -# Options setter for the `opts_df` data frame +#' An options setter for the `opts_df` data frame +#' +#' @param opts_df The `opts_df` data frame. +#' @param option The option name; a unique value in the `parameter` column of +#' `opts_df`. +#' @param value The value to set for the given `option`. +#' @noRd opts_df_set <- function(opts_df, option, value) { opts_df[which(opts_df$parameter == option), "value"] <- value @@ -803,13 +836,21 @@ opts_df_set <- function(opts_df, option, value) { opts_df } -# Options getter for the `opts_df` data frame +#' An options getter for the `opts_df` data frame +#' +#' @inheritParams opts_df_set +#' @noRd opts_df_get <- function(opts_df, option) { opts_df[which(opts_df$parameter == option), "value"] } -# Upgrade `cells_*()` to a list() if a single instance provided +#' Upgrader function for `cells_*` objects +#' +#' Upgrade a `cells_*` object to a `list()` if only a single instance is +#' provided. +#' @param locations Any `cells_*` object. +#' @noRd as_locations <- function(locations) { if (!inherits(locations, "location_cells")) { @@ -827,6 +868,9 @@ as_locations <- function(locations) { locations } +#' Create a vector of glyphs to use for footnotes +#' +#' @noRd footnote_glyphs <- function(x, glyphs) { @@ -852,9 +896,14 @@ footnote_glyphs <- function(x, glyphs_val, glyphs_rep, FUN = function(val_i, rep_i) { paste(rep(val_i, rep_i), collapse = "")} - ) %>% unname() + ) %>% + unname() } +#' Determine whether an object is a `gt_tbl` +#' +#' @param data A table object that is created using the \code{\link{gt}()} +#' function. #' @importFrom checkmate test_class #' @noRd is_gt <- function(data) { @@ -862,6 +911,11 @@ is_gt <- function(data) { checkmate::test_class(data, "gt_tbl") } +#' Stop any function if object is not a `gt_tbl` object +#' +#' @param data A table object that is created using the \code{\link{gt}()} +#' function. +#' @noRd stop_if_not_gt <- function(data) { if (!is_gt(data)) { diff --git a/R/utils_formatters.R b/R/utils_formatters.R new file mode 100644 index 0000000000..99c548b363 --- /dev/null +++ b/R/utils_formatters.R @@ -0,0 +1,369 @@ +#' Filter an internal table to a single row with filtering expressions +#' +#' @param table The table to filter down to one row. +#' @param column The column from which the single value should be obtained. +#' @param ... The arguments passed to `dplyr::filter()` +#' @import rlang +#' @importFrom dplyr filter +#' @noRd +filter_table_to_value <- function(table, column, ...) { + + filter_args_enquos <- rlang::enquos(...) + column_enquo <- rlang::enquo(column) + + filtered_tbl <- dplyr::filter(table, !!!filter_args_enquos) + + if (nrow(filtered_tbl) != 1) { + stop("Internal error in `gt:::filter_table_to_row()`:\n", + " * The filtered table doesn't result in a table of exactly one row. ", + "Found ", nrow(filtered_tbl), " rows.", + call. = FALSE) + } + + filtered_tbl %>% + dplyr::pull(!!column_enquo) +} + +#' Validate the user-supplied `locale` value +#' +#' @param locale The user-supplied `locale` value, found in several `fmt_*()` +#' functions. This is expected as `NULL` if not supplied by the user. +#' @noRd +validate_locale <- function(locale) { + + # Stop function if the `locale` provided + # isn't a valid one + if (!(locale %in% locales$base_locale_id)) { + stop("The supplied `locale` is not available in the list of supported locales.\n", + " * Use the `info_locales()` function to see which locales can be used.", + call. = FALSE) + } +} + +#' Validate the user-supplied `currency` value +#' +#' @param currency The user-supplied `currency` value, found in the +#' `fmt_currency()` function. +#' @noRd +validate_currency <- function(currency) { + + # Stop function if the `currency` provided + # isn't a valid one + if (!( + as.character(currency) %in% currency_symbols$curr_symbol | + as.character(currency) %in% currencies$curr_code | + as.character(currency) %in% currencies$curr_number)) { + stop("The supplied `currency` is not available in the list of supported currencies.\n", + " * Use the `info_currencies()` function to see which currencies can be used.\n", + " * See `?fmt_currency` to understand which input types are valid.", + call. = FALSE) + } +} + +#' Get the `sep_mark` value based on a locale +#' +#' @param locale The user-supplied `locale` value, found in several `fmt_*()` +#' functions. This is expected as `NULL` if not supplied by the user. +#' @param default The default value for the `sep_mark`. +#' @param use_seps A logical value for whether to use separators at all. +#' @importFrom dplyr filter pull +#' @noRd +get_locale_sep_mark <- function(locale = NULL, + default, + use_seps) { + + # If `use_seps` is FALSE, then force + # `sep_mark` to be an empty string + if (!use_seps) { + return("") + } + + # If `locale` is NULL then return the + # default `sep_mark` + if (is.null(locale)) { + return(default) + } + + # Stop function if the `locale` provided + # isn't a valid one + validate_locale(locale) + + # Get the correct `group_sep` value from the + # `gt:::locales` lookup table + sep_mark <- + filter_table_to_value(locales, group_sep, base_locale_id == locale) + + # TODO: Modify `locales` table to replace `""` with + # `" "` in `group_sep` column; once that is done, the + # below statement can be safely removed + if (sep_mark == "") sep_mark <- " " + + sep_mark +} + +#' Get the `dec_mark` value based on a locale +#' +#' @param locale The user-supplied `locale` value, found in several `fmt_*()` +#' functions. This is expected as `NULL` if not supplied by the user. +#' @param default The default value for the `dec_mark`. +#' @importFrom dplyr filter pull +#' @noRd +get_locale_dec_mark <- function(locale = NULL, + default) { + + # If `locale` is NULL then return the + # default `dec_mark` + if (is.null(locale)) { + return(default) + } + + # Stop function if the `locale` provided + # isn't a valid one + validate_locale(locale) + + # Get the correct `dec_sep` value from the + # `gt:::locales` lookup table + filter_table_to_value(locales, dec_sep, base_locale_id == locale) +} + +#' Determine which numbers in scientific notation would be zero order +#' +#' @param x A vector of numeric values, including `NA` values +#' @noRd +has_order_zero <- function(x) { + + ( + (x >= 1 & x < 10) | (x <= -1 & x > -10) | x == 0 + ) & !is.na(x) +} + +#' @noRd +get_currency_decimals <- function(currency, + decimals, + use_subunits) { + + # Get the number of decimal places + if (is.null(decimals) && use_subunits) { + + # Get decimal places using `get_currency_exponent()` fcn + if (currency %in% currency_symbols$curr_symbol) { + + return(2) + + } else { + + return(get_currency_exponent(currency)) + } + + } else if (is.null(decimals) && !use_subunits) { + + return(0) + + } else { + return(decimals) + } +} + +#' Apply a scaling factor to a vector of numeric values +#' +#' @param x A vector of numeric values. +#' @param scale_by A numeric scalar. +#' @noRd +scale_x_values <- function(x, + scale_by) { + checkmate::assert_numeric( + scale_by, + finite = TRUE, + any.missing = FALSE) + + len <- length(scale_by) + + # Stop function if the length of `scale_by` + # is not 1 of the length of `x` + if (!any(len == 1, len == length(x))) { + stop("The length of the `scale_by` vector must be 1 or the length of `x`.", + call. = FALSE) + } + + x * scale_by +} + +#' A `formatC()` call for `fmt_number()` and `fmt_percent()` +#' +#' @param x A vector of numeric values. +#' @param decimals The number of decimal places (`digits`). +#' @param sep_mark The separator for number groups (`big.mark`). +#' @param dec_mark The decimal separator mark (`decimal.mark`). +#' @param format The numeric format for `formatC()`. +#' @param drop_trailing_zeros Option to exclude trailing decimal zeros. +#' @noRd +format_num_to_str <- function(x, + decimals, + sep_mark, + dec_mark, + drop_trailing_zeros, + format = "f") { + + formatC( + x = x, + digits = decimals, + mode = "double", + big.mark = sep_mark, + decimal.mark = dec_mark, + format = format, + drop0trailing = drop_trailing_zeros + ) +} + +#' A `formatC()` call for `fmt_scientific()` +#' +#' @inheritParams format_num_to_str +#' @noRd +format_num_to_str_e <- function(x, + decimals, + sep_mark, + dec_mark, + drop_trailing_zeros) { + + format_num_to_str( + x, + decimals, + sep_mark, + dec_mark, + format = "e", + drop_trailing_zeros) +} + +#' A `formatC()` call for `fmt_currency()` +#' +#' @inheritParams format_num_to_str +#' @noRd +format_num_to_str_c <- function(x, + decimals, + sep_mark, + dec_mark) { + + format_num_to_str( + x, + decimals, + sep_mark, + dec_mark, + format = "f", + drop_trailing_zeros = FALSE) +} + +#' Surround formatted values with `$`s for LaTeX +#' +#' @noRd +to_latex_math_mode <- function (x) { + + x %>% + paste_between(x_2 = c("$", "$")) +} + +context_minus_mark <- function(context) { + + switch(context, + html = "−", + "-") +} + +context_negative_currency_mark <- function(context) { + + switch(context, + html = "−", + "-") +} + +context_parens_marks <- function(context) { + + switch(context, + html = c("(", ")"), + latex = c("\\left(", "\\right)"), + c("(", ")")) +} + +context_parens_marks_number <- function(context) { + + switch(context, + html = c("(", ")"), + latex = c("(", ")"), + c("(", ")")) +} + +context_exp_marks <- function(context) { + + switch(context, + html = c(" × 10", ""), + latex = c(" \\times 10^{", "}"), + c(" x 10(", ")")) +} + +context_percent_mark <- function(context) { + + switch(context, + html = "%", + latex = "\\%", + "%") +} + +context_currency_str_regex <- function(context) { + + switch(context, + latex = "\\\\$", + "\\$") +} + +context_currency_str <- function(context, currency) { + + switch(context, + html = { + get_currency_str(currency) + }, + latex = { + currency %>% + get_currency_str(fallback_to_code = TRUE) %>% + markdown_to_latex() %>% + paste_between(x_2 = c("\\text{", "}")) + }, + { + currency %>% + get_currency_str(fallback_to_code = TRUE) + }) +} + +paste_currency_str <- function(x, + currency_str, + incl_space, + placement) { + + vapply(FUN.VALUE = character(1), USE.NAMES = FALSE, x, function(x) { + + if (grepl("^-", x)) { + + x %>% + tidy_gsub("^-", "") %>% + paste_on_side( + x_side = ifelse(incl_space, " ", ""), + direction = placement + ) %>% + paste_on_side( + x_side = currency_str, + direction = placement + ) %>% + paste_left("-") + + } else { + + x %>% + paste_on_side( + x_side = ifelse(incl_space, " ", ""), + direction = placement + ) %>% + paste_on_side( + x_side = currency_str, + direction = placement + ) + } + }) +} diff --git a/R/utils_general_str_formatting.R b/R/utils_general_str_formatting.R new file mode 100644 index 0000000000..d35dfde4c8 --- /dev/null +++ b/R/utils_general_str_formatting.R @@ -0,0 +1,317 @@ +###### +# General String Formatters +###### + +#' Flexibly split a string into two pieces +#' +#' @param x The string to split into a character vector of length 2. +#' @param before,after Either an exact numeric position for where splitting will +#' occur, or a regular expression to match on a range of characters. We can +#' use either `before` or `after` (but not both) with this variable input to +#' accurately define which side of the match is the split position. +#' @noRd +split_string_2 <- function(x, + before = NULL, + after = NULL) { + + # Stop function if `x` is not of class character + if (!inherits(x, "character")) { + stop("Internal error in `gt:::paste_within()`:\n", + "* The `x` object must be of class character.", + call. = FALSE) + } + + # Stop function if the length of `x` is not 1 + if (length(x) != 1) { + stop("Internal error in `gt:::paste_within()`:\n", + "* The length of the `x` must be exactly 1.", + call. = FALSE) + } + + # Get the length of the string `x` + x_length <- nchar(x) + + # If neither of `before` or `after` has a value, + # stop the function + if (is.null(before) && is.null(after)) { + stop("Internal error in `gt:::split_string_2()`:\n", + " * Both `before` and `after` cannot be `NULL`.", + call. = FALSE) + } + + # If both `before` and `after` have values, stop + # the function + if (!is.null(before) && !is.null(after)) { + stop("Internal error in `gt:::split_string_2()`:\n", + " * A value must be provided to either `before` or `after`, not both.", + call. = FALSE) + } + + # Collapse value for either `before` or `after`; + # add a class to retain the direction-of-split + # information + if (!is.null(before)) { + input <- before + class(input) <- c("before", class(before)) + } else if (!is.null(after)) { + input <- after + class(input) <- c("after", class(after)) + } + + if (inherits(input, "character")) { + + # Use the pattern (`input`) with the input string + # `x` with `regexpr()` to get the matching output + regexpr_out <- regexpr(input, x) + + # If there is no match, return a character vector + # of length 2 (original string, then empty string) + if (as.numeric(regexpr_out) == -1) { + return(c(x, "")) + } + + # Define the start position for the matched characters + split_start <- + regexpr_out %>% + as.numeric() + + # Define the stop position for the matched characters + split_stop <- + attr(regexpr_out, "match.length", exact = TRUE) + split_start - 1 + + } else if (inherits(input, "numeric")) { + + # Stop function if the index position is not valid + if (input > x_length) { + stop("Internal error in `gt:::split_string_2()`:\n", + "* The numeric value provided cannot be greater than ", x_length, ".", + call. = FALSE) + } + + # Define the start and stop positions as + # the single `input` value + split_start <- split_stop <- input %>% as.numeric() + } + + # Perform the split either before the matched characters + if (inherits(input, "before")) { + + x_2 <- c(substr(x, 0, split_start - 1), substr(x, split_start, x_length)) + + } else if (inherits(input, "after")) { + + x_2 <- c(substr(x, 0, split_stop), substr(x, split_stop + 1, x_length)) + } + + x_2 +} + +#' Flexibly split a string into two pieces +#' +#' @param x A character vector that is to be pasted between the +#' first and second elements of `x_2`. +#' @param x_2 A character vector of length 2. +#' @noRd +paste_between <- function(x, + x_2) { + + # Stop function if `x_2` is not of class character + if (!inherits(x_2, "character")) { + stop("Internal error in `gt:::paste_between()`:\n", + "* The `x_2` object must be of class character.", + call. = FALSE) + } + + # Stop function if the length of `x_2` is not 2 + if (length(x_2) != 2) { + stop("Internal error in `gt:::paste_between()`:\n", + "* The length of the `x_2` must be exactly 2.", + call. = FALSE) + } + + # Stop function if `x` is not of class character + if (!inherits(x, "character")) { + stop("Internal error in `gt:::paste_between()`:\n", + "* The `x` object must be of class character.", + call. = FALSE) + } + + paste0(x_2[1], x, x_2[2]) +} + +#' Paste a string either onto the left or the right of another string +#' +#' @param x A character vector of length equal to that of `x_side`. +#' @param x_side Another character vector, with a length equal to that of `x`. +#' It will be pasted either to the left or to the right of `x` depending on +#' the `direction`. +#' @param direction The side that `x_side` will be relative to `x`. This can +#' be `left` or `right`. +#' @noRd +paste_on_side <- function(x, + x_side, + direction) { + + # Stop function if `direction` is not valid + if (!(direction %in% c("left", "right"))) { + stop("Internal error in `gt:::paste_on_side()`:\n", + "* The `direction` must be either `left` or `right`.", + call. = FALSE) + } + + # Stop function if `x` and `x_side` are not both of class character + if (any(!inherits(x, "character"), !inherits(x_side, "character"))) { + stop("Internal error in `gt:::paste_on_side()`:\n", + "* The `x` and `x_side` objects must be of class character.", + call. = FALSE) + } + + len <- length(x_side) + + # Stop function if the length of `x_side` is not 1 of the length of `x` + if (!any(len == 1, len == length(x))) { + stop("The length of the `x_side` vector must be 1 or the length of `x`.", + call. = FALSE) + } + + if (direction == "left") { + + return(paste0(x_side, x)) + + } else if (direction == "right") { + + return(paste0(x, x_side)) + } +} + +#' Paste a string onto the left side of another string +#' +#' @inheritParams paste_on_side +#' @param x_left Another character vector of length 1 that is to be pasted to +#' the left of `x`. +#' @noRd +paste_left <- function(x, x_left) { + paste_on_side(x, x_side = x_left, direction = "left") +} + +#' Paste a string onto the right side of another string +#' +#' @inheritParams paste_on_side +#' @param x_right Another character vector of length 1 that is to be pasted to +#' the right of `x`. +#' @noRd +paste_right <- function(x, x_right) { + paste_on_side(x, x_side = x_right, direction = "right") +} + +#' Swap adjacent text groups +#' +#' @param x A text string. +#' @param pattern_1,pattern_2 Regular expression to match on a range of +#' characters. The order of regex patterns does not need to be in the order of +#' matching in `x`. +#' @noRd +swap_adjacent_text_groups <- function(x, + pattern_1, + pattern_2) { + + # Stop function if `x` is not of class character + if (!inherits(x, "character")) { + stop("Internal error in `gt:::paste_within()`:\n", + "* The `x` object must be of class character.", + call. = FALSE) + } + + vapply(x, function(x) { + + # Return `x` as is if both patterns aren't present + if (is_false(grepl(pattern_1, x)) || is_false(grepl(pattern_2, x))) { + return(x) + } + + # Get the start and stop positions for the text groups + group_1 <- x %>% get_start_stop_positions(pattern = pattern_1) + group_2 <- x %>% get_start_stop_positions(pattern = pattern_2) + + # Return `x` as is if the patterns don't encompass text ranges + # that aren't adjacent + if (!is_adjacent_separate(group_1, group_2)) { + return(x) + } + + # Obtain a length-two vector of text groups based on the + # extracted substrings + substr <- + c( + substring(x, group_1[1], group_1[2]), + substring(x, group_2[1], group_2[2]) + ) + + # Reverse the order of the elements in `substr` + # if necessary and paste elements together + if (group_1[1] < group_2[1]) { + rev_group <- paste0(rev(substr), collapse = "") + } else { + rev_group <- paste0(substr, collapse = "") + } + + # Get the character indices that the contiguous text + # groups encompass + group_pos <- min(group_1, group_2):max(group_1, group_2) + + # Return the reversed set of patterns + paste0( + substring(x, 0, min(group_pos) - 1), + rev_group, + substring(x, max(group_pos) + 1, nchar(x)), + collapse = "" + ) + }, + FUN.VALUE = character(1), + USE.NAMES = FALSE + ) +} + +#' Get the start and stop positions for a text match +#' +#' @param x A text string. +#' @param pattern A regular expression pattern. +#' @noRd +get_start_stop_positions <- function(x, + pattern) { + + # Use the pattern (`input`) with the input string + # `x` with `regexpr()` to get the matching output + regexpr_out <- regexpr(pattern, x) + + # Define the start position for the matched characters + start_pos <- regexpr_out %>% as.numeric() + + # Define the stop position for the matched characters + stop_pos <- attr(regexpr_out, "match.length", exact = TRUE) + start_pos - 1 + + # Return a vector of length 2 + c(start_pos, stop_pos) +} + +#' Determine if text groups are adjacent and non-overlapping +#' +#' @param group_1,group_2 Vectors of length 2 with starting and stopping +#' positions in a text string. +#' @noRd +is_adjacent_separate <- function(group_1, + group_2) { + + group_1_expanded <- seq(group_1[1], group_1[2]) + group_2_expanded <- seq(group_2[1], group_2[2]) + + if (length(base::intersect(group_1_expanded, group_2_expanded)) > 0) { + return(FALSE) + } + + if (any(diff(sort(c(group_1_expanded, group_2_expanded))) > 1)) { + return(FALSE) + } + + return(TRUE) +} diff --git a/R/utils_render_html.R b/R/utils_render_html.R index 544177b2cc..501a8f8f44 100644 --- a/R/utils_render_html.R +++ b/R/utils_render_html.R @@ -1,9 +1,14 @@ -# Transform a footnote glyph to an HTML representation as a superscript + +#' Transform a footnote glyph to an HTML representation +#' +#' @noRd footnote_glyph_to_html <- function(footnote_glyph) { paste0("", footnote_glyph, "") } +#' Get the spanner column label style from an attribute table +#' #' @importFrom dplyr filter pull #' @noRd get_spanner_style <- function(spanner_style_attrs, @@ -23,6 +28,8 @@ get_spanner_style <- function(spanner_style_attrs, } } +#' Get the column label style from an attribute table +#' #' @importFrom dplyr filter pull #' @noRd get_column_style <- function(column_style_attrs, @@ -42,6 +49,9 @@ get_column_style <- function(column_style_attrs, } } +#' Create a set of inline style attributes +#' +#' @noRd create_style_attrs <- function(style_values) { style_rules <- c() @@ -58,15 +68,19 @@ create_style_attrs <- function(style_values) { style_rules } -# Taking the `body_content` vector, split into list components with one -# item per row in the output table +#' Split the body content vector into a list structure +#' +#' Taking the `body_content` vector, split into list components with one item +#' per row in the output table +#' @noRd split_body_content <- function(body_content, n_cols) { split(body_content, ceiling(seq_along(body_content) / n_cols)) } -# Apply footnotes to the data rows +#' Apply footnotes to the data rows +#' #' @importFrom dplyr filter group_by mutate ungroup select distinct #' @noRd apply_styles_to_output <- function(output_df, @@ -113,7 +127,8 @@ apply_styles_to_output <- function(output_df, split_body_content(body_content = body_styles, n_cols) } -# Apply footnotes to the data rows +#' Apply footnotes to the summary data cells +#' #' @importFrom dplyr filter group_by mutate ungroup select distinct #' @noRd apply_styles_to_summary_output <- function(summary_df, @@ -158,15 +173,18 @@ apply_styles_to_summary_output <- function(summary_df, split_body_content(body_content = summary_styles, n_cols) } -# Create the opening HTML element of a table -create_table_start_h <- function(groups_rows_df) { - +#' Create the opening HTML element of a table +#' +#' @noRd +create_table_start_h <- function() { "\n\n" } -# Create the heading component of a table, which contains the heading and -# possibly a subtitle; if there are no heading components defined this -# function will return an empty string +#' Create the heading component of a table +#' +#' The table heading component contains the heading and possibly a subtitle; if +#' there are no heading components defined this function will return an empty +#' string. #' @importFrom dplyr filter group_by mutate ungroup select distinct #' @noRd create_heading_component <- function(heading, @@ -365,7 +383,8 @@ create_heading_component <- function(heading, heading_component } -# Create the columns component of a table +#' Create the columns component of a table (HTML) +#' #' @import rlang #' @importFrom dplyr filter group_by mutate ungroup select distinct #' @noRd @@ -575,6 +594,8 @@ create_columns_component_h <- function(boxh_df, table_col_headings } +#' Create the table body component (HTML) +#' #' @importFrom dplyr mutate filter pull #' @noRd create_body_component_h <- function(row_splits_body, @@ -760,6 +781,9 @@ create_body_component_h <- function(row_splits_body, "\n") } +#' Create the table source note component (HTML) +#' +#' @noRd create_source_note_component_h <- function(source_note, n_cols) { @@ -778,6 +802,8 @@ create_source_note_component_h <- function(source_note, "\n") } +#' Create the table footnote component (HTML) +#' #' @importFrom dplyr select distinct filter pull #' @noRd create_footnote_component_h <- function(footnotes_resolved, @@ -815,7 +841,9 @@ create_footnote_component_h <- function(footnotes_resolved, footnote_component } -# Create the closing HTML element of a table +#' Create the closing HTML element of a table +#' +#' @noRd create_table_end_h <- function() { "
\n\n" diff --git a/R/utils_render_latex.R b/R/utils_render_latex.R index 649d615cab..c4b8c84893 100644 --- a/R/utils_render_latex.R +++ b/R/utils_render_latex.R @@ -1,7 +1,7 @@ # Create a vector of LaTeX packages to use as table dependencies latex_packages <- function() { - c("longtable", "booktabs", "caption") + c("amsmath", "booktabs", "caption", "longtable") } # Transform a footnote glyph to a LaTeX representation as a superscript diff --git a/man/tab_style.Rd b/man/tab_style.Rd index 905ae7e825..f9e70af555 100644 --- a/man/tab_style.Rd +++ b/man/tab_style.Rd @@ -101,7 +101,6 @@ tab_2 <- date <= "2015-12-15" ) \%>\% dplyr::select(-c(adj_close, volume)) \%>\% - dplyr::mutate(date = as.character(date)) \%>\% gt() \%>\% tab_style( style = cells_styles( diff --git a/tests/gt-examples/03-latex/latex-04-sleep.Rmd b/tests/gt-examples/03-latex/latex-04-sleep.Rmd index 3f740399ae..b90e7475e2 100644 --- a/tests/gt-examples/03-latex/latex-04-sleep.Rmd +++ b/tests/gt-examples/03-latex/latex-04-sleep.Rmd @@ -19,3 +19,4 @@ gt(data = sleep) %>% footnote = "This is a footnote", locations = cells_data(columns = 1, rows = c(2, 3, 4))) ``` + diff --git a/tests/testthat/test-conditional_fmt.R b/tests/testthat/test-conditional_fmt.R index 5f3d2038f1..fb865d5731 100644 --- a/tests/testthat/test-conditional_fmt.R +++ b/tests/testthat/test-conditional_fmt.R @@ -41,7 +41,7 @@ test_that("the `fmt_number()` function works with conditional `rows`", { rows = num_1 < 1000) %>% render_formats_test(context = "html"))[["num_1"]], c("1836.23", "2763.39", "937.2900", "643.0000", - "212.2320", "0.0000", "-23.2400")) + "212.2320", "0.0000", "−23.2400")) expect_equal( (tab %>% @@ -65,7 +65,7 @@ test_that("the `fmt_scientific()` function works with conditional `rows`", { c("1836.23", "2763.39", "9.3729 × 102", "6.4300 × 102", "2.1223 × 102", "0.0000", - "-2.3240 × 101") + "−2.3240 × 101") ) expect_equal( @@ -89,8 +89,8 @@ test_that("the `fmt_percent()` function works with conditional `rows`", { decimals = 2, rows = num_1 < 1000) %>% render_formats_test(context = "html"))[["num_1"]], - c("1836.23", "2763.39", "93,729.00%", "64,300.00%", - "21,223.20%", "0.00%", "-2,324.00%") + c("1836.23", "2763.39", "93,729.00%", "64,300.00%", + "21,223.20%", "0.00%", "−2,324.00%") ) expect_equal( @@ -100,7 +100,7 @@ test_that("the `fmt_percent()` function works with conditional `rows`", { decimals = 2, rows = char_2 %in% c("june", "july") & grepl("sa.*", char_1)) %>% render_formats_test(context = "html"))[["num_2"]], - c("3,400.00%", "74", "23", "NA", "35", "NA", "NA") + c("3,400.00%", "74", "23", "NA", "35", "NA", "NA") ) }) @@ -114,7 +114,7 @@ test_that("the `fmt_currency()` function works with conditional `rows`", { rows = num_1 < 1000) %>% render_formats_test(context = "html"))[["num_1"]], c("1836.23", "2763.39", "$937.29", "$643.00", "$212.23", - "$0.00", "$-23.24") + "$0.00", "−$23.24") ) expect_equal( diff --git a/tests/testthat/test-fmt_currency.R b/tests/testthat/test-fmt_currency.R index 6b7b721425..bf41ff1f4f 100644 --- a/tests/testthat/test-fmt_currency.R +++ b/tests/testthat/test-fmt_currency.R @@ -12,7 +12,8 @@ test_that("the `fmt_currency()` function works correctly", { "october", "november", "december"), num_1 = c(1836.23, 2763.39, 937.29, 643.00, 212.232, 0, -23.24), num_2 = c(34, 74, 23, 93, 35, 76, 57), - stringsAsFactors = FALSE) + stringsAsFactors = FALSE + ) # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset @@ -28,7 +29,9 @@ test_that("the `fmt_currency()` function works correctly", { c("names", "class", "row.names", "boxh_df", "stub_df", "footnotes_df", "styles_df", "rows_df", "cols_df", "col_labels", "grp_labels", - "arrange_groups", "data_df", "opts_df", "formats", "transforms"))) + "arrange_groups", "data_df", "opts_df", "formats", "transforms") + ) + ) # Extract vectors from the table object for comparison # to the original dataset @@ -48,12 +51,14 @@ test_that("the `fmt_currency()` function works correctly", { # that does not exist expect_error( tab %>% - fmt_currency(columns = "num_3", currency = "USD")) + fmt_currency(columns = "num_3", currency = "USD") + ) # Expect an error when using a locale that does not exist expect_error( tab %>% - fmt_currency(columns = "num_2", decimals = 2, locale = "aa_bb")) + fmt_currency(columns = "num_2", decimals = 2, locale = "aa_bb") + ) # Format the `num_1` column using defaults (currency of "USD"); # extract `output_df` and compare to expected values @@ -61,7 +66,9 @@ test_that("the `fmt_currency()` function works correctly", { (tab %>% fmt_currency(columns = "num_1") %>% render_formats_test(context = "html"))[["num_1"]], - c("$1,836.23", "$2,763.39", "$937.29", "$643.00", "$212.23", "$0.00", "$-23.24")) + c("$1,836.23", "$2,763.39", "$937.29", "$643.00", "$212.23", + "$0.00", "−$23.24") + ) # Format the `num_1` column as USD, use all other defaults; # extract `output_df` and compare to expected values @@ -69,7 +76,9 @@ test_that("the `fmt_currency()` function works correctly", { (tab %>% fmt_currency(columns = "num_1", currency = "USD") %>% render_formats_test(context = "html"))[["num_1"]], - c("$1,836.23", "$2,763.39", "$937.29", "$643.00", "$212.23", "$0.00", "$-23.24")) + c("$1,836.23", "$2,763.39", "$937.29", "$643.00", "$212.23", + "$0.00", "−$23.24") + ) # Format the `num_1` column as USD to 5 decimal places, use all # other defaults; extract `output_df` and compare to expected values @@ -78,7 +87,8 @@ test_that("the `fmt_currency()` function works correctly", { fmt_currency(columns = "num_1", currency = "USD", decimals = 5) %>% render_formats_test("html"))[["num_1"]], c("$1,836.23000", "$2,763.39000", "$937.29000", "$643.00000", - "$212.23200", "$0.00000", "$-23.24000")) + "$212.23200", "$0.00000", "−$23.24000") + ) # Format the `num_1` column as USD, and don't include the subunits; # use all other defaults; extract `output_df` and compare to @@ -87,7 +97,8 @@ test_that("the `fmt_currency()` function works correctly", { (tab %>% fmt_currency(columns = "num_1", currency = "USD", use_subunits = FALSE) %>% render_formats_test("html"))[["num_1"]], - c("$1,836", "$2,763", "$937", "$643", "$212", "$0", "$-23")) + c("$1,836", "$2,763", "$937", "$643", "$212", "$0", "−$23") + ) # Format the `num_1` column as USD, don't use digit # grouping separators, use all other defaults; extract `output_df` @@ -96,7 +107,9 @@ test_that("the `fmt_currency()` function works correctly", { (tab %>% fmt_currency(columns = "num_1", currency = "USD", use_seps = FALSE) %>% render_formats_test("html"))[["num_1"]], - c("$1836.23", "$2763.39", "$937.29", "$643.00", "$212.23", "$0.00", "$-23.24")) + c("$1836.23", "$2763.39", "$937.29", "$643.00", "$212.23", + "$0.00", "−$23.24") + ) # Format the `num_1` column to 2 decimal places, use a single space # character as digit grouping separators, use all other defaults; @@ -105,7 +118,9 @@ test_that("the `fmt_currency()` function works correctly", { (tab %>% fmt_currency(columns = "num_1", currency = "USD", sep_mark = " ") %>% render_formats_test("html"))[["num_1"]], - c("$1 836.23", "$2 763.39", "$937.29", "$643.00", "$212.23", "$0.00", "$-23.24")) + c("$1 836.23", "$2 763.39", "$937.29", "$643.00", "$212.23", + "$0.00", "−$23.24") + ) # Format the `num_1` column as USD, use a period for the digit grouping # separators and a comma for the decimal mark, use all other defaults; @@ -116,7 +131,9 @@ test_that("the `fmt_currency()` function works correctly", { columns = "num_1", currency = "USD", sep_mark = ".", dec_mark = ",") %>% render_formats_test("html"))[["num_1"]], - c("$1.836,23", "$2.763,39", "$937,29", "$643,00", "$212,23", "$0,00", "$-23,24")) + c("$1.836,23", "$2.763,39", "$937,29", "$643,00", "$212,23", + "$0,00", "−$23,24") + ) # Format the `num_1` column as USD, apply parentheses to all negative # values, use all other defaults; extract `output_df` and compare @@ -126,7 +143,9 @@ test_that("the `fmt_currency()` function works correctly", { fmt_currency( columns = "num_1", currency = "USD", negative_val = "parens") %>% render_formats_test("html"))[["num_1"]], - c("$1,836.23", "$2,763.39", "$937.29", "$643.00", "$212.23", "$0.00", "($23.24)")) + c("$1,836.23", "$2,763.39", "$937.29", "$643.00", "$212.23", + "$0.00", "($23.24)") + ) # Format the `num_1` column as USD, apply parentheses to all negative # values, use all other defaults; apply the default context, extract @@ -136,7 +155,9 @@ test_that("the `fmt_currency()` function works correctly", { fmt_currency( columns = "num_1", currency = "USD", negative_val = "parens") %>% render_formats_test("default"))[["num_1"]], - c("$1,836.23", "$2,763.39", "$937.29", "$643.00", "$212.23", "$0.00", "($23.24)")) + c("$1,836.23", "$2,763.39", "$937.29", "$643.00", "$212.23", + "$0.00", "($23.24)") + ) # Format the `num_1` column as USD to 4 decimal places, scale all values by # 1/1000, use all other defaults; extract `output_df` and compare @@ -147,7 +168,9 @@ test_that("the `fmt_currency()` function works correctly", { columns = "num_1", currency = "USD", decimals = 4, scale_by = 1/1000) %>% render_formats_test("html"))[["num_1"]], - c("$1.8362", "$2.7634", "$0.9373", "$0.6430", "$0.2122", "$0.0000", "$-0.0232")) + c("$1.8362", "$2.7634", "$0.9373", "$0.6430", "$0.2122", + "$0.0000", "−$0.0232") + ) # Format the `num_1` column as USD, prepend and append all values by 2 # different literals, use all other defaults; extract `output_df` and @@ -158,7 +181,8 @@ test_that("the `fmt_currency()` function works correctly", { columns = "num_1", currency = "USD", pattern = "a {x} b") %>% render_formats_test("html"))[["num_1"]], c("a $1,836.23 b", "a $2,763.39 b", "a $937.29 b", "a $643.00 b", - "a $212.23 b", "a $0.00 b", "a $-23.24 b")) + "a $212.23 b", "a $0.00 b", "a −$23.24 b") + ) # Format the `num_1` column as USD to 4 decimal places, scale all values # by 1/1000 and append a `K` character to the resultant values, use @@ -170,7 +194,8 @@ test_that("the `fmt_currency()` function works correctly", { scale_by = 1/1000, pattern = "{x}K") %>% render_formats_test("html"))[["num_1"]], c("$1.8362K", "$2.7634K", "$0.9373K", "$0.6430K", - "$0.2122K", "$0.0000K", "$-0.0232K")) + "$0.2122K", "$0.0000K", "−$0.0232K") + ) # Format the `num_1` column as USD, apply the `en_US` locale and use all # other defaults; extract `output_df` and compare to expected values @@ -180,7 +205,8 @@ test_that("the `fmt_currency()` function works correctly", { columns = "num_1", currency = "USD", locale = "en_US") %>% render_formats_test("html"))[["num_1"]], c("$1,836.23", "$2,763.39", "$937.29", "$643.00", - "$212.23", "$0.00", "$-23.24")) + "$212.23", "$0.00", "−$23.24") + ) # Format the `num_1` column as DKK, apply the `da_DK` locale and use all # other defaults; extract `output_df` and compare to expected values @@ -191,7 +217,8 @@ test_that("the `fmt_currency()` function works correctly", { placement = "right", incl_space = TRUE) %>% render_formats_test("html"))[["num_1"]], c("1.836,23 kr.", "2.763,39 kr.", "937,29 kr.", "643,00 kr.", - "212,23 kr.", "0,00 kr.", "-23,24 kr.")) + "212,23 kr.", "0,00 kr.", "−23,24 kr.") + ) # Format the `num_1` column as DKK, apply the `da_DK` locale and use all # other defaults; use the default context, extract `output_df`, and @@ -202,7 +229,8 @@ test_that("the `fmt_currency()` function works correctly", { columns = "num_1", currency = "DKK", locale = "da_DK") %>% render_formats_test("default"))[["num_1"]], c("kr.1.836,23", "kr.2.763,39", "kr.937,29", "kr.643,00", - "kr.212,23", "kr.0,00", "kr.-23,24")) + "kr.212,23", "kr.0,00", "-kr.23,24") + ) # Format the `num_1` column as EUR, apply the `de_AT` locale and use all # other defaults; extract `output_df` and compare to expected values @@ -212,7 +240,8 @@ test_that("the `fmt_currency()` function works correctly", { columns = "num_1", currency = "EUR", locale = "de_AT") %>% render_formats_test("html"))[["num_1"]], c("€1 836,23", "€2 763,39", "€937,29", "€643,00", - "€212,23", "€0,00", "€-23,24")) + "€212,23", "€0,00", "−€23,24") + ) # Format the `num_1` column as EUR, apply the `de_AT` locale and use all # other defaults; use the default context, extract `output_df`, and @@ -223,7 +252,8 @@ test_that("the `fmt_currency()` function works correctly", { columns = "num_1", currency = "EUR", locale = "de_AT") %>% render_formats_test("default"))[["num_1"]], c("EUR1 836,23", "EUR2 763,39", "EUR937,29", "EUR643,00", - "EUR212,23", "EUR0,00", "EUR-23,24")) + "EUR212,23", "EUR0,00", "-EUR23,24") + ) # Format the `num_1` column to 2 decimal places, apply the `et_EE` # locale and use all other defaults; extract `output_df` and compare @@ -234,7 +264,8 @@ test_that("the `fmt_currency()` function works correctly", { columns = "num_1", currency = "EUR", locale = "et_EE") %>% render_formats_test("html"))[["num_1"]], c("€1 836,23", "€2 763,39", "€937,29", "€643,00", - "€212,23", "€0,00", "€-23,24")) + "€212,23", "€0,00", "−€23,24") + ) }) test_that("the `fmt_currency()` function can scale/suffix larger numbers", { @@ -247,7 +278,8 @@ test_that("the `fmt_currency()` function can scale/suffix larger numbers", { -1.8E15, -1.7E13, -1.6E10, -1.5E8, -1.4E6, -1.3E4, -1.2E3, -1.1E1, 0, 1.1E1, 1.2E3, 1.3E4, 1.4E6, 1.5E8, 1.6E10, 1.7E13, 1.8E15), - stringsAsFactors = FALSE) + stringsAsFactors = FALSE + ) # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset @@ -260,9 +292,12 @@ test_that("the `fmt_currency()` function can scale/suffix larger numbers", { (tab %>% fmt_currency(columns = "num", decimals = 2, suffixing = TRUE) %>% render_formats_test(context = "html"))[["num"]], - c("$-1,800.00T", "$-17.00T", "$-16.00B", "$-150.00M", "$-1.40M", "$-13.00K", - "$-1.20K", "$-11.00", "$0.00", "$11.00", "$1.20K", "$13.00K", "$1.40M", - "$150.00M", "$16.00B", "$17.00T", "$1,800.00T")) + c("−$1,800.00T", "−$17.00T", "−$16.00B", + "−$150.00M", "−$1.40M", "−$13.00K", + "−$1.20K", "−$11.00", "$0.00", "$11.00", + "$1.20K", "$13.00K", "$1.40M", "$150.00M", "$16.00B", + "$17.00T", "$1,800.00T") + ) # Format the `num` column to no decimal places, have the # `suffixing` option set to TRUE (default labels, all @@ -271,9 +306,10 @@ test_that("the `fmt_currency()` function can scale/suffix larger numbers", { (tab %>% fmt_currency(columns = "num", decimals = 0, suffixing = TRUE) %>% render_formats_test(context = "html"))[["num"]], - c("$-1,800T", "$-17T", "$-16B", "$-150M", "$-1M", "$-13K", - "$-1K", "$-11", "$0", "$11", "$1K", "$13K", "$1M", - "$150M", "$16B", "$17T", "$1,800T")) + c("−$1,800T", "−$17T", "−$16B", "−$150M", + "−$1M", "−$13K", "−$1K", "−$11", "$0", + "$11", "$1K", "$13K", "$1M", "$150M", "$16B", "$17T", "$1,800T") + ) # Format the `num` column to 2 decimal places, have the # `suffixing` option set to use custom symbols across the @@ -284,9 +320,11 @@ test_that("the `fmt_currency()` function can scale/suffix larger numbers", { columns = "num", decimals = 2, suffixing = c("k", "Mn", "Bn", "Tr")) %>% render_formats_test(context = "html"))[["num"]], - c("$-1,800.00Tr", "$-17.00Tr", "$-16.00Bn", "$-150.00Mn", "$-1.40Mn", - "$-13.00k", "$-1.20k", "$-11.00", "$0.00", "$11.00", "$1.20k", - "$13.00k", "$1.40Mn", "$150.00Mn", "$16.00Bn", "$17.00Tr", "$1,800.00Tr")) + c("−$1,800.00Tr", "−$17.00Tr", "−$16.00Bn", + "−$150.00Mn", "−$1.40Mn", "−$13.00k", "−$1.20k", + "−$11.00", "$0.00", "$11.00", "$1.20k", "$13.00k", "$1.40Mn", + "$150.00Mn", "$16.00Bn", "$17.00Tr", "$1,800.00Tr") + ) # Format the `num` column to 2 decimal places, have the # `suffixing` option set to use custom symbols for the middle @@ -297,12 +335,14 @@ test_that("the `fmt_currency()` function can scale/suffix larger numbers", { columns = "num", decimals = 2, currency = "EUR", locale = "de_DE", suffixing = c(NA, "Mio.", "Mia.", NA)) %>% render_formats_test(context = "html"))[["num"]], - c("€-1.800.000,00Mia.", "€-17.000,00Mia.", "€-16,00Mia.", - "€-150,00Mio.", "€-1,40Mio.", "€-13.000,00", - "€-1.200,00", "€-11,00", "€0,00", "€11,00", - "€1.200,00", "€13.000,00", "€1,40Mio.", + c("−€1.800.000,00Mia.", "−€17.000,00Mia.", + "−€16,00Mia.", "−€150,00Mio.", + "−€1,40Mio.", "−€13.000,00", + "−€1.200,00", "−€11,00", "€0,00", + "€11,00", "€1.200,00", "€13.000,00", "€1,40Mio.", "€150,00Mio.", "€16,00Mia.", "€17.000,00Mia.", - "€1.800.000,00Mia.")) + "€1.800.000,00Mia.") + ) # Format the `num` column to 2 decimal places, have the # `suffixing` option set to use custom symbols with some NAs @@ -312,10 +352,12 @@ test_that("the `fmt_currency()` function can scale/suffix larger numbers", { columns = "num", decimals = 2, suffixing = c("K", NA, "Bn", NA, "Qa", NA, NA)) %>% render_formats_test(context = "html"))[["num"]], - c("$-1.80Qa", "$-17,000.00Bn", "$-16.00Bn", "$-150,000.00K", - "$-1,400.00K", "$-13.00K", "$-1.20K", "$-11.00", "$0.00", "$11.00", - "$1.20K", "$13.00K", "$1,400.00K", "$150,000.00K", "$16.00Bn", - "$17,000.00Bn", "$1.80Qa")) + c("−$1.80Qa", "−$17,000.00Bn", "−$16.00Bn", + "−$150,000.00K", "−$1,400.00K", "−$13.00K", + "−$1.20K", "−$11.00", "$0.00", "$11.00", "$1.20K", + "$13.00K", "$1,400.00K", "$150,000.00K", "$16.00Bn", + "$17,000.00Bn", "$1.80Qa") + ) # Format the `num` column to 2 decimal places, have the # `suffixing` option set to FALSE (the default option, where @@ -326,12 +368,13 @@ test_that("the `fmt_currency()` function can scale/suffix larger numbers", { columns = "num", decimals = 2, suffixing = FALSE) %>% render_formats_test(context = "html"))[["num"]], - c( "$-1,800,000,000,000,000.00", "$-17,000,000,000,000.00", - "$-16,000,000,000.00", "$-150,000,000.00", "$-1,400,000.00", - "$-13,000.00", "$-1,200.00", "$-11.00", "$0.00", "$11.00", - "$1,200.00", "$13,000.00", "$1,400,000.00", "$150,000,000.00", - "$16,000,000,000.00", "$17,000,000,000,000.00", - "$1,800,000,000,000,000.00")) + c( "−$1,800,000,000,000,000.00", "−$17,000,000,000,000.00", + "−$16,000,000,000.00", "−$150,000,000.00", + "−$1,400,000.00", "−$13,000.00", "−$1,200.00", + "−$11.00", "$0.00", "$11.00", "$1,200.00", "$13,000.00", + "$1,400,000.00", "$150,000,000.00", "$16,000,000,000.00", + "$17,000,000,000,000.00", "$1,800,000,000,000,000.00") + ) # Expect an error if any vector length other than # four is used for `suffixing` @@ -404,5 +447,4 @@ test_that("the `fmt_currency()` function can scale/suffix larger numbers", { suffixing = TRUE) %>% render_formats_test(context = "html"))[["num"]], "$999.99990") - }) diff --git a/tests/testthat/test-fmt_number.R b/tests/testthat/test-fmt_number.R index 2f0fc092fe..f0541371b3 100644 --- a/tests/testthat/test-fmt_number.R +++ b/tests/testthat/test-fmt_number.R @@ -61,7 +61,7 @@ test_that("the `fmt_number()` function works correctly", { (tab %>% fmt_number(columns = "num_1", decimals = 2) %>% render_formats_test(context = "html"))[["num_1"]], - c("1,836.23", "2,763.39", "937.29", "643.00", "212.23", "0.00", "-23.24")) + c("1,836.23", "2,763.39", "937.29", "643.00", "212.23", "0.00", "−23.24")) # Format the `num_1` column to 5 decimal places, use all # other defaults; extract `output_df` and compare to expected values @@ -70,7 +70,7 @@ test_that("the `fmt_number()` function works correctly", { fmt_number(columns = "num_1", decimals = 5) %>% render_formats_test("html"))[["num_1"]], c("1,836.23000", "2,763.39000", "937.29000", "643.00000", - "212.23200", "0.00000", "-23.24000")) + "212.23200", "0.00000", "−23.24000")) # Format the `num_1` column to 2 decimal places, drop the trailing # zeros, use all other defaults; extract `output_df` and compare to @@ -80,7 +80,7 @@ test_that("the `fmt_number()` function works correctly", { fmt_number(columns = "num_1", decimals = 2, drop_trailing_zeros = TRUE) %>% render_formats_test("html"))[["num_1"]], - c("1,836.23", "2,763.39", "937.29", "643", "212.23", "0", "-23.24")) + c("1,836.23", "2,763.39", "937.29", "643", "212.23", "0", "−23.24")) # Format the `num_1` column to 2 decimal places, don't use digit # grouping separators, use all other defaults; extract `output_df` @@ -89,7 +89,7 @@ test_that("the `fmt_number()` function works correctly", { (tab %>% fmt_number(columns = "num_1", decimals = 2, use_seps = FALSE) %>% render_formats_test("html"))[["num_1"]], - c("1836.23", "2763.39", "937.29", "643.00", "212.23", "0.00", "-23.24")) + c("1836.23", "2763.39", "937.29", "643.00", "212.23", "0.00", "−23.24")) # Format the `num_1` column to 2 decimal places, use a single space # character as digit grouping separators, use all other defaults; @@ -98,7 +98,7 @@ test_that("the `fmt_number()` function works correctly", { (tab %>% fmt_number(columns = "num_1", decimals = 2, sep_mark = " ") %>% render_formats_test("html"))[["num_1"]], - c("1 836.23", "2 763.39", "937.29", "643.00", "212.23", "0.00", "-23.24")) + 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, use a period for the # digit grouping separators and a comma for the decimal mark, use @@ -108,7 +108,7 @@ test_that("the `fmt_number()` function works correctly", { fmt_number(columns = "num_1", decimals = 2, sep_mark = ".", dec_mark = ",") %>% render_formats_test("html"))[["num_1"]], - c("1.836,23", "2.763,39", "937,29", "643,00", "212,23", "0,00", "-23,24")) + 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 parentheses to # all negative values, use all other defaults; extract `output_df` and @@ -117,7 +117,7 @@ test_that("the `fmt_number()` function works correctly", { (tab %>% fmt_number(columns = "num_1", decimals = 2, negative_val = "parens") %>% render_formats_test("html"))[["num_1"]], - c("1,836.23", "2,763.39", "937.29", "643.00", "212.23", "0.00", "(23.24)")) + c("1,836.23", "2,763.39", "937.29", "643.00", "212.23", "0.00", "(23.24)")) # Format the `num_1` column to 4 decimal places, scale all values by # 1/1000, use all other defaults; extract `output_df` and compare @@ -126,7 +126,7 @@ test_that("the `fmt_number()` function works correctly", { (tab %>% fmt_number(columns = "num_1", decimals = 4, scale_by = 1/1000) %>% render_formats_test("html"))[["num_1"]], - c("1.8362", "2.7634", "0.9373", "0.6430", "0.2122", "0.0000", "-0.0232")) + c("1.8362", "2.7634", "0.9373", "0.6430", "0.2122", "0.0000", "−0.0232")) # Format the `num_1` column to 2 decimal places, prepend and append # all values by 2 different literals, use all other defaults; extract @@ -136,7 +136,7 @@ test_that("the `fmt_number()` function works correctly", { fmt_number(columns = "num_1", decimals = 2, pattern = "a {x} b") %>% render_formats_test("html"))[["num_1"]], c("a 1,836.23 b", "a 2,763.39 b", "a 937.29 b", "a 643.00 b", - "a 212.23 b", "a 0.00 b", "a -23.24 b")) + "a 212.23 b", "a 0.00 b", "a −23.24 b")) # Format the `num_1` column to 4 decimal places, scale all values # by 1/1000 and append a `K` character to the resultant values, use @@ -147,7 +147,7 @@ test_that("the `fmt_number()` function works correctly", { scale_by = 1/1000, pattern = "{x}K") %>% render_formats_test("html"))[["num_1"]], c("1.8362K", "2.7634K", "0.9373K", "0.6430K", - "0.2122K", "0.0000K", "-0.0232K")) + "0.2122K", "0.0000K", "−0.0232K")) # Format the `num_1` column to 2 decimal places, apply the `en_US` # locale and use all other defaults; extract `output_df` and compare @@ -156,7 +156,7 @@ test_that("the `fmt_number()` function works correctly", { (tab %>% fmt_number(columns = "num_1", decimals = 2, locale = "en_US") %>% render_formats_test("html"))[["num_1"]], - c("1,836.23", "2,763.39", "937.29", "643.00", "212.23", "0.00", "-23.24")) + 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 `da_DK` # locale and use all other defaults; extract `output_df` and compare @@ -165,7 +165,7 @@ test_that("the `fmt_number()` function works correctly", { (tab %>% fmt_number(columns = "num_1", decimals = 2, locale = "da_DK") %>% render_formats_test("html"))[["num_1"]], - c("1.836,23", "2.763,39", "937,29", "643,00", "212,23", "0,00", "-23,24")) + 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 `de_AT` # locale and use all other defaults; extract `output_df` and compare @@ -174,7 +174,7 @@ test_that("the `fmt_number()` function works correctly", { (tab %>% fmt_number(columns = "num_1", decimals = 2, locale = "de_AT") %>% render_formats_test("html"))[["num_1"]], - c("1 836,23", "2 763,39", "937,29", "643,00", "212,23", "0,00", "-23,24")) + 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 `et_EE` # locale and use all other defaults; extract `output_df` and compare @@ -183,7 +183,7 @@ test_that("the `fmt_number()` function works correctly", { (tab %>% fmt_number(columns = "num_1", decimals = 2, locale = "et_EE") %>% render_formats_test("html"))[["num_1"]], - c("1 836,23", "2 763,39", "937,29", "643,00", "212,23", "0,00", "-23,24")) + 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 `gl_ES` # locale and use all other defaults; extract `output_df` and compare @@ -192,7 +192,7 @@ test_that("the `fmt_number()` function works correctly", { (tab %>% fmt_number(columns = "num_1", decimals = 2, locale = "gl_ES") %>% render_formats_test("html"))[["num_1"]], - c("1.836,23", "2.763,39", "937,29", "643,00", "212,23", "0,00", "-23,24")) + c("1.836,23", "2.763,39", "937,29", "643,00", "212,23", "0,00", "−23,24")) }) test_that("the `fmt_number()` function can scale/suffix larger numbers", { @@ -218,9 +218,12 @@ test_that("the `fmt_number()` function can scale/suffix larger numbers", { (tab %>% fmt_number(columns = "num", decimals = 2, suffixing = TRUE) %>% render_formats_test(context = "html"))[["num"]], - c("-1,800.00T", "-17.00T", "-16.00B", "-150.00M", "-1.40M", "-13.00K", - "-1.20K", "-11.00", "0.00", "11.00", "1.20K", "13.00K", "1.40M", - "150.00M", "16.00B", "17.00T", "1,800.00T")) + c("−1,800.00T", "−17.00T", "−16.00B", + "−150.00M", "−1.40M", "−13.00K", + "−1.20K", "−11.00", "0.00", "11.00", + "1.20K", "13.00K", "1.40M", "150.00M", "16.00B", + "17.00T", "1,800.00T") + ) # Format the `num` column to no decimal places, have the # `suffixing` option set to TRUE (default labels, all @@ -229,9 +232,9 @@ test_that("the `fmt_number()` function can scale/suffix larger numbers", { (tab %>% fmt_number(columns = "num", decimals = 0, suffixing = TRUE) %>% render_formats_test(context = "html"))[["num"]], - c("-1,800T", "-17T", "-16B", "-150M", "-1M", "-13K", - "-1K", "-11", "0", "11", "1K", "13K", "1M", - "150M", "16B", "17T", "1,800T")) + c("−1,800T", "−17T", "−16B", "−150M", + "−1M", "−13K", "−1K", "−11", "0", "11", + "1K", "13K", "1M", "150M", "16B", "17T", "1,800T")) # Format the `num` column to 2 decimal places, have the # `suffixing` option set to use custom symbols across the @@ -242,9 +245,10 @@ test_that("the `fmt_number()` function can scale/suffix larger numbers", { columns = "num", decimals = 2, suffixing = c("k", "Mn", "Bn", "Tr")) %>% render_formats_test(context = "html"))[["num"]], - c("-1,800.00Tr", "-17.00Tr", "-16.00Bn", "-150.00Mn", "-1.40Mn", "-13.00k", - "-1.20k", "-11.00", "0.00", "11.00", "1.20k", "13.00k", "1.40Mn", - "150.00Mn", "16.00Bn", "17.00Tr", "1,800.00Tr")) + c("−1,800.00Tr", "−17.00Tr", "−16.00Bn", + "−150.00Mn", "−1.40Mn", "−13.00k", + "−1.20k", "−11.00", "0.00", "11.00", "1.20k", + "13.00k", "1.40Mn", "150.00Mn", "16.00Bn", "17.00Tr", "1,800.00Tr")) # Format the `num` column to 2 decimal places, have the # `suffixing` option set to use custom symbols for the middle @@ -255,8 +259,9 @@ test_that("the `fmt_number()` function can scale/suffix larger numbers", { columns = "num", decimals = 2, suffixing = c(NA, "Mio.", "Mia.", NA)) %>% render_formats_test(context = "html"))[["num"]], - c("-1,800,000.00Mia.", "-17,000.00Mia.", "-16.00Mia.", "-150.00Mio.", - "-1.40Mio.", "-13,000.00", "-1,200.00", "-11.00", "0.00", "11.00", + c("−1,800,000.00Mia.", "−17,000.00Mia.", + "−16.00Mia.", "−150.00Mio.", "−1.40Mio.", + "−13,000.00", "−1,200.00", "−11.00", "0.00", "11.00", "1,200.00", "13,000.00", "1.40Mio.", "150.00Mio.", "16.00Mia.", "17,000.00Mia.", "1,800,000.00Mia.")) @@ -268,10 +273,10 @@ test_that("the `fmt_number()` function can scale/suffix larger numbers", { columns = "num", decimals = 2, suffixing = c("K", NA, "Bn", NA, "Qa", NA, NA)) %>% render_formats_test(context = "html"))[["num"]], - c("-1.80Qa", "-17,000.00Bn", "-16.00Bn", "-150,000.00K", - "-1,400.00K", "-13.00K", "-1.20K", "-11.00", "0.00", "11.00", - "1.20K", "13.00K", "1,400.00K", "150,000.00K", "16.00Bn", - "17,000.00Bn", "1.80Qa")) + c("−1.80Qa", "−17,000.00Bn", "−16.00Bn", + "−150,000.00K", "−1,400.00K", "−13.00K", + "−1.20K", "−11.00", "0.00", "11.00", "1.20K", "13.00K", + "1,400.00K", "150,000.00K", "16.00Bn", "17,000.00Bn", "1.80Qa")) # Format the `num` column to 2 decimal places, have the # `suffixing` option set to FALSE (the default option, where @@ -282,9 +287,10 @@ test_that("the `fmt_number()` function can scale/suffix larger numbers", { columns = "num", decimals = 2, suffixing = FALSE) %>% render_formats_test(context = "html"))[["num"]], - c("-1,800,000,000,000,000.00", "-17,000,000,000,000.00", - "-16,000,000,000.00", "-150,000,000.00", "-1,400,000.00", "-13,000.00", - "-1,200.00", "-11.00", "0.00", "11.00", "1,200.00", "13,000.00", + c("−1,800,000,000,000,000.00", "−17,000,000,000,000.00", + "−16,000,000,000.00", "−150,000,000.00", + "−1,400,000.00", "−13,000.00", "−1,200.00", + "−11.00", "0.00", "11.00", "1,200.00", "13,000.00", "1,400,000.00", "150,000,000.00", "16,000,000,000.00", "17,000,000,000,000.00", "1,800,000,000,000,000.00")) diff --git a/tests/testthat/test-fmt_percent.R b/tests/testthat/test-fmt_percent.R index c3b656b612..feb7f42f19 100644 --- a/tests/testthat/test-fmt_percent.R +++ b/tests/testthat/test-fmt_percent.R @@ -12,7 +12,8 @@ test_that("the `fmt_percent()` function works correctly", { "october", "november", "december"), num_1 = c(1836.23, 2763.39, 937.29, 643.00, 212.232, 0, -23.24), num_2 = c(34, 74, 23, 93, 35, 76, 57), - stringsAsFactors = FALSE) + stringsAsFactors = FALSE + ) # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset @@ -22,12 +23,14 @@ test_that("the `fmt_percent()` function works correctly", { # that does not exist expect_error( tab %>% - fmt_percent(columns = "num_3", decimals = 2)) + fmt_percent(columns = "num_3", decimals = 2) + ) # Expect an error when using a locale that does not exist expect_error( tab %>% - fmt_percent(columns = "num_2", decimals = 2, locale = "aa_bb")) + fmt_percent(columns = "num_2", decimals = 2, locale = "aa_bb") + ) # Format the `num_1` column to 2 decimal places, use all # other defaults; extract `output_df` and compare to expected values @@ -35,8 +38,10 @@ test_that("the `fmt_percent()` function works correctly", { (tab %>% fmt_percent(columns = "num_1", decimals = 2) %>% render_formats_test("html"))[["num_1"]], - c("183,623.00%", "276,339.00%", "93,729.00%", - "64,300.00%", "21,223.20%", "0.00%", "-2,324.00%")) + c("183,623.00%", "276,339.00%", "93,729.00%", + "64,300.00%", "21,223.20%", "0.00%", + "−2,324.00%") + ) # Format the `num_1` column to 5 decimal places, use all # other defaults; extract `output_df` and compare to expected values @@ -44,8 +49,10 @@ test_that("the `fmt_percent()` function works correctly", { (tab %>% fmt_percent(columns = "num_1", decimals = 5) %>% render_formats_test("html"))[["num_1"]], - c("183,623.00000%", "276,339.00000%", "93,729.00000%", - "64,300.00000%", "21,223.20000%", "0.00000%", "-2,324.00000%")) + c("183,623.00000%", "276,339.00000%", "93,729.00000%", + "64,300.00000%", "21,223.20000%", "0.00000%", + "−2,324.00000%") + ) # Format the `num_1` column to 2 decimal places, drop the trailing # zeros, use all other defaults; extract `output_df` and compare to @@ -55,8 +62,9 @@ test_that("the `fmt_percent()` function works correctly", { fmt_percent(columns = "num_1", decimals = 2, drop_trailing_zeros = TRUE) %>% render_formats_test("html"))[["num_1"]], - c("183,623%", "276,339%", "93,729%", "64,300%", - "21,223.2%", "0%", "-2,324%" )) + c("183,623%", "276,339%", "93,729%", "64,300%", + "21,223.2%", "0%", "−2,324%") + ) # Format the `num_1` column to 2 decimal places, don't use digit # grouping separators, use all other defaults; extract `output_df` @@ -65,8 +73,10 @@ test_that("the `fmt_percent()` function works correctly", { (tab %>% fmt_percent(columns = "num_1", decimals = 2, use_seps = FALSE) %>% render_formats_test("html"))[["num_1"]], - c("183623.00%", "276339.00%", "93729.00%", "64300.00%", - "21223.20%", "0.00%", "-2324.00%")) + c("183623.00%", "276339.00%", "93729.00%", + "64300.00%", "21223.20%", "0.00%", + "−2324.00%") + ) # Format the `num_1` column to 2 decimal places, use a single space # character as digit grouping separators, use all other defaults; @@ -75,8 +85,10 @@ test_that("the `fmt_percent()` function works correctly", { (tab %>% fmt_percent(columns = "num_1", decimals = 2, sep_mark = " ") %>% render_formats_test("html"))[["num_1"]], - c("183 623.00%", "276 339.00%", "93 729.00%", "64 300.00%", - "21 223.20%", "0.00%", "-2 324.00%")) + c("183 623.00%", "276 339.00%", "93 729.00%", + "64 300.00%", "21 223.20%", "0.00%", + "−2 324.00%") + ) # Format the `num_1` column to 2 decimal places, use a period for the # digit grouping separators and a comma for the decimal mark, use @@ -86,8 +98,10 @@ test_that("the `fmt_percent()` function works correctly", { fmt_percent(columns = "num_1", decimals = 2, sep_mark = ".", dec_mark = ",") %>% render_formats_test("html"))[["num_1"]], - c("183.623,00%", "276.339,00%", "93.729,00%", "64.300,00%", - "21.223,20%", "0,00%", "-2.324,00%")) + c("183.623,00%", "276.339,00%", "93.729,00%", + "64.300,00%", "21.223,20%", "0,00%", + "−2.324,00%") + ) # Format the `num_1` column to 2 decimal places, apply parentheses to # all negative values, use all other defaults; extract `output_df` and @@ -96,8 +110,10 @@ test_that("the `fmt_percent()` function works correctly", { (tab %>% fmt_percent(columns = "num_1", decimals = 2, negative_val = "parens") %>% render_formats_test("html"))[["num_1"]], - c("183,623.00%", "276,339.00%", "93,729.00%", "64,300.00%", - "21,223.20%", "0.00%", "(2,324.00%)")) + c("183,623.00%", "276,339.00%", "93,729.00%", + "64,300.00%", "21,223.20%", "0.00%", + "(2,324.00%)") + ) # Format the `num_1` column to 2 decimal places, prepend and append # all values by 2 different literals, use all other defaults; extract @@ -106,8 +122,11 @@ test_that("the `fmt_percent()` function works correctly", { (tab %>% fmt_percent(columns = "num_1", decimals = 2, pattern = "a {x}:n") %>% render_formats_test("html"))[["num_1"]], - c("a 183,623.00%:n", "a 276,339.00%:n", "a 93,729.00%:n", - "a 64,300.00%:n", "a 21,223.20%:n", "a 0.00%:n", "a -2,324.00%:n")) + c("a 183,623.00%:n", "a 276,339.00%:n", + "a 93,729.00%:n", "a 64,300.00%:n", + "a 21,223.20%:n", "a 0.00%:n", + "a −2,324.00%:n") + ) # Format the `num_1` column to 0 decimal places, place a space between # the percent sign (on the right) and the value, use all other defaults; @@ -117,8 +136,10 @@ test_that("the `fmt_percent()` function works correctly", { fmt_percent(columns = "num_1", decimals = 0, placement = "right", incl_space = TRUE) %>% render_formats_test("html"))[["num_1"]], - c("183,623 %", "276,339 %", "93,729 %", "64,300 %", - "21,223 %", "0 %", "-2,324 %")) + c("183,623 %", "276,339 %", "93,729 %", + "64,300 %", "21,223 %", "0 %", + "−2,324 %") + ) # Format the `num_1` column to 0 decimal places, place a space between # the percent sign (on the left) and the value, use all other defaults; @@ -128,8 +149,10 @@ test_that("the `fmt_percent()` function works correctly", { fmt_percent(columns = "num_1", decimals = 0, placement = "left", incl_space = TRUE) %>% render_formats_test("html"))[["num_1"]], - c("% 183,623", "% 276,339", "% 93,729", "% 64,300", - "% 21,223", "% 0", "% -2,324")) + c("% 183,623", "% 276,339", "% 93,729", + "% 64,300", "% 21,223", "% 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 @@ -138,8 +161,10 @@ test_that("the `fmt_percent()` function works correctly", { (tab %>% fmt_percent(columns = "num_1", decimals = 2, locale = "en_US") %>% render_formats_test("html"))[["num_1"]], - c("183,623.00%", "276,339.00%", "93,729.00%", - "64,300.00%", "21,223.20%", "0.00%", "-2,324.00%")) + c("183,623.00%", "276,339.00%", "93,729.00%", + "64,300.00%", "21,223.20%", "0.00%", + "−2,324.00%") + ) # Format the `num_1` column to 2 decimal places, apply the `da_DK` # locale and use all other defaults; extract `output_df` and compare @@ -148,8 +173,10 @@ test_that("the `fmt_percent()` function works correctly", { (tab %>% fmt_percent(columns = "num_1", decimals = 2, locale = "da_DK") %>% render_formats_test("html"))[["num_1"]], - c("183.623,00%", "276.339,00%", "93.729,00%", - "64.300,00%", "21.223,20%", "0,00%", "-2.324,00%")) + c("183.623,00%", "276.339,00%", "93.729,00%", + "64.300,00%", "21.223,20%", "0,00%", + "−2.324,00%") + ) # Format the `num_1` column to 2 decimal places, apply the `de_AT` # locale and use all other defaults; extract `output_df` and compare @@ -158,8 +185,10 @@ test_that("the `fmt_percent()` function works correctly", { (tab %>% fmt_percent(columns = "num_1", decimals = 2, locale = "de_AT") %>% render_formats_test("html"))[["num_1"]], - c("183 623,00%", "276 339,00%", "93 729,00%", - "64 300,00%", "21 223,20%", "0,00%", "-2 324,00%")) + c("183 623,00%", "276 339,00%", "93 729,00%", + "64 300,00%", "21 223,20%", "0,00%", + "−2 324,00%") + ) # Format the `num_1` column to 2 decimal places, apply the `et_EE` # locale and use all other defaults; extract `output_df` and compare @@ -168,8 +197,10 @@ test_that("the `fmt_percent()` function works correctly", { (tab %>% fmt_percent(columns = "num_1", decimals = 2, locale = "et_EE") %>% render_formats_test("html"))[["num_1"]], - c("183 623,00%", "276 339,00%", "93 729,00%", - "64 300,00%", "21 223,20%", "0,00%", "-2 324,00%")) + c("183 623,00%", "276 339,00%", "93 729,00%", + "64 300,00%", "21 223,20%", "0,00%", + "−2 324,00%") + ) # Format the `num_1` column to 2 decimal places, apply the `gl_ES` # locale and use all other defaults; extract `output_df` and compare @@ -178,6 +209,8 @@ test_that("the `fmt_percent()` function works correctly", { (tab %>% fmt_percent(columns = "num_1", decimals = 2, locale = "gl_ES") %>% render_formats_test("html"))[["num_1"]], - c("183.623,00%", "276.339,00%", "93.729,00%", - "64.300,00%", "21.223,20%", "0,00%", "-2.324,00%")) + c("183.623,00%", "276.339,00%", "93.729,00%", + "64.300,00%", "21.223,20%", "0,00%", + "−2.324,00%") + ) }) diff --git a/tests/testthat/test-fmt_scientific.R b/tests/testthat/test-fmt_scientific.R index 2eb6cc8d3f..ced4f96e51 100644 --- a/tests/testthat/test-fmt_scientific.R +++ b/tests/testthat/test-fmt_scientific.R @@ -12,7 +12,8 @@ test_that("the `fmt_scientific()` function works correctly", { "october", "november", "december"), num_1 = c(1836.23, 2763.39, 937.29, 643.00, 2.232, 0, -23.24), num_2 = c(34, 74, 23, 93, 35, 0.01, NA), - stringsAsFactors = FALSE) + stringsAsFactors = FALSE + ) # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset @@ -25,10 +26,14 @@ test_that("the `fmt_scientific()` function works correctly", { expect_true( all( names(attributes(tab)) %in% - c("names", "class", "row.names", + c( + "names", "class", "row.names", "boxh_df", "stub_df", "footnotes_df", "styles_df", "rows_df", "cols_df", "col_labels", "grp_labels", - "arrange_groups", "data_df", "opts_df", "formats", "transforms"))) + "arrange_groups", "data_df", "opts_df", "formats", "transforms" + ) + ) + ) # Extract vectors from the table object for comparison # to the original dataset @@ -48,12 +53,14 @@ test_that("the `fmt_scientific()` function works correctly", { # that does not exist expect_error( tab %>% - fmt_scientific(columns = "num_3", decimals = 2)) + fmt_scientific(columns = "num_3", decimals = 2) + ) # Expect an error when using a locale that does not exist expect_error( tab %>% - fmt_scientific(columns = "num_2", decimals = 2, locale = "aa_bb")) + fmt_scientific(columns = "num_2", decimals = 2, locale = "aa_bb") + ) # Format the `num_1` column to 2 decimal places, use all # other defaults; extract `output_df` in the HTML context @@ -68,7 +75,9 @@ test_that("the `fmt_scientific()` function works correctly", { "9.37 × 102", "6.43 × 102", "2.23", "0.00", - "-2.32 × 101")) + "−2.32 × 101" + ) + ) # Format the `num_2` column to 2 decimal places, use all # other defaults; extract `output_df` in the HTML context @@ -83,7 +92,7 @@ test_that("the `fmt_scientific()` function works correctly", { "2.30 × 101", "9.30 × 101", "3.50 × 101", - "1.00 × 10-2", + "1.00 × 10−2", "NA" ) ) @@ -97,7 +106,9 @@ test_that("the `fmt_scientific()` function works correctly", { render_formats_test("default"))[["num_1"]], c( "1.84 x 10(3)", "2.76 x 10(3)", "9.37 x 10(2)", - "6.43 x 10(2)", "2.23", "0.00", "-2.32 x 10(1)")) + "6.43 x 10(2)", "2.23", "0.00", "-2.32 x 10(1)" + ) + ) # Format the `num_1` column to 5 decimal places, use all # other defaults; extract `output_df` in the HTML context @@ -112,7 +123,9 @@ test_that("the `fmt_scientific()` function works correctly", { "9.37290 × 102", "6.43000 × 102", "2.23200", "0.00000", - "-2.32400 × 101")) + "−2.32400 × 101" + ) + ) # Format the `num_1` column to 5 decimal places, use all # other defaults; extract `output_df` in the default context @@ -127,7 +140,9 @@ test_that("the `fmt_scientific()` function works correctly", { "9.37290 x 10(2)", "6.43000 x 10(2)", "2.23200", "0.00000", - "-2.32400 x 10(1)")) + "-2.32400 x 10(1)" + ) + ) # Format the `num_1` column to 2 decimal places, use a period for the # digit grouping separators and a comma for the decimal mark, use @@ -144,7 +159,9 @@ test_that("the `fmt_scientific()` function works correctly", { "9,37 × 102", "6,43 × 102", "2,23", "0,00", - "-2,32 × 101")) + "−2,32 × 101" + ) + ) # Format the `num_1` column to 2 decimal places, use a period for the # digit grouping separators and a comma for the decimal mark, use @@ -157,7 +174,9 @@ test_that("the `fmt_scientific()` function works correctly", { render_formats_test("default"))[["num_1"]], c( "1,84 x 10(3)", "2,76 x 10(3)", "9,37 x 10(2)", - "6,43 x 10(2)", "2,23", "0,00", "-2,32 x 10(1)")) + "6,43 x 10(2)", "2,23", "0,00", "-2,32 x 10(1)" + ) + ) # Format the `num_1` column to 4 decimal places, scale all values by # 1/1000, use all other defaults; extract `output_df` in the HTML @@ -167,12 +186,14 @@ test_that("the `fmt_scientific()` function works correctly", { fmt_scientific(columns = "num_1", decimals = 4, scale_by = 1/1000) %>% render_formats_test("html"))[["num_1"]], c( - "1.8362 × 100", - "2.7634 × 100", - "9.3729 × 10-1", - "6.4300 × 10-1", - "2.2320", "0.0000", - "-2.3240 × 10-2")) + "1.8362", "2.7634", + "9.3729 × 10−1", + "6.4300 × 10−1", + "2.2320 × 10−3", + "0.0000", + "−2.3240 × 10−2" + ) + ) # Format the `num_1` column to 4 decimal places, scale all values by # 1/1000, use all other defaults; extract `output_df` in the default @@ -181,8 +202,12 @@ test_that("the `fmt_scientific()` function works correctly", { (tab %>% fmt_scientific(columns = "num_1", decimals = 4, scale_by = 1/1000) %>% render_formats_test("default"))[["num_1"]], - c("1.8362 x 10(0)", "2.7634 x 10(0)", "9.3729 x 10(-1)", - "6.4300 x 10(-1)", "2.2320", "0.0000", "-2.3240 x 10(-2)")) + c( + "1.8362", "2.7634", "9.3729 x 10(-1)", + "6.4300 x 10(-1)", "2.2320 x 10(-3)", + "0.0000", "-2.3240 x 10(-2)" + ) + ) # Format the `num_1` column to 2 decimal places, prepend and append # all values by 2 different literals, use all other defaults; extract @@ -197,7 +222,9 @@ test_that("the `fmt_scientific()` function works correctly", { "a 9.37 × 102 b", "a 6.43 × 102 b", "a 2.23 b", "a 0.00 b", - "a -2.32 × 101 b")) + "a −2.32 × 101 b" + ) + ) # Format the `num_1` column to 2 decimal places, prepend and append # all values by 2 different literals, use all other defaults; extract @@ -208,7 +235,9 @@ test_that("the `fmt_scientific()` function works correctly", { render_formats_test("default"))[["num_1"]], c( "a 1.84 x 10(3) b", "a 2.76 x 10(3) b", "a 9.37 x 10(2) b", - "a 6.43 x 10(2) b", "a 2.23 b", "a 0.00 b", "a -2.32 x 10(1) b")) + "a 6.43 x 10(2) b", "a 2.23 b", "a 0.00 b", "a -2.32 x 10(1) b" + ) + ) # 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 @@ -223,7 +252,9 @@ test_that("the `fmt_scientific()` function works correctly", { "9.37 × 102", "6.43 × 102", "2.23", "0.00", - "-2.32 × 101")) + "−2.32 × 101" + ) + ) # Format the `num_1` column to 2 decimal places, apply the `da_DK` # locale and use all other defaults; extract `output_df` in the HTML @@ -238,7 +269,9 @@ test_that("the `fmt_scientific()` function works correctly", { "9,37 × 102", "6,43 × 102", "2,23", "0,00", - "-2,32 × 101")) + "−2,32 × 101" + ) + ) # Format the `num_1` column to 2 decimal places, apply the `de_AT` # locale and use all other defaults; extract `output_df` in the HTML @@ -253,7 +286,9 @@ test_that("the `fmt_scientific()` function works correctly", { "9,37 × 102", "6,43 × 102", "2,23", "0,00", - "-2,32 × 101")) + "−2,32 × 101" + ) + ) # Format the `num_1` column to 2 decimal places, apply the `et_EE` # locale and use all other defaults; extract `output_df` in the HTML @@ -268,7 +303,9 @@ test_that("the `fmt_scientific()` function works correctly", { "9,37 × 102", "6,43 × 102", "2,23", "0,00", - "-2,32 × 101")) + "−2,32 × 101" + ) + ) # Format the `num_1` column to 2 decimal places, apply the `gl_ES` # locale and use all other defaults; extract `output_df` in the HTML @@ -283,7 +320,9 @@ test_that("the `fmt_scientific()` function works correctly", { "9,37 × 102", "6,43 × 102", "2,23", "0,00", - "-2,32 × 101")) + "−2,32 × 101" + ) + ) }) test_that("`fmt_scientific()` can handle extremely large and small values", { @@ -328,13 +367,13 @@ test_that("`fmt_scientific()` can handle extremely large and small values", { fmt_scientific(columns = "num", decimals = 5) %>% render_formats_test("html"))[["num"]], c( - "-1.50000 × 10200", - "-1.50000 × 10100", - "-2.50000", - "-3.50000 × 10-100", - "-3.50000 × 10-200", - "1.50000 × 10-200", - "1.50000 × 10-100", + "−1.50000 × 10200", + "−1.50000 × 10100", + "−2.50000", + "−3.50000 × 10−100", + "−3.50000 × 10−200", + "1.50000 × 10−200", + "1.50000 × 10−100", "2.50000", "3.50000 × 10100", "3.50000 × 10200" diff --git a/tests/testthat/test-l_conditional_fmt.R b/tests/testthat/test-l_conditional_fmt.R index 26ea18e786..71aa0a9053 100644 --- a/tests/testthat/test-l_conditional_fmt.R +++ b/tests/testthat/test-l_conditional_fmt.R @@ -10,7 +10,8 @@ data_tbl <- "october", "november", "december"), num_1 = c(1836.23, 2763.39, 937.29, 643.00, 212.232, 0, -23.24), num_2 = c(34, 74, 23, NA, 35, NA, NA), - stringsAsFactors = FALSE) + stringsAsFactors = FALSE + ) # Create a `tbl_latex` object with `gt()` and the # `data_tbl` dataset @@ -24,7 +25,8 @@ time_tbl <- time = c("16:45", "19:23", "01:30", "08:00"), datetime = c("2010-03-25 19:45", "2015-06-12 09:25", "2016-01-15 14:38", "2012-08-07 12:31"), - stringsAsFactors = FALSE) + stringsAsFactors = FALSE + ) # Create a `tbl_latex_time` object with `gt()` and the # `data_tbl` dataset @@ -39,8 +41,8 @@ test_that("the `fmt_number()` function works with conditional `rows`", { decimals = 4, rows = num_1 < 1000) %>% render_formats_test(context = "latex"))[["num_1"]], - c("1836.23", "2763.39", "937.2900", "643.0000", - "212.2320", "0.0000", "-23.2400") + c("1836.23", "2763.39", "$937.2900$", "$643.0000$", + "$212.2320$", "$0.0000$", "$-23.2400$") ) expect_equal( @@ -50,7 +52,7 @@ test_that("the `fmt_number()` function works with conditional `rows`", { decimals = 4, rows = char_2 %in% c("june", "july") & grepl("sa.*", char_1)) %>% render_formats_test(context = "latex"))[["num_2"]], - c("34.0000", "74", "23", "NA", "35", "NA", "NA") + c("$34.0000$", "74", "23", "NA", "35", "NA", "NA") ) }) @@ -63,9 +65,9 @@ test_that("the `fmt_scientific()` function works with conditional `rows`", { decimals = 4, rows = num_1 < 1000) %>% render_formats_test(context = "latex"))[["num_1"]], - c("1836.23", "2763.39", "9.3729$ \\times 10^{2}$", - "6.4300$ \\times 10^{2}$", "2.1223$ \\times 10^{2}$", "0.0000", - "-2.3240$ \\times 10^{1}$") + c("1836.23", "2763.39", "$9.3729 \\times 10^{2}$", + "$6.4300 \\times 10^{2}$", "$2.1223 \\times 10^{2}$", "$0.0000$", + "$-2.3240 \\times 10^{1}$") ) expect_equal( @@ -75,7 +77,7 @@ test_that("the `fmt_scientific()` function works with conditional `rows`", { decimals = 4, rows = char_2 %in% c("june", "july") & grepl("sa.*", char_1)) %>% render_formats_test(context = "latex"))[["num_2"]], - c("3.4000$ \\times 10^{1}$", "74", "23", "NA", "35", "NA", "NA") + c("$3.4000 \\times 10^{1}$", "74", "23", "NA", "35", "NA", "NA") ) }) @@ -88,8 +90,8 @@ test_that("the `fmt_percent()` function works with conditional `rows`", { decimals = 2, rows = num_1 < 1000) %>% render_formats_test(context = "latex"))[["num_1"]], - c("1836.23", "2763.39", "93,729.00\\%", "64,300.00\\%", - "21,223.20\\%", "0.00\\%", "-2,324.00\\%") + c("1836.23", "2763.39", "$93,729.00\\%$", "$64,300.00\\%$", + "$21,223.20\\%$", "$0.00\\%$", "$-2,324.00\\%$") ) expect_equal( @@ -99,7 +101,7 @@ test_that("the `fmt_percent()` function works with conditional `rows`", { decimals = 2, rows = char_2 %in% c("june", "july") & grepl("sa.*", char_1)) %>% render_formats_test(context = "latex"))[["num_2"]], - c("3,400.00\\%", "74", "23", "NA", "35", "NA", "NA") + c("$3,400.00\\%$", "74", "23", "NA", "35", "NA", "NA") ) }) @@ -112,8 +114,8 @@ test_that("the `fmt_currency()` function works with conditional `rows`", { currency = "USD", rows = num_1 < 1000) %>% render_formats_test(context = "latex"))[["num_1"]], - c("1836.23", "2763.39", "\\$937.29", "\\$643.00", - "\\$212.23", "\\$0.00", "\\$-23.24") + c("1836.23", "2763.39", "$\\text{\\$}937.29$", "$\\text{\\$}643.00$", + "$\\text{\\$}212.23$", "$\\text{\\$}0.00$", "$-\\text{\\$}23.24$") ) expect_equal( @@ -123,7 +125,7 @@ test_that("the `fmt_currency()` function works with conditional `rows`", { currency = "USD", rows = char_2 %in% c("june", "july") & grepl("sa.*", char_1)) %>% render_formats_test(context = "latex"))[["num_2"]], - c("\\$34.00", "74", "23", "NA", "35", "NA", "NA") + c("$\\text{\\$}34.00$", "74", "23", "NA", "35", "NA", "NA") ) }) diff --git a/tests/testthat/test-l_fmt_currency.R b/tests/testthat/test-l_fmt_currency.R index 5045b1e9f3..8fe17bbdeb 100644 --- a/tests/testthat/test-l_fmt_currency.R +++ b/tests/testthat/test-l_fmt_currency.R @@ -12,7 +12,8 @@ test_that("the `fmt_currency()` function works correctly", { "october", "november", "december"), num_1 = c(1836.23, 2763.39, 937.29, 643.00, 212.232, 0, -23.24), num_2 = c(34, 74, 23, 93, 35, 76, 57), - stringsAsFactors = FALSE) + stringsAsFactors = FALSE + ) # Create a `tbl_latex` object with `gt()` and the # `data_tbl` dataset @@ -24,8 +25,10 @@ test_that("the `fmt_currency()` function works correctly", { (tbl_latex %>% fmt_currency(columns = "num_1", currency = "USD") %>% render_formats_test(context = "latex"))[["num_1"]], - c("\\$1,836.23", "\\$2,763.39", "\\$937.29", "\\$643.00", - "\\$212.23", "\\$0.00", "\\$-23.24")) + c("$\\text{\\$}1,836.23$", "$\\text{\\$}2,763.39$", "$\\text{\\$}937.29$", + "$\\text{\\$}643.00$", "$\\text{\\$}212.23$", "$\\text{\\$}0.00$", + "$-\\text{\\$}23.24$") + ) # Format the `num_1` column as USD to 5 decimal places, use all # other defaults; extract `output_df` and compare to expected values @@ -33,8 +36,11 @@ test_that("the `fmt_currency()` function works correctly", { (tbl_latex %>% fmt_currency(columns = "num_1", currency = "USD", decimals = 5) %>% render_formats_test("latex"))[["num_1"]], - c("\\$1,836.23000", "\\$2,763.39000", "\\$937.29000", "\\$643.00000", - "\\$212.23200", "\\$0.00000", "\\$-23.24000")) + c("$\\text{\\$}1,836.23000$", "$\\text{\\$}2,763.39000$", + "$\\text{\\$}937.29000$", "$\\text{\\$}643.00000$", + "$\\text{\\$}212.23200$", "$\\text{\\$}0.00000$", + "$-\\text{\\$}23.24000$") + ) # Format the `num_1` column as USD, and don't include the subunits; # use all other defaults; extract `output_df` and compare to @@ -43,7 +49,10 @@ test_that("the `fmt_currency()` function works correctly", { (tbl_latex %>% fmt_currency(columns = "num_1", currency = "USD", use_subunits = FALSE) %>% render_formats_test("latex"))[["num_1"]], - c("\\$1,836", "\\$2,763", "\\$937", "\\$643", "\\$212", "\\$0", "\\$-23")) + c("$\\text{\\$}1,836$", "$\\text{\\$}2,763$", "$\\text{\\$}937$", + "$\\text{\\$}643$", "$\\text{\\$}212$", "$\\text{\\$}0$", + "$-\\text{\\$}23$") + ) # Format the `num_1` column as USD, don't use digit # grouping separators, use all other defaults; extract `output_df` @@ -52,8 +61,10 @@ test_that("the `fmt_currency()` function works correctly", { (tbl_latex %>% fmt_currency(columns = "num_1", currency = "USD", use_seps = FALSE) %>% render_formats_test("latex"))[["num_1"]], - c("\\$1836.23", "\\$2763.39", "\\$937.29", "\\$643.00", - "\\$212.23", "\\$0.00", "\\$-23.24")) + c("$\\text{\\$}1836.23$", "$\\text{\\$}2763.39$", "$\\text{\\$}937.29$", + "$\\text{\\$}643.00$", "$\\text{\\$}212.23$", "$\\text{\\$}0.00$", + "$-\\text{\\$}23.24$") + ) # Format the `num_1` column to 2 decimal places, use a single space # character as digit grouping separators, use all other defaults; @@ -62,8 +73,10 @@ test_that("the `fmt_currency()` function works correctly", { (tbl_latex %>% fmt_currency(columns = "num_1", currency = "USD", sep_mark = " ") %>% render_formats_test("latex"))[["num_1"]], - c("\\$1 836.23", "\\$2 763.39", "\\$937.29", "\\$643.00", - "\\$212.23", "\\$0.00", "\\$-23.24")) + c("$\\text{\\$}1 836.23$", "$\\text{\\$}2 763.39$", "$\\text{\\$}937.29$", + "$\\text{\\$}643.00$", "$\\text{\\$}212.23$", "$\\text{\\$}0.00$", + "$-\\text{\\$}23.24$") + ) # Format the `num_1` column as USD, use a period for the digit grouping # separators and a comma for the decimal mark, use all other defaults; @@ -74,8 +87,10 @@ test_that("the `fmt_currency()` function works correctly", { columns = "num_1", currency = "USD", sep_mark = ".", dec_mark = ",") %>% render_formats_test("latex"))[["num_1"]], - c("\\$1.836,23", "\\$2.763,39", "\\$937,29", "\\$643,00", - "\\$212,23", "\\$0,00", "\\$-23,24")) + c("$\\text{\\$}1.836,23$", "$\\text{\\$}2.763,39$", "$\\text{\\$}937,29$", + "$\\text{\\$}643,00$", "$\\text{\\$}212,23$", "$\\text{\\$}0,00$", + "$-\\text{\\$}23,24$") + ) # Format the `num_1` column as USD, apply parentheses to all negative # values, use all other defaults; extract `output_df` and compare @@ -85,8 +100,10 @@ test_that("the `fmt_currency()` function works correctly", { fmt_currency( columns = "num_1", currency = "USD", negative_val = "parens") %>% render_formats_test("latex"))[["num_1"]], - c("\\$1,836.23", "\\$2,763.39", "\\$937.29", "\\$643.00", - "\\$212.23", "\\$0.00", "(\\$23.24)")) + c("$\\text{\\$}1,836.23$", "$\\text{\\$}2,763.39$", "$\\text{\\$}937.29$", + "$\\text{\\$}643.00$", "$\\text{\\$}212.23$", "$\\text{\\$}0.00$", + "$\\left(\\text{\\$}23.24\\right)$") + ) # Format the `num_1` column as USD, apply parentheses to all negative # values, use all other defaults; apply the default context, extract @@ -96,8 +113,10 @@ test_that("the `fmt_currency()` function works correctly", { fmt_currency( columns = "num_1", currency = "USD", negative_val = "parens") %>% render_formats_test("latex"))[["num_1"]], - c("\\$1,836.23", "\\$2,763.39", "\\$937.29", "\\$643.00", - "\\$212.23", "\\$0.00", "(\\$23.24)")) + c("$\\text{\\$}1,836.23$", "$\\text{\\$}2,763.39$", "$\\text{\\$}937.29$", + "$\\text{\\$}643.00$", "$\\text{\\$}212.23$", "$\\text{\\$}0.00$", + "$\\left(\\text{\\$}23.24\\right)$") + ) # Format the `num_1` column as USD to 4 decimal places, scale all values by # 1/1000, use all other defaults; extract `output_df` and compare @@ -108,8 +127,10 @@ test_that("the `fmt_currency()` function works correctly", { columns = "num_1", currency = "USD", decimals = 4, scale_by = 1/1000) %>% render_formats_test("latex"))[["num_1"]], - c("\\$1.8362", "\\$2.7634", "\\$0.9373", "\\$0.6430", - "\\$0.2122", "\\$0.0000", "\\$-0.0232")) + c("$\\text{\\$}1.8362$", "$\\text{\\$}2.7634$", "$\\text{\\$}0.9373$", + "$\\text{\\$}0.6430$", "$\\text{\\$}0.2122$", "$\\text{\\$}0.0000$", + "$-\\text{\\$}0.0232$") + ) # Format the `num_1` column as USD, prepend and append all values by 2 # different literals, use all other defaults; extract `output_df` and @@ -119,8 +140,11 @@ test_that("the `fmt_currency()` function works correctly", { fmt_currency( columns = "num_1", currency = "USD", pattern = "a {x} b") %>% render_formats_test("latex"))[["num_1"]], - c("a \\$1,836.23 b", "a \\$2,763.39 b", "a \\$937.29 b", "a \\$643.00 b", - "a \\$212.23 b", "a \\$0.00 b", "a \\$-23.24 b")) + c("a $\\text{\\$}1,836.23$ b", "a $\\text{\\$}2,763.39$ b", + "a $\\text{\\$}937.29$ b", "a $\\text{\\$}643.00$ b", + "a $\\text{\\$}212.23$ b", "a $\\text{\\$}0.00$ b", + "a $-\\text{\\$}23.24$ b") + ) # Format the `num_1` column as USD to 4 decimal places, scale all values # by 1/1000 and append a `K` character to the resultant values, use @@ -131,8 +155,10 @@ test_that("the `fmt_currency()` function works correctly", { columns = "num_1", currency = "USD", decimals = 4, scale_by = 1/1000, pattern = "{x}K") %>% render_formats_test("latex"))[["num_1"]], - c("\\$1.8362K", "\\$2.7634K", "\\$0.9373K", "\\$0.6430K", - "\\$0.2122K", "\\$0.0000K", "\\$-0.0232K")) + c("$\\text{\\$}1.8362$K", "$\\text{\\$}2.7634$K", "$\\text{\\$}0.9373$K", + "$\\text{\\$}0.6430$K", "$\\text{\\$}0.2122$K", "$\\text{\\$}0.0000$K", + "$-\\text{\\$}0.0232$K") + ) # Format the `num_1` column as USD, apply the `en_US` locale and use all # other defaults; extract `output_df` and compare to expected values @@ -141,8 +167,10 @@ test_that("the `fmt_currency()` function works correctly", { fmt_currency( columns = "num_1", currency = "USD", locale = "en_US") %>% render_formats_test("latex"))[["num_1"]], - c("\\$1,836.23", "\\$2,763.39", "\\$937.29", "\\$643.00", - "\\$212.23", "\\$0.00", "\\$-23.24")) + c("$\\text{\\$}1,836.23$", "$\\text{\\$}2,763.39$", "$\\text{\\$}937.29$", + "$\\text{\\$}643.00$", "$\\text{\\$}212.23$", "$\\text{\\$}0.00$", + "$-\\text{\\$}23.24$") + ) # Format the `num_1` column as DKK, apply the `da_DK` locale and use all # other defaults; extract `output_df` and compare to expected values @@ -152,19 +180,22 @@ test_that("the `fmt_currency()` function works correctly", { columns = "num_1", currency = "DKK", locale = "da_DK", placement = "right", incl_space = TRUE) %>% render_formats_test("latex"))[["num_1"]], - c("1.836,23 kr.", "2.763,39 kr.", "937,29 kr.", "643,00 kr.", - "212,23 kr.", "0,00 kr.", "-23,24 kr.")) + c("$1.836,23 \\text{kr.}$", "$2.763,39 \\text{kr.}$", + "$937,29 \\text{kr.}$", "$643,00 \\text{kr.}$", "$212,23 \\text{kr.}$", + "$0,00 \\text{kr.}$", "$-23,24 \\text{kr.}$") + ) # Format the `num_1` column as DKK, apply the `da_DK` locale and use all - # other defaults; use the default context, extract `output_df`, and - # compare to expected values + # other defaults; extract `output_df`, and compare to expected values expect_equal( (tbl_latex %>% fmt_currency( columns = "num_1", currency = "DKK", locale = "da_DK") %>% render_formats_test("latex"))[["num_1"]], - c("kr.1.836,23", "kr.2.763,39", "kr.937,29", "kr.643,00", - "kr.212,23", "kr.0,00", "kr.-23,24")) + c("$\\text{kr.}1.836,23$", "$\\text{kr.}2.763,39$", "$\\text{kr.}937,29$", + "$\\text{kr.}643,00$", "$\\text{kr.}212,23$", "$\\text{kr.}0,00$", + "$-\\text{kr.}23,24$" ) + ) # Format the `num_1` column as EUR, apply the `de_AT` locale and use all # other defaults; extract `output_df` and compare to expected values @@ -173,8 +204,10 @@ test_that("the `fmt_currency()` function works correctly", { fmt_currency( columns = "num_1", currency = "EUR", locale = "de_AT") %>% render_formats_test("latex"))[["num_1"]], - c("EUR1 836,23", "EUR2 763,39", "EUR937,29", "EUR643,00", - "EUR212,23", "EUR0,00", "EUR-23,24")) + c("$\\text{EUR}1 836,23$", "$\\text{EUR}2 763,39$", "$\\text{EUR}937,29$", + "$\\text{EUR}643,00$", "$\\text{EUR}212,23$", "$\\text{EUR}0,00$", + "$-\\text{EUR}23,24$") + ) # Format the `num_1` column as EUR, apply the `de_AT` locale and use all # other defaults; use the default context, extract `output_df`, and @@ -184,8 +217,10 @@ test_that("the `fmt_currency()` function works correctly", { fmt_currency( columns = "num_1", currency = "EUR", locale = "de_AT") %>% render_formats_test("latex"))[["num_1"]], - c("EUR1 836,23", "EUR2 763,39", "EUR937,29", "EUR643,00", - "EUR212,23", "EUR0,00", "EUR-23,24")) + c("$\\text{EUR}1 836,23$", "$\\text{EUR}2 763,39$", "$\\text{EUR}937,29$", + "$\\text{EUR}643,00$", "$\\text{EUR}212,23$", "$\\text{EUR}0,00$", + "$-\\text{EUR}23,24$") + ) # Format the `num_1` column to 2 decimal places, apply the `et_EE` # locale and use all other defaults; extract `output_df` and compare @@ -195,6 +230,8 @@ test_that("the `fmt_currency()` function works correctly", { fmt_currency( columns = "num_1", currency = "EUR", locale = "et_EE") %>% render_formats_test("latex"))[["num_1"]], - c("EUR1 836,23", "EUR2 763,39", "EUR937,29", "EUR643,00", - "EUR212,23", "EUR0,00", "EUR-23,24")) + c("$\\text{EUR}1 836,23$", "$\\text{EUR}2 763,39$", "$\\text{EUR}937,29$", + "$\\text{EUR}643,00$", "$\\text{EUR}212,23$", "$\\text{EUR}0,00$", + "$-\\text{EUR}23,24$") + ) }) diff --git a/tests/testthat/test-l_fmt_missing.R b/tests/testthat/test-l_fmt_missing.R index 85090c8133..20230353ce 100644 --- a/tests/testthat/test-l_fmt_missing.R +++ b/tests/testthat/test-l_fmt_missing.R @@ -58,7 +58,7 @@ test_that("the `fmt_missing()` function works correctly", { ) %>% fmt_missing(columns = TRUE) %>% render_formats_test(context = "latex"))[["num_1"]], - c("---", "74.000", "---", "93.000", "---", "76.000", "---")) + c("---", "$74.000$", "---", "$93.000$", "---", "$76.000$", "---")) # Reverse the ordering: use `fmt_missing()` first # then `fmt_number()`; expect the same output as before @@ -70,5 +70,5 @@ test_that("the `fmt_missing()` function works correctly", { decimals = 3 ) %>% render_formats_test(context = "latex"))[["num_1"]], - c("---", "74.000", "---", "93.000", "---", "76.000", "---")) + c("---", "$74.000$", "---", "$93.000$", "---", "$76.000$", "---")) }) diff --git a/tests/testthat/test-l_fmt_number.R b/tests/testthat/test-l_fmt_number.R index 787f79b290..bb980cafc9 100644 --- a/tests/testthat/test-l_fmt_number.R +++ b/tests/testthat/test-l_fmt_number.R @@ -12,7 +12,8 @@ test_that("the `fmt_number()` function works correctly", { "october", "november", "december"), num_1 = c(1836.23, 2763.39, 937.29, 643.00, 212.232, 0, -23.24), num_2 = c(34, 74, 23, 93, 35, 76, 57), - stringsAsFactors = FALSE) + stringsAsFactors = FALSE + ) # Create a `tbl_latex` object with `gt()` and the # `data_tbl` dataset @@ -24,7 +25,9 @@ test_that("the `fmt_number()` function works correctly", { (tbl_latex %>% fmt_number(columns = "num_1", decimals = 2) %>% render_formats_test(context = "latex"))[["num_1"]], - c("1,836.23", "2,763.39", "937.29", "643.00", "212.23", "0.00", "-23.24")) + c("$1,836.23$", "$2,763.39$", "$937.29$", "$643.00$", + "$212.23$", "$0.00$", "$-23.24$") + ) # Format the `num_1` column to 5 decimal places, use all # other defaults; extract `output_df` and compare to expected values @@ -32,18 +35,23 @@ test_that("the `fmt_number()` function works correctly", { (tbl_latex %>% fmt_number(columns = "num_1", decimals = 5) %>% render_formats_test("latex"))[["num_1"]], - c("1,836.23000", "2,763.39000", "937.29000", "643.00000", - "212.23200", "0.00000", "-23.24000")) + c("$1,836.23000$", "$2,763.39000$", "$937.29000$", + "$643.00000$", "$212.23200$", "$0.00000$", "$-23.24000$") + ) # Format the `num_1` column to 2 decimal places, drop the trailing # zeros, use all other defaults; extract `output_df` and compare to # expected values expect_equal( (tbl_latex %>% - fmt_number(columns = "num_1", decimals = 2, - drop_trailing_zeros = TRUE) %>% + fmt_number( + columns = "num_1", decimals = 2, + drop_trailing_zeros = TRUE + ) %>% render_formats_test("latex"))[["num_1"]], - c("1,836.23", "2,763.39", "937.29", "643", "212.23", "0", "-23.24")) + c("$1,836.23$", "$2,763.39$", "$937.29$", "$643$", + "$212.23$", "$0$", "$-23.24$") + ) # Format the `num_1` column to 2 decimal places, don't use digit # grouping separators, use all other defaults; extract `output_df` @@ -52,7 +60,9 @@ test_that("the `fmt_number()` function works correctly", { (tbl_latex %>% fmt_number(columns = "num_1", decimals = 2, use_seps = FALSE) %>% render_formats_test("latex"))[["num_1"]], - c("1836.23", "2763.39", "937.29", "643.00", "212.23", "0.00", "-23.24")) + c("$1836.23$", "$2763.39$", "$937.29$", "$643.00$", + "$212.23$", "$0.00$", "$-23.24$") + ) # Format the `num_1` column to 2 decimal places, use a single space # character as digit grouping separators, use all other defaults; @@ -61,17 +71,23 @@ test_that("the `fmt_number()` function works correctly", { (tbl_latex %>% fmt_number(columns = "num_1", decimals = 2, sep_mark = " ") %>% render_formats_test("latex"))[["num_1"]], - c("1 836.23", "2 763.39", "937.29", "643.00", "212.23", "0.00", "-23.24")) + 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, use a period for the # digit grouping separators and a comma for the decimal mark, use # all other defaults; extract `output_df` and compare to expected values expect_equal( (tbl_latex %>% - fmt_number(columns = "num_1", decimals = 2, - sep_mark = ".", dec_mark = ",") %>% + fmt_number( + columns = "num_1", decimals = 2, + sep_mark = ".", dec_mark = "," + ) %>% render_formats_test("latex"))[["num_1"]], - c("1.836,23", "2.763,39", "937,29", "643,00", "212,23", "0,00", "-23,24")) + 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 parentheses to # all negative values, use all other defaults; extract `output_df` and @@ -80,7 +96,9 @@ test_that("the `fmt_number()` function works correctly", { (tbl_latex %>% fmt_number(columns = "num_1", decimals = 2, negative_val = "parens") %>% render_formats_test("latex"))[["num_1"]], - c("1,836.23", "2,763.39", "937.29", "643.00", "212.23", "0.00", "(23.24)")) + c("$1,836.23$", "$2,763.39$", "$937.29$", "$643.00$", + "$212.23$", "$0.00$", "$(23.24)$") + ) # Format the `num_1` column to 4 decimal places, scale all values by # 1/1000, use all other defaults; extract `output_df` and compare @@ -89,7 +107,9 @@ test_that("the `fmt_number()` function works correctly", { (tbl_latex %>% fmt_number(columns = "num_1", decimals = 4, scale_by = 1/1000) %>% render_formats_test("latex"))[["num_1"]], - c("1.8362", "2.7634", "0.9373", "0.6430", "0.2122", "0.0000", "-0.0232")) + c("$1.8362$", "$2.7634$", "$0.9373$", "$0.6430$", "$0.2122$", + "$0.0000$", "$-0.0232$") + ) # Format the `num_1` column to 2 decimal places, prepend and append # all values by 2 different literals, use all other defaults; extract @@ -98,8 +118,9 @@ test_that("the `fmt_number()` function works correctly", { (tbl_latex %>% fmt_number(columns = "num_1", decimals = 2, pattern = "a {x} b") %>% render_formats_test("latex"))[["num_1"]], - c("a 1,836.23 b", "a 2,763.39 b", "a 937.29 b", "a 643.00 b", - "a 212.23 b", "a 0.00 b", "a -23.24 b")) + c("a $1,836.23$ b", "a $2,763.39$ b", "a $937.29$ b", "a $643.00$ b", + "a $212.23$ b", "a $0.00$ b", "a $-23.24$ b") + ) # Format the `num_1` column to 4 decimal places, scale all values # by 1/1000 and append a `K` character to the resultant values, use @@ -109,8 +130,9 @@ test_that("the `fmt_number()` function works correctly", { fmt_number(columns = "num_1", decimals = 4, scale_by = 1/1000, pattern = "{x}K") %>% render_formats_test("latex"))[["num_1"]], - c("1.8362K", "2.7634K", "0.9373K", "0.6430K", - "0.2122K", "0.0000K", "-0.0232K")) + c("$1.8362$K", "$2.7634$K", "$0.9373$K", "$0.6430$K", + "$0.2122$K", "$0.0000$K", "$-0.0232$K") + ) # Format the `num_1` column to 2 decimal places, apply the `en_US` # locale and use all other defaults; extract `output_df` and compare @@ -119,7 +141,9 @@ test_that("the `fmt_number()` function works correctly", { (tbl_latex %>% fmt_number(columns = "num_1", decimals = 2, locale = "en_US") %>% render_formats_test("latex"))[["num_1"]], - c("1,836.23", "2,763.39", "937.29", "643.00", "212.23", "0.00", "-23.24")) + 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 `da_DK` # locale and use all other defaults; extract `output_df` and compare @@ -128,7 +152,9 @@ test_that("the `fmt_number()` function works correctly", { (tbl_latex %>% fmt_number(columns = "num_1", decimals = 2, locale = "da_DK") %>% render_formats_test("latex"))[["num_1"]], - c("1.836,23", "2.763,39", "937,29", "643,00", "212,23", "0,00", "-23,24")) + 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 `de_AT` # locale and use all other defaults; extract `output_df` and compare @@ -137,7 +163,9 @@ test_that("the `fmt_number()` function works correctly", { (tbl_latex %>% fmt_number(columns = "num_1", decimals = 2, locale = "de_AT") %>% render_formats_test("latex"))[["num_1"]], - c("1 836,23", "2 763,39", "937,29", "643,00", "212,23", "0,00", "-23,24")) + 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 `et_EE` # locale and use all other defaults; extract `output_df` and compare @@ -146,7 +174,9 @@ test_that("the `fmt_number()` function works correctly", { (tbl_latex %>% fmt_number(columns = "num_1", decimals = 2, locale = "et_EE") %>% render_formats_test("latex"))[["num_1"]], - c("1 836,23", "2 763,39", "937,29", "643,00", "212,23", "0,00", "-23,24")) + 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 `gl_ES` # locale and use all other defaults; extract `output_df` and compare @@ -155,5 +185,7 @@ test_that("the `fmt_number()` function works correctly", { (tbl_latex %>% fmt_number(columns = "num_1", decimals = 2, locale = "gl_ES") %>% render_formats_test("latex"))[["num_1"]], - c("1.836,23", "2.763,39", "937,29", "643,00", "212,23", "0,00", "-23,24")) + c("$1.836,23$", "$2.763,39$", "$937,29$", "$643,00$", + "$212,23$", "$0,00$", "$-23,24$") + ) }) diff --git a/tests/testthat/test-l_fmt_percent.R b/tests/testthat/test-l_fmt_percent.R index f8d64a3ad6..20d7d4c8e5 100644 --- a/tests/testthat/test-l_fmt_percent.R +++ b/tests/testthat/test-l_fmt_percent.R @@ -12,7 +12,8 @@ test_that("the `fmt_percent()` function works correctly", { "october", "november", "december"), num_1 = c(1836.23, 2763.39, 937.29, 643.00, 212.232, 0, -23.24), num_2 = c(34, 74, 23, 93, 35, 76, 57), - stringsAsFactors = FALSE) + stringsAsFactors = FALSE + ) # Create a `tbl_latex` object with `gt()` and the # `data_tbl` dataset @@ -24,8 +25,9 @@ test_that("the `fmt_percent()` function works correctly", { (tbl_latex %>% fmt_percent(columns = "num_1", decimals = 2) %>% render_formats_test("latex"))[["num_1"]], - c("183,623.00\\%", "276,339.00\\%", "93,729.00\\%", - "64,300.00\\%", "21,223.20\\%", "0.00\\%", "-2,324.00\\%")) + c("$183,623.00\\%$", "$276,339.00\\%$", "$93,729.00\\%$", + "$64,300.00\\%$", "$21,223.20\\%$", "$0.00\\%$", "$-2,324.00\\%$") + ) # Format the `num_1` column to 5 decimal places, use all # other defaults; extract `output_df` and compare to expected values @@ -33,19 +35,24 @@ test_that("the `fmt_percent()` function works correctly", { (tbl_latex %>% fmt_percent(columns = "num_1", decimals = 5) %>% render_formats_test("latex"))[["num_1"]], - c("183,623.00000\\%", "276,339.00000\\%", "93,729.00000\\%", - "64,300.00000\\%", "21,223.20000\\%", "0.00000\\%", "-2,324.00000\\%")) + c("$183,623.00000\\%$", "$276,339.00000\\%$", "$93,729.00000\\%$", + "$64,300.00000\\%$", "$21,223.20000\\%$", "$0.00000\\%$", + "$-2,324.00000\\%$") + ) # Format the `num_1` column to 2 decimal places, drop the trailing # zeros, use all other defaults; extract `output_df` and compare to # expected values expect_equal( (tbl_latex %>% - fmt_percent(columns = "num_1", decimals = 2, - drop_trailing_zeros = TRUE) %>% + fmt_percent( + columns = "num_1", decimals = 2, + drop_trailing_zeros = TRUE + ) %>% render_formats_test("latex"))[["num_1"]], - c("183,623\\%", "276,339\\%", "93,729\\%", "64,300\\%", - "21,223.2\\%", "0\\%", "-2,324\\%" )) + c("$183,623\\%$", "$276,339\\%$", "$93,729\\%$", "$64,300\\%$", + "$21,223.2\\%$", "$0\\%$", "$-2,324\\%$") + ) # Format the `num_1` column to 2 decimal places, don't use digit # grouping separators, use all other defaults; extract `output_df` @@ -54,8 +61,9 @@ test_that("the `fmt_percent()` function works correctly", { (tbl_latex %>% fmt_percent(columns = "num_1", decimals = 2, use_seps = FALSE) %>% render_formats_test("latex"))[["num_1"]], - c("183623.00\\%", "276339.00\\%", "93729.00\\%", "64300.00\\%", - "21223.20\\%", "0.00\\%", "-2324.00\\%")) + c("$183623.00\\%$", "$276339.00\\%$", "$93729.00\\%$", "$64300.00\\%$", + "$21223.20\\%$", "$0.00\\%$", "$-2324.00\\%$") + ) # Format the `num_1` column to 2 decimal places, use a single space # character as digit grouping separators, use all other defaults; @@ -64,19 +72,23 @@ test_that("the `fmt_percent()` function works correctly", { (tbl_latex %>% fmt_percent(columns = "num_1", decimals = 2, sep_mark = " ") %>% render_formats_test("latex"))[["num_1"]], - c("183 623.00\\%", "276 339.00\\%", "93 729.00\\%", "64 300.00\\%", - "21 223.20\\%", "0.00\\%", "-2 324.00\\%")) + c("$183 623.00\\%$", "$276 339.00\\%$", "$93 729.00\\%$", "$64 300.00\\%$", + "$21 223.20\\%$", "$0.00\\%$", "$-2 324.00\\%$") + ) # Format the `num_1` column to 2 decimal places, use a period for the # digit grouping separators and a comma for the decimal mark, use # all other defaults; extract `output_df` and compare to expected values expect_equal( (tbl_latex %>% - fmt_percent(columns = "num_1", decimals = 2, - sep_mark = ".", dec_mark = ",") %>% + fmt_percent( + columns = "num_1", decimals = 2, + sep_mark = ".", dec_mark = "," + ) %>% render_formats_test("latex"))[["num_1"]], - c("183.623,00\\%", "276.339,00\\%", "93.729,00\\%", "64.300,00\\%", - "21.223,20\\%", "0,00\\%", "-2.324,00\\%")) + c("$183.623,00\\%$", "$276.339,00\\%$", "$93.729,00\\%$", "$64.300,00\\%$", + "$21.223,20\\%$", "$0,00\\%$", "$-2.324,00\\%$") + ) # Format the `num_1` column to 2 decimal places, apply parentheses to # all negative values, use all other defaults; extract `output_df` and @@ -85,8 +97,9 @@ test_that("the `fmt_percent()` function works correctly", { (tbl_latex %>% fmt_percent(columns = "num_1", decimals = 2, negative_val = "parens") %>% render_formats_test("latex"))[["num_1"]], - c("183,623.00\\%", "276,339.00\\%", "93,729.00\\%", "64,300.00\\%", - "21,223.20\\%", "0.00\\%", "(2,324.00\\%)")) + c("$183,623.00\\%$", "$276,339.00\\%$", "$93,729.00\\%$", "$64,300.00\\%$", + "$21,223.20\\%$", "$0.00\\%$", "$\\left(2,324.00\\%\\right)$") + ) # Format the `num_1` column to 2 decimal places, prepend and append # all values by 2 different literals, use all other defaults; extract @@ -95,30 +108,38 @@ test_that("the `fmt_percent()` function works correctly", { (tbl_latex %>% fmt_percent(columns = "num_1", decimals = 2, pattern = "a {x}:n") %>% render_formats_test("latex"))[["num_1"]], - c("a 183,623.00\\%:n", "a 276,339.00\\%:n", "a 93,729.00\\%:n", - "a 64,300.00\\%:n", "a 21,223.20\\%:n", "a 0.00\\%:n", "a -2,324.00\\%:n")) + c("a $183,623.00\\%$:n", "a $276,339.00\\%$:n", "a $93,729.00\\%$:n", + "a $64,300.00\\%$:n", "a $21,223.20\\%$:n", "a $0.00\\%$:n", + "a $-2,324.00\\%$:n") + ) # Format the `num_1` column to 0 decimal places, place a space between # the percent sign (on the right) and the value, use all other defaults; # extract `output_df` and compare to expected values expect_equal( (tbl_latex %>% - fmt_percent(columns = "num_1", decimals = 0, - placement = "right", incl_space = TRUE) %>% + fmt_percent( + columns = "num_1", decimals = 0, + placement = "right", incl_space = TRUE + ) %>% render_formats_test("latex"))[["num_1"]], - c("183,623 \\%", "276,339 \\%", "93,729 \\%", "64,300 \\%", - "21,223 \\%", "0 \\%", "-2,324 \\%")) + c("$183,623 \\%$", "$276,339 \\%$", "$93,729 \\%$", "$64,300 \\%$", + "$21,223 \\%$", "$0 \\%$", "$-2,324 \\%$") + ) # Format the `num_1` column to 0 decimal places, place a space between # the percent sign (on the left) and the value, use all other defaults; # extract `output_df` and compare to expected values expect_equal( (tbl_latex %>% - fmt_percent(columns = "num_1", decimals = 0, - placement = "left", incl_space = TRUE) %>% + fmt_percent( + columns = "num_1", decimals = 0, + placement = "left", incl_space = TRUE + ) %>% render_formats_test("latex"))[["num_1"]], - c("\\% 183,623", "\\% 276,339", "\\% 93,729", "\\% 64,300", - "\\% 21,223", "\\% 0", "\\% -2,324")) + c("$\\% 183,623$", "$\\% 276,339$", "$\\% 93,729$", "$\\% 64,300$", + "$\\% 21,223$", "$\\% 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 @@ -127,8 +148,9 @@ test_that("the `fmt_percent()` function works correctly", { (tbl_latex %>% fmt_percent(columns = "num_1", decimals = 2, locale = "en_US") %>% render_formats_test("latex"))[["num_1"]], - c("183,623.00\\%", "276,339.00\\%", "93,729.00\\%", - "64,300.00\\%", "21,223.20\\%", "0.00\\%", "-2,324.00\\%")) + c("$183,623.00\\%$", "$276,339.00\\%$", "$93,729.00\\%$", + "$64,300.00\\%$", "$21,223.20\\%$", "$0.00\\%$", "$-2,324.00\\%$") + ) # Format the `num_1` column to 2 decimal places, apply the `da_DK` # locale and use all other defaults; extract `output_df` and compare @@ -137,8 +159,9 @@ test_that("the `fmt_percent()` function works correctly", { (tbl_latex %>% fmt_percent(columns = "num_1", decimals = 2, locale = "da_DK") %>% render_formats_test("latex"))[["num_1"]], - c("183.623,00\\%", "276.339,00\\%", "93.729,00\\%", - "64.300,00\\%", "21.223,20\\%", "0,00\\%", "-2.324,00\\%")) + c("$183.623,00\\%$", "$276.339,00\\%$", "$93.729,00\\%$", + "$64.300,00\\%$", "$21.223,20\\%$", "$0,00\\%$", "$-2.324,00\\%$") + ) # Format the `num_1` column to 2 decimal places, apply the `de_AT` # locale and use all other defaults; extract `output_df` and compare @@ -147,8 +170,9 @@ test_that("the `fmt_percent()` function works correctly", { (tbl_latex %>% fmt_percent(columns = "num_1", decimals = 2, locale = "de_AT") %>% render_formats_test("latex"))[["num_1"]], - c("183 623,00\\%", "276 339,00\\%", "93 729,00\\%", - "64 300,00\\%", "21 223,20\\%", "0,00\\%", "-2 324,00\\%")) + c("$183 623,00\\%$", "$276 339,00\\%$", "$93 729,00\\%$", + "$64 300,00\\%$", "$21 223,20\\%$", "$0,00\\%$", "$-2 324,00\\%$") + ) # Format the `num_1` column to 2 decimal places, apply the `et_EE` # locale and use all other defaults; extract `output_df` and compare @@ -157,8 +181,9 @@ test_that("the `fmt_percent()` function works correctly", { (tbl_latex %>% fmt_percent(columns = "num_1", decimals = 2, locale = "et_EE") %>% render_formats_test("latex"))[["num_1"]], - c("183 623,00\\%", "276 339,00\\%", "93 729,00\\%", - "64 300,00\\%", "21 223,20\\%", "0,00\\%", "-2 324,00\\%")) + c("$183 623,00\\%$", "$276 339,00\\%$", "$93 729,00\\%$", + "$64 300,00\\%$", "$21 223,20\\%$", "$0,00\\%$", "$-2 324,00\\%$") + ) # Format the `num_1` column to 2 decimal places, apply the `gl_ES` # locale and use all other defaults; extract `output_df` and compare @@ -167,6 +192,7 @@ test_that("the `fmt_percent()` function works correctly", { (tbl_latex %>% fmt_percent(columns = "num_1", decimals = 2, locale = "gl_ES") %>% render_formats_test("latex"))[["num_1"]], - c("183.623,00\\%", "276.339,00\\%", "93.729,00\\%", - "64.300,00\\%", "21.223,20\\%", "0,00\\%", "-2.324,00\\%")) + c("$183.623,00\\%$", "$276.339,00\\%$", "$93.729,00\\%$", + "$64.300,00\\%$", "$21.223,20\\%$", "$0,00\\%$", "$-2.324,00\\%$") + ) }) diff --git a/tests/testthat/test-l_fmt_scientific.R b/tests/testthat/test-l_fmt_scientific.R index 572ea2e3dd..de8b210c95 100644 --- a/tests/testthat/test-l_fmt_scientific.R +++ b/tests/testthat/test-l_fmt_scientific.R @@ -12,7 +12,8 @@ test_that("the `fmt_scientific()` function works correctly", { "october", "november", "december"), num_1 = c(1836.23, 2763.39, 937.29, 643.00, 2.232, 0, -23.24), num_2 = c(34, 74, 23, 93, 35, 76, 57), - stringsAsFactors = FALSE) + stringsAsFactors = FALSE + ) # Create a `tbl_latex` object with `gt()` and the # `data_tbl` dataset @@ -26,9 +27,10 @@ test_that("the `fmt_scientific()` function works correctly", { fmt_scientific(columns = "num_1", decimals = 2) %>% 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}$")) + "$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 5 decimal places, use all # other defaults; extract `output_df` in the HTML context @@ -38,9 +40,10 @@ test_that("the `fmt_scientific()` function works correctly", { fmt_scientific(columns = "num_1", decimals = 5) %>% render_formats_test("latex"))[["num_1"]], c( - "1.83623$ \\times 10^{3}$", "2.76339$ \\times 10^{3}$", - "9.37290$ \\times 10^{2}$", "6.43000$ \\times 10^{2}$", - "2.23200", "0.00000", "-2.32400$ \\times 10^{1}$")) + "$1.83623 \\times 10^{3}$", "$2.76339 \\times 10^{3}$", + "$9.37290 \\times 10^{2}$", "$6.43000 \\times 10^{2}$", + "$2.23200$", "$0.00000$", "$-2.32400 \\times 10^{1}$") + ) # Format the `num_1` column to 2 decimal places, use a period for the # digit grouping separators and a comma for the decimal mark, use @@ -48,13 +51,16 @@ test_that("the `fmt_scientific()` function works correctly", { # compare to expected values expect_equal( (tbl_latex %>% - fmt_scientific(columns = "num_1", decimals = 2, - sep_mark = ".", dec_mark = ",") %>% + fmt_scientific( + columns = "num_1", decimals = 2, + sep_mark = ".", dec_mark = "," + ) %>% 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}$")) + "$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 4 decimal places, scale all values by # 1/1000, use all other defaults; extract `output_df` in the HTML @@ -64,9 +70,10 @@ test_that("the `fmt_scientific()` function works correctly", { fmt_scientific(columns = "num_1", decimals = 4, scale_by = 1/1000) %>% render_formats_test("latex"))[["num_1"]], c( - "1.8362$ \\times 10^{0}$", "2.7634$ \\times 10^{0}$", - "9.3729$ \\times 10^{-1}$", "6.4300$ \\times 10^{-1}$", - "2.2320", "0.0000", "-2.3240$ \\times 10^{-2}$")) + "$1.8362$", "$2.7634$", + "$9.3729 \\times 10^{-1}$", "$6.4300 \\times 10^{-1}$", + "$2.2320 \\times 10^{-3}$", "$0.0000$", "$-2.3240 \\times 10^{-2}$") + ) # Format the `num_1` column to 2 decimal places, prepend and append # all values by 2 different literals, use all other defaults; extract @@ -76,9 +83,10 @@ test_that("the `fmt_scientific()` function works correctly", { fmt_scientific(columns = "num_1", decimals = 2, pattern = "a {x} b") %>% render_formats_test("latex"))[["num_1"]], c( - "a 1.84$ \\times 10^{3}$ b", "a 2.76$ \\times 10^{3}$ b", - "a 9.37$ \\times 10^{2}$ b", "a 6.43$ \\times 10^{2}$ b", - "a 2.23 b", "a 0.00 b", "a -2.32$ \\times 10^{1}$ b")) + "a $1.84 \\times 10^{3}$ b", "a $2.76 \\times 10^{3}$ b", + "a $9.37 \\times 10^{2}$ b", "a $6.43 \\times 10^{2}$ b", + "a $2.23$ b", "a $0.00$ b", "a $-2.32 \\times 10^{1}$ b") + ) # 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 @@ -88,9 +96,10 @@ test_that("the `fmt_scientific()` function works correctly", { fmt_scientific(columns = "num_1", decimals = 2, locale = "en_US") %>% 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}$")) + "$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 `da_DK` # locale and use all other defaults; extract `output_df` in the HTML @@ -100,9 +109,10 @@ test_that("the `fmt_scientific()` function works correctly", { fmt_scientific(columns = "num_1", decimals = 2, locale = "da_DK") %>% 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}$")) + "$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 `de_AT` # locale and use all other defaults; extract `output_df` in the HTML @@ -112,9 +122,10 @@ test_that("the `fmt_scientific()` function works correctly", { fmt_scientific(columns = "num_1", decimals = 2, locale = "de_AT") %>% 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}$")) + "$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 `et_EE` # locale and use all other defaults; extract `output_df` in the HTML @@ -124,9 +135,10 @@ test_that("the `fmt_scientific()` function works correctly", { fmt_scientific(columns = "num_1", decimals = 2, locale = "et_EE") %>% 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}$")) + "$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 `gl_ES` # locale and use all other defaults; extract `output_df` in the HTML @@ -136,7 +148,8 @@ test_that("the `fmt_scientific()` function works correctly", { fmt_scientific(columns = "num_1", decimals = 2, locale = "gl_ES") %>% 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}$")) + "$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}$") + ) }) diff --git a/tests/testthat/test-util_functions.R b/tests/testthat/test-util_functions.R index 1a64c5c680..8905fd7917 100644 --- a/tests/testthat/test-util_functions.R +++ b/tests/testthat/test-util_functions.R @@ -80,54 +80,46 @@ test_that("the `get_time_format()` function works correctly", { c("%H:%M:%S", "%H:%M", "%I:%M:%S %P", "%I:%M %P", "%I %P")) }) -test_that("the `is_currency_valid()` function works correctly", { +test_that("the `validate_currency()` function works correctly", { # Expect that specific currency names supplied to - # `is_currency_valid()` will all return TRUE - lapply(currency_symbols$curr_symbol, is_currency_valid) %>% - unlist() %>% - all() %>% - expect_true() + # `validate_currency()` will all return NULL + expect_null( + lapply(currency_symbols$curr_symbol, validate_currency) %>% + unlist() + ) # Expect that invalid currency names supplied to - # `is_currency_valid()` will all return FALSE - lapply(c("thaler", "tetarteron"), is_currency_valid) %>% - unlist() %>% - all() %>% - expect_false() + # `validate_currency()` will result in an error + expect_error(lapply(c("thaler", "tetarteron"), validate_currency)) + # Expect that specific currency codes supplied to - # `is_currency_valid()` will all return TRUE - lapply(currencies$curr_code, is_currency_valid) %>% - unlist() %>% - all() %>% - expect_true() + # `validate_currency()` will all return NULL + expect_null( + lapply(currencies$curr_code, validate_currency) %>% + unlist() + ) # Expect that invalid currency codes supplied to - # `is_currency_valid()` will all return FALSE - lapply(c("AAA", "ZZZ"), is_currency_valid) %>% - unlist() %>% - all() %>% - expect_false() + # `validate_currency()` will result in an error + expect_error(lapply(c("AAA", "ZZZ"), validate_currency)) # Expect that specific currency codes (3-number) - # supplied to `is_currency_valid()` will all return TRUE - lapply(currencies$curr_number, is_currency_valid) %>% - unlist() %>% - all() %>% - expect_true() + # supplied to `validate_currency()` will return NULL + expect_null( + lapply(currencies$curr_number, validate_currency) %>% + unlist() + ) - lapply(as.numeric(currencies$curr_number), is_currency_valid) %>% - unlist() %>% - all() %>% - expect_true() + expect_null( + lapply(as.numeric(currencies$curr_number), validate_currency) %>% + unlist() + ) # Expect that invalid currency codes supplied to - # `is_currency_valid()` will all return FALSE - lapply(c(999, 998), is_currency_valid) %>% - unlist() %>% - all() %>% - expect_false() + # `validate_currency()` will return an error + expect_error(lapply(c(999, 998), validate_currency)) }) test_that("the `get_currency_str()` function works correctly", { @@ -260,26 +252,6 @@ test_that("the `get_currency_exponent()` function works correctly", { expect_equal(rep(0, 7)) }) -test_that("the `get_locale_sep_mark()` function works correctly", { - - # Expect that `get_locale_sep_mark()` will return - # different group separator symbols with specific locale IDs - lapply(c("af", "be", "en_GB", "et", "fr_FR", "ru", "th", "de"), - get_locale_sep_mark) %>% - unlist() %>% - expect_equal(c(" ", " ", ",", " ", " ", " ", ",", ".")) -}) - -test_that("the `get_locale_dec_mark()` function works correctly", { - - # Expect that `get_locale_dec_mark()` will return - # different decimal separator symbols with specific locale IDs - lapply(c("af", "be", "en_GB", "et", "fr_FR", "ru", "th", "de"), - get_locale_dec_mark) %>% - unlist() %>% - expect_equal(c(",", ",", ".", ",", ",", ",", ".", ",")) -}) - test_that("the `process_text()` function works correctly", { # Create the `simple_text` variable, which is text diff --git a/tests/testthat/test-utils_formatters.R b/tests/testthat/test-utils_formatters.R new file mode 100644 index 0000000000..9f1639208b --- /dev/null +++ b/tests/testthat/test-utils_formatters.R @@ -0,0 +1,306 @@ +context("Ensuring that the utility functions for the formatters work correctly") + +test_that("the `filter_table_to_value()` function works correctly", { + + # Expect that filtering the `locales` table with + # `filter_table_to_value()` will return a single value + # so long as the filtering expressions are well chosen + expect_equal( + locales %>% + filter_table_to_value(lang, base_locale_id == "en_US"), + "en" + ) + + # Expect an error with `filter_table_to_value()` if the + # chosen filtering expressions result in a number of + # returned rows not equal to 1 + expect_error( + locales %>% + filter_table_to_value(base_locale_id, lang == "en") + ) +}) + +test_that("the `get_locale_sep_mark()` function works correctly", { + + # Expect that a `locale` which is `NULL` will return the + # default value + expect_equal( + c( + get_locale_sep_mark(locale = NULL, default = ",", use_seps = TRUE), + get_locale_sep_mark(locale = NULL, default = ".", use_seps = TRUE), + get_locale_sep_mark(locale = NULL, default = " ", use_seps = TRUE) + ), + c(",", ".", " ") + ) + + # Expect that an invalid `locale` will result in + # an error + expect_error( + get_locale_sep_mark(locale = "do_IT", default = ",", use_seps = TRUE) + ) + + # Expect that when `use_seps` is `FALSE`, we always + # get an empty string `""` returned + expect_equal( + get_locale_sep_mark(locale = "en_US", default = ",", use_seps = FALSE), + get_locale_sep_mark(locale = "do_IT", default = ",", use_seps = FALSE), + get_locale_sep_mark(locale = NULL, default = ",", use_seps = FALSE), + get_locale_sep_mark(locale = NULL, use_seps = FALSE), + get_locale_sep_mark(use_seps = FALSE), + "" + ) + + # Expect the correct `sep_mark` values for a range of locales + expect_equal( + c( + get_locale_sep_mark(locale = "fr_CF", default = ",", use_seps = TRUE), + get_locale_sep_mark(locale = "en_JE", default = ",", use_seps = TRUE), + get_locale_sep_mark(locale = "en_KY", default = ",", use_seps = TRUE), + get_locale_sep_mark(locale = "ln_CF", default = ",", use_seps = TRUE), + get_locale_sep_mark(locale = "en_MO", default = ",", use_seps = TRUE), + get_locale_sep_mark(locale = "teo_UG", default = ",", use_seps = TRUE), + get_locale_sep_mark(locale = "en_IL", default = ",", use_seps = TRUE), + get_locale_sep_mark(locale = "pt_PT", default = ",", use_seps = TRUE), + get_locale_sep_mark(locale = "en_DE", default = ",", use_seps = TRUE) + ), + c(" ", ",", ",", ".", ",", ",", ",", " ", ".") + ) +}) + +test_that("the `get_locale_dec_mark()` function works correctly", { + + # Expect that a `locale` which is `NULL` will return the + # default value + expect_equal( + c( + get_locale_dec_mark(locale = NULL, default = "."), + get_locale_dec_mark(locale = NULL, default = ","), + get_locale_dec_mark(locale = NULL, default = " ") + ), + c(".", ",", " ") + ) + + # Expect that an invalid `locale` will result in + # an error + expect_error( + get_locale_dec_mark(locale = "do_IT", default = ".") + ) + + # Expect the correct `dec_mark` values for a range of locales + expect_equal( + c( + get_locale_dec_mark(locale = "fr_CF", default = "."), + get_locale_dec_mark(locale = "en_JE", default = "."), + get_locale_dec_mark(locale = "en_KY", default = "."), + get_locale_dec_mark(locale = "ln_CF", default = "."), + get_locale_dec_mark(locale = "en_MO", default = "."), + get_locale_dec_mark(locale = "teo_UG", default = "."), + get_locale_dec_mark(locale = "en_IL", default = "."), + get_locale_dec_mark(locale = "pt_PT", default = "."), + get_locale_dec_mark(locale = "en_DE", default = ".") + ), + c(",", ".", ".", ",", ".", ".", ".", ",", ",") + ) +}) + +test_that("the `has_order_zero()` function works correctly", { + + # Create numeric vectors, with and without + # NA values + x <- c(-500, -50, -5, -0.5, -0.05, 0, 0.05, 0.5, 5, 50, 500) + x_has_NA <- c(NA_real_, -50, -5, -0.5, -0.05, 0, 0.05, 0.5, 5, 50, NA_real_) + + # Expect that a vector of numbers introduced + # to `has_order_zero()` will result in a equal- + # length logical vector (for vectors that have + # and don't have NA values) + expect_length( + x %>% has_order_zero(), + length(x) + ) + + expect_length( + x_has_NA %>% has_order_zero(), + length(x_has_NA) + ) + + expect_type( + x %>% has_order_zero(), + "logical" + ) + + expect_type( + x_has_NA %>% has_order_zero(), + "logical" + ) + + # Expect the correct logical values for + # vectors that have and don't have NA values + expect_equal( + x %>% has_order_zero(), + x_has_NA %>% has_order_zero(), + c(FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, + FALSE, FALSE, TRUE, FALSE, FALSE) + ) +}) + +test_that("the `split_string_2()` function works correctly", { + + test_str <- "-HK$4,299" + + # Expect certain length 2 character vectors from a series + # of `split_string_2()` operations with regex matching + expect_equal(split_string_2(x = test_str, before = "HK"), c("-", "HK$4,299")) + expect_equal(split_string_2(x = test_str, after = "HK"), c("-HK", "$4,299")) + expect_equal(split_string_2(x = test_str, before = "\\$"), c("-HK", "$4,299")) + expect_equal(split_string_2(x = test_str, after = "\\$"), c("-HK$", "4,299")) + expect_equal(split_string_2(x = test_str, before = "9"), c("-HK$4,2", "99")) + expect_equal(split_string_2(x = test_str, after = "9"), c("-HK$4,29", "9")) + expect_equal(split_string_2(x = test_str, before = "99"), c("-HK$4,2", "99")) + expect_equal(split_string_2(x = test_str, after = "99"), c("-HK$4,299", "")) + expect_equal(split_string_2(x = test_str, before = "9"), c("-HK$4,2", "99")) + expect_equal(split_string_2(x = test_str, before = "$"), c("-HK$4,299", "")) + expect_equal(split_string_2(x = test_str, after = "$"), c("-HK$4,299", "")) + expect_equal(split_string_2(x = test_str, before = ".$"), c("-HK$4,29", "9")) + expect_equal(split_string_2(x = test_str, after = ".$"), c("-HK$4,299", "")) + expect_equal(split_string_2(x = test_str, before = "^."), c("", "-HK$4,299")) + expect_equal(split_string_2(x = test_str, after = "^."), c("-", "HK$4,299")) + expect_equal(split_string_2(x = test_str, before = "x"), c("-HK$4,299", "")) + expect_equal(split_string_2(x = test_str, after = "x"), c("-HK$4,299", "")) + + # Expect certain length 2 character vectors from a series + # of `split_string_2()` operations with numeric positions + expect_equal(split_string_2(x = test_str, before = 0), c("", "-HK$4,299")) + expect_equal(split_string_2(x = test_str, before = 1), c("", "-HK$4,299")) + expect_equal(split_string_2(x = test_str, before = 2), c("-", "HK$4,299")) + expect_equal(split_string_2(x = test_str, before = 3), c("-H", "K$4,299")) + expect_equal(split_string_2(x = test_str, before = 4), c("-HK", "$4,299")) + expect_equal(split_string_2(x = test_str, before = 5), c("-HK$", "4,299")) + expect_equal(split_string_2(x = test_str, before = 6), c("-HK$4", ",299")) + expect_equal(split_string_2(x = test_str, before = 7), c("-HK$4,", "299")) + expect_equal(split_string_2(x = test_str, before = 8), c("-HK$4,2", "99")) + expect_equal(split_string_2(x = test_str, before = 9), c("-HK$4,29", "9")) + expect_equal(split_string_2(x = test_str, after = 0), c("", "-HK$4,299")) + expect_equal(split_string_2(x = test_str, after = 1), c("-", "HK$4,299")) + expect_equal(split_string_2(x = test_str, after = 2), c("-H", "K$4,299")) + expect_equal(split_string_2(x = test_str, after = 3), c("-HK", "$4,299")) + expect_equal(split_string_2(x = test_str, after = 4), c("-HK$", "4,299")) + expect_equal(split_string_2(x = test_str, after = 5), c("-HK$4", ",299")) + expect_equal(split_string_2(x = test_str, after = 6), c("-HK$4,", "299")) + expect_equal(split_string_2(x = test_str, after = 7), c("-HK$4,2", "99")) + expect_equal(split_string_2(x = test_str, after = 8), c("-HK$4,29", "9")) + expect_equal(split_string_2(x = test_str, after = 9), c("-HK$4,299", "")) + + # Expect an error if `x` is not of class character + expect_error(split_string_2(x = 23432, before = "34")) + + # Expect an error if the length of `x` is not 1 + expect_error(split_string_2(x = c("345", "234"), before = "34")) + + # Expect an error if neither of `before` or `after` has a value + expect_error(split_string_2(x = "23432")) + + # Expect an error if both `before` and `after` have values + expect_error(split_string_2(x = "23432", before = "3", after = "2")) + + # Expect an error if the index position is not valid + expect_error(split_string_2(x = "23432", before = 10)) +}) + +test_that("the `paste_between()` function works correctly", { + + # Expect a correctly formed string with `paste_between()` + expect_equal( + paste_between(x_2 = c("left", "right"), "-between-"), + "left-between-right" + ) + + # Expect multiple correctly formed strings with `paste_between()` + expect_equal( + paste_between(x_2 = c("left", "right"), c("-a-", "-b-", "-c-")), + c("left-a-right", "left-b-right", "left-c-right") + ) + + # Expect an error if the class of `x_2` is not `character` + expect_error(paste_between(x_2 = 1:2, "-between-")) + + # Expect an error if the class of `x_between` is not `character` + expect_error(paste_between(x_2 = c("left", "right"), 1)) + + # Expect an error if the length of `x_2` is not 2 + expect_error(paste_between(x_2 = "left", "between")) +}) + +test_that("the `paste_on_side()` function works correctly", { + + # Expect a correctly formed string with `paste_on_side()`, + # pasting to the left + expect_equal( + paste_on_side(x = "center", x_side = "left-", direction = "left"), + "left-center" + ) + + # Expect a correctly formed string with `paste_on_side()`, + # pasting to the right + expect_equal( + paste_on_side(x = "center", x_side = "-right", direction = "right"), + "center-right" + ) + + # Expect an error if `direction` is not valid + expect_error(paste_on_side(x = "center", x_side = "c", direction = "center")) +}) + +test_that("the `paste_left()` function works correctly", { + + # Expect correctly formed strings with `paste_left()` + expect_equal( + paste_left(x = "center", "left-"), + "left-center" + ) + expect_equal( + paste_left(x = c("a", "b", "c"), "left-"), + c("left-a", "left-b", "left-c") + ) + expect_equal( + paste_left(x = c("c1", "c2", "c3"), c("l1-", "l2-", "l3-")), + c("l1-c1", "l2-c2", "l3-c3") + ) + + # Expect an error if the class of `x` is not `character` + expect_error(paste_left(x = 1, x_left = "left")) + + # Expect an error if the class of `x_left` is not `character` + expect_error(paste_left(x = "center", x_left = 1)) + + # Expect an error if the length of `x_left` is not 1 of the length of `x` + expect_error(paste_left(x = "center", x_left = c("l1", "l2", "l3"))) + expect_error(paste_left(x = c("c1", "c2", "c3"), x_left = c("l1", "l2"))) +}) + +test_that("the `paste_right()` function works correctly", { + + # Expect correctly formed strings with `paste_right()` + expect_equal( + paste_right(x = "center", "-right"), + "center-right" + ) + expect_equal( + paste_right(x = c("a", "b", "c"), "-right"), + c("a-right", "b-right", "c-right") + ) + expect_equal( + paste_right(x = c("c1", "c2", "c3"), c("-r1", "-r2", "-r3")), + c("c1-r1", "c2-r2", "c3-r3") + ) + + # Expect an error if the class of `x` is not `character` + expect_error(paste_right(x = 1, x_right = "right")) + + # Expect an error if the class of `x_right` is not `character` + expect_error(paste_right(x = "center", x_right = 1)) + + # Expect an error if the length of `x_right` is not 1 of the length of `x` + expect_error(paste_left(x = "center", x_right = c("r1", "r2", "r3"))) + expect_error(paste_left(x = c("c1", "c2", "c3"), x_right = c("r1", "r2"))) +})