rstudio/gt

cols_align_decimal() fails with accounting = TRUE when summary rows are added.

Open

#1,165 opened on 2022年12月9日

GitHub で見る
 (1 comment) (0 reactions) (1 assignee)R (2,146 stars) (222 forks)batch import
Difficulty: [3] AdvancedEffort: [2] MediumGood First IssuePriority: [3] HighType: ★ Enhancement

説明

Since you seem to have summary_rows on the mind right now . . . cols_align_decimal() fails for accounting formatting on the summary rows when summary_rows are included. Grouping without adding summary rows works as intended.

Reproducible example

Reprexes include gtsave() to get the correct representation, since the normal reprex output is not indicative.

1. Grouped without summary rows (working correctly):

# Grouped, no summaries
library(gt)
dplyr::tibble(
  char = LETTERS[1:8],
  num = c(1.2, -33.52, 9023.2, -283.527, 0.401, -123.1, NA, 41),
  group = c(rep("Group 1", 4), rep("Group 2", 4))) |> 
  dplyr::group_by(group) |> 
  gt() |> 
  fmt_currency(num, 
               accounting = TRUE, 
               decimals = 2) |> 
  cols_align_decimal() |> 
  gtsave("out.png")

Created on 2022-12-09 with reprex v2.0.2 out

2. Grouped with summary rows, 2 decimals (bug)

# Grouped, no summaries, decimals = 2
library(gt)
dplyr::tibble(
  char = LETTERS[1:8],
  num = c(1.2, -33.52, 9023.2, -283.527, 0.401, -123.1, NA, 41),
  group = c(rep("Group 1", 4), rep("Group 2", 4))) |> 
  dplyr::group_by(group) |> 
  gt() |> 
  fmt_currency(num, 
               accounting = TRUE, 
               decimals = 2) |> 
  summary_rows(groups = TRUE,
               columns = c("num"),
               fns = list(`Group Diff` = ~ sum(., na.rm = TRUE)),
               formatter = fmt_currency,
               decimals = 2,
               accounting = TRUE) |>
  cols_align_decimal() |> 
  gtsave("out2.png")

Created on 2022-12-09 with reprex v2.0.2

out2

Same outcome when decimals = 0, which as you know is my pet use case, and grand_summary_rows() seems to present the same issue.

Session info

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.2 (2022-10-31)
#>  os       macOS Ventura 13.0.1
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Chicago
#>  date     2022-12-09
#>  pandoc   2.19.2 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  assertthat    0.2.1      2019-03-21 [1] RSPM (R 4.2.1)
#>  chromote      0.1.1      2022-09-07 [1] RSPM (R 4.2.2)
#>  cli           3.4.1      2022-09-23 [1] RSPM (R 4.2.1)
#>  colorspace    2.0-3      2022-02-21 [1] RSPM (R 4.2.1)
#>  DBI           1.1.3      2022-06-18 [1] RSPM (R 4.2.1)
#>  digest        0.6.30     2022-10-18 [1] RSPM (R 4.2.1)
#>  dplyr         1.0.10     2022-09-01 [1] RSPM (R 4.2.1)
#>  evaluate      0.17       2022-10-07 [1] RSPM (R 4.2.1)
#>  fansi         1.0.3      2022-03-24 [1] RSPM (R 4.2.1)
#>  fastmap       1.1.0      2021-01-25 [1] RSPM (R 4.2.1)
#>  fs            1.5.2      2021-12-08 [1] RSPM (R 4.2.1)
#>  generics      0.1.3      2022-07-05 [1] RSPM (R 4.2.1)
#>  ggplot2       3.4.0      2022-11-04 [1] RSPM (R 4.2.2)
#>  glue          1.6.2      2022-02-24 [1] RSPM (R 4.2.1)
#>  gt          * 0.8.0.9000 2022-12-09 [1] Github (rstudio/gt@d638baa)
#>  gtable        0.3.1      2022-09-01 [1] RSPM (R 4.2.1)
#>  highr         0.9        2021-04-16 [1] RSPM (R 4.2.1)
#>  htmltools     0.5.4      2022-12-07 [1] RSPM (R 4.2.2)
#>  jsonlite      1.8.4      2022-12-06 [1] RSPM (R 4.2.2)
#>  knitr         1.40       2022-08-24 [1] RSPM (R 4.2.1)
#>  later         1.3.0      2021-08-18 [1] RSPM (R 4.2.1)
#>  lifecycle     1.0.3      2022-10-07 [1] RSPM (R 4.2.1)
#>  magrittr      2.0.3      2022-03-30 [1] RSPM (R 4.2.1)
#>  munsell       0.5.0      2018-06-12 [1] RSPM (R 4.2.1)
#>  pillar        1.8.1      2022-08-19 [1] RSPM (R 4.2.1)
#>  pkgconfig     2.0.3      2019-09-22 [1] RSPM (R 4.2.1)
#>  processx      3.8.0      2022-10-26 [1] RSPM (R 4.2.1)
#>  promises      1.2.0.1    2021-02-11 [1] RSPM (R 4.2.1)
#>  ps            1.7.2      2022-10-26 [1] RSPM (R 4.2.1)
#>  purrr         0.3.5      2022-10-06 [1] RSPM (R 4.2.1)
#>  R.cache       0.16.0     2022-07-21 [1] RSPM (R 4.2.1)
#>  R.methodsS3   1.8.2      2022-06-13 [1] RSPM (R 4.2.1)
#>  R.oo          1.25.0     2022-06-12 [1] RSPM (R 4.2.1)
#>  R.utils       2.12.2     2022-11-11 [1] RSPM (R 4.2.2)
#>  R6            2.5.1      2021-08-19 [1] RSPM (R 4.2.1)
#>  Rcpp          1.0.9      2022-07-08 [1] RSPM (R 4.2.1)
#>  reprex        2.0.2      2022-08-17 [1] RSPM (R 4.2.1)
#>  rlang         1.0.6      2022-09-24 [1] RSPM (R 4.2.1)
#>  rmarkdown     2.17       2022-10-07 [1] RSPM (R 4.2.1)
#>  rstudioapi    0.14       2022-08-22 [1] RSPM (R 4.2.1)
#>  sass          0.4.4      2022-11-24 [1] RSPM (R 4.2.2)
#>  scales        1.2.1      2022-08-20 [1] RSPM (R 4.2.1)
#>  sessioninfo   1.2.2      2021-12-06 [1] RSPM (R 4.2.1)
#>  stringi       1.7.8      2022-07-11 [1] RSPM (R 4.2.1)
#>  stringr       1.4.1      2022-08-20 [1] RSPM (R 4.2.1)
#>  styler        1.8.0      2022-10-22 [1] RSPM (R 4.2.1)
#>  tibble        3.1.8      2022-07-22 [1] RSPM (R 4.2.1)
#>  tidyselect    1.2.0      2022-10-10 [1] RSPM (R 4.2.1)
#>  utf8          1.2.2      2021-07-24 [1] RSPM (R 4.2.1)
#>  vctrs         0.5.1      2022-11-16 [1] RSPM (R 4.2.2)
#>  webshot2      0.1.0      2022-05-18 [1] RSPM (R 4.2.2)
#>  websocket     1.4.1      2021-08-18 [1] RSPM (R 4.2.1)
#>  withr         2.5.0      2022-03-03 [1] RSPM (R 4.2.1)
#>  xfun          0.34       2022-10-18 [1] RSPM (R 4.2.1)
#>  yaml          2.3.6      2022-10-18 [1] RSPM (R 4.2.1)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

コントリビューターガイド