Search code examples
rgt

GT Table 0.9.0 "summary_rows(groups = everything()," throws an error message


I get that the code for summary_rows() has changed what I don't understand is what I need to do to get my old code to work. Please help I've spend hours trying to solve this problem.

I have a custom function in the summary_rows code;

fns_labels_exc0 <-
list(
Low = ~min(.[. > 0], na.rm = TRUE),
High = ~max(.[. > 0], na.rm = TRUE),
Median = ~median(.[. > 0], na.rm = TRUE),
Average = ~mean(.[. > 0], na.rm = TRUE))

My old code;

clean_data_b03_1y %>% gt(rowname_col = "mls") |>
tab_header(title = md("Comparable Product - 12 Month Market Summary")) |>
tab_stubhead(label = "MLS Number") |>
tab_options(table.width = pct(100)) |>
fmt_integer(columns = year_built, use_seps = FALSE) |>
fmt_currency(columns = c(concession, original_list_price, close_price),
currency = "USD", decimals = 0) |>
fmt_number(columns = c(living_area, lot_size_sf), decimals = 0) %>%
fmt_percent(columns = olp_cp, decimals = 2) |>
summary_rows(groups = NULL, columns = c(concession, original_list_price, close_price),
fns = fns_labels_exc0, formatter = fmt_currency, decimals = 0)

When I run my old code the output contains the table but no summary rows at the bottom. There are also no warnings or errors in the compiler window.

If I change the summary_rows code to:

summary_rows(groups = everything(), columns = c(concession, original_list_price, close_price),
fns = fns_labels_exc0, formatter = fmt_currency, decimals = 0)

I get this error message;

Error: ! everything() must be used within a selecting function. ℹ See ?tidyselect::faq-selection-context for details.

I do not actually have any groups the summary_rows code I have actually target specific columns with column specific formatting i.e. numbers, currency.

If I drop the groups = argument, because its a default, I get the same error message;

summary_rows(columns = c(concession, original_list_price, close_price),
fns = fns_labels_exc0, formatter = fmt_currency, decimals = 0)

Error: ! everything() must be used within a selecting function. ℹ See ?tidyselect::faq-selection-context for details.

I'm running R version 4.3.1 (2023-06-16) -- "Beagle Scouts" R Studio Version 2023.06.2+561 (2023.06.2+561) package ‘gt’ version 0.9.0


Solution

  • I ran into this same problem recently. Have you tried using the function grand_summary_rows() instead of summary_rows? I don't have the same data from your example clean_data_b03_1y, but I think this re-post of your code with a small change might provide a solution:

    clean_data_b03_1y %>% gt(rowname_col = "mls") |>
    tab_header(title = md("Comparable Product - 12 Month Market Summary")) |>
    tab_stubhead(label = "MLS Number") |>
    tab_options(table.width = pct(100)) |>
    fmt_integer(columns = year_built, use_seps = FALSE) |>
    fmt_currency(columns = c(concession, original_list_price, close_price),
    currency = "USD", decimals = 0) |>
    fmt_number(columns = c(living_area, lot_size_sf), decimals = 0) %>%
    fmt_percent(columns = olp_cp, decimals = 2) |>
    grand_summary_rows(columns = c(concession, original_list_price, close_price),
    fns = fns_labels_exc0, formatter = fmt_currency, decimals = 0)
    

    It's just a change from summary_rows to grand_summary_rows and deleting the groups = NULL argument. That should work. I had a similar issue and this change resolved it for me.