11gt_tbl_single <- gt :: gt(head(mtcars ))
22gt_tbl_list <- list (gt :: gt(head(mtcars )), gt :: gt(tail(mtcars )))
3+ gtsummary_single <- gtsummary :: tbl_summary(gtsummary :: trial [, c(" age" , " trt" )])
4+ gtsummary_list <- list (
5+ gtsummary :: tbl_summary(gtsummary :: trial [, c(" age" , " trt" )]),
6+ gtsummary :: tbl_summary(gtsummary :: trial [, c(" grade" , " trt" )])
7+ )
38
49test_that(" save_txt() works with a single gt_tbl" , {
510 file_path <- tempfile(fileext = " .txt" )
@@ -15,7 +20,7 @@ test_that("save_txt() works with a single gt_tbl", {
1520 # setup chunk attaches gt
1621 expect_match(res [9 ], " library(gt)" , fixed = TRUE )
1722 # first table chunk renders correctly
18- expect_match(res [19 ], " as_kable_txt(x[[1]])" , fixed = TRUE )
23+ expect_match(res [23 ], " as_kable_txt(x[[1]])" , fixed = TRUE )
1924})
2025
2126test_that(" save_txt() works with a list of gt_tbl objects" , {
@@ -32,8 +37,8 @@ test_that("save_txt() works with a list of gt_tbl objects", {
3237 # setup chunk attaches gt
3338 expect_match(res [9 ], " library(gt)" , fixed = TRUE )
3439 # both table chunks rendered
35- expect_match(res [19 ], " as_kable_txt(x[[1]])" , fixed = TRUE )
36- expect_match(res [25 ], " as_kable_txt(x[[2]])" , fixed = TRUE )
40+ expect_match(res [23 ], " as_kable_txt(x[[1]])" , fixed = TRUE )
41+ expect_match(res [29 ], " as_kable_txt(x[[2]])" , fixed = TRUE )
3742})
3843
3944test_that(" save_txt() fails with incorrect inputs" , {
@@ -63,7 +68,7 @@ test_that("save_txt() fails with incorrect inputs", {
6368 )
6469})
6570
66- test_that(" save_txt() fails with non-gt_tbl objects" , {
71+ test_that(" save_txt() fails with non-gt_tbl/gtsummary objects" , {
6772 # flextable is not supported for txt output
6873 skip_if_not_installed(" flextable" )
6974 ft <- flextable :: flextable(head(mtcars ))
@@ -82,3 +87,69 @@ test_that("save_txt() works with .md extension", {
8287 )
8388 expect_true(file.exists(file_path ))
8489})
90+
91+ test_that(" save_txt() works with a single gtsummary object" , {
92+ skip_if_not_installed(" gtsummary" )
93+ file_path <- tempfile(fileext = " .txt" )
94+ expect_error(
95+ res <- gtsummary_single | >
96+ save_txt(path = file_path ),
97+ NA
98+ )
99+ expect_true(file.exists(file_path ))
100+
101+ # YAML header uses md_document output
102+ expect_match(res [3 ], " md_document" , fixed = TRUE )
103+ # setup chunk attaches both gt and gtsummary
104+ expect_match(res [9 ], " library(gt)" , fixed = TRUE )
105+ expect_match(res [10 ], " library(gtsummary)" , fixed = TRUE )
106+ # as_kable_txt dispatches on gtsummary class
107+ expect_match(res , " gtsummary::as_kable(obj)" , fixed = TRUE , all = FALSE )
108+ # first table chunk renders correctly
109+ expect_match(res [length(res ) - 1L ], " as_kable_txt(x[[1]])" , fixed = TRUE )
110+ })
111+
112+ test_that(" save_txt() works with a list of gtsummary objects" , {
113+ skip_if_not_installed(" gtsummary" )
114+ file_path <- tempfile(fileext = " .txt" )
115+ expect_error(
116+ res <- gtsummary_list | >
117+ save_txt(path = file_path ),
118+ NA
119+ )
120+ expect_true(file.exists(file_path ))
121+
122+ # both gt and gtsummary attached
123+ expect_match(res [9 ], " library(gt)" , fixed = TRUE )
124+ expect_match(res [10 ], " library(gtsummary)" , fixed = TRUE )
125+ # both table chunks rendered
126+ expect_match(res , " as_kable_txt(x[[1]])" , fixed = TRUE , all = FALSE )
127+ expect_match(res , " as_kable_txt(x[[2]])" , fixed = TRUE , all = FALSE )
128+ })
129+
130+ test_that(" save_txt() works with a mixed list of gt_tbl and gtsummary objects" , {
131+ skip_if_not_installed(" gtsummary" )
132+ mixed_list <- list (gt_tbl_single , gtsummary_single )
133+ file_path <- tempfile(fileext = " .txt" )
134+ expect_error(
135+ res <- mixed_list | >
136+ save_txt(path = file_path ),
137+ NA
138+ )
139+ expect_true(file.exists(file_path ))
140+
141+ # both packages attached because list contains a gtsummary object
142+ expect_match(res [9 ], " library(gt)" , fixed = TRUE )
143+ expect_match(res [10 ], " library(gtsummary)" , fixed = TRUE )
144+ # both chunks present
145+ expect_match(res , " as_kable_txt(x[[1]])" , fixed = TRUE , all = FALSE )
146+ expect_match(res , " as_kable_txt(x[[2]])" , fixed = TRUE , all = FALSE )
147+ })
148+
149+ test_that(" save_txt() only attaches gt when no gtsummary objects present" , {
150+ res <- save_txt(gt_tbl_single , path = tempfile(fileext = " .txt" ))
151+
152+ # only gt is attached — no gtsummary library() call in the header
153+ expect_match(res [9 ], " library(gt)" , fixed = TRUE )
154+ expect_false(any(grepl(" library(gtsummary)" , res , fixed = TRUE )))
155+ })
0 commit comments