Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number options #453

Merged
merged 5 commits into from
Oct 21, 2024
Merged

Number options #453

merged 5 commits into from
Oct 21, 2024

Conversation

teunbrand
Copy link
Contributor

This PR aims to fix #376.

Briefly, it adds a new function number_options() that globally sets options for formatting numbers.
Subsequently, the label_*() functions use these options by default.

Rendered examples of the documentation:

devtools::load_all("~/packages/scales/")
#> ℹ Loading scales

# Default number formatting
x <- c(0.1, 1, 1000)
label_number()(x)
#> [1] "0.1"     "1.0"     "1 000.0"

# Now again with new options set
number_options(style_positive = "plus", decimal.mark = ",")
label_number()(x)
#> [1] "+0,1"     "+1,0"     "+1 000,0"

# The options are the argument names with a 'scales.'-prefix
options("scales.style_positive")
#> $scales.style_positive
#> [1] "plus"

# Resetting the options to their defaults
number_options()
label_number()(x)
#> [1] "0.1"     "1.0"     "1 000.0"

Created on 2024-10-03 with reprex v2.1.1

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make sure. The prior behaviour when passing in NULL to all these arguments was to error, right? We are not breaking any expected behaviour here

@teunbrand
Copy link
Contributor Author

teunbrand commented Oct 21, 2024

In current scales, using prefix = NULL or suffix = NULL doesn't throw errors, it just doesn't add a prefix/suffix.
The big.mark, decimal.mark, style_positive and style_negative options did not accept NULL.

So I guess technically this PR might break prefix/suffix, but only if people go out of their way to write more verbose code than necessary.

If this is a problem, we can always make the argument a specific getOption() instead of internally resolving the defaults.

@thomasp85
Copy link
Member

I think this is probably fine

@thomasp85 thomasp85 merged commit 16036b0 into r-lib:main Oct 21, 2024
12 checks passed
@teunbrand teunbrand deleted the number_options branch October 21, 2024 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: global options for decimal.mark and big.mark
2 participants