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

Add the fmt_fraction() formatter function #753

Merged
merged 70 commits into from Feb 8, 2022
Merged

Conversation

rich-iannone
Copy link
Member

@rich-iannone rich-iannone commented May 5, 2021

This adds the fmt_fraction() formatter, which allows for flexible formatting of numerical values to mixed fractions of configurable accuracy. Example:

library(gt)
library(tidyverse)

# Define values
range_0_1 <- c(0.0001, 0.001, 0.01, 0.1, 0.25, 0.4, 0.5, 0.6, 0.75, 0.9, 0.99, 0.999, 0.9999)
range_0_1_minus <- range_0_1 * -1
range_1_2 <- range_0_1 + 1
range_1_2_minus <- range_0_1_minus - 1
exact_numbers <- c(-1, 0, 1)
not_numbers <- c(NA_real_, NaN, Inf, -Inf)

# Generate a gt table that demonstrate use of `fmt_fraction()`
dplyr::tibble(
  x = c(
    range_0_1, range_0_1_minus, range_1_2, range_1_2_minus,
    exact_numbers, not_numbers
  ),
  category = c(
    rep("0 to 1", length(range_0_1)),
    rep("0 to -1", length(range_0_1_minus)),
    rep("1 to 2", length(range_1_2)),
    rep("-1 to -2", length(range_1_2_minus)),
    rep("Exact Numbers", length(exact_numbers)),
    rep("Not Numbers", length(not_numbers))
  ),
  low = x,
  med = x,
  high = x,
  halves = x,
  quarters = x,
  eighths = x,
  sixteenths = x,
  hundredths = x
) %>%
  gt(rowname_col = "x", groupname_col = "category") %>%
  fmt_fraction(columns = low, accuracy = "low") %>%
  fmt_fraction(columns = med, accuracy = "med") %>%
  fmt_fraction(columns = high, accuracy = "high") %>%
  fmt_fraction(columns = halves, accuracy = "/2") %>%
  fmt_fraction(columns = quarters, accuracy = "/4") %>%
  fmt_fraction(columns = eighths, accuracy = "/8") %>%
  fmt_fraction(columns = sixteenths, accuracy = "/16") %>%
  fmt_fraction(columns = hundredths, accuracy = "/100") %>%
  cols_width(everything() ~ px(100)) %>%
  opt_all_caps()

fractions-table

Fixes: #402

* master: (40 commits)
  Update cols_width_rtf.md
  Add print method for `rtf_text`; update tests
  Use `stats::na.omit()` instead of `na.omit()`
  Refactor stmt that obtains `group_id`
  Add snapshot tests for summary rows in RTF
  Correct the handling of `summary_df`
  Add manual test cases for RTF table summaries
  Make correction to object name
  Update comment
  Ensure that all cases of using summaries works for RTF
  Place the internally generated stub column at end of series
  Make changes based on code review
  Add snapshot tests for `cols_width()`/RTF
  Include more tests for `cols_width()`
  Ensure that tables with stubs work with `cols_width()`
  Update manual tests to current API
  Add new tests for `tab_row_group()`
  Add docs for `group` arg in `tab_row_group()`
  Add `group` arg to `tab_row_group()`
  Fix bad paren
  ...
R/format_data.R Outdated Show resolved Hide resolved
@rich-iannone rich-iannone changed the title Add the fmt_fraction formatter function Add the fmt_fraction() formatter function May 29, 2021
* master: (24 commits)
  Fix for possible column name duplication when generating summary rows (#792)
  Change the `data` arg to `.data` to avoid partial matching issues (#772)
  Add the `force_sign` arg to several numeric formatters (#793)
  Add the `fmt_engineering()` and `fmt_integer()` formatter functions (#758)
  HTML summary row styling fix (#782)
  Refactor and improve rendering of title/subtitle components in HTML/LaTeX tables (#779)
  LaTeX bugfix for summary rows, and, implementation of grand summary rows (#768)
  Reinstate once failing test
  Include stub var in resolution of visible vars
  Use `seq_len()` instead of `seq()`
  Fix typo in README (#765)
  Increment version number (#762)
  Increase version number
  Documentation fixes (#759)
  Add missing topics to _pkgdown.yml
  Add `accounting` options for `fmt_percent()` and `fmt_number()` (#756)
  Simplify arguments for RTF page numbering
  Add the `fmt_bytes()` formatter function (#750)
  Simplify page numbering text
  Add testthat snapshot tests for RTF page numbering
  ...
* master:
  Add padding options for heading and column labels (#795)
@rich-iannone rich-iannone marked this pull request as ready for review February 2, 2022 20:42
R/format_data.R Outdated Show resolved Hide resolved
R/format_data.R Outdated Show resolved Hide resolved
@rich-iannone rich-iannone merged commit ca475a2 into master Feb 8, 2022
@rich-iannone rich-iannone deleted the fmt-fraction branch February 8, 2022 19:36
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.

Add a format function to generate fractions from numbers
2 participants