After updating flextable from 0.6.10 to 0.7.3 I can't add footers that apply to the same row but multiple columns any longer. Simple reproducible example where I'd like to mark all "N/A" values in columns "p" and "p_adj" with the same footer:
p_values <- c("0.01", "N/A", "0.02", "N/A", "0.03", "N/A")
df <- data.frame(mean = seq(1,6),
p = p_values,
p_adj = p_values)
df |>
flextable() |>
footnote(i = ~ p == "N/A",
j = c("p", "p_adj"),
value = flextable::as_paragraph("Sample size too low"),
ref_symbols = "a",
part = "body")
This results in an error:
Error in data.frame(i = i, j = j) :
arguments imply differing number of rows: 3, 2
Application to only one column works fine:
df |>
flextable() |>
footnote(i = ~ p == "N/A",
j = c("p"),
value = flextable::as_paragraph("Sample size too low"),
ref_symbols = "a",
part = "body")
I have tried for hours, but can't come up with a good solution. Simple solutions that don't work:
aka:
df |>
flextable() |>
footnote(i = ~ rep(p == "N/A", 2),
j = c("p", "p_adj"),
value = flextable::as_paragraph("Sample size too low"),
ref_symbols = "a",
part = "body")
This results in the error below:
Error in get_rows_id(x[[part]], i) :
invalid row selection: length(i) [12] != nrow(dataset) [6]
Does anyone have an idea how to work with the updated flextable::footnote() function in this circumstance?
The comment by Kat pointed me in the right direction!
The simple solution for the example provided above is:
df |>
flextable() |>
footnote(i = rep(which(df$p == "N/A"), 2),
j = c("p", "p_adj"),
value = flextable::as_paragraph("Sample size too low"),
ref_symbols = "a",
part = "body")
Referencing the original data does not work well if you are using complex flextables that use grouping because flextable will insert extra rows. To address that create the flextable in steps and reference the actual flextable body dataset:
ft <- df |>
flextable()
ft <- ft |>
footnote(i = rep(which(ft[["body"]][["dataset"]][["p"]] == "N/A"), 2),
j = c("p", "p_adj"),
value = flextable::as_paragraph("Sample size too low"),
ref_symbols = "a",
part = "body")
ft