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

Update apply_fmt_fn() to not overwrite existing formatted statistics #285

Closed
ddsjoberg opened this issue Aug 2, 2024 · 0 comments · Fixed by #305
Closed

Update apply_fmt_fn() to not overwrite existing formatted statistics #285

ddsjoberg opened this issue Aug 2, 2024 · 0 comments · Fixed by #305
Milestone

Comments

@ddsjoberg
Copy link
Collaborator

In some cases, users want apply formatting functions across a statistic. in the example below, the rounding function ensures the rounded statistics sum to the same number as the raw stats.

Perhaps we could also introduce a apply_fmt_fn(by) argument, and the statistics would be applied to the group of statistics. My only reservation is that this would fail when there are a mix a formatting functions that get applied to vectors and one-in-one-out formatting function (which would apply to more complex cases). But in those cases, the users can easily program around it.

library(cards)

ex <-
  ard_categorical(mtcars, variables = cyl, statistic = ~"p") |> 
  dplyr::mutate(
    .by = "stat_name",
    stat_fmt = 
      if (stat_name[1] == "p") JLutils::round_preserve_sum(unlist(stat) * 100, digits = 2) |> as.list()
      else purrr::map(stat, ~NULL)
  )
#> Registered S3 method overwritten by 'GGally':
#>   method from   
#>   +.gg   ggplot2
ex
#> {cards} data frame: 3 x 10
#>   variable variable_level stat_name stat_label  stat stat_fmt
#> 1      cyl              4         p          % 0.344    34.37
#> 2      cyl              6         p          % 0.219    21.88
#> 3      cyl              8         p          % 0.438    43.75
#> ℹ 4 more variables: context, fmt_fn, warning, error

ex |> apply_fmt_fn()
#> {cards} data frame: 3 x 10
#>   variable variable_level stat_name stat_label  stat stat_fmt
#> 1      cyl              4         p          % 0.344     34.4
#> 2      cyl              6         p          % 0.219     21.9
#> 3      cyl              8         p          % 0.438     43.8
#> ℹ 4 more variables: context, fmt_fn, warning, error

Created on 2024-08-02 with reprex v2.1.0

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 a pull request may close this issue.

1 participant