From d0333abe617c8e9d17640437b575a24d8a164106 Mon Sep 17 00:00:00 2001 From: Malan Bosman Date: Thu, 13 Aug 2026 15:09:48 +0200 Subject: [PATCH] Print the rows in x that do not appear in y in print.compare_ard() The branch reporting rows present in `x` but not in `y` built the data frame and discarded it, so the block was announced and never shown. The `rows_in_y_not_x` branch below it already printed. Adds snapshot tests for `print.compare_ard()`, which had no coverage. --- R/print.R | 2 +- tests/testthat/_snaps/print.md | 120 +++++++++++++++++++++++++++++++++ tests/testthat/test-print.R | 27 ++++++++ 3 files changed, 148 insertions(+), 1 deletion(-) diff --git a/R/print.R b/R/print.R index 467177dd4..4b6403410 100644 --- a/R/print.R +++ b/R/print.R @@ -182,7 +182,7 @@ print.compare_ard <- function(x, ...) { cli::cli_alert_success("No rows in {.arg x} that do not appear in {.arg y}.") } else { cli::cli_h3("Rows in {.arg x} that do not appear in {.arg y}.") - as.data.frame(x$rows_in_x_not_y) + as.data.frame(x$rows_in_x_not_y) |> print() } if (nrow(x$rows_in_y_not_x) == 0L) { cli::cli_alert_success("No rows in {.arg y} that do not appear in {.arg x}.") diff --git a/tests/testthat/_snaps/print.md b/tests/testthat/_snaps/print.md index 77e92b0a1..612ce604d 100644 --- a/tests/testthat/_snaps/print.md +++ b/tests/testthat/_snaps/print.md @@ -165,3 +165,123 @@ # i 4 more variables: context , stat_label , # fmt_fun , error +# print.compare_ard() works + + Code + compare_ard(ard, ard) + Message + The comparison `keys` are "group1", "group1_level", "variable", "variable_level", and "stat_name". + The comparison columns are "stat_label" and "stat". + + -- Mis-matched Rows ------------------------------------------------------------ + v No rows in `x` that do not appear in `y`. + v No rows in `y` that do not appear in `x`. + + -- Comparison Results ---------------------------------------------------------- + v No differences found in column "stat_label". + v No differences found in column "stat". + +--- + + Code + compare_ard(ard, ard_subset) + Message + The comparison `keys` are "group1", "group1_level", "variable", "variable_level", and "stat_name". + The comparison columns are "stat_label" and "stat". + + -- Mis-matched Rows ------------------------------------------------------------ + + -- Rows in `x` that do not appear in `y`. + Output + group1 group1_level variable variable_level stat_name + 1 ARM Placebo AGEGR1 >80 n + 2 ARM Placebo AGEGR1 >80 N + 3 ARM Placebo AGEGR1 >80 p + 4 ARM Xanomeline High Dose AGEGR1 >80 n + 5 ARM Xanomeline High Dose AGEGR1 >80 N + 6 ARM Xanomeline High Dose AGEGR1 >80 p + 7 ARM Xanomeline Low Dose AGEGR1 >80 n + 8 ARM Xanomeline Low Dose AGEGR1 >80 N + 9 ARM Xanomeline Low Dose AGEGR1 >80 p + Message + v No rows in `y` that do not appear in `x`. + + -- Comparison Results ---------------------------------------------------------- + v No differences found in column "stat_label". + ! Differences found in column "stat" for 12 rows. + Output + group1 group1_level variable variable_level stat_name stat.x + 1 ARM Placebo AGEGR1 65-80 N 86 + 2 ARM Placebo AGEGR1 65-80 p 0.4883721 + 3 ARM Placebo AGEGR1 <65 N 86 + 4 ARM Placebo AGEGR1 <65 p 0.1627907 + 5 ARM Xanomeline High Dose AGEGR1 65-80 N 84 + 6 ARM Xanomeline High Dose AGEGR1 65-80 p 0.6547619 + 7 ARM Xanomeline High Dose AGEGR1 <65 N 84 + 8 ARM Xanomeline High Dose AGEGR1 <65 p 0.1309524 + 9 ARM Xanomeline Low Dose AGEGR1 65-80 N 84 + 10 ARM Xanomeline Low Dose AGEGR1 65-80 p 0.5595238 + stat.y difference + 1 56 Mean relative difference: 0.3488372 + 2 0.75 Mean relative difference: 0.5357143 + 3 56 Mean relative difference: 0.3488372 + 4 0.25 Mean relative difference: 0.5357143 + 5 66 Mean relative difference: 0.2142857 + 6 0.8333333 Mean relative difference: 0.2727273 + 7 66 Mean relative difference: 0.2142857 + 8 0.1666667 Mean relative difference: 0.2727273 + 9 55 Mean relative difference: 0.3452381 + 10 0.8545455 Mean relative difference: 0.5272727 + +--- + + Code + compare_ard(ard_subset, ard) + Message + The comparison `keys` are "group1", "group1_level", "variable", "variable_level", and "stat_name". + The comparison columns are "stat_label" and "stat". + + -- Mis-matched Rows ------------------------------------------------------------ + v No rows in `x` that do not appear in `y`. + + -- Rows in `y` that do not appear in `x`. + Output + group1 group1_level variable variable_level stat_name + 1 ARM Placebo AGEGR1 >80 n + 2 ARM Placebo AGEGR1 >80 N + 3 ARM Placebo AGEGR1 >80 p + 4 ARM Xanomeline High Dose AGEGR1 >80 n + 5 ARM Xanomeline High Dose AGEGR1 >80 N + 6 ARM Xanomeline High Dose AGEGR1 >80 p + 7 ARM Xanomeline Low Dose AGEGR1 >80 n + 8 ARM Xanomeline Low Dose AGEGR1 >80 N + 9 ARM Xanomeline Low Dose AGEGR1 >80 p + Message + + -- Comparison Results ---------------------------------------------------------- + v No differences found in column "stat_label". + ! Differences found in column "stat" for 12 rows. + Output + group1 group1_level variable variable_level stat_name stat.x + 1 ARM Placebo AGEGR1 65-80 N 56 + 2 ARM Placebo AGEGR1 65-80 p 0.75 + 3 ARM Placebo AGEGR1 <65 N 56 + 4 ARM Placebo AGEGR1 <65 p 0.25 + 5 ARM Xanomeline High Dose AGEGR1 65-80 N 66 + 6 ARM Xanomeline High Dose AGEGR1 65-80 p 0.8333333 + 7 ARM Xanomeline High Dose AGEGR1 <65 N 66 + 8 ARM Xanomeline High Dose AGEGR1 <65 p 0.1666667 + 9 ARM Xanomeline Low Dose AGEGR1 65-80 N 55 + 10 ARM Xanomeline Low Dose AGEGR1 65-80 p 0.8545455 + stat.y difference + 1 86 Mean relative difference: 0.5357143 + 2 0.4883721 Mean relative difference: 0.3488372 + 3 86 Mean relative difference: 0.5357143 + 4 0.1627907 Mean relative difference: 0.3488372 + 5 84 Mean relative difference: 0.2727273 + 6 0.6547619 Mean relative difference: 0.2142857 + 7 84 Mean relative difference: 0.2727273 + 8 0.1309524 Mean relative difference: 0.2142857 + 9 84 Mean relative difference: 0.5272727 + 10 0.5595238 Mean relative difference: 0.3452381 + diff --git a/tests/testthat/test-print.R b/tests/testthat/test-print.R index 0cf596609..5e6b87efd 100644 --- a/tests/testthat/test-print.R +++ b/tests/testthat/test-print.R @@ -61,3 +61,30 @@ test_that("print.card() keeps non-NULL warning/error columns when narrow", { print(ard, width = 60) ) }) + +test_that("print.compare_ard() works", { + ard <- ard_tabulate(ADSL, by = "ARM", variables = "AGEGR1") + + # equal ARDs + expect_snapshot( + compare_ard(ard, ard) + ) + + # ARDs that differ in both their rows and their values: `x` holds the rows + # for the ">80" age group and `y` does not, and the percentages differ + ard_subset <- + ard_tabulate( + dplyr::filter(ADSL, AGEGR1 != ">80"), + by = "ARM", + variables = "AGEGR1" + ) + + expect_snapshot( + compare_ard(ard, ard_subset) + ) + + # rows in `y` that do not appear in `x` + expect_snapshot( + compare_ard(ard_subset, ard) + ) +})