From ff0dd764275adc4204418b35a4611766713cefd5 Mon Sep 17 00:00:00 2001 From: Becca Krouse <14199771+bzkrouse@users.noreply.github.com> Date: Tue, 14 Jan 2025 14:40:30 -0500 Subject: [PATCH] 342 sorting (#368) **What changes are proposed in this pull request?** * Style this entry in a way that can be copied directly into `NEWS.md`. (#, @) Provide more detail here as needed. **Reference GitHub issue associated with pull request.** _e.g., 'closes #'_ closes #342 Also resolves #326 -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [x] **All** GitHub Action workflows pass with a :white_check_mark: - [x] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [x] If a bug was fixed, a unit test was added. - [x] Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): `devtools::test_coverage()` - [x] Request a reviewer Reviewer Checklist (if item does not apply, mark is as complete) - [x] If a bug was fixed, a unit test was added. - [x] Run `pkgdown::build_site()`. Check the R console for errors, and review the rendered website. - [x] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` When the branch is ready to be merged: - [x] Update `NEWS.md` with the changes from this pull request under the heading "`# cards (development version)`". If there is an issue associated with the pull request, reference it in parentheses at the end update (see `NEWS.md` for examples). - [x] **All** GitHub Action workflows pass with a :white_check_mark: - [x] Approve Pull Request - [x] Merge the PR. Please use "Squash and merge" or "Rebase and merge". --------- Co-authored-by: Daniel Sjoberg --- NEWS.md | 2 + R/ard_attributes.R | 1 + R/ard_categorical.R | 8 +- R/ard_continuous.R | 1 + R/tidy_ard_order.R | 8 +- R/tidy_as_ard.R | 1 + man/tidy_ard_order.Rd | 2 +- tests/testthat/_snaps/ard_categorical.md | 70 ++++------ tests/testthat/_snaps/ard_hierarchical.md | 8 +- .../testthat/_snaps/ard_stack_hierarchical.md | 24 ++-- tests/testthat/_snaps/bind_ard.md | 120 ++++++------------ tests/testthat/_snaps/print.md | 14 +- tests/testthat/_snaps/rename_ard_columns.md | 47 ++++--- tests/testthat/_snaps/tidy_ard_row_order.md | 24 ++++ tests/testthat/test-ard_categorical.R | 30 +++++ tests/testthat/test-ard_stack.R | 10 +- tests/testthat/test-ard_stack_hierarchical.R | 2 +- tests/testthat/test-bind_ard.R | 1 + tests/testthat/test-rename_ard_columns.R | 5 +- tests/testthat/test-tidy_ard_row_order.R | 22 ++++ 20 files changed, 218 insertions(+), 182 deletions(-) create mode 100644 tests/testthat/_snaps/tidy_ard_row_order.md create mode 100644 tests/testthat/test-tidy_ard_row_order.R diff --git a/NEWS.md b/NEWS.md index e122cf849..817dae778 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # cards 0.4.0.9012 +* Results are now sorted in a consistent manner, by descending groups and strata. (#342, #326) + # cards 0.4.0 ## New Features and Functions diff --git a/R/ard_attributes.R b/R/ard_attributes.R index 622fb293f..110562c68 100644 --- a/R/ard_attributes.R +++ b/R/ard_attributes.R @@ -99,6 +99,7 @@ ard_attributes.data.frame <- function(data, error = list(NULL) ) |> cards::tidy_ard_column_order() |> + tidy_ard_row_order() |> as_card() } diff --git a/R/ard_categorical.R b/R/ard_categorical.R index 663ad275f..9a0f672ad 100644 --- a/R/ard_categorical.R +++ b/R/ard_categorical.R @@ -201,6 +201,7 @@ ard_categorical.data.frame <- function(data, df_result_final |> dplyr::mutate(context = "categorical") |> tidy_ard_column_order() |> + tidy_ard_row_order() |> as_card() } @@ -410,11 +411,12 @@ ard_categorical.data.frame <- function(data, } df_table <- - dplyr::right_join( + dplyr::left_join( + df_original_strata |> dplyr::arrange(across(all_of(strata))), df_table, - df_original_strata, by = strata - ) + ) |> + dplyr::select(all_of(names(df_table))) } df_table diff --git a/R/ard_continuous.R b/R/ard_continuous.R index 07b5f2a29..505d0e8bf 100644 --- a/R/ard_continuous.R +++ b/R/ard_continuous.R @@ -175,6 +175,7 @@ ard_continuous.data.frame <- function(data, df_results |> dplyr::mutate(context = "continuous") |> tidy_ard_column_order() |> + tidy_ard_row_order() |> as_card() } diff --git a/R/tidy_ard_order.R b/R/tidy_ard_order.R index c62e87b14..ec576cefa 100644 --- a/R/tidy_ard_order.R +++ b/R/tidy_ard_order.R @@ -5,7 +5,7 @@ #' #' - `tidy_ard_column_order()` relocates columns of the ARD to the standard order. #' -#' - `tidy_ard_row_order()` orders rows of ARD according to variables, groups, and +#' - `tidy_ard_row_order()` orders rows of ARD according to groups and #' strata, while retaining the order of the input ARD. #' #' @param x (`data.frame`)\cr @@ -55,14 +55,10 @@ tidy_ard_column_order <- function(x) { tidy_ard_row_order <- function(x) { set_cli_abort_call() - check_class(x, cls = "card") - # get columns that dictate ordering cols <- x |> dplyr::select( - all_ard_variables("names"), - all_ard_groups("names"), - all_ard_groups("levels") + all_ard_groups(c("names", "levels")) ) |> names() diff --git a/R/tidy_as_ard.R b/R/tidy_as_ard.R index 78c5771d3..66599bae4 100644 --- a/R/tidy_as_ard.R +++ b/R/tidy_as_ard.R @@ -112,5 +112,6 @@ tidy_as_ard <- function(lst_tidy, !!!lst_ard_columns, ) |> tidy_ard_column_order() |> + tidy_ard_row_order() |> as_card() } diff --git a/man/tidy_ard_order.Rd b/man/tidy_ard_order.Rd index a0ce3388b..3a80d8ef0 100644 --- a/man/tidy_ard_order.Rd +++ b/man/tidy_ard_order.Rd @@ -21,7 +21,7 @@ an ARD data frame of class 'card' ARD functions for relocating columns and rows to the standard order. \itemize{ \item \code{tidy_ard_column_order()} relocates columns of the ARD to the standard order. -\item \code{tidy_ard_row_order()} orders rows of ARD according to variables, groups, and +\item \code{tidy_ard_row_order()} orders rows of ARD according to groups and strata, while retaining the order of the input ARD. } } diff --git a/tests/testthat/_snaps/ard_categorical.md b/tests/testthat/_snaps/ard_categorical.md index 0620aaf62..01ee08197 100644 --- a/tests/testthat/_snaps/ard_categorical.md +++ b/tests/testthat/_snaps/ard_categorical.md @@ -91,56 +91,36 @@ "p") ~ "n (pct)"))), stat_name %in% c("n", "p")), variable, stat_name, stat_label)) Output - variable stat_name stat_label - 1 AGEGR1 n n (pct) - 2 AGEGR1 p n (pct) - 19 SEX n n - 20 SEX p % + variable stat_name stat_label + 1 AGEGR1 n n (pct) + 2 AGEGR1 p n (pct) + 7 SEX n n + 8 SEX p % # ard_categorical(denominator='row') works Code - as.data.frame(dplyr::select(apply_fmt_fn(ard_with_args), -fmt_fn, -warning, - -error)) + as.data.frame(dplyr::select(apply_fmt_fn(ard_with_args), -fmt_fn, -warning, -error)) Output - group1 group1_level variable variable_level context stat_name - 1 ARM Placebo AGEGR1 65-80 categorical n - 2 ARM Placebo AGEGR1 65-80 categorical N - 3 ARM Xanomeline High Dose AGEGR1 65-80 categorical n - 4 ARM Xanomeline High Dose AGEGR1 65-80 categorical N - 5 ARM Xanomeline Low Dose AGEGR1 65-80 categorical n - 6 ARM Xanomeline Low Dose AGEGR1 65-80 categorical N - 7 ARM Placebo AGEGR1 <65 categorical n - 8 ARM Placebo AGEGR1 <65 categorical N - 9 ARM Xanomeline High Dose AGEGR1 <65 categorical n - 10 ARM Xanomeline High Dose AGEGR1 <65 categorical N - 11 ARM Xanomeline Low Dose AGEGR1 <65 categorical n - 12 ARM Xanomeline Low Dose AGEGR1 <65 categorical N - 13 ARM Placebo AGEGR1 >80 categorical n - 14 ARM Placebo AGEGR1 >80 categorical N - 15 ARM Xanomeline High Dose AGEGR1 >80 categorical n - 16 ARM Xanomeline High Dose AGEGR1 >80 categorical N - 17 ARM Xanomeline Low Dose AGEGR1 >80 categorical n - 18 ARM Xanomeline Low Dose AGEGR1 >80 categorical N - stat_label stat stat_fmt - 1 n 42 42.00 - 2 N 144 144 - 3 n 55 55.00 - 4 N 144 144 - 5 n 47 47.00 - 6 N 144 144 - 7 n 14 14.00 - 8 N 33 33 - 9 n 11 11.00 - 10 N 33 33 - 11 n 8 8.00 - 12 N 33 33 - 13 n 30 30.00 - 14 N 77 77 - 15 n 18 18.00 - 16 N 77 77 - 17 n 29 29.00 - 18 N 77 77 + group1 group1_level variable variable_level context stat_name stat_label stat stat_fmt + 1 ARM Placebo AGEGR1 65-80 categorical n n 42 42.00 + 2 ARM Placebo AGEGR1 65-80 categorical N N 144 144 + 3 ARM Placebo AGEGR1 <65 categorical n n 14 14.00 + 4 ARM Placebo AGEGR1 <65 categorical N N 33 33 + 5 ARM Placebo AGEGR1 >80 categorical n n 30 30.00 + 6 ARM Placebo AGEGR1 >80 categorical N N 77 77 + 7 ARM Xanomeline High Dose AGEGR1 65-80 categorical n n 55 55.00 + 8 ARM Xanomeline High Dose AGEGR1 65-80 categorical N N 144 144 + 9 ARM Xanomeline High Dose AGEGR1 <65 categorical n n 11 11.00 + 10 ARM Xanomeline High Dose AGEGR1 <65 categorical N N 33 33 + 11 ARM Xanomeline High Dose AGEGR1 >80 categorical n n 18 18.00 + 12 ARM Xanomeline High Dose AGEGR1 >80 categorical N N 77 77 + 13 ARM Xanomeline Low Dose AGEGR1 65-80 categorical n n 47 47.00 + 14 ARM Xanomeline Low Dose AGEGR1 65-80 categorical N N 144 144 + 15 ARM Xanomeline Low Dose AGEGR1 <65 categorical n n 8 8.00 + 16 ARM Xanomeline Low Dose AGEGR1 <65 categorical N N 33 33 + 17 ARM Xanomeline Low Dose AGEGR1 >80 categorical n n 29 29.00 + 18 ARM Xanomeline Low Dose AGEGR1 >80 categorical N N 77 77 # ard_categorical(denominator=) works diff --git a/tests/testthat/_snaps/ard_hierarchical.md b/tests/testthat/_snaps/ard_hierarchical.md index c6a62aa92..12cbe7648 100644 --- a/tests/testthat/_snaps/ard_hierarchical.md +++ b/tests/testthat/_snaps/ard_hierarchical.md @@ -28,9 +28,9 @@ {cards} data frame: 1 x 15 Output group1 group1_level group2 group2_level group3 group3_level variable - 1 TRTA Placebo AESEV MILD AESOC GASTROIN… AEDECOD + 1 TRTA Placebo AESEV MILD AESOC CARDIAC … AEDECOD variable_level stat_name stat_label stat - 1 ABDOMINA… n n 0 + 1 ATRIAL F… n n 0 Message i 4 more variables: context, fmt_fn, warning, error @@ -48,9 +48,9 @@ {cards} data frame: 1 x 15 Output group1 group1_level group2 group2_level group3 group3_level variable - 1 TRTA Placebo AESEV MILD AESOC GASTROIN… AEDECOD + 1 TRTA Placebo AESEV MILD AESOC CARDIAC … AEDECOD variable_level stat_name stat_label stat - 1 ABDOMINA… n n 0 + 1 ATRIAL F… n n 0 Message i 4 more variables: context, fmt_fn, warning, error diff --git a/tests/testthat/_snaps/ard_stack_hierarchical.md b/tests/testthat/_snaps/ard_stack_hierarchical.md index 373e75896..8ba274f54 100644 --- a/tests/testthat/_snaps/ard_stack_hierarchical.md +++ b/tests/testthat/_snaps/ard_stack_hierarchical.md @@ -116,18 +116,18 @@ group1 group1_level group2 group2_level group3 group3_level variable variable_level stat_name stat_label stat 1 AESEV MILD AESOC GENERAL … n n 4 2 AESEV MILD AESOC SKIN AND… n n 1 - 3 AESEV MODERATE AESOC GENERAL … n n 0 - 4 AESEV MODERATE AESOC SKIN AND… n n 1 - 5 AESOC GENERAL … n n 4 - 6 AESOC SKIN AND… n n 2 - 7 AESEV MILD AESOC GENERAL … AEDECOD APPLICAT… n n 2 - 8 AESEV MILD AESOC GENERAL … AEDECOD APPLICAT… n n 2 - 9 AESEV MILD AESOC SKIN AND… AEDECOD ERYTHEMA n n 1 - 10 AESEV MILD AESOC SKIN AND… AEDECOD PRURITUS… n n 0 - 11 AESEV MODERATE AESOC GENERAL … AEDECOD APPLICAT… n n 0 - 12 AESEV MODERATE AESOC GENERAL … AEDECOD APPLICAT… n n 0 - 13 AESEV MODERATE AESOC SKIN AND… AEDECOD ERYTHEMA n n 0 - 14 AESEV MODERATE AESOC SKIN AND… AEDECOD PRURITUS… n n 1 + 3 AESEV MILD AESOC GENERAL … AEDECOD APPLICAT… n n 2 + 4 AESEV MILD AESOC GENERAL … AEDECOD APPLICAT… n n 2 + 5 AESEV MILD AESOC SKIN AND… AEDECOD ERYTHEMA n n 1 + 6 AESEV MILD AESOC SKIN AND… AEDECOD PRURITUS… n n 0 + 7 AESEV MODERATE AESOC GENERAL … n n 0 + 8 AESEV MODERATE AESOC SKIN AND… n n 1 + 9 AESEV MODERATE AESOC GENERAL … AEDECOD APPLICAT… n n 0 + 10 AESEV MODERATE AESOC GENERAL … AEDECOD APPLICAT… n n 0 + 11 AESEV MODERATE AESOC SKIN AND… AEDECOD ERYTHEMA n n 0 + 12 AESEV MODERATE AESOC SKIN AND… AEDECOD PRURITUS… n n 1 + 13 AESOC GENERAL … n n 4 + 14 AESOC SKIN AND… n n 2 15 AESOC GENERAL … AEDECOD APPLICAT… n n 2 16 AESOC GENERAL … AEDECOD APPLICAT… n n 2 17 AESOC SKIN AND… AEDECOD ERYTHEMA n n 1 diff --git a/tests/testthat/_snaps/bind_ard.md b/tests/testthat/_snaps/bind_ard.md index 390ff0d77..9ed1d5f74 100644 --- a/tests/testthat/_snaps/bind_ard.md +++ b/tests/testthat/_snaps/bind_ard.md @@ -18,96 +18,56 @@ # bind_ard() .order argument works Code - dplyr::select(as.data.frame(bind_ard(ard_categorical(ADSL, by = "ARM", - variables = "SEX") %>% { + dplyr::select(as.data.frame(bind_ard(ard_categorical(ADSL, by = "ARM", variables = "SEX") %>% { dplyr::slice(., sample.int(nrow(.))) }, .order = TRUE)), -c(context, fmt_fn, warning, error)) Output - group1 group1_level variable variable_level stat_name stat_label - 1 ARM Xanomeline Low Dose SEX M n n - 2 ARM Xanomeline Low Dose SEX M p % - 3 ARM Xanomeline Low Dose SEX F p % - 4 ARM Xanomeline Low Dose SEX F N N - 5 ARM Xanomeline Low Dose SEX F n n - 6 ARM Xanomeline Low Dose SEX M N N - 7 ARM Xanomeline High Dose SEX F p % - 8 ARM Xanomeline High Dose SEX F n n - 9 ARM Xanomeline High Dose SEX M p % - 10 ARM Xanomeline High Dose SEX M n n - 11 ARM Xanomeline High Dose SEX M N N - 12 ARM Xanomeline High Dose SEX F N N - 13 ARM Placebo SEX M N N - 14 ARM Placebo SEX M p % - 15 ARM Placebo SEX F n n - 16 ARM Placebo SEX F N N - 17 ARM Placebo SEX F p % - 18 ARM Placebo SEX M n n - stat - 1 34 - 2 0.4047619 - 3 0.5952381 - 4 84 - 5 50 - 6 84 - 7 0.4761905 - 8 40 - 9 0.5238095 - 10 44 - 11 84 - 12 84 - 13 86 - 14 0.3837209 - 15 53 - 16 86 - 17 0.6162791 - 18 33 + group1 group1_level variable variable_level stat_name stat_label stat + 1 ARM Xanomeline Low Dose SEX M n n 34 + 2 ARM Xanomeline Low Dose SEX M p % 0.4047619 + 3 ARM Xanomeline Low Dose SEX F p % 0.5952381 + 4 ARM Xanomeline Low Dose SEX M N N 84 + 5 ARM Xanomeline Low Dose SEX F n n 50 + 6 ARM Xanomeline Low Dose SEX F N N 84 + 7 ARM Placebo SEX M p % 0.3837209 + 8 ARM Placebo SEX M n n 33 + 9 ARM Placebo SEX F n n 53 + 10 ARM Placebo SEX F N N 86 + 11 ARM Placebo SEX F p % 0.6162791 + 12 ARM Placebo SEX M N N 86 + 13 ARM Xanomeline High Dose SEX M N N 84 + 14 ARM Xanomeline High Dose SEX M p % 0.5238095 + 15 ARM Xanomeline High Dose SEX F p % 0.4761905 + 16 ARM Xanomeline High Dose SEX F N N 84 + 17 ARM Xanomeline High Dose SEX F n n 40 + 18 ARM Xanomeline High Dose SEX M n n 44 --- Code - dplyr::select(as.data.frame(bind_ard(ard_categorical(ADSL, by = "ARM", - variables = "SEX") %>% { + dplyr::select(as.data.frame(bind_ard(ard_categorical(ADSL, by = "ARM", variables = "SEX") %>% { dplyr::slice(., sample.int(nrow(.))) }, .order = FALSE)), -c(context, fmt_fn, warning, error)) Output - group1 group1_level variable variable_level stat_name stat_label - 1 ARM Placebo SEX F p % - 2 ARM Xanomeline High Dose SEX M N N - 3 ARM Xanomeline High Dose SEX F n n - 4 ARM Xanomeline Low Dose SEX F n n - 5 ARM Xanomeline Low Dose SEX F p % - 6 ARM Placebo SEX F n n - 7 ARM Placebo SEX M n n - 8 ARM Placebo SEX M N N - 9 ARM Xanomeline High Dose SEX F p % - 10 ARM Xanomeline High Dose SEX F N N - 11 ARM Xanomeline High Dose SEX M p % - 12 ARM Placebo SEX F N N - 13 ARM Xanomeline Low Dose SEX M p % - 14 ARM Placebo SEX M p % - 15 ARM Xanomeline Low Dose SEX F N N - 16 ARM Xanomeline High Dose SEX M n n - 17 ARM Xanomeline Low Dose SEX M N N - 18 ARM Xanomeline Low Dose SEX M n n - stat - 1 0.6162791 - 2 84 - 3 40 - 4 50 - 5 0.5952381 - 6 53 - 7 33 - 8 86 - 9 0.4761905 - 10 84 - 11 0.5238095 - 12 86 - 13 0.4047619 - 14 0.3837209 - 15 84 - 16 44 - 17 84 - 18 34 + group1 group1_level variable variable_level stat_name stat_label stat + 1 ARM Placebo SEX F p % 0.6162791 + 2 ARM Xanomeline Low Dose SEX F N N 84 + 3 ARM Placebo SEX M n n 33 + 4 ARM Xanomeline High Dose SEX F n n 40 + 5 ARM Xanomeline High Dose SEX F p % 0.4761905 + 6 ARM Placebo SEX F n n 53 + 7 ARM Xanomeline High Dose SEX M n n 44 + 8 ARM Xanomeline High Dose SEX M N N 84 + 9 ARM Placebo SEX M p % 0.3837209 + 10 ARM Placebo SEX M N N 86 + 11 ARM Xanomeline Low Dose SEX F p % 0.5952381 + 12 ARM Placebo SEX F N N 86 + 13 ARM Xanomeline Low Dose SEX M p % 0.4047619 + 14 ARM Xanomeline High Dose SEX M p % 0.5238095 + 15 ARM Xanomeline High Dose SEX F N N 84 + 16 ARM Xanomeline Low Dose SEX F n n 50 + 17 ARM Xanomeline Low Dose SEX M N N 84 + 18 ARM Xanomeline Low Dose SEX M n n 34 # bind_ard(.distinct) diff --git a/tests/testthat/_snaps/print.md b/tests/testthat/_snaps/print.md index b9b5beb23..56ce94c16 100644 --- a/tests/testthat/_snaps/print.md +++ b/tests/testthat/_snaps/print.md @@ -32,13 +32,13 @@ 1 ARM Placebo AGEGR1 65-80 n n 42 2 ARM Placebo AGEGR1 65-80 N N 86 3 ARM Placebo AGEGR1 65-80 p % 0.488 - 4 ARM Xanomeli… AGEGR1 65-80 n n 55 - 5 ARM Xanomeli… AGEGR1 65-80 N N 84 - 6 ARM Xanomeli… AGEGR1 65-80 p % 0.655 - 7 ARM Xanomeli… AGEGR1 65-80 n n 47 - 8 ARM Xanomeli… AGEGR1 65-80 N N 84 - 9 ARM Xanomeli… AGEGR1 65-80 p % 0.56 - 10 ARM Placebo AGEGR1 <65 n n 14 + 4 ARM Placebo AGEGR1 <65 n n 14 + 5 ARM Placebo AGEGR1 <65 N N 86 + 6 ARM Placebo AGEGR1 <65 p % 0.163 + 7 ARM Placebo AGEGR1 >80 n n 30 + 8 ARM Placebo AGEGR1 >80 N N 86 + 9 ARM Placebo AGEGR1 >80 p % 0.349 + 10 ARM Xanomeli… AGEGR1 65-80 n n 55 Message i 17 more rows i Use `print(n = ...)` to see more rows diff --git a/tests/testthat/_snaps/rename_ard_columns.md b/tests/testthat/_snaps/rename_ard_columns.md index ede57863d..8347d30e1 100644 --- a/tests/testthat/_snaps/rename_ard_columns.md +++ b/tests/testthat/_snaps/rename_ard_columns.md @@ -1,24 +1,37 @@ # rename_ard_columns(unlist) Code - rename_ard_columns(apply_fmt_fn(ard_categorical(ADSL, by = ARM, variables = AGEGR1)), - unlist = c(stat, stat_fmt)) + dplyr::select(as.data.frame(rename_ard_columns(apply_fmt_fn(ard_categorical(ADSL, by = ARM, variables = AGEGR1)), + unlist = c(stat, stat_fmt))), -c(fmt_fn, warning, error)) Output - # A tibble: 27 x 10 - ARM AGEGR1 context stat_name stat_label stat stat_fmt fmt_fn warning - - 1 Placebo 65-80 catego~ n n 42 42 - 2 Placebo 65-80 catego~ N N 86 86 - 3 Placebo 65-80 catego~ p % 0.488 48.8 - 4 Xanomelin~ 65-80 catego~ n n 55 55 - 5 Xanomelin~ 65-80 catego~ N N 84 84 - 6 Xanomelin~ 65-80 catego~ p % 0.655 65.5 - 7 Xanomelin~ 65-80 catego~ n n 47 47 - 8 Xanomelin~ 65-80 catego~ N N 84 84 - 9 Xanomelin~ 65-80 catego~ p % 0.560 56.0 - 10 Placebo <65 catego~ n n 14 14 - # i 17 more rows - # i 1 more variable: error + ARM AGEGR1 context stat_name stat_label stat stat_fmt + 1 Placebo 65-80 categorical n n 42.0000000 42 + 2 Placebo 65-80 categorical N N 86.0000000 86 + 3 Placebo 65-80 categorical p % 0.4883721 48.8 + 4 Placebo <65 categorical n n 14.0000000 14 + 5 Placebo <65 categorical N N 86.0000000 86 + 6 Placebo <65 categorical p % 0.1627907 16.3 + 7 Placebo >80 categorical n n 30.0000000 30 + 8 Placebo >80 categorical N N 86.0000000 86 + 9 Placebo >80 categorical p % 0.3488372 34.9 + 10 Xanomeline High Dose 65-80 categorical n n 55.0000000 55 + 11 Xanomeline High Dose 65-80 categorical N N 84.0000000 84 + 12 Xanomeline High Dose 65-80 categorical p % 0.6547619 65.5 + 13 Xanomeline High Dose <65 categorical n n 11.0000000 11 + 14 Xanomeline High Dose <65 categorical N N 84.0000000 84 + 15 Xanomeline High Dose <65 categorical p % 0.1309524 13.1 + 16 Xanomeline High Dose >80 categorical n n 18.0000000 18 + 17 Xanomeline High Dose >80 categorical N N 84.0000000 84 + 18 Xanomeline High Dose >80 categorical p % 0.2142857 21.4 + 19 Xanomeline Low Dose 65-80 categorical n n 47.0000000 47 + 20 Xanomeline Low Dose 65-80 categorical N N 84.0000000 84 + 21 Xanomeline Low Dose 65-80 categorical p % 0.5595238 56.0 + 22 Xanomeline Low Dose <65 categorical n n 8.0000000 8 + 23 Xanomeline Low Dose <65 categorical N N 84.0000000 84 + 24 Xanomeline Low Dose <65 categorical p % 0.0952381 9.5 + 25 Xanomeline Low Dose >80 categorical n n 29.0000000 29 + 26 Xanomeline Low Dose >80 categorical N N 84.0000000 84 + 27 Xanomeline Low Dose >80 categorical p % 0.3452381 34.5 # rename_ard_columns(unlist) messaging diff --git a/tests/testthat/_snaps/tidy_ard_row_order.md b/tests/testthat/_snaps/tidy_ard_row_order.md new file mode 100644 index 000000000..59176f9c8 --- /dev/null +++ b/tests/testthat/_snaps/tidy_ard_row_order.md @@ -0,0 +1,24 @@ +# tidy_ard_row_order() works + + Code + dplyr::select(ard_categorical(data.frame(x1 = sample(LETTERS[1:5], 30, replace = TRUE), x2 = sample(LETTERS[6:10], 30, + replace = TRUE), x3 = sample(LETTERS[11:15], 30, replace = TRUE), zz = 1L, aa = 1L), by = x1:x3, variables = c(zz, aa), + statistic = everything() ~ "n"), all_ard_groups(), all_ard_variables()) + Message + {cards} data frame: 250 x 8 + Output + group1 group1_level group2 group2_level group3 group3_level variable variable_level + 1 x1 A x2 F x3 K zz 1 + 2 x1 A x2 F x3 K aa 1 + 3 x1 A x2 F x3 L zz 1 + 4 x1 A x2 F x3 L aa 1 + 5 x1 A x2 F x3 M zz 1 + 6 x1 A x2 F x3 M aa 1 + 7 x1 A x2 F x3 N zz 1 + 8 x1 A x2 F x3 N aa 1 + 9 x1 A x2 F x3 O zz 1 + 10 x1 A x2 F x3 O aa 1 + Message + i 240 more rows + i Use `print(n = ...)` to see more rows + diff --git a/tests/testthat/test-ard_categorical.R b/tests/testthat/test-ard_categorical.R index 76ff0b635..6c30dbdd2 100644 --- a/tests/testthat/test-ard_categorical.R +++ b/tests/testthat/test-ard_categorical.R @@ -344,6 +344,7 @@ test_that("ard_categorical(denominator='cell') works", { }) test_that("ard_categorical(denominator='row') works", { + withr::local_options(list(width = 120)) expect_error( ard_crosstab_row <- ard_categorical(ADSL, variables = "AGEGR1", by = "ARM", denominator = "row"), NA @@ -886,3 +887,32 @@ test_that("ard_categorical() errors with incomplete factor columns", { ard_categorical(variables = am) ) }) + +test_that("ard_categorical() ordering for multiple strata", { + adae_mini <- ADAE |> + dplyr::select(USUBJID, TRTA, AESOC, AEDECOD) |> + dplyr::filter(AESOC %in% unique(AESOC)[1:4]) |> + dplyr::group_by(AESOC) |> + dplyr::filter(AEDECOD %in% unique(AEDECOD)[1:5]) |> + dplyr::ungroup() + + res_actual <- ard_categorical( + adae_mini |> unique() |> dplyr::mutate(any_ae = TRUE), + strata = c(AESOC, AEDECOD), + by = TRTA, + variables = any_ae + ) |> + dplyr::select(group2_level, group3_level) |> + tidyr::unnest(everything()) |> + unique() + + + expect_equal( + res_actual, + adae_mini |> + dplyr::select(group2_level = AESOC, group3_level = AEDECOD) |> + unique() |> + dplyr::arrange(group2_level, group3_level), + ignore_attr = TRUE + ) +}) diff --git a/tests/testthat/test-ard_stack.R b/tests/testthat/test-ard_stack.R index 0e8990336..18e068e19 100644 --- a/tests/testthat/test-ard_stack.R +++ b/tests/testthat/test-ard_stack.R @@ -98,9 +98,9 @@ test_that("ard_stack() adding overalls", { bind_ard( ard_continuous(data = mtcars, by = "cyl", variables = "mpg"), ard_dichotomous(data = mtcars, by = "cyl", variables = "vs"), - ard_categorical(data = mtcars, variables = "cyl"), ard_continuous(data = mtcars, variables = "mpg"), ard_dichotomous(data = mtcars, variables = "vs"), + ard_categorical(data = mtcars, variables = "cyl"), .update = TRUE, .order = TRUE ) @@ -127,9 +127,9 @@ test_that("ard_stack() adding missing/attributes", { bind_ard( ard_continuous(data = mtcars, by = "cyl", variables = "mpg"), ard_dichotomous(data = mtcars, by = "cyl", variables = "vs"), - ard_categorical(data = mtcars, variables = "cyl"), ard_missing(data = mtcars, by = "cyl", variables = c("mpg", "vs")), - ard_attributes(mtcars, variables = c("cyl", "mpg", "vs")), + ard_categorical(data = mtcars, variables = "cyl"), + ard_attributes(mtcars, variables = c("mpg", "vs", "cyl")), .update = TRUE, .order = TRUE ) @@ -157,9 +157,9 @@ test_that("ard_stack() adding missing/attributes", { ard_missing(data = mtcars, by = "cyl", variables = c("mpg", "vs")), ard_continuous(data = mtcars, variables = "mpg"), ard_dichotomous(data = mtcars, variables = "vs"), - ard_missing(data = mtcars, variables = c("mpg", "vs")), ard_categorical(data = mtcars, variables = "cyl"), - ard_attributes(mtcars, variables = c("cyl", "mpg", "vs")), + ard_missing(data = mtcars, variables = c("mpg", "vs")), + ard_attributes(mtcars, variables = c("mpg", "vs", "cyl")), .update = TRUE, .order = TRUE ) diff --git a/tests/testthat/test-ard_stack_hierarchical.R b/tests/testthat/test-ard_stack_hierarchical.R index 215c654de..7f96719d8 100644 --- a/tests/testthat/test-ard_stack_hierarchical.R +++ b/tests/testthat/test-ard_stack_hierarchical.R @@ -535,7 +535,7 @@ test_that("ard_stack_hierarchical_count(attributes)", { dplyr::filter(context %in% "attributes") |> dplyr::select(-all_missing_columns()), ADAE_small |> - ard_attributes(variables = c(AESOC, AEDECOD, TRTA)) |> + ard_attributes(variables = c(TRTA, AESOC, AEDECOD)) |> dplyr::select(-all_missing_columns()) ) }) diff --git a/tests/testthat/test-bind_ard.R b/tests/testthat/test-bind_ard.R index 3ed0ec2cc..8ebd16940 100644 --- a/tests/testthat/test-bind_ard.R +++ b/tests/testthat/test-bind_ard.R @@ -23,6 +23,7 @@ test_that("ARD helpers messaging", { }) test_that("bind_ard() .order argument works", { + withr::local_options(list(width = 120)) withr::local_seed(1123) expect_snapshot( bind_ard( diff --git a/tests/testthat/test-rename_ard_columns.R b/tests/testthat/test-rename_ard_columns.R index f5c414956..4d70abe38 100644 --- a/tests/testthat/test-rename_ard_columns.R +++ b/tests/testthat/test-rename_ard_columns.R @@ -10,11 +10,14 @@ test_that("rename_ard_columns(columns)", { }) test_that("rename_ard_columns(unlist)", { + withr::local_options(list(width = 120)) expect_snapshot( ADSL |> ard_categorical(by = ARM, variables = AGEGR1) |> apply_fmt_fn() |> - rename_ard_columns(unlist = c(stat, stat_fmt)) + rename_ard_columns(unlist = c(stat, stat_fmt)) |> + as.data.frame() |> + dplyr::select(-c(fmt_fn, warning, error)) ) }) diff --git a/tests/testthat/test-tidy_ard_row_order.R b/tests/testthat/test-tidy_ard_row_order.R new file mode 100644 index 000000000..bc6965a3f --- /dev/null +++ b/tests/testthat/test-tidy_ard_row_order.R @@ -0,0 +1,22 @@ +test_that("tidy_ard_row_order() works", { + skip_if_not(is_pkg_installed("withr")) + withr::local_options(list(width = 120)) + withr::local_seed(1) + + # ensure rows are ordered within descending groups but not variables + expect_snapshot( + data.frame( + x1 = sample(LETTERS[1:5], 30, replace = TRUE), + x2 = sample(LETTERS[6:10], 30, replace = TRUE), + x3 = sample(LETTERS[11:15], 30, replace = TRUE), + zz = 1L, + aa = 1L + ) |> + ard_categorical( + by = x1:x3, + variables = c(zz, aa), + statistic = everything() ~ "n" + ) |> + dplyr::select(all_ard_groups(), all_ard_variables()) + ) +})