Skip to content

Commit 753cc8c

Browse files
authored
Merge pull request #5 from humanpred/feature/allow-table1-in-tab-list
Allow table1 objects in new_tab_list()
2 parents 9a9b761 + 8fc614b commit 753cc8c

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ RoxygenNote: 7.3.2
1313
Imports:
1414
knitr,
1515
vctrs
16-
Suggests:
16+
Suggests:
1717
dplyr,
1818
glue,
1919
pander,
2020
rmarkdown,
2121
spelling,
22+
table1,
2223
testthat (>= 3.0.0),
2324
tibble,
2425
tidyr

R/objects.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ new_tab_list <- function(x) {
1919
if (!inherits(x, "list")) {
2020
stop("`x` must be a list")
2121
}
22-
x_null <- vapply(X = x, FUN = is.null, FUN.VALUE = TRUE)
23-
x_df <- vapply(X = x, FUN = inherits, "data.frame", FUN.VALUE = TRUE)
24-
if (!all(x_null | x_df)) {
25-
stop("The contents of 'x' must be NULL or a 'data.frame'-like object")
22+
x_null <- vapply(X = x, FUN = is.null, FUN.VALUE = TRUE)
23+
x_df <- vapply(X = x, FUN = inherits, "data.frame", FUN.VALUE = TRUE)
24+
x_table1 <- vapply(X = x, FUN = inherits, "table1", FUN.VALUE = TRUE)
25+
if (!all(x_null | x_df | x_table1)) {
26+
stop("The contents of 'x' must be NULL, a 'data.frame'-like object, or a 'table1' object")
2627
}
2728
vctrs::new_vctr(x, class = "tab_list")
2829
}

tests/testthat/test-objects.R

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,24 @@ test_that("new_tab_list", {
1515
)
1616
expect_error(
1717
new_tab_list(list("A")),
18-
regexp = "The contents of 'x' must be NULL or a 'data.frame'-like object"
18+
regexp = "The contents of 'x' must be NULL, a 'data.frame'-like object, or a 'table1' object"
1919
)
2020
})
2121

22+
test_that("new_tab_list accepts table1 objects", {
23+
skip_if_not_installed("table1")
24+
d <- data.frame(x = c(1, 2, 3), g = c("a", "a", "b"))
25+
t1 <- table1::table1(~ x | g, data = d)
26+
expect_s3_class(t1, "table1")
27+
28+
new_tl <- new_tab_list(list(t1))
29+
expect_s3_class(new_tl, "tab_list")
30+
31+
new_tt <- new_tab_tibble(tibble::tibble(table = list(t1), caption = "Test table1"))
32+
expect_s3_class(new_tt, "tab_tibble")
33+
expect_s3_class(new_tt$table, "tab_list")
34+
})
35+
2236
test_that("vctrs methods", {
2337
d_tab_prep <- tidyr::nest(mtcars, table = !"cyl")
2438
d_tab_prep <- dplyr::mutate(d_tab_prep, caption = glue::glue("Cars with {cyl} cylinders"))

0 commit comments

Comments
 (0)