| output |
|---|
github_document |
The pager package makes it simple to save tables and plots as Word, HTML, or plain text documents.
Tables of class gtsummary, gt, and flextable, as well as ggplot and grob plots, are supported.
This is accomplished by rendering the objects via R Markdown.
The package also supports lists of objects. When a list is passed, each element is placed on a separate page (Word), separated by a horizontal rule (HTML), or separated by a horizontal rule (plain text).
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 a Word document using the default portrait orientation reference document.
gtsummary::as_flex_table(tbl) |>
save_docx(path = tempfile(fileext = ".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_docx(path = tempfile(fileext = ".docx"))The code below will save the table as a self-contained HTML file.
tbl |>
gtsummary::as_gt() |>
save_html(path = tempfile(fileext = ".html"))A paginated table can also be saved as HTML — each page is separated by a horizontal rule.
gtsummary::tbl_split_by_rows(tbl, row_numbers = seq(20, nrow(tbl), by = 20)) |>
save_html(path = tempfile(fileext = ".html"))The code below will save the table as a plain text (Markdown-formatted) file.
tbl |>
save_txt(path = tempfile(fileext = ".txt"))save_txt() also accepts gt tables directly.
gt::gt(head(mtcars)) |>
save_txt(path = tempfile(fileext = ".txt"))A list of tables can also be saved as plain text — each table is separated by a horizontal rule.
list(gt::gt(head(mtcars)), gt::gt(tail(mtcars))) |>
save_txt(path = tempfile(fileext = ".txt"))