Skip to content

Commit

Permalink
Start on implementing positive and negative styles
Browse files Browse the repository at this point in the history
Fixes #249. Fixes #262.
  • Loading branch information
hadley committed Mar 21, 2022
1 parent eb3e544 commit f042201
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Imports:
munsell (>= 0.5),
R6,
RColorBrewer,
rlang,
viridisLite
Suggests:
bit64,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export(viridis_pal)
export(wrap_format)
export(yj_trans)
export(zero_range)
import(rlang)
importFrom(R6,R6Class)
importFrom(graphics,par)
importFrom(graphics,plot)
Expand Down
21 changes: 9 additions & 12 deletions R/label-dollar.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
#' demo_continuous(c(-100, 100), labels = label_dollar(negative_parens = TRUE))
#'
#' # In finance the short scale is most prevalent
#' dollar <- label_dollar(rescale_large = rescale_short_scale())
#' demo_log10(c(1, 1e18), breaks = log_breaks(7, 1e3), labels = dollar)
#' short <- label_dollar(rescale_large = rescale_short_scale())
#' demo_log10(c(1, 1e18), breaks = log_breaks(7, 1e3), labels = short)
#'
#' # In other contexts the long scale might be used
#' long <- label_dollar(prefix = "", rescale_large = rescale_long_scale())
Expand All @@ -56,7 +56,7 @@
label_dollar <- function(accuracy = NULL, scale = 1, prefix = "$",
suffix = "", big.mark = ",", decimal.mark = ".",
trim = TRUE, largest_with_cents = 100000,
negative_parens = FALSE, rescale_large = NULL,
negative_parens = deprecated(), rescale_large = NULL,
...) {
force_all(
accuracy,
Expand Down Expand Up @@ -127,9 +127,6 @@ dollar <- function(x, accuracy = NULL, scale = 1, prefix = "$",
big.mark <- " "
}

negative <- !is.na(x) & x < 0
x <- abs(x)

if (!is.null(rescale_large)) {
if (!(is.integer(rescale_large) && all(rescale_large > 0))) {
stop("`rescale_large` must be positive integers.", call. = FALSE)
Expand All @@ -142,6 +139,11 @@ dollar <- function(x, accuracy = NULL, scale = 1, prefix = "$",
scale <- scale * rescale$scale
}

if (lifecycle::is_present(negative_parens)) {
lifecycle::deprecate_warn("1.2.0", "dollar(negative_parens)", "dollar(style_negative)")
style_negative <- if (negative_parens) "parens" else "minus"
}

amount <- number(
x,
accuracy = accuracy,
Expand All @@ -151,15 +153,10 @@ dollar <- function(x, accuracy = NULL, scale = 1, prefix = "$",
big.mark = big.mark,
decimal.mark = decimal.mark,
trim = trim,
# style_negative = style_negative,
...
)

if (negative_parens) {
amount <- paste0(ifelse(negative, "(", ""), amount, ifelse(negative, ")", ""))
} else {
amount <- paste0(ifelse(negative, "-", ""), amount)
}

# restore NAs from input vector
amount[is.na(x)] <- NA
names(amount) <- names(x)
Expand Down
21 changes: 21 additions & 0 deletions R/label-number.r
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,25 @@ comma_format <- label_comma
#' @return A character vector of `length(x)`.
number <- function(x, accuracy = NULL, scale = 1, prefix = "",
suffix = "", big.mark = " ", decimal.mark = ".",
style_positive = c("none", "plus"),
style_negative = c("hyphen", "minus", "parens"),
trim = TRUE, ...) {
if (length(x) == 0) {
return(character())
}

style_positive <- arg_match(style_positive)
style_negative <- arg_match(style_negative)

accuracy <- accuracy %||% precision(x * scale)
x <- round_any(x, accuracy / scale)
nsmall <- -floor(log10(accuracy))
nsmall <- min(max(nsmall, 0), 20)

sign <- sign(x)
sign[is.na(sign)] <- 0
x <- abs(x)

ret <- format(
scale * x,
big.mark = big.mark,
Expand All @@ -167,6 +177,17 @@ number <- function(x, accuracy = NULL, scale = 1, prefix = "",
ret <- paste0(prefix, ret, suffix)
ret[is.infinite(x)] <- as.character(x[is.infinite(x)])

if (style_negative == "hyphen") {
ret[sign < 0] <- paste0("-", ret[sign < 0])
} else if (style_negative == "minus") {
ret[sign < 0] <- paste0("\u2212", ret[sign < 0])
} else if (style_negative == "parens") {
ret[sign < 0] <- paste0("(", ret[sign < 0], ")")
}
if (style_positive == "plus") {
ret[sign > 0] <- paste0("+", ret[sign > 0])
}

# restore NAs from input vector
ret[is.na(x)] <- NA
names(ret) <- names(x)
Expand Down
1 change: 1 addition & 0 deletions R/scales-package.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' @keywords internal
#' @importFrom R6 R6Class
#' @import rlang
"_PACKAGE"

# The following block is used by usethis to automatically manage
Expand Down
4 changes: 2 additions & 2 deletions man/label_dollar.Rd

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

2 changes: 2 additions & 0 deletions man/number.Rd

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

1 change: 1 addition & 0 deletions scales.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageCleanBeforeInstall: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace

0 comments on commit f042201

Please sign in to comment.