Repository navigation
Add support for large-number suffixing - #135
Conversation
|
Hi! Just loved this package and this function. It is so handy! I would say it would be great to have it working with negatives values as well, what do you think? library(gt)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
tribble(
~"Name",~"Population",
"x",172975223,
"y",11204214000,
"t",-901202001
) %>%
gt() %>%
fmt_number(
columns = vars(Population),
suffixing = TRUE
)
Created on 2019-01-20 by the reprex One solution would be to add abs() to the fmt() function, like the example below. I don't know if this solution is good enough, it looks a little verbose. What do you think? if (to_suffix) {
# Prepare vectors of scalars
scale_by <-
dplyr::case_when(
abs(x[non_na_x]) < 1E3 ~ 1,
abs(x[non_na_x]) >= 1E3 & abs(x[non_na_x]) < 1E6 &
!is.na(num_suffixes[1]) ~ 1/1E3,
abs(x[non_na_x]) >= 1E6 & abs(x[non_na_x]) < 1E9 &
!is.na(num_suffixes[2])~ 1/1E6,
abs(x[non_na_x]) >= 1E7 & abs(x[non_na_x]) < 1E12 &
!is.na(num_suffixes[3])~ 1/1E9,
abs(x[non_na_x]) >= 1E12 ~ 1/1E12,
TRUE ~ 1
)
# Perform NA replacement to vector of
# suffix symbols
num_suffixes[which(is.na(num_suffixes))] <- ""
# Prepare vector of suffixes
suffixes <-
dplyr::case_when(
abs(x[non_na_x]) < 1E3 ~ "",
abs(x[non_na_x]) < 1E6 ~ num_suffixes[1],
abs(x[non_na_x]) < 1E9 ~ num_suffixes[2],
abs(x[non_na_x]) < 1E12 ~ num_suffixes[3],
abs(x[non_na_x]) >= 1E12 ~ num_suffixes[4],
TRUE ~ ""
)
} |
|
@douglaszickuhr thanks for pointing this out! Completely missed negative values but, yes, they ought to be included. Thanks much for providing some revised code. The solution with |
|
Hi @douglaszickuhr, I could give you credit for this change in this PR by adding you as a co-author to the upcoming commit. Let me know if that's okay with you. If that is okay, a thing I'd need is for you to sign the individual or corporate contributor agreement as appropriate. Then, send the signed copy over to [email protected]. |
|
@rich-iannone Sure, no problem! I will sign the doc and send that over. What about the PR, should I submit one? |
|
@douglaszickuhr Great, thanks for getting that contributor agreement in. No need to file a separate PR, what I'll do momentarily is add you as a co-author in a commit for this PR. This will put you in the contributors list! |
Co-Authored-By: Douglas Zickuhr <[email protected]>
|
Is there an edge case with 999999.999999 and |
jcheng5
left a comment
There was a problem hiding this comment.
Please refactor to eliminate repeated code
|
Everything is now ready for a final look. Additional tests were written to catch some edge cases around |
| } | ||
|
|
||
| # Get the `suffixing_inputs` | ||
| suffixing_inputs <- get_suffixing_inputs(suffixing) |
There was a problem hiding this comment.
Rename function as normalize_***() and return as a vector.
| # logical will also be set to TRUE and the | ||
| # default set of symbols will be assigned | ||
| # to `num_suffixes` | ||
| num_suffixes <- c("K", "M", "B", "T") |
There was a problem hiding this comment.
Return early at each block
* master: (83 commits) Improvements to documentation for the `info_paletteer()` function (#164) Refactor code related to the `suffixing` feature of `fmt_number()` and `fmt_currency()` (#163) Add support for large-number suffixing (#135) Update API listing Modify Appveyor YAML file Add the `gtsave()` function (#130) Update help file using roxygen Update roxygen documentation Update pkgdown site Update help file using roxygen Update roxygen documentation Update pkgdown site Update help file using roxygen Modify roxygen documentation Update pkgdown site Modify pkgdown YAML file Update help file using roxygen Modify roxygen documentation Update pkgdown site Update help file using roxygen ...
This change makes it possible to apply automatic scaling and suffixing (with a predefined or custom symbol set) to larger numbers—thousands, millions, billions, trillions.
A single argument
suffixingprovides the interface for this transformation infmt_number()andfmt_currency(). The argument accepts aTRUEorFALSE(the default), wherebyTRUEperforms the autoscaling/suffixing with a default set of symbols.The
suffixingargument could also accept a 4-element character vector understood to represent replacement symbols for the stockK,M,B, andT(NAvalues can be used here to exclude those particular ranges from taking part in the transformation).By way of example, we can use
countrypopsto build a gt table and format all of the columns withfmt_number()whilst usingsuffixing = TRUEand this yields the following output table:
The alternative before the change is to use a very large number of
fmt_number()statements that each use conditional statements to target cells within fixed ranges before applying numerical scaling and pattern-based formatting to each.Fixes #134