Skip to content

Decimal alignment with cols_align_decimal() #1058

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

Merged
merged 17 commits into from
Sep 16, 2022
Merged

Decimal alignment with cols_align_decimal() #1058

merged 17 commits into from
Sep 16, 2022

Conversation

rich-iannone
Copy link
Member

This adds the cols_align_decimal() function, which is to be used for lining up numeric values by the decimal mark. The function ensures that columns targeted are right-aligned, that accounting notation is supported, and that footnote marks don't interfere.

Fixes: #139
Fixes: #144
Fixes: #560
Fixes: #673

@rich-iannone rich-iannone marked this pull request as ready for review September 16, 2022 16:58
@cscheid
Copy link
Member

cscheid commented Sep 16, 2022

I know some people who literally refuse to use anything but LaTeX tables exactly because of this feature!

@cscheid
Copy link
Member

cscheid commented Sep 16, 2022

@domoritz Sorry to randomly tag you on this, but check out what's coming to quarto's tables soon! 😍 https://github.com/rstudio/gt/pull/1058/files#diff-0561d116b26297ef34a596107e310c46f00d108472133cc5468dc644fe0e124f

Copy link
Member

@cscheid cscheid left a comment

Choose a reason for hiding this comment

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

This looks excellent.

@cscheid cscheid merged commit 8c7d246 into master Sep 16, 2022
@rich-iannone rich-iannone deleted the number-alignment branch September 16, 2022 19:25
@domoritz
Copy link

😍 that's awesome. Thank you for randomly tagging me.

@steveputman
Copy link
Contributor

This is great, @rich-iannone!

It's fairly common, when using accounting = TRUE, that numbers are rounded (so decimals = 0). That combination of options (unless drop_trailing_dec_mark = FALSE is also used) doesn't align properly, which obviously makes sense given that there are no decimals for cols_align_decimal() to attack.

Do you think there's any way to address that combination of options (for example by (a) faking the decimal by imputing the drop_trailing_dec_mark = FALSE option, running align_to_char(), then stripping out the decimal or (b) applying the original approach of inserting an invisible parenthesis after nonnegative numbers)? Happy to take a stab but not sure of the best approach.

Thanks!

library(gt)

dplyr::tibble(
  char = LETTERS[1:8],
  num = c(1.2, -33.52, 9023.2, -283.527, 0.401, -123.1, NA, 41)) |> 
  gt() |> 
  tab_header("fmt_currency / accounting = TRUE / decimals = 0") |> 
  fmt_currency(num, accounting = TRUE, decimals = 0) |> 
  cols_align_decimal()
fmt_currency / accounting = TRUE / decimals = 0
char num
A     $1
B  ($34 ) 
C $9,023
D ($284 ) 
E     $0
F ($123 ) 
G NA
H    $41

Created on 2022-10-13 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants