-
-
Notifications
You must be signed in to change notification settings - Fork 12
modified sort_ard_hierarchical function #550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
2a6700f
b19ad25
330dbb2
72bc6fb
1e76dfc
eaa1c9c
b31c8ad
b42a717
b748c36
20089c3
e7945fa
9dd1e58
ba31ef2
4ab5bb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| Sys.getenv() | ||
| library(cards) | ||
| library(gtsummary) | ||
| ard <- ard_tabulate( | ||
| data = cards::ADAE, | ||
| variables = c(AESOC, AEDECOD), | ||
| denominator = cards::ADSL | ||
| ) | ||
| ae_tbl <- tbl_ard_summary( | ||
| ard, | ||
| by = TRTA | ||
| ) | ||
| ard <- ard_tabulate( | ||
| data = cards::ADAE, | ||
| by = TRTA | ||
| variables = c(AESOC, AEDECOD), | ||
| ard <- ard_tabulate( | ||
| data = cards::ADAE, | ||
| by = TRTA, | ||
| variables = c(AESOC, AEDECOD), | ||
| denominator = cards::ADSL | ||
| ) | ||
| ae_tbl <- tbl_ard_summary( | ||
| ard, | ||
| by = TRTA | ||
| ) | ||
| ae_tbl | ||
| ae_tbl <- tbl_ard_summary( | ||
| ard, | ||
| by = TRTA, | ||
| statistic = "{n} (p)}" | ||
| ) | ||
| ae_tbl <- tbl_ard_summary( | ||
| ard, | ||
| by = TRTA, | ||
| statistic = "{n} ({p})" | ||
| ) | ||
| ae_tbl <- tbl_ard_summary( | ||
| ard, | ||
| by = TRTA, | ||
| statistic = list(all_categorical() ~ "{n} ({p})") | ||
| ) | ||
| ae_tbl | ||
| ae_tbl <- tbl_ard_summary( | ||
| ard, | ||
| by = TRTA, | ||
| statistic = list(all_categorical() ~ "{n} ({p})") | ||
| ) |> | ||
| sort_ard_hierarchical() | ||
| ard <- ard_tabulate( | ||
| data = cards::ADAE, | ||
| by = TRTA, | ||
| variables = c(AESOC, AEDECOD), | ||
| denominator = cards::ADSL | ||
| ) |> | ||
| sort_ard_hierarchical() | ||
| ard <- ard_stack_hierarchical( | ||
| data = cards::ADAE, | ||
| by = TRTA, | ||
| variables = c(AESOC, AEDECOD), | ||
| denominator = cards::ADSL, | ||
| id = USUBJID | ||
| ) |> | ||
| sort_ard_hierarchical() | ||
| ae_tbl <- tbl_ard_summary( | ||
| ard, | ||
| by = TRTA, | ||
| statistic = list(all_categorical() ~ "{n} ({p})") | ||
| ) | ||
| ae_tbl <- tbl_ard_hierarchical( | ||
| ard, | ||
| by = TRTA, | ||
| statistic = list(all_categorical() ~ "{n} ({p})") | ||
| ) | ||
| ae_tbl <- tbl_ard_hierarchical( | ||
| ard, | ||
| by = TRTA, | ||
| variables = c(AESOC, AEDECOD), | ||
| statistic = list(all_categorical() ~ "{n} ({p})") | ||
| ) | ||
| ae_tbl | ||
| ard <- ard_stack_hierarchical( | ||
| data = cards::ADAE, | ||
| by = TRTA, | ||
| variables = c(AESOC, AEDECOD), | ||
| denominator = cards::ADSL, | ||
| statistic = list(all_categorical() ~ "{n} ({p})"), | ||
| id = USUBJID | ||
| ) |> | ||
| sort_ard_hierarchical() | ||
| ae_tbl <- tbl_ard_hierarchical( | ||
| ard, | ||
| by = TRTA, | ||
| variables = c(AESOC, AEDECOD), | ||
| statistic = list(all_categorical() ~ "{n} ({p})") | ||
| ) | ||
| ae_tbl | ||
| ard <- ard_stack_hierarchical( | ||
| data = cards::ADAE, | ||
| by = TRTA, | ||
| variables = c(AESOC, AEDECOD), | ||
| denominator = cards::ADSL, | ||
| id = USUBJID | ||
| ) |> | ||
| sort_ard_hierarchical() | ||
| ae_tbl <- tbl_ard_hierarchical( | ||
| ard, | ||
| by = TRTA, | ||
| variables = c(AESOC, AEDECOD) | ||
| ) | ||
| ae_tbl | ||
| devtools::load_all() | ||
| getwd() | ||
| setwd("D:/ARDs/cards") | ||
| getwd() | ||
| devtools::load_all() | ||
| ard <- ard_stack_hierarchical( | ||
| data = cards::ADAE, | ||
| by = TRTA, | ||
| variables = c(AESOC, AEDECOD), | ||
| denominator = cards::ADSL, | ||
| statistic = list(all_categorical() ~ "{n} ({p})"), | ||
| id = USUBJID | ||
| ) |> | ||
| sort_ard_hierarchical() | ||
| ADSL | ||
| table(ADSL$TRTA) | ||
| ard <- ard_stack_hierarchical( | ||
| data = cards::ADAE, | ||
| by = TRTA, | ||
| variables = c(AESOC, AEDECOD), | ||
| denominator = cards::ADSL, | ||
| statistic = list(all_categorical() ~ "{n} ({p})"), | ||
| id = USUBJID | ||
| ) |> | ||
| sort_ard_hierarchical(sort_col = Placebo) | ||
| ard <- ard_stack_hierarchical( | ||
| data = cards::ADAE, | ||
| by = TRTA, | ||
| variables = c(AESOC, AEDECOD), | ||
| denominator = cards::ADSL, | ||
| statistic = list(all_categorical() ~ "{n} ({p})"), | ||
| id = USUBJID | ||
| ) |> | ||
| sort_ard_hierarchical(sort_col = 'Placebo') | ||
| ae_tbl <- tbl_ard_hierarchical( | ||
| ard, | ||
| by = TRTA, | ||
| variables = c(AESOC, AEDECOD) | ||
| ) | ||
| ae_tbl | ||
| ard <- ard_stack_hierarchical( | ||
| data = cards::ADAE, | ||
| by = TRTA, | ||
| variables = c(AESOC, AEDECOD), | ||
| denominator = cards::ADSL, | ||
| statistic = list(all_categorical() ~ "{n} ({p})"), | ||
| id = USUBJID | ||
| ) |> | ||
| sort_ard_hierarchical(sort_col = 'Xanomeline High Dose') | ||
| ae_tbl <- tbl_ard_hierarchical( | ||
| ard, | ||
| by = TRTA, | ||
| variables = c(AESOC, AEDECOD) | ||
| ) | ||
| ae_tbl | ||
| devtools::check() | ||
| devtools::install_dev_deps() | ||
| devtools::check() | ||
| devtools::test_coverage() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,9 @@ | |
| #' sums, otherwise `p` is used. If neither `n` nor `p` are present in `x` for the variable, an error will occur. | ||
| #' | ||
| #' Defaults to `everything() ~ "descending"`. | ||
| #' @param sort_col \cr | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. after the argument name, we put the expected type of input. maybe that is causing your latex issue
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay,thanks for the hint. |
||
| #' specify the name of the treatment column you want to sort by, or leave it blank to sort by the sum | ||
| #' across all treatment columns. | ||
| #' | ||
| #' @return an ARD data frame of class 'card' | ||
| #' @seealso [filter_ard_hierarchical()] | ||
|
|
@@ -51,11 +54,19 @@ | |
| #' denominator = ADSL | ||
| #' ) |> | ||
| #' sort_ard_hierarchical(sort = list(AESOC ~ "alphanumeric", AEDECOD ~ "descending")) | ||
| #' | ||
| #' ard_stack_hierarchical_count( | ||
| #' ADAE, | ||
| #' variables = c(AESOC, AEDECOD), | ||
| #' by = TRTA, | ||
| #' denominator = ADSL | ||
| #' ) |> | ||
| #' sort_ard_hierarchical(sort_col = "Placebo") | ||
| NULL | ||
|
|
||
| #' @rdname sort_ard_hierarchical | ||
| #' @export | ||
| sort_ard_hierarchical <- function(x, sort = everything() ~ "descending") { | ||
| sort_ard_hierarchical <- function(x, sort = everything() ~ "descending", sort_col = NULL) { | ||
| set_cli_abort_call() | ||
|
|
||
| # check and process inputs --------------------------------------------------------------------- | ||
|
|
@@ -156,7 +167,7 @@ sort_ard_hierarchical <- function(x, sort = everything() ~ "descending") { | |
| # descending sort | ||
| x_sort <- x_sort |> | ||
| # calculate sums for each group at the current level, then get group indices | ||
| .append_hierarchy_sums(ard_args, cols, i) | ||
| .append_hierarchy_sums(ard_args, cols, i, sort_col) | ||
| } else { | ||
| # alphanumeric sort | ||
| x_sort <- x_sort |> | ||
|
|
@@ -248,7 +259,7 @@ sort_ard_hierarchical <- function(x, sort = everything() ~ "descending") { | |
| } | ||
|
|
||
| # this function calculates and appends group sums/ordering for the current hierarchy level (across `by` variables) | ||
| .append_hierarchy_sums <- function(x, ard_args, cols, i) { | ||
| .append_hierarchy_sums <- function(x, ard_args, cols, i, sort_col = sort_col) { | ||
| cur_var <- names(cols)[i] # get current grouping variable | ||
| next_var <- names(cols)[i + 1] # get next grouping variable | ||
|
|
||
|
|
@@ -275,6 +286,20 @@ sort_ard_hierarchical <- function(x, sort = everything() ~ "descending") { | |
| sort_stat <- if (n_stat) "n" else "p" # statistic used to calculate group sums | ||
|
|
||
| # calculate group sums | ||
|
|
||
| # Introduced the ability to sort the ARD based on a particular treatment column | ||
|
|
||
| if (!is.null(sort_col)) { | ||
| x <- x |> | ||
| dplyr::mutate(stat = dplyr::case_when( | ||
| stat_name == sort_stat & variable == dplyr::last(ard_args$variables) & group1_level == sort_col ~ stat, | ||
| stat_name == sort_stat & variable == dplyr::last(ard_args$variables) & group1_level != sort_col ~ list(0), | ||
| TRUE ~ stat | ||
| )) | ||
| } else { | ||
| x <- x | ||
| } | ||
|
|
||
| sum_i <- paste0("sum_group_", i) # sum column label | ||
| x_sums <- x |> | ||
| dplyr::filter( | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to add this to .gitignore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(the file)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh my goodness! can you add, please? I am surprised it wasn't already there! 😱