The pager package makes it simple to save tables of class gtsummary, gt, and flextable as a Word document using a reference document. This is accomplished by creating the document via R markdown using the reference_docx:
field.
The package also supports lists of table objects. When a list is passed, a page break is placed between each table in the list.
You can install the development version of pager from GitHub with:
# install.packages("pak")
pak::pak("insightsengineering/pager")
To begin, let’s create a summary table.
library(pager)
# create table
tbl <-
cards::ADAE[1:150,] |>
gtsummary::tbl_hierarchical(
variables = c(AESOC, AETERM),
by = TRTA,
denominator = cards::ADSL,
id = USUBJID,
)
The code below will save the table as Word document using the default portrait orientation reference document.
gtsummary::as_flex_table(tbl) |>
save_with_rmarkdown(path = tempfile(fileext = ".docx"))
#> ✔ Writing '/var/folders/6f/gdjf_vxj2wl3jhmxdkd1hd_w0000gn/T//RtmpcdsRdZ/filee351225cee86.docx'
The example below first splits the summary table into a list of tables. Each table is saved to a separate page in the resulting Word document.
gtsummary::tbl_split_by_rows(tbl, row_numbers = seq(20, nrow(tbl), by = 20)) |>
save_with_rmarkdown(path = tempfile(fileext = ".docx"))
#> ✔ Writing '/var/folders/6f/gdjf_vxj2wl3jhmxdkd1hd_w0000gn/T//RtmpcdsRdZ/filee35112a6c071.docx'