From 37d340c455dd10bf8b4e305105f9ef4254b272ac Mon Sep 17 00:00:00 2001 From: olivroy Date: Thu, 15 Aug 2024 13:47:58 -0400 Subject: [PATCH 1/2] Improve stubhead rendering if footnotes are supplied with `cells_stubhead()` --- NEWS.md | 2 +- R/render_as_i_html.R | 4 ++++ tests/testthat/test-i_html.R | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index ed7f71d504..3b7cc064e7 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 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/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)) }) From e81b722351224f6b179b9af140ac0569b3d4a58b Mon Sep 17 00:00:00 2001 From: olivroy Date: Thu, 15 Aug 2024 13:51:37 -0400 Subject: [PATCH 2/2] Fix footnote mark rendering for `cells_stub_summary_rows()` + `cells_stub_grand_summary_rows()` --- 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..e9239a26f8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). # gt 0.11.0 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.+