diff --git a/NEWS.md b/NEWS.md index 0612b0532..c4131bf3a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,9 @@ # gt (development version) -* Fixed an issue where `md("")` would fail in Quarto (@olivroy, #1769). +* `tab_row_group()` gives a more precise error message when `rows` can't be resolved correctly (#1535). (@olivroy, #1770) +* Fixed an issue where `md("")` would fail in Quarto. (@olivroy, #1769) + # gt 0.11.0 ## New features diff --git a/R/resolver.R b/R/resolver.R index cb477c191..07aa17869 100644 --- a/R/resolver.R +++ b/R/resolver.R @@ -644,7 +644,23 @@ normalize_resolved <- function( unknown_resolved <- setdiff(resolved, item_names) if (length(unknown_resolved) != 0) { - resolver_stop_on_character(item_label = item_label, unknown_resolved = unknown_resolved, call = call) + if (all(is.na(item_names)) && item_label == "row") { + # Send a more informative message when the gt table has no rows + # rows need to be initialized with `rownames_to_stub = TRUE` or with `rowname_col = ` + # Issue #1535 (Override the resolver default error message.) + + cli::cli_abort(c( + "Can't find named rows in the table", + "i" = "In {.help [gt()](gt::gt)}, use {.code rownames_to_stub = TRUE} or specify {.arg rowname_col} to initialize row names in the table." + ), call = call) + } + + # Potentially use arg_match() when rlang issue is solved? + resolver_stop_on_character( + item_label = item_label, + unknown_resolved = unknown_resolved, + call = call + ) } resolved <- item_names %in% resolved @@ -693,7 +709,7 @@ resolver_stop_on_character <- function( # Specify cli pluralization l <- length(unknown_resolved) cli::cli_abort( - "{item_label}{cli::qty(l)}{?s} {.code {unknown_resolved}} + "{item_label}{cli::qty(l)}{?s} {.str {unknown_resolved}} do{?es/} not exist in the data.", call = call ) diff --git a/tests/testthat/_snaps/tab_footnote.md b/tests/testthat/_snaps/tab_footnote.md index 45e03ef08..c7468c3b9 100644 --- a/tests/testthat/_snaps/tab_footnote.md +++ b/tests/testthat/_snaps/tab_footnote.md @@ -579,7 +579,7 @@ Error in `tab_footnote()`: ! Can't add footnote "First data cell.". Caused by error in `cells_column_spanners()`: - ! Spanner `valuer` does not exist in the data. + ! Spanner "valuer" does not exist in the data. Code tab_footnote(start_gt, footnote = "First data cell.", locations = cells_column_spanners( 3)) @@ -595,7 +595,7 @@ Error in `tab_footnote()`: ! Can't add footnote "Footnote error.". Caused by error in `cells_body()`: - ! Row `Mazda RX7` does not exist in the data. + ! Row "Mazda RX7" does not exist in the data. # tab_footnote() errors well when it can't resolve location diff --git a/tests/testthat/_snaps/tab_remove.md b/tests/testthat/_snaps/tab_remove.md index e85e81272..c28c7e2de 100644 --- a/tests/testthat/_snaps/tab_remove.md +++ b/tests/testthat/_snaps/tab_remove.md @@ -110,12 +110,12 @@ rm_spanners(t_sp, "span2") Condition Error in `rm_spanners()`: - ! Spanner `span2` does not exist in the data. + ! Spanner "span2" does not exist in the data. Code rm_spanners(t_sp, c("span1", "span2", "span3")) Condition Error in `rm_spanners()`: - ! Spanners `span2` and `span3` do not exist in the data. + ! Spanners "span2" and "span3" do not exist in the data. # Table footnotes can be removed using `rm_footnotes()` diff --git a/tests/testthat/_snaps/tab_style.md b/tests/testthat/_snaps/tab_style.md index af44cde65..24e73d61e 100644 --- a/tests/testthat/_snaps/tab_style.md +++ b/tests/testthat/_snaps/tab_style.md @@ -1,7 +1,7 @@ -# tab_style errors if problems occur +# tab_style() errors if locations can't be resolved Code - data %>% tab_style(style = list(cell_fill(color = "green"), cell_text(color = "white")), + tab_style(data, style = list(cell_fill(color = "green"), cell_text(color = "white")), locations = cells_summary(groups = "Mercs", columns = starts_with("x"), rows = 2)) Condition Error in `tab_style()`: @@ -9,11 +9,8 @@ Caused by error in `cells_summary()`: ! The location requested could not be resolved. * Review the expression provided as `columns`. - ---- - Code - data %>% tab_style(style = list(cell_fill(color = "green"), cell_text(color = "white")), + tab_style(data, style = list(cell_fill(color = "green"), cell_text(color = "white")), locations = cells_summary(groups = "Mercs", columns = starts_with("m"), rows = starts_with( "x"))) Condition @@ -22,11 +19,8 @@ Caused by error in `cells_summary()`: ! The location requested could not be resolved. * Review the expression provided as `rows`. - ---- - Code - data %>% tab_style(style = list(cell_fill(color = "green"), cell_text(color = "white")), + tab_style(data, style = list(cell_fill(color = "green"), cell_text(color = "white")), locations = cells_column_labels(`non existent`)) Condition Error in `tab_style()`: @@ -34,11 +28,8 @@ Caused by error in `cells_column_labels()`: ! Can't select columns that don't exist. x Column `non existent` doesn't exist. - ---- - Code - data %>% tab_style(style = list(cell_fill(color = "green"), cell_text(color = "white")), + tab_style(data, style = list(cell_fill(color = "green"), cell_text(color = "white")), locations = cells_column_spanners(2)) Condition Error in `tab_style()`: @@ -80,17 +71,7 @@ Error in `tab_style()`: ! Failed to style the body of the table. Caused by error in `cells_body()`: - ! Row `Mazda RX7` does not exist in the data. - -# tab_row_group warns when others_label is not empty - - Code - a_gt <- data %>% tab_row_group(others_label = "Others1") - Condition - Warning: - Since gt v0.3.0 the `others_label` argument has been deprecated. - * Use `tab_options(row_group.default_label =