From a71f0c8dd5ab24e082bb7592db29fa259cd653f6 Mon Sep 17 00:00:00 2001 From: Alanah Jonas <156101954+alanahjonas95@users.noreply.github.com> Date: Fri, 9 Oct 2026 11:01:07 +0100 Subject: [PATCH] Fix get_ard_statistics(.attributes) selecting columns by position .create_list_for_attributes() looped over seq_along(attributes), so the 1st, 2nd, ... ARD columns were attached rather than the columns named in .attributes. Loop over the names instead, and correct the test and snapshot that had recorded the wrong values. Closes #611 Co-Authored-By: Claude Sonnet 5.5 --- NEWS.md | 2 ++ R/get_ard_statistics.R | 2 +- tests/testthat/_snaps/get_ard_statistics.md | 20 +++++++------------- tests/testthat/test-get_ard_statistics.R | 18 +++++++++++++++++- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/NEWS.md b/NEWS.md index e3d349f78..d54c3a2aa 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ ## Bug Fixes +* Fixed a bug in `get_ard_statistics()` where the `.attributes` argument attached the values of the first columns of the ARD, rather than the values of the columns named in the argument. (#611, @alanahjonas95) + * Fixed a bug in `print.compare_ard()` where the rows present in `x` but not in `y` were announced and then not printed. (#605, @malanbos) * `print.compare_ard()` now prints its tables through the ARD print method rather than coercing them with `as.data.frame()`. Scalar list-column values (e.g. `group1_level`, `variable_level`, `stat`) are shown rather than collapsed, and long tables are truncated with the withheld row count reported in the footer. (#605) diff --git a/R/get_ard_statistics.R b/R/get_ard_statistics.R index 1c378a7ad..253ea1c97 100644 --- a/R/get_ard_statistics.R +++ b/R/get_ard_statistics.R @@ -77,7 +77,7 @@ get_ard_statistics <- function(x, #' cards:::.create_list_for_attributes(ard, c("group1", "group1_level"), 1) .create_list_for_attributes <- function(ard_subset, attributes, i) { ret <- list() - for (attr in seq_along(attributes)) { + for (attr in attributes) { ret <- c(ret, list(ard_subset[[attr]][[i]])) } stats::setNames(ret, nm = attributes) diff --git a/tests/testthat/_snaps/get_ard_statistics.md b/tests/testthat/_snaps/get_ard_statistics.md index 0434f933a..3db8d997b 100644 --- a/tests/testthat/_snaps/get_ard_statistics.md +++ b/tests/testthat/_snaps/get_ard_statistics.md @@ -18,27 +18,21 @@ Code get_ard_statistics(ard, group1_level %in% "Placebo", variable_level %in% - "65-80", .attributes = c("warning", "error")) + "65-80", .attributes = "stat_label") Output $n [1] 42 - attr(,"warning") - [1] "ARM" - attr(,"error") - [1] "Placebo" + attr(,"stat_label") + [1] "n" $N [1] 86 - attr(,"warning") - [1] "ARM" - attr(,"error") - [1] "Placebo" + attr(,"stat_label") + [1] "N" $p [1] 0.4883721 - attr(,"warning") - [1] "ARM" - attr(,"error") - [1] "Placebo" + attr(,"stat_label") + [1] "%" diff --git a/tests/testthat/test-get_ard_statistics.R b/tests/testthat/test-get_ard_statistics.R index 570d8791d..6bafb3d95 100644 --- a/tests/testthat/test-get_ard_statistics.R +++ b/tests/testthat/test-get_ard_statistics.R @@ -14,7 +14,23 @@ test_that("get_ard_statistics() works", { ard, group1_level %in% "Placebo", variable_level %in% "65-80", - .attributes = c("warning", "error") + .attributes = "stat_label" + ) + ) + + # attributes are taken from the requested columns, not by column position + expect_equal( + get_ard_statistics( + ard, + group1_level %in% "Placebo", + variable_level %in% "65-80", + .attributes = c("stat_label", "stat_name") + ) |> + lapply(attributes), + list( + n = list(stat_label = "n", stat_name = "n"), + N = list(stat_label = "N", stat_name = "N"), + p = list(stat_label = "%", stat_name = "p") ) ) })