Save a gtsummary table or a flextable to a Word (.docx) file using the
flextable package.
The header and footer arguments control where the table caption and the
footnote-region content (footnotes, source notes, and abbreviations) are
placed in the Word document. When TRUE, this content is moved to the Word
document's page header and footer regions (where it repeats on every
page) instead of appearing in the flow of the table itself. When FALSE, the
content is rendered as part of the table, matching as_flex_table().
A collection of tables is also accepted: a tbl_split object (from
tbl_split_by_rows() or tbl_split_by_columns()), or a plain list of
flextables. Each table is written to its own Word section so that each table's
caption and footnote-region content populate that section's own header/footer
regions, one table per page.
A flextable object (or a list of them) is also accepted. Its caption
(flextable::set_caption()) is relocated to the Word header and its footer
part (flextable::add_footer_lines()) to the Word footer, matching the
gtsummary behavior.
Usage
save_flex_docx(
x,
path,
header = FALSE,
footer = FALSE,
page = NULL,
page_location = c("footer-right", "footer-center", "footer-left", "header-right",
"header-center", "header-left"),
pr_section = NULL,
...
)Arguments
- x
(
gtsummary,tbl_split,flextable, orlist)
a gtsummary table, atbl_splitobject (a list of gtsummary tables), aflextableobject, or a plain list offlextableobjects- path
(
string)
file path to write the Word (.docx) file to- header
(scalar
logical)
whether to place the table caption in the Word document's page header region. WhenFALSE, the caption is rendered as the table caption. Default isFALSE.(scalar
logical)
whether to place the footnotes, source notes, and abbreviations in the Word document's page footer region. WhenFALSE, this content is rendered in the table's footer. Default isFALSE.- page
(
string)
an optional string added as a page-number line in a header/footer region. The tokens{PAGE}and{NUMPAGES}are replaced with live Word fields for the current page number and total page count (e.g."Page {PAGE} of {NUMPAGES}"); all other text is added verbatim. Default isNULL(no page line). This is independent of theheader/footerarguments.- page_location
(
string)
where to place thepagetext, as"<region>-<alignment>". Must be one of"footer-right"(default),"footer-center","footer-left","header-right","header-center", or"header-left".- pr_section
(
officer::prop_section)
an optionalofficer::prop_section()object used as the base Word section, giving fine-grained control over page margins, page size, orientation, and section columns (e.g.officer::prop_section(page_margins = officer::page_mar(top = 0.5))). The section's header and footer regions are always managed bysave_flex_docx()(the relocated caption and notes), so anyheader_default/footer_defaultset onpr_sectionare ignored. For a collection (tbl_splitor a list of flextables) the samepr_sectionis applied to every table's section, and the pagingtypeis fixed to"nextPage"(anytypeonpr_sectionis ignored) so tables page correctly. Overrides thesave_flex_docx-lst:pr_sectiontheme element. Default isNULL.- ...
These dots are for future extensions and must be empty.
Limitations
Relocating the caption to the Word header (header = TRUE) and the
footnotes, source notes, and abbreviations to the Word footer
(footer = TRUE) is lossy.
Relocated content is rendered as plain text: markdown
and HTML are not interpreted and emphasis markers (**bold**, _italic_)
are stripped. Only the font family and size are carried over; colors,
indentation, per-cell styling, and column structure are not preserved.
Examples
tbl <-
trial |>
tbl_summary(by = trt, include = c(age, grade)) |>
modify_caption("**Table 1. Patient Characteristics**")
# save the table, placing caption in the header and notes in the footer
save_flex_docx(tbl, path = tempfile(fileext = ".docx"))
# add a "Page X of Y" line to the footer
save_flex_docx(
tbl,
path = tempfile(fileext = ".docx"),
page = "Page {PAGE} of {NUMPAGES}"
)
# a split table is written with one table per section/page
trial |>
tbl_summary(by = trt, include = c(age, marker, grade), missing = ~"no") |>
modify_footnote_body(
"Footnotes only appear on the pages where the mark is present",
columns = "label",
rows = label == "Age"
) |>
tbl_split_by_rows(variables = marker) |>
save_flex_docx(path = tempfile(fileext = ".docx"))
# a flextable (or a list of flextables) is also accepted
ft <-
as_flex_table(tbl) |>
flextable::set_caption("Table 1")
save_flex_docx(ft, path = tempfile(fileext = ".docx"))
# customize the Word page margins and orientation via a prop_section()
save_flex_docx(
tbl,
path = tempfile(fileext = ".docx"),
pr_section = officer::prop_section(
page_margins = officer::page_mar(top = 0.5, bottom = 0.5),
page_size = officer::page_size(orient = "landscape")
)
)
