Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix when placing flextable footnote to multiple columns and rows #2063

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
12 changes: 11 additions & 1 deletion R/as_flex_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,17 @@ table_styling_to_flextable_calls <- function(x, ...) {
tidyr::nest(location_ids = c("row_numbers", "column_id")) %>%
dplyr::mutate(
row_numbers = map(.data$location_ids, ~ getElement(.x, "row_numbers") |> unique()),
column_id = map(.data$location_ids, ~ getElement(.x, "column_id") |> unique())
column_id =
pmap(
list(.data$location_ids,
.data$tab_location,
.data$row_numbers),
\(location_ids, tab_location, row_numbers) {
col_ids <- getElement(location_ids, "column_id") |> unique()
if (tab_location == "body") col_ids <- rep(col_ids, length(row_numbers)) # styler: off
col_ids
}
)
)

flextable_calls[["footnote"]] <-
Expand Down
57 changes: 57 additions & 0 deletions tests/testthat/test-as_flex_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,63 @@ test_that("as_flex_table passes table footnotes & footnote abbreviations correct
c(fn1[2], fn2[2]), # correct labels
c("another new footnote", "replace old footnote")
)

# testing one footnote passed to multiple columns and rows, addresses issue #2062
expect_silent(
my_tbl_summary |>
modify_footnote(stat_0 = NA) |>
modify_table_styling(
columns = c(label, stat_0),
rows = (variable %in% "trt") & (row_type == "level"),
footnote = "my footnote"
) |>
as_flex_table()
Melkiades marked this conversation as resolved.
Show resolved Hide resolved
)
})

test_that("as_flex_table passes multiple table footnotes correctly", {
trial_reduced <- trial |>
select(age, grade, stage, trt) |>
filter(trt %in% "Drug A") |>
filter(grade %in% "I") |>
mutate(grade = factor(grade, levels = c("I")))

out <- trial_reduced |>
tbl_summary(
by = trt,
include = grade
) |>
modify_table_styling(
columns = stat_1,
rows = (variable %in% "grade") & (row_type == "level"),
footnote = "Cell-level foonotes here."
) |>
modify_table_styling(
columns = label,
rows = label == "grade",
footnote = "i.e. Grade"
) |>
modify_table_styling(
columns = label,
rows = label == "Characteristic",
footnote = "i.e. char"
) |>
modify_table_styling(
columns = label,
rows = label == "I",
footnote = "i.e. 1"
) |>
as_flex_table()

dchunk <- flextable::information_data_chunk(out)
cell_1 <- dchunk |> filter(.part %in% "footer")

expect_equal(cell_1$txt, c(
"1", "n (%)", "",
"2", "i.e. Grade", "",
"3", "i.e. 1", "",
"4", "Cell-level foonotes here.", ""
))
})

test_that("as_flex_table passes table indentation correctly", {
Expand Down
Loading