diff --git a/R/label-number.R b/R/label-number.R index 5ae60123..dadba4c7 100644 --- a/R/label-number.R +++ b/R/label-number.R @@ -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`. @@ -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, ...) { @@ -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, ...) { @@ -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 diff --git a/man/comma.Rd b/man/comma.Rd index 91b5f915..45894091 100644 --- a/man/comma.Rd +++ b/man/comma.Rd @@ -26,7 +26,7 @@ number_format( 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, @@ -81,6 +81,10 @@ width (see \code{\link[base:format]{base::format()}}).} \itemize{ \item \code{"none"} (the default): no change, e.g. \code{1}. \item \code{"plus"}: preceded by \code{+}, e.g. \code{+1}. +\item \code{"space"}: preceded by a Unicode "figure space", i.e., a space equally +as wide as a number or \code{+}. Compared to \code{"none"}, adding a figure space +can ensure numbers remain properly aligned when they are left- or +right-justified. }} \item{style_negative}{A string that determines the style of negative numbers: diff --git a/man/label_number.Rd b/man/label_number.Rd index 5cdd0b18..a4949625 100644 --- a/man/label_number.Rd +++ b/man/label_number.Rd @@ -12,7 +12,7 @@ label_number( 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, @@ -58,6 +58,10 @@ decimal point.} \itemize{ \item \code{"none"} (the default): no change, e.g. \code{1}. \item \code{"plus"}: preceded by \code{+}, e.g. \code{+1}. +\item \code{"space"}: preceded by a Unicode "figure space", i.e., a space equally +as wide as a number or \code{+}. Compared to \code{"none"}, adding a figure space +can ensure numbers remain properly aligned when they are left- or +right-justified. }} \item{style_negative}{A string that determines the style of negative numbers: diff --git a/man/number.Rd b/man/number.Rd index e5f6554a..0ddba102 100644 --- a/man/number.Rd +++ b/man/number.Rd @@ -15,7 +15,7 @@ number( 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, @@ -55,6 +55,10 @@ decimal point.} \itemize{ \item \code{"none"} (the default): no change, e.g. \code{1}. \item \code{"plus"}: preceded by \code{+}, e.g. \code{+1}. +\item \code{"space"}: preceded by a Unicode "figure space", i.e., a space equally +as wide as a number or \code{+}. Compared to \code{"none"}, adding a figure space +can ensure numbers remain properly aligned when they are left- or +right-justified. }} \item{style_negative}{A string that determines the style of negative numbers: