Skip to content

Commit 42a5ba9

Browse files
authored
Updated order of grouping variables in ARDs (#394)
In most cases, the order of the grouping variables doesn't really matter. Two exceptions are the hierarchical ARD and the results from `ard_strata()`. Rather than changing all the ARDs to have the grouping begin with the largest group (which is counterintuitive for the hierarchical ARDs), I think we can just change `ard_strata()` results. **What changes are proposed in this pull request?** * Added the `all_ard_group_n(types)` argument to allow separate selection of `groupX` and `groupX_level` columns. * Added the `tidy_ard_column_order(group_order)` argument that allows users to specify whether the grouping variables are listed in ascending order (the default) or descending order. The output of `ard_strata()` now calls `tidy_ard_column_order(group_order="descending")`. I made some other small changes in the hierarchical functions that don't use positional matching of the columns, and rather relies on the column names. I think it's just a bit more robust to potential changes in the future. **Reference GitHub issue associated with pull request.** _e.g., 'closes #<issue number>'_ closes #366 -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [ ] If a bug was fixed, a unit test was added. - [ ] Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): `devtools::test_coverage()` - [ ] Request a reviewer Reviewer Checklist (if item does not apply, mark is as complete) - [ ] If a bug was fixed, a unit test was added. - [ ] Run `pkgdown::build_site()`. Check the R console for errors, and review the rendered website. - [ ] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` When the branch is ready to be merged: - [ ] 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). - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge". _Optional Reverse Dependency Checks_: Install `checked` with `pak::pak("Genentech/checked")` or `pak::pak("checked")` ```shell # Check dev versions of `cardx`, `gtsummary`, and `tfrmt` which are in the `ddsjoberg` R Universe Rscript -e "options(checked.check_envvars = c(NOT_CRAN = TRUE)); checked::check_rev_deps(path = '.', n = parallel::detectCores() - 2L, repos = c('https://ddsjoberg.r-universe.dev', 'https://cloud.r-project.org'))" # Check CRAN reverse dependencies but run tests skipped on CRAN Rscript -e "options(checked.check_envvars = c(NOT_CRAN = TRUE)); checked::check_rev_deps(path = '.', n = parallel::detectCores() - 2, repos = 'https://cloud.r-project.org')" # Check CRAN reverse dependencies in a CRAN-like environment Rscript -e "options(checked.check_envvars = c(NOT_CRAN = FALSE), checked.check_build_args = '--as-cran'); checked::check_rev_deps(path = '.', n = parallel::detectCores() - 2, repos = 'https://cloud.r-project.org')" ```
1 parent ee80777 commit 42a5ba9

15 files changed

+170
-128
lines changed

NEWS.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
# cards 0.4.0.9032
22

3-
* Results are now sorted in a consistent manner, by descending groups and strata. (#342, #326)
3+
## New Features and Functions
44

55
* Added functions `rename_ard_groups_shift()` and `rename_ard_groups_reverse()` for renaming the grouping variables in the ARD. (#344)
66

7+
* Added an option to specify the default rounding in the package: `cards.round_type`. See `?cards.options` for details. (#384)
8+
79
* Added the `print_ard_conditions(condition_type)` argument, which allows users to select to return conditions as messages (the default), or have warnings returned as warnings and errors as errors. (#386)
810

9-
* Added an option to specify the default rounding in the package: `cards.round_type`. See `?cards.options` for details. (#384)
11+
* Added the `all_ard_group_n(types)` argument to allow separate selection of `groupX` and `groupX_level` columns.
12+
13+
* Added the `tidy_ard_column_order(group_order)` argument that allows users to specify whether the grouping variables are listed in ascending order (the default) or descending order. The output of `ard_strata()` now calls `tidy_ard_column_order(group_order="descending")`.
14+
15+
## Other Updates
16+
17+
* Results are now sorted in a consistent manner, by descending groups and strata. (#342, #326)
18+
19+
## Lifecycle Updates
1020

1121
* Function `label_cards()` has been renamed to `label_round()`, which more clearly communicates that is returns a rounding function.
1222

R/ard_hierarchical.R

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ ard_hierarchical.data.frame <- function(data,
135135
)
136136

137137
# renaming columns -----------------------------------------------------------
138-
df_result <- .rename_last_group_as_variable(df_result)
138+
df_result <- .rename_last_group_as_variable(df_result, by = by, variables = variables)
139139

140140
# return ard -----------------------------------------------------------------
141141
df_result |>
@@ -177,7 +177,7 @@ ard_hierarchical_count.data.frame <- function(data,
177177
fmt_fn = fmt_fn,
178178
stat_label = stat_label
179179
) |>
180-
.rename_last_group_as_variable() |>
180+
.rename_last_group_as_variable(by = by, variables = variables) |>
181181
dplyr::mutate(context = "hierarchical_count") |>
182182
as_card()
183183
}
@@ -196,15 +196,12 @@ ard_hierarchical_count.data.frame <- function(data,
196196
#' @examples
197197
#' data <- data.frame(x = 1, y = 2, group1 = 3, group2 = 4)
198198
#'
199-
#' cards:::.rename_last_group_as_variable(data)
200-
.rename_last_group_as_variable <- function(df_result) {
201-
df_result <- dplyr::select(df_result, -all_ard_variables())
202-
203-
last_two_grouping_columns <-
204-
dplyr::select(df_result, all_ard_groups()) |>
205-
names() |>
206-
utils::tail(n = 2L) |>
207-
stats::setNames(c("variable", "variable_level"))
208-
209-
dplyr::rename(df_result, !!!last_two_grouping_columns)
199+
#' cards:::.rename_last_group_as_variable(data, by = "ARM", variables = "AESOC")
200+
.rename_last_group_as_variable <- function(df_result, by, variables) {
201+
df_result |>
202+
dplyr::select(-all_ard_variables()) |>
203+
dplyr::rename(
204+
variable = all_ard_group_n(n = length(c(by, variables)), types = "names"),
205+
variable_level = all_ard_group_n(n = length(c(by, variables)), types = "levels")
206+
)
210207
}

R/ard_strata.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,5 @@ ard_strata <- function(.data, .by = NULL, .strata = NULL, .f, ...) {
8383
df_nested_data |>
8484
tidyr::unnest(cols = all_of("ard")) |>
8585
as_card() |>
86-
tidy_ard_column_order()
86+
tidy_ard_column_order(group_order = "descending")
8787
}

R/selectors.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,15 @@ all_ard_variables <- function(types = c("names", "levels")) {
6363

6464
#' @export
6565
#' @rdname selectors
66-
all_ard_group_n <- function(n) {
66+
all_ard_group_n <- function(n, types = c("names", "levels")) {
67+
types <- arg_match(types, values = c("names", "levels"), multiple = TRUE)
68+
69+
group_cols <- character(0L)
70+
if ("names" %in% types) group_cols <- c(group_cols, paste0("group", n)) # styler: off
71+
if ("levels" %in% types) group_cols <- c(group_cols, paste0("group", n, "_level")) # styler: off
72+
6773
check_integerish(n)
68-
any_of(sort(c(paste0("group", n), paste0("group", n, "_level"))))
74+
any_of(sort(group_cols))
6975
}
7076

7177
#' @export

R/tidy_ard_order.R

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
#' - `tidy_ard_column_order()` relocates columns of the ARD to the standard order.
77
#'
88
#' - `tidy_ard_row_order()` orders rows of ARD according to groups and
9-
#' strata, while retaining the order of the input ARD.
9+
#' strata (group 1, then group2, etc), while retaining the column order of the input ARD.
1010
#'
1111
#' @param x (`data.frame`)\cr
1212
#' an ARD data frame of class 'card'
13+
#' @param group_order (`string`)\cr
14+
#' specifies the ordering of the grouping variables.
15+
#' Must be one of `c("ascending", "descending")`.
16+
#' Default is `"ascending"`, where grouping variables begin with `"group1"` variables,
17+
#' followed by `"group2"` variables, etc.
1318
#'
1419
#' @return an ARD data frame of class 'card'
1520
#' @name tidy_ard_order
@@ -28,14 +33,23 @@ NULL
2833

2934
#' @rdname tidy_ard_order
3035
#' @export
31-
tidy_ard_column_order <- function(x) {
36+
tidy_ard_column_order <- function(x, group_order = c("ascending", "descending")) {
3237
set_cli_abort_call()
38+
group_order <- arg_match(group_order)
3339

34-
group_cols <- dplyr::select(x, all_ard_groups()) |>
35-
names() |>
36-
sort()
37-
group_cols <- group_cols[order(str_extract_all(group_cols, "\\d+") |> unlist() |> as.integer())]
40+
# specify the ordering the grouping variables
41+
group_cols <-
42+
data.frame(colname = dplyr::select(x, all_ard_groups()) |> names()) |>
43+
dplyr::arrange(
44+
case_switch(
45+
group_order == "ascending" ~ as.integer(unlist(str_extract_all(.data$colname, "\\d+"))),
46+
group_order == "descending" ~ dplyr::desc(as.integer(unlist(str_extract_all(.data$colname, "\\d+"))))
47+
),
48+
.data$colname
49+
) |>
50+
dplyr::pull("colname")
3851

52+
# selecting the columns in the tidy order
3953
dplyr::select(
4054
x,
4155
all_of(group_cols),
@@ -57,10 +71,15 @@ tidy_ard_row_order <- function(x) {
5771

5872
# get columns that dictate ordering
5973
cols <- x |>
60-
dplyr::select(
61-
all_ard_groups(c("names", "levels"))
62-
) |>
74+
dplyr::select(all_ard_groups(c("names", "levels"))) |>
6375
names()
76+
if (!is_empty(cols)) {
77+
max_group_n <- as.integer(unlist(str_extract_all(cols, "\\d+"))) |> max()
78+
cols <-
79+
map(seq_len(max_group_n), ~ c(paste0("group", .x), paste0("group", .x, "_level"))) |>
80+
unlist() |>
81+
intersect(cols)
82+
}
6483

6584
# perform the ordering
6685
x |> dplyr::arrange(across(all_of(cols), .fns = function(x) match(x, unique(x))))

0 commit comments

Comments
 (0)