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
6 changes: 6 additions & 0 deletions R/ard_pairwise.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ ard_pairwise <- function(data, variable, .f, include = NULL) {
}
)

# Assign attributes to the list of ARDs
attr(lst_ard, "args") <- list(
variable = variable,
include = include
)

# return result --------------------------------------------------------------
lst_ard
}
10 changes: 9 additions & 1 deletion R/ard_strata.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,16 @@ ard_strata <- function(.data, .by = NULL, .strata = NULL, .f, ...) {
}

# unnest ard data frame and return final table -------------------------------
df_nested_data |>
ard_full <- df_nested_data |>
tidyr::unnest(cols = all_of("ard")) |>
as_card(check = FALSE) |>
tidy_ard_column_order(group_order = "descending")

# append attributes ----------------------------------------------------------
attr(ard_full, "args") <- list(
by = .by,
strata = .strata
)

ard_full
}
12 changes: 12 additions & 0 deletions tests/testthat/test-ard_pairwise.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,15 @@ test_that("ard_pairwise(include) messaging", {
)
)
})

test_that("ard_pairwise() attaches 'args' attribute", {
res <- ard_pairwise(
data = mtcars,
variable = "am",
.f = \(x) ard_summary(x, variables = "mpg")
)

args <- attr(res, "args")
expect_equal(args$variable, "am")
expect_equal(args$include, c(0, 1))
})
28 changes: 25 additions & 3 deletions tests/testthat/test-ard_strata.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@ test_that("ard_strata() works", {
tidy_ard_column_order() |>
tidy_ard_row_order(),
ard_summary(ADSL, by = c(SEX, AGEGR1, ARM), variables = AGE) |>
tidy_ard_row_order()
tidy_ard_row_order(),
ignore_attr = TRUE
)
})

test_that("ard_strata(by,strata) when both empty", {
expect_equal(
ard_strata(ADSL, .f = ~ ard_summary(.x, variables = AGE)),
ard_summary(ADSL, variables = AGE)
ard_summary(ADSL, variables = AGE),
ignore_attr = TRUE
)

expect_equal(
ard_strata(ADSL, .f = ~ ard_summary(.x, by = ARM, variables = AGE)),
ard_summary(ADSL, by = ARM, variables = AGE)
ard_summary(ADSL, by = ARM, variables = AGE),
ignore_attr = TRUE
)
})

Expand Down Expand Up @@ -74,3 +77,22 @@ test_that("ard_strata computes stats for parameter specific strata", {

expect_snapshot(as.data.frame(tbl))
})


test_that("ard_strata() attaches 'args' attribute", {
# Test with both .by and .strata
res <- ard_strata(
ADSL,
.by = ARM,
.strata = SEX,
.f = ~ ard_summary(.x, variables = AGE)
)

expect_equal(attr(res, "args")$by, "ARM")
expect_equal(attr(res, "args")$strata, "SEX")

# Test with only .by
res_by <- ard_strata(ADSL, .by = ARM, .f = ~ ard_summary(.x, variables = AGE))
expect_equal(attr(res_by, "args")$by, "ARM")
expect_equal(attr(res_by, "args")$strata, character(0))
})
Loading