diff --git a/NAMESPACE b/NAMESPACE index 0682d01e..4f9a1766 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -155,6 +155,7 @@ export(number) export(number_bytes) export(number_bytes_format) export(number_format) +export(number_options) export(oob_censor) export(oob_censor_any) export(oob_discard) diff --git a/R/label-currency.R b/R/label-currency.R index 56979a2f..98d18e3d 100644 --- a/R/label-currency.R +++ b/R/label-currency.R @@ -48,8 +48,11 @@ #' scale_cut = c(0, k = 1e3, m = 1e6, bn = 1e9, tn = 1e12) #' ) #' demo_log10(c(1, 1e12), breaks = log_breaks(5, 1e3), labels = gbp) -label_currency <- function(accuracy = NULL, scale = 1, prefix = "$", - suffix = "", big.mark = ",", decimal.mark = ".", +label_currency <- function(accuracy = NULL, scale = 1, + prefix = NULL, + suffix = NULL, + big.mark = NULL, + decimal.mark = NULL, trim = TRUE, largest_with_fractional = 100000, ...) { force_all( @@ -144,13 +147,17 @@ dollar_format <- function(accuracy = NULL, scale = 1, prefix = "$", #' @export #' @rdname dollar_format #' @param x A numeric vector -dollar <- function(x, accuracy = NULL, scale = 1, prefix = "$", - suffix = "", big.mark = ",", decimal.mark = ".", +dollar <- function(x, accuracy = NULL, scale = 1, prefix = NULL, + suffix = NULL, big.mark = NULL, decimal.mark = NULL, trim = TRUE, largest_with_cents = 100000, negative_parens = deprecated(), style_negative = c("hyphen", "minus", "parens"), scale_cut = NULL, ...) { + prefix <- prefix %||% getOption("scales.currency.prefix", default = "$") + suffix <- suffix %||% getOption("scales.currency.suffix", default = "") + big.mark <- big.mark %||% getOption("scales.currency.big.mark", default = ",") + decimal.mark <- decimal.mark %||% getOption("scales.currency.decimal.mark", default = ".") if (length(x) == 0) { return(character()) } diff --git a/R/label-number.R b/R/label-number.R index 3c06b353..b5dd04de 100644 --- a/R/label-number.R +++ b/R/label-number.R @@ -28,8 +28,11 @@ #' processed so that `prefix = "$"` will yield (e.g.) `-$1` and `($1)`. #' @param suffix Additional text to display after the number. #' @param big.mark Character used between every 3 digits to separate thousands. +#' The default (`NULL`) retrieves the setting from the +#' [number options][number_options]. #' @param decimal.mark The character to be used to indicate the numeric -#' decimal point. +#' decimal point. The default (`NULL`) retrieves the setting from the +#' [number options][number_options]. #' @param style_positive A string that determines the style of positive numbers: #' #' * `"none"` (the default): no change, e.g. `1`. @@ -38,6 +41,9 @@ #' 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. +#' +#' The default (`NULL`) retrieves the setting from the +#' [number options][number_options]. #' @param style_negative A string that determines the style of negative numbers: #' #' * `"hyphen"` (the default): preceded by a standard hypen `-`, e.g. `-1`. @@ -45,6 +51,9 @@ #' nicety that ensures `-` aligns with the horizontal bar of the #' the horizontal bar of `+`. #' * `"parens"`, wrapped in parentheses, e.g. `(1)`. +#' +#' The default (`NULL`) retrieves the setting from the +#' [number options][number_options]. #' @param scale_cut Named numeric vector that allows you to rescale large #' (or small) numbers and add a prefix. Built-in helpers include: #' * `cut_short_scale()`: [10^3, 10^6) = K, [10^6, 10^9) = M, [10^9, 10^12) = B, [10^12, Inf) = T. @@ -109,9 +118,9 @@ #' demo_continuous(c(32, 212), labels = label_number(suffix = "\u00b0F")) #' 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", "space"), - style_negative = c("hyphen", "minus", "parens"), + suffix = "", big.mark = NULL, decimal.mark = NULL, + style_positive = NULL, + style_negative = NULL, scale_cut = NULL, trim = TRUE, ...) { force_all( @@ -225,17 +234,21 @@ comma_format <- label_comma #' @inheritParams label_number #' @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", "space"), - style_negative = c("hyphen", "minus", "parens"), + suffix = "", big.mark = NULL, decimal.mark = NULL, + style_positive = NULL, + style_negative = NULL, scale_cut = NULL, trim = TRUE, ...) { if (length(x) == 0) { return(character()) } + big.mark <- big.mark %||% getOption("scales.big.mark", default = " ") + decimal.mark <- decimal.mark %||% getOption("scales.decimal.mark", default = ".") + style_positive <- style_positive %||% getOption("scales.style_positive", default = "none") + style_negative <- style_negative %||% getOption("scales.style_negative", default = "hyphen") - style_positive <- arg_match(style_positive) - style_negative <- arg_match(style_negative) + style_positive <- arg_match(style_positive, c("none", "plus", "space")) + style_negative <- arg_match(style_negative, c("hyphen", "minus", "parens")) if (!is.null(scale_cut)) { cut <- scale_cut(x, @@ -298,6 +311,58 @@ number <- function(x, accuracy = NULL, scale = 1, prefix = "", ret } +#' Number options +#' +#' Control the settings for formatting numbers globally. +#' +#' @inheritParams label_number +#' @param currency.prefix,currency.suffix,currency.decimal.mark,currency.big.mark +#' Settings for [`label_currency()`] passed on without the `currency.`-prefix. +#' @param ordinal.rules Setting for [`label_ordinal()`] passed on without the +#' `ordinal.`-prefix. +#' +#' @return The old options invisibly +#' @export +#' +#' @examples +#' # Default number formatting +#' x <- c(0.1, 1, 1000) +#' label_number()(x) +#' +#' # Now again with new options set +#' number_options(style_positive = "plus", decimal.mark = ",") +#' label_number()(x) +#' +#' # The options are the argument names with a 'scales.'-prefix +#' options("scales.style_positive") +#' +#' # Resetting the options to their defaults +#' number_options() +#' label_number()(x) +number_options <- function( + decimal.mark = ".", + big.mark = " ", + style_positive = c("none", "plus", "space"), + style_negative = c("hyphen", "minus", "parens"), + currency.prefix = "$", + currency.suffix = "", + currency.decimal.mark = decimal.mark, + currency.big.mark = setdiff(c(".", ","), currency.decimal.mark)[1], + ordinal.rules = ordinal_english() +) { + opts <- options( + scales.decimal.mark = decimal.mark, + scales.big.mark = big.mark, + scales.style_positive = arg_match(style_positive), + scales.style_negative = arg_match(style_negative), + scales.currency.prefix = currency.prefix, + scales.currency.suffix = currency.suffix, + scales.currency.decimal.mark = currency.decimal.mark, + scales.currency.big.mark = currency.big.mark, + scales.ordinal.rules = ordinal.rules + ) + invisible(opts) +} # Helpers ----------------------------------------------------------------- diff --git a/R/label-ordinal.R b/R/label-ordinal.R index b9de36f4..c4cdeafc 100644 --- a/R/label-ordinal.R +++ b/R/label-ordinal.R @@ -28,8 +28,8 @@ #' labels = label_ordinal(), #' breaks = breaks_width(2) #' ) -label_ordinal <- function(prefix = "", suffix = "", big.mark = " ", - rules = ordinal_english(), ...) { +label_ordinal <- function(prefix = "", suffix = "", big.mark = NULL, + rules = NULL, ...) { force_all(prefix, suffix, big.mark, rules, ...) function(x) { ordinal( @@ -96,7 +96,9 @@ ordinal_format <- label_ordinal #' @export #' @rdname ordinal_format ordinal <- function(x, prefix = "", suffix = "", big.mark = " ", - rules = ordinal_english(), ...) { + rules = NULL, ...) { + + rules <- rules %||% getOption("scales.ordinal.rules", default = ordinal_english()) na_idx <- is.na(x) x <- round(x, digits = 0) x[na_idx] <- 1 # replace NAs with dummy value diff --git a/R/label-percent.R b/R/label-percent.R index 5e482d31..2dbd3d7d 100644 --- a/R/label-percent.R +++ b/R/label-percent.R @@ -15,7 +15,7 @@ #' ) #' demo_continuous(c(0, .01), labels = french_percent) label_percent <- function(accuracy = NULL, scale = 100, prefix = "", - suffix = "%", big.mark = " ", decimal.mark = ".", + suffix = "%", big.mark = NULL, decimal.mark = NULL, trim = TRUE, ...) { number_format( accuracy = accuracy, @@ -45,7 +45,7 @@ percent_format <- label_percent #' @export #' @rdname percent_format percent <- function(x, accuracy = NULL, scale = 100, prefix = "", - suffix = "%", big.mark = " ", decimal.mark = ".", + suffix = "%", big.mark = NULL, decimal.mark = NULL, trim = TRUE, ...) { number( x = x, diff --git a/R/label-pvalue.R b/R/label-pvalue.R index ef35aa68..2b6a328b 100644 --- a/R/label-pvalue.R +++ b/R/label-pvalue.R @@ -18,7 +18,7 @@ #' # Or provide your own prefixes #' prefix <- c("p < ", "p = ", "p > ") #' demo_continuous(c(0, 1), labels = label_pvalue(prefix = prefix)) -label_pvalue <- function(accuracy = .001, decimal.mark = ".", prefix = NULL, add_p = FALSE) { +label_pvalue <- function(accuracy = .001, decimal.mark = NULL, prefix = NULL, add_p = FALSE) { force_all(accuracy, decimal.mark, add_p) function(x) { pvalue( @@ -49,9 +49,10 @@ pvalue_format <- label_pvalue #' @export pvalue <- function(x, accuracy = .001, - decimal.mark = ".", + decimal.mark = NULL, prefix = NULL, add_p = FALSE) { + decimal.mark <- decimal.mark %||% getOption("scales.decimal.mark", default = ".") out <- number(x, accuracy, decimal.mark = decimal.mark) below <- number(accuracy, accuracy, decimal.mark = decimal.mark) above <- number(1 - accuracy, accuracy, decimal.mark = decimal.mark) diff --git a/R/label-scientific.R b/R/label-scientific.R index 694073d9..ade16cad 100644 --- a/R/label-scientific.R +++ b/R/label-scientific.R @@ -14,7 +14,7 @@ #' #' demo_log10(c(1, 1e9)) label_scientific <- function(digits = 3, scale = 1, prefix = "", suffix = "", - decimal.mark = ".", trim = TRUE, ...) { + decimal.mark = NULL, trim = TRUE, ...) { force_all(digits, scale, prefix, suffix, decimal.mark, trim, ...) function(x) { scientific( @@ -45,11 +45,12 @@ scientific_format <- label_scientific #' @export #' @rdname scientific_format scientific <- function(x, digits = 3, scale = 1, prefix = "", suffix = "", - decimal.mark = ".", trim = TRUE, ...) { + decimal.mark = NULL, trim = TRUE, ...) { if (length(x) == 0) { return(character()) } x <- signif(x * scale, digits) + decimal.mark <- decimal.mark %||% getOption("scales.decimal.mark", default = ".") ret <- paste0( prefix, format(x, decimal.mark = decimal.mark, trim = trim, scientific = TRUE, ...), diff --git a/R/labels-retired.R b/R/labels-retired.R index be1e2fe6..62026af1 100644 --- a/R/labels-retired.R +++ b/R/labels-retired.R @@ -111,7 +111,7 @@ trans_format <- function(trans, format = scientific_format()) { #' demo_continuous(c(0, 2500), labels = km) unit_format <- function(accuracy = NULL, scale = 1, prefix = "", unit = "m", sep = " ", suffix = paste0(sep, unit), - big.mark = " ", decimal.mark = ".", + big.mark = NULL, decimal.mark = NULL, trim = TRUE, ...) { number_format( accuracy = accuracy, diff --git a/_pkgdown.yml b/_pkgdown.yml index bf7ea339..12c48200 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -19,6 +19,7 @@ reference: contents: - starts_with("label_") - matches("format") + - number_options - title: Axis breaks desc: > diff --git a/man/comma.Rd b/man/comma.Rd index 38f9b7f9..bb03854a 100644 --- a/man/comma.Rd +++ b/man/comma.Rd @@ -24,10 +24,10 @@ number_format( scale = 1, prefix = "", suffix = "", - big.mark = " ", - decimal.mark = ".", - style_positive = c("none", "plus", "space"), - style_negative = c("hyphen", "minus", "parens"), + big.mark = NULL, + decimal.mark = NULL, + style_positive = NULL, + style_negative = NULL, scale_cut = NULL, trim = TRUE, ... @@ -65,10 +65,13 @@ processed so that \code{prefix = "$"} will yield (e.g.) \verb{-$1} and \verb{($1 \item{suffix}{Additional text to display after the number.} -\item{big.mark}{Character used between every 3 digits to separate thousands.} +\item{big.mark}{Character used between every 3 digits to separate thousands. +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{decimal.mark}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{trim}{Logical, if \code{FALSE}, values are right-justified to a common width (see \code{\link[base:format]{base::format()}}).} @@ -85,7 +88,10 @@ width (see \code{\link[base:format]{base::format()}}).} 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. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{style_negative}{A string that determines the style of negative numbers: \itemize{ @@ -94,7 +100,10 @@ right-justified. nicety that ensures \code{-} aligns with the horizontal bar of the the horizontal bar of \code{+}. \item \code{"parens"}, wrapped in parentheses, e.g. \code{(1)}. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{scale_cut}{Named numeric vector that allows you to rescale large (or small) numbers and add a prefix. Built-in helpers include: diff --git a/man/dollar_format.Rd b/man/dollar_format.Rd index fc65b2df..0791f2c6 100644 --- a/man/dollar_format.Rd +++ b/man/dollar_format.Rd @@ -23,10 +23,10 @@ dollar( x, accuracy = NULL, scale = 1, - prefix = "$", - suffix = "", - big.mark = ",", - decimal.mark = ".", + prefix = NULL, + suffix = NULL, + big.mark = NULL, + decimal.mark = NULL, trim = TRUE, largest_with_cents = 1e+05, negative_parens = deprecated(), @@ -55,10 +55,13 @@ large.} \item{prefix, suffix}{Symbols to display before and after value.} -\item{big.mark}{Character used between every 3 digits to separate thousands.} +\item{big.mark}{Character used between every 3 digits to separate thousands. +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{decimal.mark}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{trim}{Logical, if \code{FALSE}, values are right-justified to a common width (see \code{\link[base:format]{base::format()}}).} diff --git a/man/label_bytes.Rd b/man/label_bytes.Rd index 02054433..266649bc 100644 --- a/man/label_bytes.Rd +++ b/man/label_bytes.Rd @@ -35,9 +35,12 @@ large.} applied to absolute value before \code{style_positive} and \code{style_negative} are processed so that \code{prefix = "$"} will yield (e.g.) \verb{-$1} and \verb{($1)}.} \item{\code{suffix}}{Additional text to display after the number.} - \item{\code{big.mark}}{Character used between every 3 digits to separate thousands.} + \item{\code{big.mark}}{Character used between every 3 digits to separate thousands. +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{decimal.mark}}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{style_positive}}{A string that determines the style of positive numbers: \itemize{ \item \code{"none"} (the default): no change, e.g. \code{1}. @@ -46,7 +49,10 @@ decimal point.} 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. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{style_negative}}{A string that determines the style of negative numbers: \itemize{ \item \code{"hyphen"} (the default): preceded by a standard hypen \code{-}, e.g. \code{-1}. @@ -54,7 +60,10 @@ right-justified. nicety that ensures \code{-} aligns with the horizontal bar of the the horizontal bar of \code{+}. \item \code{"parens"}, wrapped in parentheses, e.g. \code{(1)}. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{scale_cut}}{Named numeric vector that allows you to rescale large (or small) numbers and add a prefix. Built-in helpers include: \itemize{ diff --git a/man/label_currency.Rd b/man/label_currency.Rd index a29feb17..c0118d92 100644 --- a/man/label_currency.Rd +++ b/man/label_currency.Rd @@ -7,10 +7,10 @@ label_currency( accuracy = NULL, scale = 1, - prefix = "$", - suffix = "", - big.mark = ",", - decimal.mark = ".", + prefix = NULL, + suffix = NULL, + big.mark = NULL, + decimal.mark = NULL, trim = TRUE, largest_with_fractional = 1e+05, ... @@ -29,10 +29,13 @@ large.} \item{prefix, suffix}{Symbols to display before and after value.} -\item{big.mark}{Character used between every 3 digits to separate thousands.} +\item{big.mark}{Character used between every 3 digits to separate thousands. +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{decimal.mark}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{trim}{Logical, if \code{FALSE}, values are right-justified to a common width (see \code{\link[base:format]{base::format()}}).} @@ -48,7 +51,10 @@ width (see \code{\link[base:format]{base::format()}}).} 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. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{style_negative}}{A string that determines the style of negative numbers: \itemize{ \item \code{"hyphen"} (the default): preceded by a standard hypen \code{-}, e.g. \code{-1}. @@ -56,7 +62,10 @@ right-justified. nicety that ensures \code{-} aligns with the horizontal bar of the the horizontal bar of \code{+}. \item \code{"parens"}, wrapped in parentheses, e.g. \code{(1)}. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{scale_cut}}{Named numeric vector that allows you to rescale large (or small) numbers and add a prefix. Built-in helpers include: \itemize{ diff --git a/man/label_date.Rd b/man/label_date.Rd index 539bf5c9..0e6c64da 100644 --- a/man/label_date.Rd +++ b/man/label_date.Rd @@ -56,9 +56,12 @@ large.} applied to absolute value before \code{style_positive} and \code{style_negative} are processed so that \code{prefix = "$"} will yield (e.g.) \verb{-$1} and \verb{($1)}.} \item{\code{suffix}}{Additional text to display after the number.} - \item{\code{big.mark}}{Character used between every 3 digits to separate thousands.} + \item{\code{big.mark}}{Character used between every 3 digits to separate thousands. +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{decimal.mark}}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{style_positive}}{A string that determines the style of positive numbers: \itemize{ \item \code{"none"} (the default): no change, e.g. \code{1}. @@ -67,7 +70,10 @@ decimal point.} 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. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{style_negative}}{A string that determines the style of negative numbers: \itemize{ \item \code{"hyphen"} (the default): preceded by a standard hypen \code{-}, e.g. \code{-1}. @@ -75,7 +81,10 @@ right-justified. nicety that ensures \code{-} aligns with the horizontal bar of the the horizontal bar of \code{+}. \item \code{"parens"}, wrapped in parentheses, e.g. \code{(1)}. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{trim}}{Logical, if \code{FALSE}, values are right-justified to a common width (see \code{\link[base:format]{base::format()}}).} }} diff --git a/man/label_number.Rd b/man/label_number.Rd index cb91f2a9..322f20db 100644 --- a/man/label_number.Rd +++ b/man/label_number.Rd @@ -10,10 +10,10 @@ label_number( scale = 1, prefix = "", suffix = "", - big.mark = " ", - decimal.mark = ".", - style_positive = c("none", "plus", "space"), - style_negative = c("hyphen", "minus", "parens"), + big.mark = NULL, + decimal.mark = NULL, + style_positive = NULL, + style_negative = NULL, scale_cut = NULL, trim = TRUE, ... @@ -49,10 +49,13 @@ processed so that \code{prefix = "$"} will yield (e.g.) \verb{-$1} and \verb{($1 \item{suffix}{Additional text to display after the number.} -\item{big.mark}{Character used between every 3 digits to separate thousands.} +\item{big.mark}{Character used between every 3 digits to separate thousands. +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{decimal.mark}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{style_positive}{A string that determines the style of positive numbers: \itemize{ @@ -62,7 +65,10 @@ decimal point.} 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. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{style_negative}{A string that determines the style of negative numbers: \itemize{ @@ -71,7 +77,10 @@ right-justified. nicety that ensures \code{-} aligns with the horizontal bar of the the horizontal bar of \code{+}. \item \code{"parens"}, wrapped in parentheses, e.g. \code{(1)}. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{scale_cut}{Named numeric vector that allows you to rescale large (or small) numbers and add a prefix. Built-in helpers include: diff --git a/man/label_number_si.Rd b/man/label_number_si.Rd index fd8c8ece..c3a9eeaf 100644 --- a/man/label_number_si.Rd +++ b/man/label_number_si.Rd @@ -28,9 +28,12 @@ prefix.} \item{\code{prefix}}{Additional text to display before the number. The suffix is applied to absolute value before \code{style_positive} and \code{style_negative} are processed so that \code{prefix = "$"} will yield (e.g.) \verb{-$1} and \verb{($1)}.} - \item{\code{big.mark}}{Character used between every 3 digits to separate thousands.} + \item{\code{big.mark}}{Character used between every 3 digits to separate thousands. +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{decimal.mark}}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{style_positive}}{A string that determines the style of positive numbers: \itemize{ \item \code{"none"} (the default): no change, e.g. \code{1}. @@ -39,7 +42,10 @@ decimal point.} 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. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{style_negative}}{A string that determines the style of negative numbers: \itemize{ \item \code{"hyphen"} (the default): preceded by a standard hypen \code{-}, e.g. \code{-1}. @@ -47,7 +53,10 @@ right-justified. nicety that ensures \code{-} aligns with the horizontal bar of the the horizontal bar of \code{+}. \item \code{"parens"}, wrapped in parentheses, e.g. \code{(1)}. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{scale_cut}}{Named numeric vector that allows you to rescale large (or small) numbers and add a prefix. Built-in helpers include: \itemize{ diff --git a/man/label_ordinal.Rd b/man/label_ordinal.Rd index 471c0ebd..77da0587 100644 --- a/man/label_ordinal.Rd +++ b/man/label_ordinal.Rd @@ -7,13 +7,7 @@ \alias{ordinal_spanish} \title{Label ordinal numbers (1st, 2nd, 3rd, etc)} \usage{ -label_ordinal( - prefix = "", - suffix = "", - big.mark = " ", - rules = ordinal_english(), - ... -) +label_ordinal(prefix = "", suffix = "", big.mark = NULL, rules = NULL, ...) ordinal_english() @@ -24,7 +18,9 @@ ordinal_spanish() \arguments{ \item{prefix, suffix}{Symbols to display before and after value.} -\item{big.mark}{Character used between every 3 digits to separate thousands.} +\item{big.mark}{Character used between every 3 digits to separate thousands. +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{rules}{Named list of regular expressions, matched in order. Name gives suffix, and value specifies which numbers to match.} @@ -42,7 +38,8 @@ Applied to rescaled data.} formatting. This is useful if the underlying data is very small or very large.} \item{\code{decimal.mark}}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{style_positive}}{A string that determines the style of positive numbers: \itemize{ \item \code{"none"} (the default): no change, e.g. \code{1}. @@ -51,7 +48,10 @@ decimal point.} 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. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{style_negative}}{A string that determines the style of negative numbers: \itemize{ \item \code{"hyphen"} (the default): preceded by a standard hypen \code{-}, e.g. \code{-1}. @@ -59,7 +59,10 @@ right-justified. nicety that ensures \code{-} aligns with the horizontal bar of the the horizontal bar of \code{+}. \item \code{"parens"}, wrapped in parentheses, e.g. \code{(1)}. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{scale_cut}}{Named numeric vector that allows you to rescale large (or small) numbers and add a prefix. Built-in helpers include: \itemize{ diff --git a/man/label_percent.Rd b/man/label_percent.Rd index a705538c..4fe707a8 100644 --- a/man/label_percent.Rd +++ b/man/label_percent.Rd @@ -9,8 +9,8 @@ label_percent( scale = 100, prefix = "", suffix = "\%", - big.mark = " ", - decimal.mark = ".", + big.mark = NULL, + decimal.mark = NULL, trim = TRUE, ... ) @@ -33,10 +33,13 @@ processed so that \code{prefix = "$"} will yield (e.g.) \verb{-$1} and \verb{($1 \item{suffix}{Additional text to display after the number.} -\item{big.mark}{Character used between every 3 digits to separate thousands.} +\item{big.mark}{Character used between every 3 digits to separate thousands. +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{decimal.mark}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{trim}{Logical, if \code{FALSE}, values are right-justified to a common width (see \code{\link[base:format]{base::format()}}).} @@ -52,7 +55,10 @@ width (see \code{\link[base:format]{base::format()}}).} 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. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{style_negative}}{A string that determines the style of negative numbers: \itemize{ \item \code{"hyphen"} (the default): preceded by a standard hypen \code{-}, e.g. \code{-1}. @@ -60,7 +66,10 @@ right-justified. nicety that ensures \code{-} aligns with the horizontal bar of the the horizontal bar of \code{+}. \item \code{"parens"}, wrapped in parentheses, e.g. \code{(1)}. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{\code{scale_cut}}{Named numeric vector that allows you to rescale large (or small) numbers and add a prefix. Built-in helpers include: \itemize{ diff --git a/man/label_pvalue.Rd b/man/label_pvalue.Rd index a2bdbb87..f38d49f6 100644 --- a/man/label_pvalue.Rd +++ b/man/label_pvalue.Rd @@ -6,7 +6,7 @@ \usage{ label_pvalue( accuracy = 0.001, - decimal.mark = ".", + decimal.mark = NULL, prefix = NULL, add_p = FALSE ) @@ -20,7 +20,8 @@ difference between adjacent values. Applied to rescaled data.} \item{decimal.mark}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{prefix}{A character vector of length 3 giving the prefixes to put in front of numbers. The default values are \code{c("p<", "p=", "p>")} diff --git a/man/label_scientific.Rd b/man/label_scientific.Rd index 97db3367..b28a8608 100644 --- a/man/label_scientific.Rd +++ b/man/label_scientific.Rd @@ -9,7 +9,7 @@ label_scientific( scale = 1, prefix = "", suffix = "", - decimal.mark = ".", + decimal.mark = NULL, trim = TRUE, ... ) @@ -24,7 +24,8 @@ large.} \item{prefix, suffix}{Symbols to display before and after value.} \item{decimal.mark}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{trim}{Logical, if \code{FALSE}, values are right-justified to a common width (see \code{\link[base:format]{base::format()}}).} diff --git a/man/number.Rd b/man/number.Rd index 2ac5a42c..57696e96 100644 --- a/man/number.Rd +++ b/man/number.Rd @@ -14,10 +14,10 @@ number( scale = 1, prefix = "", suffix = "", - big.mark = " ", - decimal.mark = ".", - style_positive = c("none", "plus", "space"), - style_negative = c("hyphen", "minus", "parens"), + big.mark = NULL, + decimal.mark = NULL, + style_positive = NULL, + style_negative = NULL, scale_cut = NULL, trim = TRUE, ... @@ -49,10 +49,13 @@ processed so that \code{prefix = "$"} will yield (e.g.) \verb{-$1} and \verb{($1 \item{suffix}{Additional text to display after the number.} -\item{big.mark}{Character used between every 3 digits to separate thousands.} +\item{big.mark}{Character used between every 3 digits to separate thousands. +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{decimal.mark}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{style_positive}{A string that determines the style of positive numbers: \itemize{ @@ -62,7 +65,10 @@ decimal point.} 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. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{style_negative}{A string that determines the style of negative numbers: \itemize{ @@ -71,7 +77,10 @@ right-justified. nicety that ensures \code{-} aligns with the horizontal bar of the the horizontal bar of \code{+}. \item \code{"parens"}, wrapped in parentheses, e.g. \code{(1)}. -}} +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{scale_cut}{Named numeric vector that allows you to rescale large (or small) numbers and add a prefix. Built-in helpers include: diff --git a/man/number_options.Rd b/man/number_options.Rd new file mode 100644 index 00000000..d9c81aa5 --- /dev/null +++ b/man/number_options.Rd @@ -0,0 +1,79 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/label-number.R +\name{number_options} +\alias{number_options} +\title{Number options} +\usage{ +number_options( + decimal.mark = ".", + big.mark = " ", + style_positive = c("none", "plus", "space"), + style_negative = c("hyphen", "minus", "parens"), + currency.prefix = "$", + currency.suffix = "", + currency.decimal.mark = decimal.mark, + currency.big.mark = setdiff(c(".", ","), currency.decimal.mark)[1], + ordinal.rules = ordinal_english() +) +} +\arguments{ +\item{decimal.mark}{The character to be used to indicate the numeric +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} + +\item{big.mark}{Character used between every 3 digits to separate thousands. +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} + +\item{style_positive}{A string that determines the style of positive numbers: +\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. +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} + +\item{style_negative}{A string that determines the style of negative numbers: +\itemize{ +\item \code{"hyphen"} (the default): preceded by a standard hypen \code{-}, e.g. \code{-1}. +\item \code{"minus"}, uses a proper Unicode minus symbol. This is a typographical +nicety that ensures \code{-} aligns with the horizontal bar of the +the horizontal bar of \code{+}. +\item \code{"parens"}, wrapped in parentheses, e.g. \code{(1)}. +} + +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} + +\item{currency.prefix, currency.suffix, currency.decimal.mark, currency.big.mark}{Settings for \code{\link[=label_currency]{label_currency()}} passed on without the \code{currency.}-prefix.} + +\item{ordinal.rules}{Setting for \code{\link[=label_ordinal]{label_ordinal()}} passed on without the +\code{ordinal.}-prefix.} +} +\value{ +The old options invisibly +} +\description{ +Control the settings for formatting numbers globally. +} +\examples{ +# Default number formatting +x <- c(0.1, 1, 1000) +label_number()(x) + +# Now again with new options set +number_options(style_positive = "plus", decimal.mark = ",") +label_number()(x) + +# The options are the argument names with a 'scales.'-prefix +options("scales.style_positive") + +# Resetting the options to their defaults +number_options() +label_number()(x) +} diff --git a/man/ordinal_format.Rd b/man/ordinal_format.Rd index c2a692a3..1de83ed9 100644 --- a/man/ordinal_format.Rd +++ b/man/ordinal_format.Rd @@ -5,27 +5,16 @@ \alias{ordinal} \title{Superseded interface to \code{label_ordinal()}} \usage{ -ordinal_format( - prefix = "", - suffix = "", - big.mark = " ", - rules = ordinal_english(), - ... -) +ordinal_format(prefix = "", suffix = "", big.mark = NULL, rules = NULL, ...) -ordinal( - x, - prefix = "", - suffix = "", - big.mark = " ", - rules = ordinal_english(), - ... -) +ordinal(x, prefix = "", suffix = "", big.mark = " ", rules = NULL, ...) } \arguments{ \item{prefix, suffix}{Symbols to display before and after value.} -\item{big.mark}{Character used between every 3 digits to separate thousands.} +\item{big.mark}{Character used between every 3 digits to separate thousands. +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{rules}{Named list of regular expressions, matched in order. Name gives suffix, and value specifies which numbers to match.} diff --git a/man/percent_format.Rd b/man/percent_format.Rd index aad6ad30..26120995 100644 --- a/man/percent_format.Rd +++ b/man/percent_format.Rd @@ -10,8 +10,8 @@ percent_format( scale = 100, prefix = "", suffix = "\%", - big.mark = " ", - decimal.mark = ".", + big.mark = NULL, + decimal.mark = NULL, trim = TRUE, ... ) @@ -22,8 +22,8 @@ percent( scale = 100, prefix = "", suffix = "\%", - big.mark = " ", - decimal.mark = ".", + big.mark = NULL, + decimal.mark = NULL, trim = TRUE, ... ) @@ -46,10 +46,13 @@ processed so that \code{prefix = "$"} will yield (e.g.) \verb{-$1} and \verb{($1 \item{suffix}{Additional text to display after the number.} -\item{big.mark}{Character used between every 3 digits to separate thousands.} +\item{big.mark}{Character used between every 3 digits to separate thousands. +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{decimal.mark}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{trim}{Logical, if \code{FALSE}, values are right-justified to a common width (see \code{\link[base:format]{base::format()}}).} diff --git a/man/pvalue_format.Rd b/man/pvalue_format.Rd index ddf72dd9..a6be62a3 100644 --- a/man/pvalue_format.Rd +++ b/man/pvalue_format.Rd @@ -7,12 +7,12 @@ \usage{ pvalue_format( accuracy = 0.001, - decimal.mark = ".", + decimal.mark = NULL, prefix = NULL, add_p = FALSE ) -pvalue(x, accuracy = 0.001, decimal.mark = ".", prefix = NULL, add_p = FALSE) +pvalue(x, accuracy = 0.001, decimal.mark = NULL, prefix = NULL, add_p = FALSE) } \arguments{ \item{accuracy}{A number to round to. Use (e.g.) \code{0.01} to show 2 decimal @@ -23,7 +23,8 @@ difference between adjacent values. Applied to rescaled data.} \item{decimal.mark}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{prefix}{A character vector of length 3 giving the prefixes to put in front of numbers. The default values are \code{c("p<", "p=", "p>")} diff --git a/man/scientific_format.Rd b/man/scientific_format.Rd index dfa483d9..e0b2e715 100644 --- a/man/scientific_format.Rd +++ b/man/scientific_format.Rd @@ -10,7 +10,7 @@ scientific_format( scale = 1, prefix = "", suffix = "", - decimal.mark = ".", + decimal.mark = NULL, trim = TRUE, ... ) @@ -21,7 +21,7 @@ scientific( scale = 1, prefix = "", suffix = "", - decimal.mark = ".", + decimal.mark = NULL, trim = TRUE, ... ) @@ -36,7 +36,8 @@ large.} \item{prefix, suffix}{Symbols to display before and after value.} \item{decimal.mark}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{trim}{Logical, if \code{FALSE}, values are right-justified to a common width (see \code{\link[base:format]{base::format()}}).} diff --git a/man/unit_format.Rd b/man/unit_format.Rd index edbaad83..615a2411 100644 --- a/man/unit_format.Rd +++ b/man/unit_format.Rd @@ -11,8 +11,8 @@ unit_format( unit = "m", sep = " ", suffix = paste0(sep, unit), - big.mark = " ", - decimal.mark = ".", + big.mark = NULL, + decimal.mark = NULL, trim = TRUE, ... ) @@ -39,10 +39,13 @@ processed so that \code{prefix = "$"} will yield (e.g.) \verb{-$1} and \verb{($1 \item{suffix}{Additional text to display after the number.} -\item{big.mark}{Character used between every 3 digits to separate thousands.} +\item{big.mark}{Character used between every 3 digits to separate thousands. +The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{decimal.mark}{The character to be used to indicate the numeric -decimal point.} +decimal point. The default (\code{NULL}) retrieves the setting from the +\link[=number_options]{number options}.} \item{trim}{Logical, if \code{FALSE}, values are right-justified to a common width (see \code{\link[base:format]{base::format()}}).} diff --git a/tests/testthat/test-label-number.R b/tests/testthat/test-label-number.R index ba6094e8..d003816e 100644 --- a/tests/testthat/test-label-number.R +++ b/tests/testthat/test-label-number.R @@ -193,3 +193,49 @@ test_that("built-in functions return expected values", { expect_equal(number(1e9, scale_cut = cut_long_scale()), "1 000M") expect_equal(number(1e9, scale_cut = cut_si("m")), "1 Gm") }) + +# options ----------------------------------------------------------------- + +test_that("number_options are observed", { + + number_options(decimal.mark = ",", big.mark = ".") + expect_equal( + label_number()(c(0.1, 10, 1e6)), + c("0,1", "10,0", "1.000.000,0") + ) + + number_options(style_positive = "plus", style_negative = "parens") + expect_equal( + label_number()(c(-0.1, 0, 1)), + c("(0.1)", "0.0", "+1.0") + ) + + number_options( + currency.prefix = "", currency.suffix = " GBP", + currency.decimal.mark = ",", currency.big.mark = "." + ) + # Regular number are not affected + expect_equal( + label_number()(c(0.1, 10, 1e6)), + c("0.1", "10.0", "1 000 000.0") + ) + # But currency is affected + expect_equal( + label_currency(accuracy = 0.1)(c(0.1, 10, 1e6)), + c("0,1 GBP", "10,0 GBP", "1.000.000,0 GBP") + ) + + number_options(ordinal.rules = ordinal_french(plural = TRUE)) + expect_equal( + label_ordinal()(1:4), + c("1ers", "2es", "3es", "4es") + ) + + # Can be reset + number_options() + expect_equal( + label_ordinal()(1:4), + c("1st", "2nd", "3rd", "4th") + ) +}) +