https://github.com/insightsengineering/pager
Save multipage tables as 'Word' documents via 'rmarkdown'
https://github.com/insightsengineering/pager
r
Last synced: about 2 months ago
JSON representation
Save multipage tables as 'Word' documents via 'rmarkdown'
- Host: GitHub
- URL: https://github.com/insightsengineering/pager
- Owner: insightsengineering
- Created: 2025-08-26T22:43:32.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2026-03-02T07:54:35.000Z (3 months ago)
- Last Synced: 2026-03-02T11:53:52.615Z (3 months ago)
- Topics: r
- Language: R
- Homepage: https://insightsengineering.github.io/pager/
- Size: 142 KB
- Stars: 2
- Watchers: 0
- Forks: 1
- Open Issues: 16
-
Metadata Files:
- Readme: README.Rmd
Awesome Lists containing this project
README
---
output: github_document
---
# pager
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[](https://CRAN.R-project.org/package=pager)
[](https://github.com/insightsengineering/pager/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/insightsengineering/pager)
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).
## Installation
You can install the development version of pager from [GitHub](https://github.com/) with:
``` r
# install.packages("pak")
pak::pak("insightsengineering/pager")
```
## Example
To begin, let's create a summary table.
``` r
library(pager)
# create table
tbl <-
cards::ADAE[1:150, ] |>
gtsummary::tbl_hierarchical(
variables = c(AESOC, AETERM),
by = TRTA,
denominator = cards::ADSL,
id = USUBJID,
)
```
### Word (.docx)
The code below will save the table as a Word document using the default portrait orientation reference document.
``` r
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.
``` r
gtsummary::tbl_split_by_rows(tbl, row_numbers = seq(20, nrow(tbl), by = 20)) |>
save_docx(path = tempfile(fileext = ".docx"))
```
### HTML (.html)
The code below will save the table as a self-contained HTML file.
``` r
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.
``` r
gtsummary::tbl_split_by_rows(tbl, row_numbers = seq(20, nrow(tbl), by = 20)) |>
save_html(path = tempfile(fileext = ".html"))
```
### Plain text (.txt)
The code below will save the table as a plain text (Markdown-formatted) file.
``` r
tbl |>
save_txt(path = tempfile(fileext = ".txt"))
```
`save_txt()` also accepts `gt` tables directly.
``` r
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.
``` r
list(gt::gt(head(mtcars)), gt::gt(tail(mtcars))) |>
save_txt(path = tempfile(fileext = ".txt"))
```