rstudio / gt

Easily generate information-rich, publication-quality tables from R
https://gt.rstudio.com
Other
2.04k stars 211 forks source link

tab_row_group with row_group_as_column = TRUE yields sprintf error (read comments) #1552

Open kendonB opened 10 months ago

kendonB commented 10 months ago

Prework

Description

Describe the bug clearly and concisely.

Reproducible example

library(gt)
gtcars |>
  dplyr::select(model, year, hp, trq) |>
  dplyr::slice(1:8) |>
  gt(
    rowname_col = "model",
    row_group_as_column = TRUE
  ) |>
  tab_row_group(
    label = "numbered",
    rows = matches("^[0-9]")
  ) |>
  row_group_order(groups = c(NA, "numbered")) |>
  tab_options(row_group.default_label = "Others")
#> Warning in mapply(SIMPLIFY = FALSE, USE.NAMES = FALSE, row_df, col_id_i, :
#> longer argument not a multiple of length of shorter
... (warning repeated many times - might be a clue)
year hp trq
Others GT 2017 647 550 Others GT 2017
California 2015 553 557 California 2015 553
GTC4Lusso 2017 680 514 GTC4Lusso 2017 680
FF 2015 652 504 FF 2015 652
numbered 458 Speciale 2015 597 398 numbered 458 Speciale 2015
458 Spider 2015 562 398 458 Spider 2015 562
458 Italia 2014 562 398 458 Italia 2014 562
488 GTB 2016 661 561 488 GTB 2016 661

Created on 2024-01-25 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.2 (2023-10-31 ucrt) #> os Windows 11 x64 (build 22631) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_New Zealand.utf8 #> ctype English_New Zealand.utf8 #> tz Pacific/Auckland #> date 2024-01-25 #> pandoc 3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.1 2023-03-23 [1] CRAN (R 4.3.1) #> digest 0.6.33 2023-07-07 [1] CRAN (R 4.3.1) #> dplyr 1.1.3 2023-09-03 [1] CRAN (R 4.3.2) #> evaluate 0.23 2023-11-01 [1] CRAN (R 4.3.2) #> fansi 1.0.5 2023-10-08 [1] CRAN (R 4.3.2) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.1) #> fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.1) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.1) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.3.1) #> gt * 0.10.0 2023-10-07 [1] CRAN (R 4.3.2) #> htmltools 0.5.7 2023-11-03 [1] CRAN (R 4.3.2) #> knitr 1.45 2023-10-30 [1] CRAN (R 4.3.2) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.2) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.1) #> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.1) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.1) #> purrr 1.0.2 2023-08-10 [1] CRAN (R 4.3.1) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.3.1) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.3.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.3.0) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.3.1) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.1) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.3.1) #> rlang 1.1.2 2023-11-04 [1] CRAN (R 4.3.2) #> rmarkdown 2.25 2023-09-18 [1] CRAN (R 4.3.2) #> rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.3.1) #> sass 0.4.7 2023-07-15 [1] CRAN (R 4.3.1) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.1) #> styler 1.10.2 2023-08-29 [1] CRAN (R 4.3.2) #> tibble 3.2.1 2023-03-20 [1] CRAN (R 4.3.1) #> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.3.1) #> utf8 1.2.4 2023-10-22 [1] CRAN (R 4.3.2) #> vctrs 0.6.4 2023-10-12 [1] CRAN (R 4.3.2) #> withr 2.5.2 2023-10-30 [1] CRAN (R 4.3.2) #> xfun 0.41 2023-11-01 [1] CRAN (R 4.3.2) #> xml2 1.3.5 2023-07-06 [1] CRAN (R 4.3.1) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.3.0) #> #> [1] C:/Users/KennyBell/AppData/Local/R/win-library/4.3 #> [2] C:/Program Files/R/R-4.3.2/library #> #> ────────────────────────────────────────────────────────────────────────────── ```

Expected result

Columns should not be repeated.

Session info

