Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add accounting options for fmt_percent() and fmt_number() #756

Merged
merged 7 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions R/format_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
#' @param use_seps An option to use digit group separators. The type of digit
#' group separator is set by `sep_mark` and overridden if a locale ID is
#' provided to `locale`. This setting is `TRUE` by default.
#' @param accounting An option to use accounting style for values. With `FALSE`
#' (the default), negative values will be shown with a minus sign. Using
#' `accounting = TRUE` will put negative values in parentheses.
#' @param scale_by A value to scale the input. The default is `1.0`. All numeric
#' values will be multiplied by this value first before undergoing formatting.
#' This value will be ignored if using any of the `suffixing` options (i.e.,
Expand Down Expand Up @@ -148,6 +151,7 @@ fmt_number <- function(data,
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = TRUE,
use_seps = TRUE,
accounting = FALSE,
scale_by = 1.0,
suffixing = FALSE,
pattern = "{x}",
Expand Down Expand Up @@ -201,7 +205,8 @@ fmt_number <- function(data,
# Create the `suffix_df` object
suffix_df <- create_suffix_df(x, decimals, suffix_labels, scale_by)

x %>%
x_str <-
x %>%
# Scale the `x_vals` by the `scale_by` values
scale_x_values(suffix_df$scale_by) %>%
# Format numeric values to character-based numbers
Expand All @@ -215,6 +220,14 @@ fmt_number <- function(data,
# With large-number suffixing support, we paste the
# vector of suffixes to the right of the values
paste_right(suffix_df$suffix)

x_str <-
x_str %>%
format_as_accounting(
x = x, context = context, accounting = accounting
)

x_str
}
)
)
Expand Down Expand Up @@ -556,6 +569,7 @@ fmt_percent <- function(data,
drop_trailing_dec_mark = TRUE,
scale_values = TRUE,
use_seps = TRUE,
accounting = FALSE,
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
Expand Down Expand Up @@ -584,7 +598,7 @@ fmt_percent <- function(data,
columns = {{ columns }},
rows = {{ rows }},
symbol = "%",
accounting = FALSE,
accounting = accounting,
decimals = decimals,
drop_trailing_zeros = drop_trailing_zeros,
drop_trailing_dec_mark = drop_trailing_dec_mark,
Expand Down Expand Up @@ -661,9 +675,6 @@ fmt_percent <- function(data,
#' used.
#' @param use_subunits An option for whether the subunits portion of a currency
#' value should be displayed. By default, this is `TRUE`.
#' @param accounting An option to use accounting style for currency values. With
#' `FALSE` (the default), negative values will be shown with a minus sign.
#' Using `accounting = TRUE` will put negative values in parentheses.
#' @param placement The placement of the currency symbol. This can be either be
#' `left` (the default) or `right`.
#' @param incl_space An option for whether to include a space between the value
Expand Down Expand Up @@ -716,10 +727,10 @@ fmt_currency <- function(data,
rows = everything(),
currency = "USD",
use_subunits = TRUE,
accounting = FALSE,
decimals = NULL,
drop_trailing_dec_mark = TRUE,
use_seps = TRUE,
accounting = FALSE,
scale_by = 1.0,
suffixing = FALSE,
pattern = "{x}",
Expand Down
24 changes: 12 additions & 12 deletions R/utils_formatters.R
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,10 @@ format_as_accounting <- function(x_str,
context,
accounting) {

if (!accounting) {
return(x_str)
}

# TODO: Handle using `x_abs_str` instead

# Store logical vector of `x` < 0
Expand All @@ -609,19 +613,15 @@ format_as_accounting <- function(x_str,
return(x_str)
}

# Handle case where negative values are to be placed within parentheses
if (accounting) {

# Create the minus and parens marks for the context
minus_mark <- context_minus_mark(context)
parens_marks <- context_parens_marks(context)
# Create the minus and parens marks for the context
minus_mark <- context_minus_mark(context)
parens_marks <- context_parens_marks(context)

# Selectively remove minus sign and paste between parentheses
x_str[x_lt0] <-
x_str[x_lt0] %>%
tidy_gsub(minus_mark, "", fixed = TRUE) %>%
paste_between(x_2 = parens_marks)
}
# Selectively remove minus sign and paste between parentheses
x_str[x_lt0] <-
x_str[x_lt0] %>%
tidy_gsub(minus_mark, "", fixed = TRUE) %>%
paste_between(x_2 = parens_marks)

x_str
}
Expand Down
10 changes: 5 additions & 5 deletions man/fmt_currency.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/fmt_number.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/fmt_percent.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading