Skip to content

Commit

Permalink
Add figure space as style_positive option
Browse files Browse the repository at this point in the history
  • Loading branch information
bwiernik committed Nov 1, 2023
1 parent 48f82cf commit d0c9548
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
10 changes: 8 additions & 2 deletions R/label-number.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#'
#' * `"none"` (the default): no change, e.g. `1`.
#' * `"plus"`: preceded by `+`, e.g. `+1`.
#' * `"space"`: preceded by a Unicode "figure space", i.e., a space equally
#' as wide as a number or `+`. Compared to `"none"`, adding a figure space
#' can ensure numbers remain properly aligned when they are left- or
#' right-justified.
#' @param style_negative A string that determines the style of negative numbers:
#'
#' * `"hyphen"` (the default): preceded by a standard hypen `-`, e.g. `-1`.
Expand Down Expand Up @@ -103,7 +107,7 @@
#' demo_continuous(c(0, 100), labels = label_number(suffix = "\u00b0C"))
label_number <- function(accuracy = NULL, scale = 1, prefix = "",
suffix = "", big.mark = " ", decimal.mark = ".",
style_positive = c("none", "plus"),
style_positive = c("none", "plus", "space"),
style_negative = c("hyphen", "minus", "parens"),
scale_cut = NULL,
trim = TRUE, ...) {
Expand Down Expand Up @@ -219,7 +223,7 @@ 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_positive = c("none", "plus", "space"),
style_negative = c("hyphen", "minus", "parens"),
scale_cut = NULL,
trim = TRUE, ...) {
Expand Down Expand Up @@ -280,6 +284,8 @@ number <- function(x, accuracy = NULL, scale = 1, prefix = "",
}
if (style_positive == "plus") {
ret[sign > 0] <- paste0("+", ret[sign > 0])
} else if (style_positive == "space") {
ret[sign > 0] <- paste0("\u2007", ret[sign > 0])
}

# restore NAs from input vector
Expand Down
6 changes: 5 additions & 1 deletion man/comma.Rd

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

6 changes: 5 additions & 1 deletion man/label_number.Rd

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

6 changes: 5 additions & 1 deletion man/number.Rd

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

0 comments on commit d0c9548

Please sign in to comment.