Skip to content

Add support for large-number suffixing - #135

Merged
rich-iannone merged 42 commits into
masterfrom
dynamic-suffix
Jan 31, 2019
Merged

rich-iannone merged 42 commits into
masterfrom
dynamic-suffix

Conversation

@rich-iannone

Copy link
Copy Markdown
Member

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 suffixing provides the interface for this transformation in fmt_number() and fmt_currency(). The argument accepts a TRUE or FALSE (the default), whereby TRUE performs the autoscaling/suffixing with a default set of symbols.

The suffixing argument could also accept a 4-element character vector understood to represent replacement symbols for the stock K, M, B, and T (NA values can be used here to exclude those particular ranges from taking part in the transformation).

By way of example, we can use countrypops to build a gt table and format all of the columns with fmt_number() whilst using suffixing = TRUE

countrypops %>%
  dplyr::select(
    country_code_3, year, population) %>%
  dplyr::filter(
    country_code_3 %in% c(
      "CHN", "IND", "USA", "PAK", "IDN")
  ) %>%
  dplyr::filter(year > 1975 & year %% 5 == 0) %>%
  tidyr::spread(year, population) %>%
  dplyr::arrange(desc(`2015`)) %>%
  gt(rowname_col = "country_code_3") %>%
  fmt_number(
    columns = TRUE,
    decimals = 2,
    suffixing = TRUE
  )

and this yields the following output table:

suffixing_example

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

@douglaszickuhr

douglaszickuhr commented Jan 20, 2019 •

Copy link
Copy Markdown

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
  ) 

Name

Population

x

172.98M

y

11.20B

t

-901,202,001.00

Created on 2019-01-20 by the reprex
package
(v0.2.0).

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 ~ ""
    )
}

@rich-iannone

Copy link
Copy Markdown
Member Author

@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 abs() seems like the way to go!

@rich-iannone

Copy link
Copy Markdown
Member Author

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].

@douglaszickuhr

Copy link
Copy Markdown

@rich-iannone Sure, no problem! I will sign the doc and send that over. What about the PR, should I submit one?

@rich-iannone

Copy link
Copy Markdown
Member Author

@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!

Comment thread R/format_data.R Resolved
@jcheng5

jcheng5 commented Jan 25, 2019

Copy link
Copy Markdown
Member

Is there an edge case with 999999.999999 and decimals = 2? (Seems like you'd want 1M, but you'd get 1000K?)

@jcheng5 jcheng5 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please refactor to eliminate repeated code

Comment thread R/format_data.R Resolved
@rstudio rstudio deleted a comment from r-cheologist Jan 25, 2019
Comment thread R/utils.R Resolved
Comment thread R/utils.R Resolved
Comment thread R/utils.R Resolved
@rich-iannone

Copy link
Copy Markdown
Member Author

Everything is now ready for a final look. Additional tests were written to catch some edge cases around NA placement in a character vector supplied to suffixing. Documentation for the suffixing argument was edited to correspond to the new behavior.

@rich-iannone
rich-iannone requested a review from jcheng5 January 31, 2019 20:42
@rich-iannone
rich-iannone merged commit 38cc22b into master Jan 31, 2019
@rich-iannone
rich-iannone deleted the dynamic-suffix branch January 31, 2019 20:52
Comment thread R/format_data.R
}

# Get the `suffixing_inputs`
suffixing_inputs <- get_suffixing_inputs(suffixing)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Rename function as normalize_***() and return as a vector.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Changed in #163.

Comment thread R/utils.R
}
}

# Get large-number suffixing inputs

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Improve comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is now much improved (in #163).

Comment thread R/utils.R
# 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")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Return early at each block

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This function has been rewritten (#163).

rich-iannone added a commit that referenced this pull request Feb 8, 2019
* 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
  ...
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.

Have numbers formatted with suffixes like K, M, B, T

3 participants