Skip to content

Commit c882722

Browse files
MelkiadesBFalquetCopilot
authored
Fixing the class checks and some minors (#9)
We should get the defaults from other packages for this section :) --------- Signed-off-by: b_falquet <64274616+BFalquet@users.noreply.github.com> Co-authored-by: b_falquet <bfalquet@protonmail.com> Co-authored-by: b_falquet <64274616+BFalquet@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent a24c117 commit c882722

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

R/save_with_rmarkdown.R

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#' `reference_docx:` R markdown field.
1313
#'
1414
#' @returns x (invisibly)
15-
#' @export
1615
#'
1716
#' @examples
1817
#' # create table
@@ -25,13 +24,20 @@
2524
#' id = USUBJID,
2625
#' )
2726
#'
28-
#' # save as docx
27+
#' # save as docx with flextable
2928
#' gtsummary::as_flex_table(tbl) |>
29+
#' flextable::set_table_properties(layout = "autofit") |> # otherwise is going too wide
30+
#' save_with_rmarkdown(path = tempfile(fileext = ".docx"))
31+
#'
32+
#' # save as docx with gt
33+
#' tbl |>
3034
#' save_with_rmarkdown(path = tempfile(fileext = ".docx"))
3135
#'
32-
#' # split the tqble and save paginatted table
36+
#' # split the table and save paginated table
3337
#' gtsummary::tbl_split_by_rows(tbl, row_numbers = seq(20, nrow(tbl), by = 20)) |>
3438
#' save_with_rmarkdown(path = tempfile(fileext = ".docx"))
39+
#'
40+
#' @export
3541
save_with_rmarkdown <- function(x,
3642
path,
3743
reference_docx = get_reference_docx("portrait")) {
@@ -42,6 +48,9 @@ save_with_rmarkdown <- function(x,
4248
check_string(path)
4349
check_string(reference_docx, allow_empty = TRUE)
4450

51+
# convert path to absolute path to ensure output is created in the correct location
52+
path <- normalizePath(path, winslash = "/", mustWork = FALSE)
53+
4554
check_class(x, cls = c(accepted_table_classes(), "list"))
4655
# check each object in the list is a table
4756
if (inherits(x, "list") && some(x, ~!inherits(.x, accepted_table_classes()))) {
@@ -60,9 +69,8 @@ save_with_rmarkdown <- function(x,
6069
# preparing for r markdown code vector ---------------------------------------
6170
pkg_to_attach <-
6271
ifelse(inherits(x, "list"), map(x, class), list(x)) |>
63-
map_chr(class) |>
64-
unlist() |>
65-
intersect(accepted_table_classes())
72+
map(\(xi) intersect(class(xi), accepted_table_classes())) |>
73+
unlist()
6674
pkg_to_attach <- ifelse(pkg_to_attach == "gt_tbl", "gt", pkg_to_attach)
6775

6876
# string of the yaml header

man/save_with_rmarkdown.Rd

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-save_with_rmarkdown.R

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,50 @@ test_that("save_with_rmarkdown() works with flextable", {
99
)
1010

1111
# test with a single table
12+
file_path <- tempfile(fileext = ".docx")
1213
expect_error(
1314
gtsummary::as_flex_table(tbl) |>
14-
save_with_rmarkdown(, path = tempfile(fileext = ".docx")),
15+
save_with_rmarkdown(, path = file_path),
1516
NA
1617
)
18+
expect_true(file.exists(file_path))
1719

1820
# test with a list of tables
21+
file_path <- tempfile(fileext = ".docx")
1922
expect_error(
2023
gtsummary::tbl_split_by_rows(tbl, row_numbers = 20) |>
2124
map(gtsummary::as_flex_table) |>
22-
save_with_rmarkdown(, path = tempfile(fileext = ".docx")),
25+
save_with_rmarkdown(, path = file_path),
2326
NA
2427
)
28+
expect_true(file.exists(file_path))
29+
})
30+
31+
test_that("save_with_rmarkdown() works with gtsummary table", {
32+
tbl <-
33+
cards::ADAE[1:150,] |>
34+
gtsummary::tbl_hierarchical(
35+
variables = c(AESOC, AETERM),
36+
by = TRTA,
37+
denominator = cards::ADSL,
38+
id = USUBJID,
39+
)
40+
41+
# test with a single table
42+
file_path <- tempfile(fileext = ".docx")
43+
expect_error(
44+
tbl |>
45+
save_with_rmarkdown(, path = file_path),
46+
NA
47+
)
48+
expect_true(file.exists(file_path))
49+
50+
# test with a list of tables
51+
file_path <- tempfile(fileext = ".docx")
52+
expect_error(
53+
gtsummary::tbl_split_by_rows(tbl, row_numbers = 20) |>
54+
save_with_rmarkdown(, path = file_path),
55+
NA
56+
)
57+
expect_true(file.exists(file_path))
2558
})

0 commit comments

Comments
 (0)