save_with_rmarkdown.RdSave Input in a .docx file via R markdown.
save_with_rmarkdown(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_with_rmarkdown(path = tempfile(fileext = ".docx"))
#> ✔ Writing /tmp/RtmpcuAGiS/file1cae5d24a32a.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_with_rmarkdown(path = tempfile(fileext = ".docx"))
#> ✔ Writing /tmp/RtmpcuAGiS/file1cae409fb3b0.docx
# save as docx with gt
tbl |>
gtsummary::as_gt() |>
save_with_rmarkdown(path = tempfile(fileext = ".docx"))
#> ✔ Writing /tmp/RtmpcuAGiS/file1cae555b7fb5.docx
# Specify column width manually with gt
tbl |>
gtsummary::as_gt() |>
gt::cols_width(
dplyr::everything() ~ gt::px(100)
) |>
save_with_rmarkdown(path = tempfile(fileext = ".docx"))
#> ✔ Writing /tmp/RtmpcuAGiS/file1cae558c756a.docx
# split the table and save paginated table
gtsummary::tbl_split_by_rows(tbl, row_numbers = seq(20, nrow(tbl), by = 20)) |>
save_with_rmarkdown(path = tempfile(fileext = ".docx"))
#> ✔ Writing /tmp/RtmpcuAGiS/file1cae7e3d1a69.docx
# save ggplot as docx
library(ggplot2)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()
save_with_rmarkdown(p, path = tempfile(fileext = ".docx"))
#> ✔ Writing /tmp/RtmpcuAGiS/file1cae5b26aa94.docx