Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions R/location_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand Down
43 changes: 43 additions & 0 deletions tests/testthat/_snaps/tab_footnote.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test-tab_footnote.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 <-
Expand Down