End the reproducible example with a call to sessionInfo() in the same session (e.g. reprex(session_info = TRUE)) and include the output.

kendonB commented 10 months ago

It's a different bug in the latest CRAN. I get the error

Error in sprintf("<%s %s%sclass=\"gt_row %s%s\"%s>%s</%s>", elements,  : 
  arguments cannot be recycled to the same length
kendonB commented 10 months ago

I traced the issue to how alignment_classes get handled here:

https://github.com/rstudio/gt/blob/c8a2bda0f002b6d29adb5b96bbd80d14a9fbb6d9/R/utils_render_html.R#L979

I think the code just needs to strip off the right number of leading elements

teunbrand commented 10 months ago

I think the issue might be in dt_boxhead_get_alignments_in_stub(). With a 2-column stub, it returns 4 NAs for the group label, whereas only 2 values are needed for both the group label and the rowname.

library(gt)
x <- gtcars |>
  dplyr::select(model, year, hp, trq) |>
  dplyr::slice(1:8) |>
  gt(
    rowname_col = "model",
    row_group_as_column = TRUE
  ) |>
  tab_row_group(
    label = "numbered",
    rows = matches("^[0-9]")
  ) |>
  row_group_order(groups = c(NA, "numbered")) |>
  tab_options(row_group.default_label = "Others")

x <- gt:::build_data(x, context = "html")

gt:::dt_boxhead_get_alignments_in_stub(x)
#> [1] NA     NA     NA     NA     "left"

Created on 2024-01-30 with reprex v2.1.0

joeycouse commented 9 months ago

Assuming the issue I am encountering here is related this bug. I'm getting the same sprintf error as mentioned above.

library(gt)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

gtcars |> 
  group_by(ctry_origin, mfr) |> 
  gt(row_group_as_column = F)
model year trim bdy_style hp hp_rpm trq trq_rpm mpg_c mpg_h drivetrain trsmn msrp
United States - Ford
GT 2017 Base Coupe coupe 647 6250 550 5900 11 18 rwd 7a 447000
Italy - Ferrari
458 Speciale 2015 Base Coupe coupe 597 9000 398 6000 13 17 rwd 7a 291744
458 Spider 2015 Base convertible 562 9000 398 6000 13 17 rwd 7a 263553
458 Italia 2014 Base Coupe coupe 562 9000 398 6000 13 17 rwd 7a 233509
488 GTB 2016 Base Coupe coupe 661 8000 561 3000 15 22 rwd 7a 245400
California 2015 Base Convertible convertible 553 7500 557 4750 16 23 rwd 7a 198973
GTC4Lusso 2017 Base Coupe coupe 680 8250 514 5750 12 17 awd 7a 298000
FF 2015 Base Coupe coupe 652 8000 504 6000 11 16 awd 7a 295000
F12Berlinetta 2015 Base Coupe coupe 731 8250 509 6000 11 16 rwd 7a 319995
LaFerrari 2015 Base Coupe coupe 949 9000 664 6750 12 16 rwd 7a 1416362
Japan - Acura
NSX 2017 Base Coupe coupe 573 6500 476 2000 21 22 awd 9a 156000
Japan - Nissan
GT-R 2016 Premium Coupe coupe 545 6400 436 3200 16 22 awd 6a 101770
Italy - Lamborghini
Aventador 2015 LP 700-4 Coupe coupe 700 8250 507 5500 11 18 awd 7a 397500
Huracan 2015 LP 610-4 Coupe coupe 610 8250 413 6500 16 20 rwd 7a 237250
Gallardo 2014 LP 550-2 Coupe coupe 550 8000 398 6500 12 20 rwd 6a 191900
United Kingdom - Bentley
Continental GT 2016 V8 Coupe coupe 500 6000 487 1700 15 25 awd 8am 198500
Italy - Maserati
Granturismo 2016 Sport Coupe coupe 454 7600 384 4750 13 21 rwd 6am 132825
Quattroporte 2016 S Sedan sedan 404 5500 406 1500 16 23 rwd 8am 99900
Ghibli 2016 Base Sedan sedan 345 5250 369 1750 17 24 rwd 8am 70600
Germany - BMW
6-Series 2016 640 I Coupe coupe 315 5800 330 1400 20 30 rwd 8am 77300
i8 2016 Mega World Coupe coupe 357 5800 420 3700 28 29 awd 6am 140700
M4 2016 Base Coupe coupe 425 5500 406 1850 17 24 rwd 6m 65700
M5 2016 Base Sedan sedan 560 6000 500 1500 15 22 rwd 7am 94100
M6 2016 Base Coupe coupe 560 6000 500 1500 15 22 rwd 7a 113400
United Kingdom - Aston Martin
DB11 2017 Base Coupe coupe 608 6500 516 1500 15 21 rwd 8am 211195
Rapide S 2016 Base Sedan sedan 552 6650 465 5500 14 21 rwd 8am 205300
Vanquish 2016 Base Coupe coupe 568 6650 465 5500 13 21 rwd 8am 287250
Vantage 2016 V8 GT (Manual) Coupe coupe 430 7300 361 5000 13 19 rwd 6m 103300
United States - Chevrolet
Corvette 2016 Z06 Coupe coupe 650 6400 650 3600 15 22 rwd 7m 88345
United States - Dodge
Viper 2017 GT Coupe coupe 645 5000 600 5000 12 19 rwd 6m 95895
Germany - Audi
R8 2015 4.2 (Manual) Coupe coupe 430 7900 317 4500 11 20 awd 6m 115900
RS 7 2016 Quattro Hatchback hatchback 560 5700 516 1750 15 25 awd 8am 108900
S6 2016 Premium Plus quattro Sedan sedan 450 5800 406 1400 18 27 awd 7a 70900
S7 2016 Prestige quattro Hatchback hatchback 450 5800 406 1400 17 27 awd 7a 82900
S8 2016 Base Sedan sedan 520 5800 481 1700 15 25 awd 8am 114900
United Kingdom - Lotus
Evora 2017 2+2 Coupe coupe 400 7000 302 3500 16 24 rwd 6m 91900
United Kingdom - Jaguar
F-Type 2016 Base (Manual) Coupe coupe 340 6500 332 3500 16 24 rwd 6m 65000
Germany - Mercedes-Benz
AMG GT 2016 S Coupe coupe 503 6250 479 1750 16 22 rwd 7a 129900
SL-Class 2016 SL400 Convertible convertible 329 5250 354 1600 20 27 rwd 7am 85050
United States - Tesla
Model S 2017 75D sedan 259 6100 243 NA NA NA awd 1dd 74500
Germany - Porsche
718 Boxster 2017 Base Convertible convertible 300 6500 280 1950 21 28 rwd 6m 56000
718 Cayman 2017 Base Coupe coupe 300 6500 280 1950 20 29 rwd 6m 53900
911 2016 Carrera Coupe coupe 350 7400 287 5600 20 28 rwd 7m 84300
Panamera 2016 Base Sedan sedan 310 6200 295 3750 18 28 rwd 7a 78100
United Kingdom - McLaren
570 2016 Base Coupe coupe 570 7500 443 5000 16 23 rwd 7a 184900
United Kingdom - Rolls-Royce
Dawn 2016 Base Convertible convertible 563 5250 575 1500 12 19 rwd 8a 335000
Wraith 2016 Base Coupe coupe 624 5600 590 1500 13 21 rwd 8a 304350

gtcars |> 
  group_by(ctry_origin, mfr) |> 
  gt(row_group_as_column = T)
#> Warning in boxhead$var == var: longer object length is not a multiple of
#> shorter object length
#> Error in sprintf("<%s %s%sclass=\"gt_row %s%s\"%s>%s</%s>", elements, : arguments cannot be recycled to the same length

Created on 2024-02-14 with reprex v2.0.2