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

Prompt users about pillar::num() for customising the rounding of numbers #913

Closed
charliejhadley opened this issue Jul 27, 2021 · 4 comments · Fixed by #945
Closed

Prompt users about pillar::num() for customising the rounding of numbers #913

charliejhadley opened this issue Jul 27, 2021 · 4 comments · Fixed by #945
Milestone

Comments

@charliejhadley
Copy link

charliejhadley commented Jul 27, 2021

The {pillar} package has recently introduced num() to aid in customising how {tibble} prints rounded numbers.

There have been a few issues requesting this feature, I originally opened r-lib/pillar#97 which includes both a reprex and suggested fix r-lib/pillar#97 (comment)

It's difficult to tell {pillar} not to round numbers straddling 0 - 1

library("tidyverse")
#> ── Attaching packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
#> ✔ ggplot2 2.2.1     ✔ purrr   0.2.4
#> ✔ tibble  1.4.2     ✔ dplyr   0.7.4
#> ✔ tidyr   0.8.0     ✔ stringr 1.2.0
#> ✔ readr   1.1.1     ✔ forcats 0.2.0
#> ── Conflicts ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag()    masks stats::lag()
my_numbers <-  c(233, 486, 565, 785)
straddle_data <- tibble(
  big = 1000 * my_numbers + 23,
  stradle = my_numbers / 1000 + 100,
  small = my_numbers / 100000
)
straddle_data
#> # A tibble: 4 x 3
#>      big straddle   small
#>    <dbl>   <dbl>   <dbl>
#> 1 233023     100 0.00233
#> 2 486023     100 0.00486
#> 3 565023     101 0.00565
#> 4 785023     101 0.00785

Which is now controllable with

library(pillar)

extra_digits <- function(x) {
  x <- sort(abs(x))
  delta <- diff(x)
  x <- x[-1]

  keep <- which((delta != 0) & is.finite(delta))
  if (length(keep) == 0) {
    return(0)
  }

  x <- x[keep]
  delta <- delta[keep]

  ceiling(log10(max(x / delta)))
}

num_with_extra_digits <- function(x) {
  num(x, sigfig = 3 + extra_digits(x))
}

my_numbers <- c(233, 486, 565, 785)

tibble::tibble(
  big = num_with_extra_digits(1000 * my_numbers + 23),
  straddle = num_with_extra_digits(my_numbers / 1000 + 100),
  straddle2 = num_with_extra_digits(my_numbers / 1000 + 1000),
  straddle3 = num_with_extra_digits(my_numbers / 10000 + 10000),
  small = num_with_extra_digits(my_numbers / 100000)
)
#> # A tibble: 4 × 5
#>       big straddle straddle2  straddle3   small
#>   <num:4>  <num:7>   <num:8>   <num:10> <num:4>
#> 1  233023  100.233  1000.233 10000.0233 0.00233
#> 2  486023  100.486  1000.486 10000.0486 0.00486
#> 3  565023  100.565  1000.565 10000.0565 0.00565
#> 4  785023  100.785  1000.785 10000.0785 0.00785

I feel it would be useful for {tibble} to make users aware of the num() functionality.

Would it be possible to print a message to users on a once per session basis, specifically if {pillar} trims a number like 100.233 to 100. ?

@krlmlr
Copy link
Member

krlmlr commented Jul 28, 2021

This will soon be even easier in pillar. I'm not sure about a startup message, perhaps we can add more links to the documentation to make this particular formatting options easier to discover?

@krlmlr krlmlr added this to the 3.1.4 milestone Jul 29, 2021
@krlmlr
Copy link
Member

krlmlr commented Aug 1, 2021

With pillar 1.6.2:

library(tibble)
packageVersion("pillar")
#> [1] '1.6.2'

my_numbers <- c(233, 486, 565, 785)

tibble::tibble(
  big = num(1000 * my_numbers + 23, extra_sigfig = TRUE),
  straddle = num(my_numbers / 1000 + 100, extra_sigfig = TRUE),
  straddle2 = num(my_numbers / 1000 + 1000, extra_sigfig = TRUE),
  straddle3 = num(my_numbers / 10000 + 10000, extra_sigfig = TRUE),
  small = num(my_numbers / 100000, extra_sigfig = TRUE)
)
#> # A tibble: 4 × 5
#>      big straddle straddle2  straddle3   small
#>    <num>    <num>     <num>      <num>   <num>
#> 1 233023  100.233  1000.233 10000.0233 0.00233
#> 2 486023  100.486  1000.486 10000.0486 0.00486
#> 3 565023  100.565  1000.565 10000.0565 0.00565
#> 4 785023  100.785  1000.785 10000.0785 0.00785

Created on 2021-08-01 by the reprex package (v2.0.0.9000)

Now we have the following path to discover this information:

  1. https://tibble.tidyverse.org/dev/index.html (soon without /dev, and from README.md too)
  2. vignette("tibble")
  3. "Tibbles vs. data frames" section, "Printing" subsection
  4. vignette("digits", package = "pillar") in "See also" in that section

From there on it becomes a bit fuzzy. Some information is in vignette("numbers"), but there's no link. Now that num() is reexported in tibble, maybe we can duplicate the documentation in ?num and vignette("digits") here and also brush up vignette("digits")? Do we still need the startup message? Curious to hear your thoughts.

@krlmlr
Copy link
Member

krlmlr commented Aug 1, 2021

Action items:

  • Move vignettes("digits") and vignettes("numbers") here
  • Move num() and char() documentation here
  • Clean up and enhance numbers vignette
  • Mention new extra_sigfig
  • Add cross-references
  • Clean up in pillar: relink digits vignette, shorten numbers vignette, replace ?num, ?char and print.tbl() with internal stubs
  • Revisit

krlmlr added a commit that referenced this issue Aug 6, 2021
- Move `vignette("digits")`, `vignette("numbers")`, `?num` and `?char` from the pillar package here (#913).
@github-actions
Copy link
Contributor

github-actions bot commented Aug 7, 2022

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants