From cfc1ea89de56d375effae8b60dbafe83e82078ca Mon Sep 17 00:00:00 2001 From: olivroy Date: Thu, 15 Aug 2024 13:48:40 -0400 Subject: [PATCH] Correctly display footnote marks in the summary stub. --- NEWS.md | 2 ++ R/z_utils_render_footnotes.R | 4 ++- tests/testthat/test-tab_footnote.R | 56 ++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 3b7cc064e7..ad93c4f799 100644 --- a/NEWS.md +++ b/NEWS.md @@ -19,6 +19,8 @@ * More support for `cells_stubhead()` styling and footnotes in interactive tables. +* `tab_footnote()` now correctly adds footnote marks in the `cells_stub_summary()` and `cells_stub_grand_summary()` (@olivroy, #1832). + ## Bug fixes * Improved error messages for the `text_transform()` function if `locations` couldn't be resolved. (@olivroy, #1774) diff --git a/R/z_utils_render_footnotes.R b/R/z_utils_render_footnotes.R index b286996ffb..eca85bcd9a 100644 --- a/R/z_utils_render_footnotes.R +++ b/R/z_utils_render_footnotes.R @@ -712,8 +712,10 @@ apply_footnotes_to_summary <- function(data, context = "html") { list_of_summaries <- dt_summary_df_get(data = data) footnotes_tbl <- dt_footnotes_get(data = data) + # make sure rownames are recognized to add footnote marks + # to cells_stub_grand_summary() / cells_stub_summary() #1832 # dplyr::coalesce() - footnotes_tbl$colname[is.na(footnotes_tbl$colname)] <- "rowname" + footnotes_tbl$colname[is.na(footnotes_tbl$colname)] <- "::rowname::" summary_df_list <- list_of_summaries$summary_df_display_list if ("summary_cells" %in% footnotes_tbl$locname) { diff --git a/tests/testthat/test-tab_footnote.R b/tests/testthat/test-tab_footnote.R index e61c4f51f4..a827f05245 100644 --- a/tests/testthat/test-tab_footnote.R +++ b/tests/testthat/test-tab_footnote.R @@ -261,6 +261,62 @@ test_that("tab_footnote() works for summary location", { ) }) +test_that("tab_footnote() adds footnote marks for in the summary stub (#1832)", { + # Apply a footnote to the grand summary stub cells. + tab1 <- + tab_footnote( + data, + footnote = "Grand summary stub footnote.", + locations = list( + cells_stub_grand_summary(2) + ) + ) + # A footnote in the grand summary + tab2 <- tab_footnote( + data, + footnote = "Summary stub mean sum footnote.", + locations = list( + # FIXME doesn't work without specifying groups manually. + # Because not all groups have a summary + cells_stub_summary(groups = c(1, 3)) + ) + ) + # Expect that the internal `footnotes_df` data frame will have + # its `locname` column entirely populated with `gramd_summary_cell` + expect_setequal( + dt_footnotes_get(tab1)$locname, + c("grand_summary_cells") + ) + expect_setequal( + dt_footnotes_get(tab2)$locname, + c("summary_cells") + ) + # Expect the colname to be NA + expect_setequal( + dt_footnotes_get(tab1)$colname, + NA_character_ + ) + # Expect tab2 to be in hp. + expect_setequal( + dt_footnotes_get(tab2)$colname, + NA_character_ + ) + # Expect that the internal `footnotes_df` data frame will have + # its `text` column entirely populated with the footnote text + expect_setequal( + unlist(dt_footnotes_get(tab1)$footnotes), + "Grand summary stub footnote." + ) + expect_setequal( + unlist(dt_footnotes_get(tab2)$footnotes), + "Summary stub mean sum footnote." + ) + # Make sure there is a footnote mark in the body (i.e. before the tfoot part) + expect_match_html(tab1, "gt_footnote_marks.+