diff --git a/NEWS.md b/NEWS.md index 3bba75e5c8..6a1784723a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -17,7 +17,7 @@ * `opt_interactive()` now works when columns are substituted with `sub_*()` (@olivroy, #1759). -* More support for `cells_stubhead()` styling in interactive tables. +* More support for `cells_stubhead()` styling and footnotes in interactive tables. ## Bug fixes @@ -43,6 +43,8 @@ * Performance improvement for footnote rendering (@olivroy, #1818). +* `tab_footnote()` now correctly adds footnote marks in the `cells_stub_summary()` and `cells_stub_grand_summary()` (@olivroy, #1832). + * Fixed an issue where `tab_spanner_delim()` would fail to resolve a duplicate id (@olivroy, #1821). * `tidyselect::where()`, `tidyselect::all_of()`, `tidyselect::any_of()` are now re-exported by gt. diff --git a/R/render_as_i_html.R b/R/render_as_i_html.R index 462da5841e..18c3f89f58 100644 --- a/R/render_as_i_html.R +++ b/R/render_as_i_html.R @@ -242,6 +242,8 @@ render_as_ihtml <- function(data, id) { # Create colDef row name with special ".rownames" from reactable. row_name_col_def <- list(reactable::colDef( name = rowname_label, + # make sure the cells_stubhead() footnote renders properly. + html = TRUE, style = list( fontWeight = stub_font_weight ), @@ -371,6 +373,8 @@ render_as_ihtml <- function(data, id) { group_col_defs[[i]] <- reactable::colDef( name = group_label, + # make sure the cells_stubhead() footnote renders properly. + html = TRUE, style = list( `font-weight` = row_group_font_weight ), 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-i_html.R b/tests/testthat/test-i_html.R index 599f829cf2..f73247b5c3 100644 --- a/tests/testthat/test-i_html.R +++ b/tests/testthat/test-i_html.R @@ -114,6 +114,28 @@ test_that("Interactive tables won't fail when using different options", { cells_stubhead() ) %>% opt_interactive() + # footnote with cells_stubhead() works + tbl_gt_i_31 <- exibble %>% + gt(groupname_col = "group", row_group_as_column = TRUE) %>% + tab_stubhead( + "stubhead label" + ) %>% + tab_footnote( + footnote = "Stubhead foot", + locations = cells_stubhead() + ) %>% + opt_interactive() + # footnote with `cells_stubhead()` works + tbl_gt_i_32 <- exibble %>% + gt(rownames_to_stub = TRUE) %>% + tab_stubhead( + "stubhead label" + ) %>% + tab_footnote( + footnote = "Stubhead foot", + locations = cells_stubhead() + ) %>% + opt_interactive() capture_output(expect_no_error(tbl_gt_i_01)) capture_output(expect_no_error(tbl_gt_i_02)) @@ -145,5 +167,7 @@ test_that("Interactive tables won't fail when using different options", { capture_output(expect_no_error(tbl_gt_i_28)) capture_output(expect_no_error(tbl_gt_i_29)) capture_output(expect_no_error(tbl_gt_i_30)) + capture_output(expect_no_error(tbl_gt_i_31)) + capture_output(expect_no_error(tbl_gt_i_32)) }) 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.+