Skip to content

Commit

Permalink
Ensuring tbl_ard_*() function can ingest ARDs from their tbl_*()
Browse files Browse the repository at this point in the history
…counterparts (#1996)

* updates

* Update DESCRIPTION
  • Loading branch information
ddsjoberg authored Sep 25, 2024
1 parent d9b1952 commit 34b2339
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ Depends:
R (>= 4.2)
Imports:
cards (>= 0.2.2.9009),
cli (>= 3.6.1),
cli (>= 3.6.3),
dplyr (>= 1.1.3),
glue (>= 1.6.2),
gt (>= 0.10.0),
lifecycle (>= 1.0.3),
rlang (>= 1.1.1),
tidyr (>= 1.3.0),
vctrs
vctrs (>= 0.6.4)
Suggests:
aod (>= 1.3.3),
broom (>= 1.0.5),
Expand Down
5 changes: 5 additions & 0 deletions R/tbl_continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ tbl_continuous <- function(data,


.add_gts_column_to_cards_continuous <- function(cards, variables, by) {
if ("gts_column" %in% names(cards)) {
cli::cli_inform("The {.val gts_column} column is alread present. Defining the column has been skipped.")
return(cards)
}

# adding the name of the column the stats will populate
if (is_empty(by)) {
cards$gts_column <-
Expand Down
5 changes: 5 additions & 0 deletions R/tbl_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,11 @@ tbl_summary <- function(data,
}

.add_gts_column_to_cards_summary <- function(cards, variables, by) {
if ("gts_column" %in% names(cards)) {
cli::cli_inform("The {.val gts_column} column is alread present. Defining the column has been skipped.")
return(cards)
}

# adding the name of the column the stats will populate
if (is_empty(by)) {
cards$gts_column <-
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-tbl_ard_continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,25 @@ test_that("tbl_ard_continuous(value) messaging", {
tbl_ard_continuous(by = "trt", variable = "age", include = "grade", value = grade ~ letters)
)
})

test_that("tbl_ard_continuous() existing 'gts_column'", {
# test there is no error when passing an ARD with an existing 'gts_column'
tbl <-
tbl_continuous(
data = trial,
variable = age,
by = trt,
include = grade
)
expect_equal(
tbl_ard_continuous(
cards = tbl$cards[[1]],
variable = age,
by = trt,
include = grade
) |>
modify_header(all_stat_cols() ~ "**{level}** \nN = {n}") |>
as.data.frame(),
as.data.frame(tbl)
)
})
18 changes: 17 additions & 1 deletion tests/testthat/test-tbl_ard_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ test_that("tbl_ard_summary(by) messaging", {
tbl_ard_summary(by = trt)
)

# when ARD is stratified, but `by` arg not specified
# when ARD is stratified, but `by` arg not specified
expect_snapshot(
error = TRUE,
cards::ard_stack(
Expand Down Expand Up @@ -281,3 +281,19 @@ test_that("tbl_ard_summary(overall)", {
)
})

test_that("tbl_ard_summary() existing 'gts_column'", {
# test there is no error when passing an ARD with an existing 'gts_column'
tbl <- tbl_summary(trial, by = trt, include = c(age, grade, response))
expect_equal(
tbl_ard_summary(
cards = tbl$cards[[1]],
include = c(age, grade, response),
by = trt,
missing = "ifany"
) |>
modify_header(all_stat_cols() ~ "**{level}** \nN = {n}") |>
as.data.frame(),
as.data.frame(tbl)
)
})

16 changes: 16 additions & 0 deletions tests/testthat/test-tbl_ard_wide_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,19 @@ test_that("tbl_ard_summary(label) argument works", {
"Updated AGE!"
)
})

test_that("tbl_ard_wide_summary() existing 'gts_column'", {
# test there is no error when passing an ARD with an existing 'gts_column'
tbl <-
trial |>
tbl_wide_summary(include = c(response, grade))
expect_equal(
tbl_ard_wide_summary(
cards = tbl$cards[[1]],
include = c(response, grade)
) |>
as.data.frame(),
as.data.frame(tbl)
)
})

0 comments on commit 34b2339

Please sign in to comment.