Skip to content

Commit

Permalink
342 sorting (#368)
Browse files Browse the repository at this point in the history
**What changes are proposed in this pull request?**
* Style this entry in a way that can be copied directly into `NEWS.md`.
(#<issue number>, @<username>)

Provide more detail here as needed.

**Reference GitHub issue associated with pull request.** _e.g., 'closes
#<issue number>'_
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 ✅
- [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 ✅
- [x] Approve Pull Request
- [x] Merge the PR. Please use "Squash and merge" or "Rebase and merge".

---------

Co-authored-by: Daniel Sjoberg <[email protected]>
  • Loading branch information
bzkrouse and ddsjoberg authored Jan 14, 2025
1 parent 33f566f commit ff0dd76
Show file tree
Hide file tree
Showing 20 changed files with 218 additions and 182 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions R/ard_attributes.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ ard_attributes.data.frame <- function(data,
error = list(NULL)
) |>
cards::tidy_ard_column_order() |>
tidy_ard_row_order() |>
as_card()
}

Expand Down
8 changes: 5 additions & 3 deletions R/ard_categorical.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions R/ard_continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
8 changes: 2 additions & 6 deletions R/tidy_ard_order.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
1 change: 1 addition & 0 deletions R/tidy_as_ard.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,6 @@ tidy_as_ard <- function(lst_tidy,
!!!lst_ard_columns,
) |>
tidy_ard_column_order() |>
tidy_ard_row_order() |>
as_card()
}
2 changes: 1 addition & 1 deletion man/tidy_ard_order.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 25 additions & 45 deletions tests/testthat/_snaps/ard_categorical.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<data frame with counts>) works

Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/_snaps/ard_hierarchical.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
24 changes: 12 additions & 12 deletions tests/testthat/_snaps/ard_stack_hierarchical.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,18 @@
group1 group1_level group2 group2_level group3 group3_level variable variable_level stat_name stat_label stat
1 AESEV MILD <NA> <NA> AESOC GENERAL … n n 4
2 AESEV MILD <NA> <NA> AESOC SKIN AND… n n 1
3 AESEV MODERATE <NA> <NA> AESOC GENERAL … n n 0
4 AESEV MODERATE <NA> <NA> AESOC SKIN AND… n n 1
5 <NA> <NA> <NA> AESOC GENERAL … n n 4
6 <NA> <NA> <NA> AESOC SKIN AND… n n 2
7 AESEV MILD AESOC GENERAL … <NA> AEDECOD APPLICAT… n n 2
8 AESEV MILD AESOC GENERAL … <NA> AEDECOD APPLICAT… n n 2
9 AESEV MILD AESOC SKIN AND… <NA> AEDECOD ERYTHEMA n n 1
10 AESEV MILD AESOC SKIN AND… <NA> AEDECOD PRURITUS… n n 0
11 AESEV MODERATE AESOC GENERAL … <NA> AEDECOD APPLICAT… n n 0
12 AESEV MODERATE AESOC GENERAL … <NA> AEDECOD APPLICAT… n n 0
13 AESEV MODERATE AESOC SKIN AND… <NA> AEDECOD ERYTHEMA n n 0
14 AESEV MODERATE AESOC SKIN AND… <NA> AEDECOD PRURITUS… n n 1
3 AESEV MILD AESOC GENERAL … <NA> AEDECOD APPLICAT… n n 2
4 AESEV MILD AESOC GENERAL … <NA> AEDECOD APPLICAT… n n 2
5 AESEV MILD AESOC SKIN AND… <NA> AEDECOD ERYTHEMA n n 1
6 AESEV MILD AESOC SKIN AND… <NA> AEDECOD PRURITUS… n n 0
7 AESEV MODERATE <NA> <NA> AESOC GENERAL … n n 0
8 AESEV MODERATE <NA> <NA> AESOC SKIN AND… n n 1
9 AESEV MODERATE AESOC GENERAL … <NA> AEDECOD APPLICAT… n n 0
10 AESEV MODERATE AESOC GENERAL … <NA> AEDECOD APPLICAT… n n 0
11 AESEV MODERATE AESOC SKIN AND… <NA> AEDECOD ERYTHEMA n n 0
12 AESEV MODERATE AESOC SKIN AND… <NA> AEDECOD PRURITUS… n n 1
13 <NA> <NA> <NA> AESOC GENERAL … n n 4
14 <NA> <NA> <NA> AESOC SKIN AND… n n 2
15 AESOC GENERAL … <NA> <NA> AEDECOD APPLICAT… n n 2
16 AESOC GENERAL … <NA> <NA> AEDECOD APPLICAT… n n 2
17 AESOC SKIN AND… <NA> <NA> AEDECOD ERYTHEMA n n 1
Expand Down
Loading

0 comments on commit ff0dd76

Please sign in to comment.