Skip to content

Commit

Permalink
Merge pull request #1833 from olivroy/cov-footnote
Browse files Browse the repository at this point in the history
fix footnote marks in summary stub + footmark render in `cells_stubhead()` in `opt_interactive()`
  • Loading branch information
rich-iannone authored Aug 16, 2024
2 parents 6b63c32 + 32407c5 commit f7faec9
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions R/render_as_i_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
Expand Down Expand Up @@ -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
),
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
24 changes: 24 additions & 0 deletions tests/testthat/test-i_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))

})
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 f7faec9

Please sign in to comment.