save_docx.RdSave input in a .docx file via R markdown.
save_docx(x, path, reference_docx = get_reference_docx("portrait"))(gtsummary/gt_tbl/flextable/ggplot/grob/list)
object of class 'gtsummary', 'gt_tbl' (gt table), 'flextable', 'ggplot',
'grob', or a list of these objects.
(path)
path to save file to, e.g. "rendered_table.docx"
(path)
path to reference document that when not NULL is passed to the
reference_docx: R markdown field.
(invisibly) a string corresponding to the content of the intermediate .rmd file that is rendered as
.docx.
# create table
tbl <-
cards::ADAE[1:150, ] |>
gtsummary::tbl_hierarchical(
variables = c(AESOC, AETERM),
by = TRTA,
denominator = cards::ADSL,
id = USUBJID,
)
# save as docx with flextable
gtsummary::as_flex_table(tbl) |>
flextable::set_table_properties(layout = "autofit") |> # otherwise is going too wide
save_docx(path = tempfile(fileext = ".docx"))
#> ✔ Writing /tmp/RtmpVH4Fyk/file1a96da841af.docx
# Specify column width manually with flextable
gtsummary::as_flex_table(tbl) |>
flextable::width(j = 1, width = 2) |>
flextable::width(j = 2:3, width = 1.5) |>
save_docx(path = tempfile(fileext = ".docx"))
#> ✔ Writing /tmp/RtmpVH4Fyk/file1a9671046120.docx
# save as docx with gt
tbl |>
gtsummary::as_gt() |>
save_docx(path = tempfile(fileext = ".docx"))
#> ✔ Writing /tmp/RtmpVH4Fyk/file1a96294b55c.docx
# Specify column width manually with gt
tbl |>
gtsummary::as_gt() |>
gt::cols_width(
dplyr::everything() ~ gt::px(100)
) |>
save_docx(path = tempfile(fileext = ".docx"))
#> ✔ Writing /tmp/RtmpVH4Fyk/file1a963e832d63.docx
# split the table and save paginated table
gtsummary::tbl_split_by_rows(tbl, row_numbers = seq(20, nrow(tbl), by = 20)) |>
save_docx(path = tempfile(fileext = ".docx"))
#> ✔ Writing /tmp/RtmpVH4Fyk/file1a96282c6692.docx
# save ggplot as docx
library(ggplot2)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()
save_docx(p, path = tempfile(fileext = ".docx"))
#> ✔ Writing /tmp/RtmpVH4Fyk/file1a96581ed9b2.docx