Skip to content

Commit

Permalink
Correctly display footnote marks in the summary stub.
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed Aug 15, 2024
1 parent 37d340c commit cfc1ea8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion R/z_utils_render_footnotes.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
56 changes: 56 additions & 0 deletions tests/testthat/test-tab_footnote.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.+<tfoot class")
expect_match_html(tab2, "gt_footnote_marks.+<tfoot class")

})

test_that("tab_footnote() works in row groups", {

# Apply a footnote to the `Mazdas` row group cell
Expand Down

0 comments on commit cfc1ea8

Please sign in to comment.