|
28 | 28 | #' processed so that `prefix = "$"` will yield (e.g.) `-$1` and `($1)`.
|
29 | 29 | #' @param suffix Additional text to display after the number.
|
30 | 30 | #' @param big.mark Character used between every 3 digits to separate thousands.
|
| 31 | +#' The default (`NULL`) retrieves the setting from the |
| 32 | +#' [number options][number_options]. |
31 | 33 | #' @param decimal.mark The character to be used to indicate the numeric
|
32 |
| -#' decimal point. |
| 34 | +#' decimal point. The default (`NULL`) retrieves the setting from the |
| 35 | +#' [number options][number_options]. |
33 | 36 | #' @param style_positive A string that determines the style of positive numbers:
|
34 | 37 | #'
|
35 | 38 | #' * `"none"` (the default): no change, e.g. `1`.
|
|
38 | 41 | #' as wide as a number or `+`. Compared to `"none"`, adding a figure space
|
39 | 42 | #' can ensure numbers remain properly aligned when they are left- or
|
40 | 43 | #' right-justified.
|
| 44 | +#' |
| 45 | +#' The default (`NULL`) retrieves the setting from the |
| 46 | +#' [number options][number_options]. |
41 | 47 | #' @param style_negative A string that determines the style of negative numbers:
|
42 | 48 | #'
|
43 | 49 | #' * `"hyphen"` (the default): preceded by a standard hypen `-`, e.g. `-1`.
|
44 | 50 | #' * `"minus"`, uses a proper Unicode minus symbol. This is a typographical
|
45 | 51 | #' nicety that ensures `-` aligns with the horizontal bar of the
|
46 | 52 | #' the horizontal bar of `+`.
|
47 | 53 | #' * `"parens"`, wrapped in parentheses, e.g. `(1)`.
|
| 54 | +#' |
| 55 | +#' The default (`NULL`) retrieves the setting from the |
| 56 | +#' [number options][number_options]. |
48 | 57 | #' @param scale_cut Named numeric vector that allows you to rescale large
|
49 | 58 | #' (or small) numbers and add a prefix. Built-in helpers include:
|
50 | 59 | #' * `cut_short_scale()`: [10^3, 10^6) = K, [10^6, 10^9) = M, [10^9, 10^12) = B, [10^12, Inf) = T.
|
|
109 | 118 | #' demo_continuous(c(32, 212), labels = label_number(suffix = "\u00b0F"))
|
110 | 119 | #' demo_continuous(c(0, 100), labels = label_number(suffix = "\u00b0C"))
|
111 | 120 | label_number <- function(accuracy = NULL, scale = 1, prefix = "",
|
112 |
| - suffix = "", big.mark = " ", decimal.mark = ".", |
113 |
| - style_positive = c("none", "plus", "space"), |
114 |
| - style_negative = c("hyphen", "minus", "parens"), |
| 121 | + suffix = "", big.mark = NULL, decimal.mark = NULL, |
| 122 | + style_positive = NULL, |
| 123 | + style_negative = NULL, |
115 | 124 | scale_cut = NULL,
|
116 | 125 | trim = TRUE, ...) {
|
117 | 126 | force_all(
|
@@ -225,17 +234,21 @@ comma_format <- label_comma
|
225 | 234 | #' @inheritParams label_number
|
226 | 235 | #' @return A character vector of `length(x)`.
|
227 | 236 | number <- function(x, accuracy = NULL, scale = 1, prefix = "",
|
228 |
| - suffix = "", big.mark = " ", decimal.mark = ".", |
229 |
| - style_positive = c("none", "plus", "space"), |
230 |
| - style_negative = c("hyphen", "minus", "parens"), |
| 237 | + suffix = "", big.mark = NULL, decimal.mark = NULL, |
| 238 | + style_positive = NULL, |
| 239 | + style_negative = NULL, |
231 | 240 | scale_cut = NULL,
|
232 | 241 | trim = TRUE, ...) {
|
233 | 242 | if (length(x) == 0) {
|
234 | 243 | return(character())
|
235 | 244 | }
|
| 245 | + big.mark <- big.mark %||% getOption("scales.big.mark", default = " ") |
| 246 | + decimal.mark <- decimal.mark %||% getOption("scales.decimal.mark", default = ".") |
| 247 | + style_positive <- style_positive %||% getOption("scales.style_positive", default = "none") |
| 248 | + style_negative <- style_negative %||% getOption("scales.style_negative", default = "hyphen") |
236 | 249 |
|
237 |
| - style_positive <- arg_match(style_positive) |
238 |
| - style_negative <- arg_match(style_negative) |
| 250 | + style_positive <- arg_match(style_positive, c("none", "plus", "space")) |
| 251 | + style_negative <- arg_match(style_negative, c("hyphen", "minus", "parens")) |
239 | 252 |
|
240 | 253 | if (!is.null(scale_cut)) {
|
241 | 254 | cut <- scale_cut(x,
|
@@ -298,6 +311,58 @@ number <- function(x, accuracy = NULL, scale = 1, prefix = "",
|
298 | 311 | ret
|
299 | 312 | }
|
300 | 313 |
|
| 314 | +#' Number options |
| 315 | +#' |
| 316 | +#' Control the settings for formatting numbers globally. |
| 317 | +#' |
| 318 | +#' @inheritParams label_number |
| 319 | +#' @param currency.prefix,currency.suffix,currency.decimal.mark,currency.big.mark |
| 320 | +#' Settings for [`label_currency()`] passed on without the `currency.`-prefix. |
| 321 | +#' @param ordinal.rules Setting for [`label_ordinal()`] passed on without the |
| 322 | +#' `ordinal.`-prefix. |
| 323 | +#' |
| 324 | +#' @return The old options invisibly |
| 325 | +#' @export |
| 326 | +#' |
| 327 | +#' @examples |
| 328 | +#' # Default number formatting |
| 329 | +#' x <- c(0.1, 1, 1000) |
| 330 | +#' label_number()(x) |
| 331 | +#' |
| 332 | +#' # Now again with new options set |
| 333 | +#' number_options(style_positive = "plus", decimal.mark = ",") |
| 334 | +#' label_number()(x) |
| 335 | +#' |
| 336 | +#' # The options are the argument names with a 'scales.'-prefix |
| 337 | +#' options("scales.style_positive") |
| 338 | +#' |
| 339 | +#' # Resetting the options to their defaults |
| 340 | +#' number_options() |
| 341 | +#' label_number()(x) |
| 342 | +number_options <- function( |
| 343 | + decimal.mark = ".", |
| 344 | + big.mark = " ", |
| 345 | + style_positive = c("none", "plus", "space"), |
| 346 | + style_negative = c("hyphen", "minus", "parens"), |
| 347 | + currency.prefix = "$", |
| 348 | + currency.suffix = "", |
| 349 | + currency.decimal.mark = decimal.mark, |
| 350 | + currency.big.mark = setdiff(c(".", ","), currency.decimal.mark)[1], |
| 351 | + ordinal.rules = ordinal_english() |
| 352 | +) { |
| 353 | + opts <- options( |
| 354 | + scales.decimal.mark = decimal.mark, |
| 355 | + scales.big.mark = big.mark, |
| 356 | + scales.style_positive = arg_match(style_positive), |
| 357 | + scales.style_negative = arg_match(style_negative), |
| 358 | + scales.currency.prefix = currency.prefix, |
| 359 | + scales.currency.suffix = currency.suffix, |
| 360 | + scales.currency.decimal.mark = currency.decimal.mark, |
| 361 | + scales.currency.big.mark = currency.big.mark, |
| 362 | + scales.ordinal.rules = ordinal.rules |
| 363 | + ) |
| 364 | + invisible(opts) |
| 365 | +} |
301 | 366 |
|
302 | 367 | # Helpers -----------------------------------------------------------------
|
303 | 368 |
|
|
0 commit comments