Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions R/save_with_rmarkdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#' `reference_docx:` R markdown field.
#'
#' @returns x (invisibly)
#' @export
#'
#' @examples
#' # create table
Expand All @@ -25,13 +24,20 @@
#' id = USUBJID,
#' )
#'
#' # save as docx
#' # 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"))
#'
#' # save as docx with gt
#' tbl |>
#' save_with_rmarkdown(path = tempfile(fileext = ".docx"))
#'
#' # split the tqble and save paginatted table
#' gtsummary::tbl_split_by_rows(tbl, row_numbers = seq(20, nrow(tbl), by = 20)) |>
#' save_with_rmarkdown(path = tempfile(fileext = ".docx"))
#'
#' @export
save_with_rmarkdown <- function(x,
path,
reference_docx = get_reference_docx("portrait")) {
Expand Down Expand Up @@ -60,9 +66,8 @@ save_with_rmarkdown <- function(x,
# preparing for r markdown code vector ---------------------------------------
pkg_to_attach <-
ifelse(inherits(x, "list"), map(x, class), list(x)) |>
map_chr(class) |>
unlist() |>
intersect(accepted_table_classes())
map(\(xi) intersect(class(xi), accepted_table_classes())) |>
unlist()
pkg_to_attach <- ifelse(pkg_to_attach == "gt_tbl", "gt", pkg_to_attach)

# string of the yaml header
Expand Down
8 changes: 7 additions & 1 deletion man/save_with_rmarkdown.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions tests/testthat/test-save_with_rmarkdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,28 @@ test_that("save_with_rmarkdown() works with flextable", {
NA
)
})

test_that("save_with_rmarkdown() works with gtsummary table", {
tbl <-
cards::ADAE[1:150,] |>
gtsummary::tbl_hierarchical(
variables = c(AESOC, AETERM),
by = TRTA,
denominator = cards::ADSL,
id = USUBJID,
)

# test with a single table
expect_error(
tbl |>
save_with_rmarkdown(, path = tempfile(fileext = ".docx")),
NA
)

# test with a list of tables
expect_error(
gtsummary::tbl_split_by_rows(tbl, row_numbers = 20) |>
save_with_rmarkdown(, path = tempfile(fileext = ".docx")),
NA
)
})