Skip to content

Commit 16036b0

Browse files
authored
Number options (#453)
1 parent bea228a commit 16036b0

28 files changed

+406
-126
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export(number)
155155
export(number_bytes)
156156
export(number_bytes_format)
157157
export(number_format)
158+
export(number_options)
158159
export(oob_censor)
159160
export(oob_censor_any)
160161
export(oob_discard)

R/label-currency.R

+11-4
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@
4848
#' scale_cut = c(0, k = 1e3, m = 1e6, bn = 1e9, tn = 1e12)
4949
#' )
5050
#' demo_log10(c(1, 1e12), breaks = log_breaks(5, 1e3), labels = gbp)
51-
label_currency <- function(accuracy = NULL, scale = 1, prefix = "$",
52-
suffix = "", big.mark = ",", decimal.mark = ".",
51+
label_currency <- function(accuracy = NULL, scale = 1,
52+
prefix = NULL,
53+
suffix = NULL,
54+
big.mark = NULL,
55+
decimal.mark = NULL,
5356
trim = TRUE, largest_with_fractional = 100000,
5457
...) {
5558
force_all(
@@ -144,13 +147,17 @@ dollar_format <- function(accuracy = NULL, scale = 1, prefix = "$",
144147
#' @export
145148
#' @rdname dollar_format
146149
#' @param x A numeric vector
147-
dollar <- function(x, accuracy = NULL, scale = 1, prefix = "$",
148-
suffix = "", big.mark = ",", decimal.mark = ".",
150+
dollar <- function(x, accuracy = NULL, scale = 1, prefix = NULL,
151+
suffix = NULL, big.mark = NULL, decimal.mark = NULL,
149152
trim = TRUE, largest_with_cents = 100000,
150153
negative_parens = deprecated(),
151154
style_negative = c("hyphen", "minus", "parens"),
152155
scale_cut = NULL,
153156
...) {
157+
prefix <- prefix %||% getOption("scales.currency.prefix", default = "$")
158+
suffix <- suffix %||% getOption("scales.currency.suffix", default = "")
159+
big.mark <- big.mark %||% getOption("scales.currency.big.mark", default = ",")
160+
decimal.mark <- decimal.mark %||% getOption("scales.currency.decimal.mark", default = ".")
154161
if (length(x) == 0) {
155162
return(character())
156163
}

R/label-number.R

+74-9
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@
2828
#' processed so that `prefix = "$"` will yield (e.g.) `-$1` and `($1)`.
2929
#' @param suffix Additional text to display after the number.
3030
#' @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].
3133
#' @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].
3336
#' @param style_positive A string that determines the style of positive numbers:
3437
#'
3538
#' * `"none"` (the default): no change, e.g. `1`.
@@ -38,13 +41,19 @@
3841
#' as wide as a number or `+`. Compared to `"none"`, adding a figure space
3942
#' can ensure numbers remain properly aligned when they are left- or
4043
#' right-justified.
44+
#'
45+
#' The default (`NULL`) retrieves the setting from the
46+
#' [number options][number_options].
4147
#' @param style_negative A string that determines the style of negative numbers:
4248
#'
4349
#' * `"hyphen"` (the default): preceded by a standard hypen `-`, e.g. `-1`.
4450
#' * `"minus"`, uses a proper Unicode minus symbol. This is a typographical
4551
#' nicety that ensures `-` aligns with the horizontal bar of the
4652
#' the horizontal bar of `+`.
4753
#' * `"parens"`, wrapped in parentheses, e.g. `(1)`.
54+
#'
55+
#' The default (`NULL`) retrieves the setting from the
56+
#' [number options][number_options].
4857
#' @param scale_cut Named numeric vector that allows you to rescale large
4958
#' (or small) numbers and add a prefix. Built-in helpers include:
5059
#' * `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 @@
109118
#' demo_continuous(c(32, 212), labels = label_number(suffix = "\u00b0F"))
110119
#' demo_continuous(c(0, 100), labels = label_number(suffix = "\u00b0C"))
111120
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,
115124
scale_cut = NULL,
116125
trim = TRUE, ...) {
117126
force_all(
@@ -225,17 +234,21 @@ comma_format <- label_comma
225234
#' @inheritParams label_number
226235
#' @return A character vector of `length(x)`.
227236
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,
231240
scale_cut = NULL,
232241
trim = TRUE, ...) {
233242
if (length(x) == 0) {
234243
return(character())
235244
}
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")
236249

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"))
239252

240253
if (!is.null(scale_cut)) {
241254
cut <- scale_cut(x,
@@ -298,6 +311,58 @@ number <- function(x, accuracy = NULL, scale = 1, prefix = "",
298311
ret
299312
}
300313

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+
}
301366

302367
# Helpers -----------------------------------------------------------------
303368

R/label-ordinal.R

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
#' labels = label_ordinal(),
2929
#' breaks = breaks_width(2)
3030
#' )
31-
label_ordinal <- function(prefix = "", suffix = "", big.mark = " ",
32-
rules = ordinal_english(), ...) {
31+
label_ordinal <- function(prefix = "", suffix = "", big.mark = NULL,
32+
rules = NULL, ...) {
3333
force_all(prefix, suffix, big.mark, rules, ...)
3434
function(x) {
3535
ordinal(
@@ -96,7 +96,9 @@ ordinal_format <- label_ordinal
9696
#' @export
9797
#' @rdname ordinal_format
9898
ordinal <- function(x, prefix = "", suffix = "", big.mark = " ",
99-
rules = ordinal_english(), ...) {
99+
rules = NULL, ...) {
100+
101+
rules <- rules %||% getOption("scales.ordinal.rules", default = ordinal_english())
100102
na_idx <- is.na(x)
101103
x <- round(x, digits = 0)
102104
x[na_idx] <- 1 # replace NAs with dummy value

R/label-percent.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#' )
1616
#' demo_continuous(c(0, .01), labels = french_percent)
1717
label_percent <- function(accuracy = NULL, scale = 100, prefix = "",
18-
suffix = "%", big.mark = " ", decimal.mark = ".",
18+
suffix = "%", big.mark = NULL, decimal.mark = NULL,
1919
trim = TRUE, ...) {
2020
number_format(
2121
accuracy = accuracy,
@@ -45,7 +45,7 @@ percent_format <- label_percent
4545
#' @export
4646
#' @rdname percent_format
4747
percent <- function(x, accuracy = NULL, scale = 100, prefix = "",
48-
suffix = "%", big.mark = " ", decimal.mark = ".",
48+
suffix = "%", big.mark = NULL, decimal.mark = NULL,
4949
trim = TRUE, ...) {
5050
number(
5151
x = x,

R/label-pvalue.R

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#' # Or provide your own prefixes
1919
#' prefix <- c("p < ", "p = ", "p > ")
2020
#' demo_continuous(c(0, 1), labels = label_pvalue(prefix = prefix))
21-
label_pvalue <- function(accuracy = .001, decimal.mark = ".", prefix = NULL, add_p = FALSE) {
21+
label_pvalue <- function(accuracy = .001, decimal.mark = NULL, prefix = NULL, add_p = FALSE) {
2222
force_all(accuracy, decimal.mark, add_p)
2323
function(x) {
2424
pvalue(
@@ -49,9 +49,10 @@ pvalue_format <- label_pvalue
4949
#' @export
5050
pvalue <- function(x,
5151
accuracy = .001,
52-
decimal.mark = ".",
52+
decimal.mark = NULL,
5353
prefix = NULL,
5454
add_p = FALSE) {
55+
decimal.mark <- decimal.mark %||% getOption("scales.decimal.mark", default = ".")
5556
out <- number(x, accuracy, decimal.mark = decimal.mark)
5657
below <- number(accuracy, accuracy, decimal.mark = decimal.mark)
5758
above <- number(1 - accuracy, accuracy, decimal.mark = decimal.mark)

R/label-scientific.R

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#'
1515
#' demo_log10(c(1, 1e9))
1616
label_scientific <- function(digits = 3, scale = 1, prefix = "", suffix = "",
17-
decimal.mark = ".", trim = TRUE, ...) {
17+
decimal.mark = NULL, trim = TRUE, ...) {
1818
force_all(digits, scale, prefix, suffix, decimal.mark, trim, ...)
1919
function(x) {
2020
scientific(
@@ -45,11 +45,12 @@ scientific_format <- label_scientific
4545
#' @export
4646
#' @rdname scientific_format
4747
scientific <- function(x, digits = 3, scale = 1, prefix = "", suffix = "",
48-
decimal.mark = ".", trim = TRUE, ...) {
48+
decimal.mark = NULL, trim = TRUE, ...) {
4949
if (length(x) == 0) {
5050
return(character())
5151
}
5252
x <- signif(x * scale, digits)
53+
decimal.mark <- decimal.mark %||% getOption("scales.decimal.mark", default = ".")
5354
ret <- paste0(
5455
prefix,
5556
format(x, decimal.mark = decimal.mark, trim = trim, scientific = TRUE, ...),

R/labels-retired.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ trans_format <- function(trans, format = scientific_format()) {
111111
#' demo_continuous(c(0, 2500), labels = km)
112112
unit_format <- function(accuracy = NULL, scale = 1, prefix = "",
113113
unit = "m", sep = " ", suffix = paste0(sep, unit),
114-
big.mark = " ", decimal.mark = ".",
114+
big.mark = NULL, decimal.mark = NULL,
115115
trim = TRUE, ...) {
116116
number_format(
117117
accuracy = accuracy,

_pkgdown.yml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ reference:
1919
contents:
2020
- starts_with("label_")
2121
- matches("format")
22+
- number_options
2223

2324
- title: Axis breaks
2425
desc: >

man/comma.Rd

+17-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dollar_format.Rd

+9-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/label_bytes.Rd

+13-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)