rstudio / gt

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

tidyselect error in `gt::summary_rows()` without groups (in v0.9.0) #1292

Closed salim-b closed 5 months ago

salim-b commented 1 year ago

Prework

Description

See the error message.

Reproducible example

mtcars |>
        dplyr::select(gear) |>
        gt::gt(rownames_to_stub = TRUE) |>
        gt::summary_rows(fns = "sum")
#> Error:
#> ! `everything()` must be used within a *selecting* function.
#> ℹ See <https://tidyselect.r-lib.org/reference/faq-selection-context.html> for
#>   details.
#> Backtrace:
#>     ▆
#>  1. └─gt::summary_rows(...)
#>  2.   └─tidyselect::everything()
#>  3.     ├─vars %||% peek_vars(fn = "everything")
#>  4.     └─tidyselect::peek_vars(fn = "everything")
#>  5.       └─cli::cli_abort(...)
#>  6.         └─rlang::abort(...)

Created on 2023-04-04 with reprex v2.0.2

Expected result

I'd expect the above expression to return the same as gt::grand_summary_rows(fns = "sum")

Reprex: ``` r mtcars |> dplyr::select(gear) |> gt::gt(rownames_to_stub = TRUE) |> gt::grand_summary_rows(fns = "sum") ```
gear
Mazda RX4 4
Mazda RX4 Wag 4
Datsun 710 4
Hornet 4 Drive 3
Hornet Sportabout 3
Valiant 3
Duster 360 3
Merc 240D 4
Merc 230 4
Merc 280 4
Merc 280C 4
Merc 450SE 3
Merc 450SL 3
Merc 450SLC 3
Cadillac Fleetwood 3
Lincoln Continental 3
Chrysler Imperial 3
Fiat 128 4
Honda Civic 4
Toyota Corolla 4
Toyota Corona 3
Dodge Challenger 3
AMC Javelin 3
Camaro Z28 3
Pontiac Firebird 3
Fiat X1-9 4
Porsche 914-2 5
Lotus Europa 5
Ford Pantera L 5
Ferrari Dino 5
Maserati Bora 5
Volvo 142E 4
sum 118
Created on 2023-04-04 with [reprex v2.0.2](https://reprex.tidyverse.org)

Workaround

One can set groups to the undocumented sentinel ":GRAND_SUMMARY:":

Reprex: ``` r mtcars |> dplyr::select(gear) |> gt::gt(rownames_to_stub = TRUE) |> gt::summary_rows(fns = "sum", groups = ":GRAND_SUMMARY:") ```
gear
Mazda RX4 4
Mazda RX4 Wag 4
Datsun 710 4
Hornet 4 Drive 3
Hornet Sportabout 3
Valiant 3
Duster 360 3
Merc 240D 4
Merc 230 4
Merc 280 4
Merc 280C 4
Merc 450SE 3
Merc 450SL 3
Merc 450SLC 3
Cadillac Fleetwood 3
Lincoln Continental 3
Chrysler Imperial 3
Fiat 128 4
Honda Civic 4
Toyota Corolla 4
Toyota Corona 3
Dodge Challenger 3
AMC Javelin 3
Camaro Z28 3
Pontiac Firebird 3
Fiat X1-9 4
Porsche 914-2 5
Lotus Europa 5
Ford Pantera L 5
Ferrari Dino 5
Maserati Bora 5
Volvo 142E 4
sum 118
Created on 2023-04-04 with [reprex v2.0.2](https://reprex.tidyverse.org)

Session info

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.3 (2023-03-15) #> os Ubuntu 22.04.2 LTS #> system x86_64, linux-gnu #> ui X11 #> language en_US.utf8 #> collate en_US.utf8 #> ctype en_US.utf8 #> tz Europe/Zurich #> date 2023-04-04 #> pandoc 3.1.2 @ /usr/bin/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.1 2023-03-23 [1] RSPM (R 4.2.0) #> digest 0.6.31 2022-12-11 [1] RSPM (R 4.2.0) #> dplyr 1.1.1 2023-03-22 [1] CRAN (R 4.2.3) #> evaluate 0.20 2023-01-17 [1] RSPM (R 4.2.0) #> fansi 1.0.4 2023-01-22 [1] RSPM (R 4.2.0) #> fastmap 1.1.1 2023-02-24 [1] RSPM (R 4.2.0) #> fs 1.6.1 2023-02-06 [1] RSPM (R 4.2.0) #> generics 0.1.3 2022-07-05 [1] RSPM (R 4.2.0) #> glue 1.6.2 2022-02-24 [1] RSPM (R 4.2.0) #> gt 0.9.0 2023-03-31 [1] RSPM (R 4.2.0) #> htmltools 0.5.5 2023-03-23 [1] RSPM (R 4.2.0) #> knitr 1.42 2023-01-25 [1] RSPM (R 4.2.0) #> lifecycle 1.0.3 2022-10-07 [1] RSPM (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] RSPM (R 4.2.0) #> pillar 1.9.0 2023-03-22 [1] RSPM (R 4.2.0) #> pkgconfig 2.0.3 2019-09-22 [1] RSPM (R 4.2.0) #> purrr 1.0.1 2023-01-10 [1] RSPM (R 4.2.0) #> R.cache 0.16.0 2022-07-21 [1] RSPM (R 4.2.0) #> R.methodsS3 1.8.2 2022-06-13 [1] RSPM (R 4.2.0) #> R.oo 1.25.0 2022-06-12 [1] RSPM (R 4.2.0) #> R.utils 2.12.2 2022-11-11 [1] RSPM (R 4.2.0) #> R6 2.5.1 2021-08-19 [1] RSPM (R 4.2.0) #> reprex 2.0.2 2022-08-17 [1] RSPM (R 4.2.0) #> rlang 1.1.0 2023-03-14 [1] RSPM (R 4.2.0) #> rmarkdown 2.21 2023-03-26 [1] RSPM (R 4.2.0) #> rstudioapi 0.14 2022-08-22 [1] RSPM (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] RSPM (R 4.2.0) #> styler 1.9.1 2023-03-04 [1] RSPM (R 4.2.0) #> tibble 3.2.1 2023-03-20 [1] RSPM (R 4.2.0) #> tidyselect 1.2.0 2022-10-10 [1] RSPM (R 4.2.0) #> utf8 1.2.3 2023-01-31 [1] RSPM (R 4.2.0) #> vctrs 0.6.1 2023-03-22 [1] RSPM (R 4.2.0) #> withr 2.5.0 2022-03-03 [1] RSPM (R 4.2.0) #> xfun 0.38 2023-03-24 [1] RSPM (R 4.2.0) #> xml2 1.3.3 2021-11-30 [1] RSPM (R 4.2.0) #> yaml 2.3.7 2023-01-23 [1] RSPM (R 4.2.0) #> #> [1] /home/salim/.software/managed/r #> [2] /usr/local/lib/R/site-library #> [3] /usr/lib/R/site-library #> [4] /usr/lib/R/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
rich-iannone commented 1 year ago

Thanks for the bug report here. I absolutely agree that if there are no groups at build time, summary_rows() should just act as grand_summary_rows() does.

olivroy commented 5 months ago

We ended up just improving the error message. It is better to use grand_summary_rows(). Any usage where summary_rows() is better than grand_summary_rows()?

salim-b commented 5 months ago

Any usage where summary_rows() is better than grand_summary_rows()?

No, I don't think so. I just thought the original error message was really not user-friendly and when I looked closer I saw that grand_summary_rows() is just a wrapper around summary_rows(groups = ":GRAND_SUMMARY:")...

We ended up just improving the error message.

Totally fine with me, thanks. 🙂