# rtf_encode() only accepts doc_type = "table" or "figure".
# To produce a single combined RTF, write each table separately then
# use r2rtf::assemble_rtf() to merge them into one file.
arm_names <- levels(adsl_safe$TRT01A)
ae_df <- adae_safe |>
dplyr::count(TRT01A, AEBODSYS, AEDECOD, name = "n") |>
dplyr::left_join(
adsl_safe |> dplyr::count(TRT01A, name = "N"),
by = "TRT01A"
) |>
dplyr::mutate(cell = sprintf("%d (%.1f%%)", n, n / N * 100)) |>
dplyr::select(AEBODSYS, AEDECOD, TRT01A, cell) |>
tidyr::pivot_wider(
names_from = TRT01A,
values_from = cell,
values_fill = "0 (0.0%)"
) |>
dplyr::arrange(AEBODSYS, AEDECOD) |>
dplyr::slice_head(n = 12)
col_header <- paste(
c("System Organ Class", "Preferred Term", arm_names),
collapse = " | "
)
# Write table 1 (portrait)
file1 <- tempfile(fileext = ".rtf")
demo_df |>
r2rtf::rtf_page(orientation = "portrait") |>
r2rtf::rtf_title(title = "Table 14.1.1", subtitle = "Demographics") |>
r2rtf::rtf_colheader(
colheader = "Treatment | N | Age, Mean (SD) | Female, n (%)",
col_rel_width = c(3, 1, 2, 2)
) |>
r2rtf::rtf_body(
col_rel_width = c(3, 1, 2, 2),
text_justification = c("l", "c", "c", "c")
) |>
r2rtf::rtf_encode() |>
r2rtf::write_rtf(file1)
# Write table 2 (landscape)
file2 <- tempfile(fileext = ".rtf")
ae_df |>
r2rtf::rtf_page(orientation = "landscape") |>
r2rtf::rtf_title(title = "Table 14.3.1",
subtitle = "TEAEs by SOC and Preferred Term") |>
r2rtf::rtf_colheader(
colheader = col_header,
col_rel_width = c(3, 3, rep(2, length(arm_names)))
) |>
r2rtf::rtf_body(
col_rel_width = c(3, 3, rep(2, length(arm_names))),
text_justification = c("l", "l", rep("c", length(arm_names)))
) |>
r2rtf::rtf_encode() |>
r2rtf::write_rtf(file2)
# Assemble into one combined RTF file
r2rtf::assemble_rtf(
input = c(file1, file2),
output = "tlf_package_day27.rtf"
)
cat("Exported: tlf_package_day27.rtf\n")