diff --git a/R/location_methods.R b/R/location_methods.R index 6ed8a5bc74..8383f33d67 100644 --- a/R/location_methods.R +++ b/R/location_methods.R @@ -61,6 +61,24 @@ add_summary_location_row <- function( summary_data <- dt_summary_get(data = data) + # Redundant if #1608 is fixed. + msg_no_summary <- c( + "!" = "Can't find summary data.", + "i" = "Call {.fn summary_rows} before {.fn tab_footnote}/{.fn tab_style}." + ) + if (length(summary_data) == 0) { + # Error early if can't find summary data. and try to add + cli::cli_abort(msg_no_summary, call = call) + } else { + # try to identify if only grand summary rows are present + # by checking if all grps are grand summary. + grps <- unlist(lapply(summary_data, function(x) x$groups)) + + if (identical(unique(grps), ":GRAND_SUMMARY:")) { + cli::cli_abort(msg_no_summary, call = call) + } + } + summary_data_summaries <- vapply( seq(summary_data), @@ -183,6 +201,24 @@ add_grand_summary_location_row <- function( call <- call(class(loc)[[1L]]) summary_data <- dt_summary_get(data = data) + # Redundant if #1608 is fixed. + msg_no_grand_summary <- c( + "!" = "Can't find grand summary data.", + "i" = "Call {.fn grand_summary_rows} before {.fn tab_footnote}/{.fn tab_style}." + ) + if (length(summary_data) == 0) { + # Error early if can't find summary data. and try to add + cli::cli_abort(msg_no_grand_summary, call = call) + } else { + # try to identify if only summary rows are present + # by checking if any grps are grand summary. + grps <- unlist(lapply(summary_data, function(x) x$groups)) + + if (!":GRAND_SUMMARY:" %in% grps) { + cli::cli_abort(msg_no_grand_summary, call = call) + } + } + id_vals <- unique( unlist( diff --git a/tests/testthat/_snaps/tab_footnote.md b/tests/testthat/_snaps/tab_footnote.md index 8c655f9a7f..87df9a15f6 100644 --- a/tests/testthat/_snaps/tab_footnote.md +++ b/tests/testthat/_snaps/tab_footnote.md @@ -1,3 +1,46 @@ +# tab_footnote() + cells_grand_summary_rows() errors well with absent grand summary rows + + Code + exibble %>% gt() %>% tab_footnote("foot grand summary", + cells_stub_grand_summary()) + Condition + Error in `tab_footnote()`: + ! Can't add footnote "foot grand summary". + Caused by error in `cells_stub_grand_summary()`: + ! Can't find grand summary data. + i Call `grand_summary_rows()` before `tab_footnote()`/`tab_style()`. + Code + exibble %>% gt(groupname_col = "group") %>% summary_rows(columns = tidyselect::where( + is.numeric), fns = "sum") %>% tab_footnote("foot grand summary", + cells_grand_summary()) + Condition + Error in `tab_footnote()`: + ! Can't add footnote "foot grand summary". + Caused by error in `cells_grand_summary()`: + ! Can't find grand summary data. + i Call `grand_summary_rows()` before `tab_footnote()`/`tab_style()`. + +# tab_footnote() + cells_summary() errors well with absent summary rows + + Code + exibble %>% gt(groupname_col = "group") %>% tab_footnote("foot summary", + cells_summary()) + Condition + Error in `tab_footnote()`: + ! Can't add footnote "foot summary". + Caused by error in `cells_summary()`: + ! Can't find summary data. + i Call `summary_rows()` before `tab_footnote()`/`tab_style()`. + Code + exibble %>% gt(groupname_col = "group") %>% grand_summary_rows(columns = tidyselect::where( + is.numeric), fns = "sum") %>% tab_footnote("foot summary", cells_stub_summary()) + Condition + Error in `tab_footnote()`: + ! Can't add footnote "foot summary". + Caused by error in `cells_stub_summary()`: + ! Can't find summary data. + i Call `summary_rows()` before `tab_footnote()`/`tab_style()`. + # Footnotes with no location are rendered correctly Code diff --git a/tests/testthat/test-tab_footnote.R b/tests/testthat/test-tab_footnote.R index ebcaf81ae6..7ea8fcb52e 100644 --- a/tests/testthat/test-tab_footnote.R +++ b/tests/testthat/test-tab_footnote.R @@ -261,6 +261,38 @@ test_that("tab_footnote() works for summary location", { ) }) +test_that("tab_footnote() + cells_grand_summary_rows() errors well with absent grand summary rows", { + + # Error well if grand summary is not initialized + expect_snapshot(error = TRUE, { + # error well if no grand summary exists. + exibble %>% + gt() %>% + tab_footnote("foot grand summary", cells_stub_grand_summary()) + + # Error well if a summary is present. + exibble %>% + gt(groupname_col = "group") %>% + summary_rows(columns = tidyselect::where(is.numeric), fns = "sum") %>% + tab_footnote("foot grand summary", cells_grand_summary()) + }) +}) + +test_that("tab_footnote() + cells_summary() errors well with absent summary rows", { + # Error well if summary is not initialized + expect_snapshot(error = TRUE, { + # error well if no grand summary exists. + exibble %>% + gt(groupname_col = "group") %>% + tab_footnote("foot summary", cells_summary()) + + # Error well if a summary is present. + exibble %>% + gt(groupname_col = "group") %>% + grand_summary_rows(columns = tidyselect::where(is.numeric), fns = "sum") %>% + tab_footnote("foot summary", cells_stub_summary()) + }) + test_that("tab_footnote() adds footnote marks for in the summary stub (#1832)", { # Apply a footnote to the grand summary stub cells. tab1 <-