Skip to content
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

issue with pre functions #491

Merged
merged 14 commits into from
Apr 26, 2023
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export(aet10_1_main)
export(aet10_1_post)
export(aet10_1_pre)
export(args_ls)
export(assert_all_tablenames)
export(assert_colnames)
export(assert_only_one_paramcd)
export(check_col_contains)
export(chevron_g)
export(chevron_l)
Expand Down
18 changes: 9 additions & 9 deletions R/aet01.R
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,12 @@ aet01_2_lyt <- function(arm_var,
#'
#' @export
#'
aet01_2_pre <- function(adam_db, arm_var, safety_var, medconcept_var, ...) {
checkmate::assert_class(adam_db, "dm")

aet01_2_pre <- function(adam_db, arm_var = "ACTARM", safety_var = list(
"FATAL", "SER", "SERWD", "SERDSM",
"RELSER", "WD", "DSM", "REL", "RELWD", "RELDSM", "SEV"
),
medconcept_var = list("SMQ01", "SMQ02", "CQ01"), ...) {
assert_all_tablenames(adam_db, c("adsl", "adae"))
aet01_2_check(adam_db, arm_var = arm_var, safety_var = safety_var, medconcept_var = medconcept_var)

labs <- formatters::var_labels(adam_db$adae)
Expand Down Expand Up @@ -514,12 +517,9 @@ aet01_2_pre <- function(adam_db, arm_var, safety_var, medconcept_var, ...) {
#' @export
aet01_2_check <- function(adam_db,
req_tables = c("adsl", "adae"),
arm_var = "ACTARM",
safety_var = list(
"FATAL", "SER", "SERWD", "SERDSM",
"RELSER", "WD", "DSM", "REL", "RELWD", "RELDSM", "SEV"
),
medconcept_var = list("SMQ01", "SMQ02", "CQ01")) {
arm_var,
safety_var,
medconcept_var) {
assert_all_tablenames(adam_db, req_tables)
checkmate::assert_list(safety_var, types = "character")
safety_var <- unlist(safety_var)
Expand Down
33 changes: 20 additions & 13 deletions R/aet01_aesi.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#'
#' @inheritParams gen_args
#' @param aesi_vars (`list`) the AESI variables to be included in the summary. Defaults to `NA`.
#'
#' @param grade_groups (`list`) the grade groups to be displayed.
#' @details
#' * Does not remove rows with zero counts by default.
#'
Expand Down Expand Up @@ -34,8 +34,19 @@ aet01_aesi_1_main <- function(adam_db,
arm_var = "ACTARM",
aesi_vars = list(NA_character_),
deco = std_deco("AET01_AESI"),
grade_groups = NULL,
...) {
checkmate::assert_list(aesi_vars, types = "character")
checkmate::assert_list(grade_groups, null.ok = TRUE)
if (is.null(grade_groups)) {
grade_groups <- list(
"Grade 1" = "1",
"Grade 2" = "2",
"Grade 3" = "3",
"Grade 4" = "4",
"Grade 5" = "5"
)
}
aesi_vars <- unlist(aesi_vars)
if ("ALL" %in% aesi_vars) aesi_vars <- c("ALL_ALLRES", "ALL_NOTRES", "ALL_SER", "ALL_REL")
if (any(grepl("^ALL_", aesi_vars))) {
Expand All @@ -58,7 +69,8 @@ aet01_aesi_1_main <- function(adam_db,
arm_var = arm_var,
aesi_vars = all_aesi_vars,
deco = deco,
lbl_aesi_vars = lbl_aesi_vars
lbl_aesi_vars = lbl_aesi_vars,
grade_groups
clarkliming marked this conversation as resolved.
Show resolved Hide resolved
)

tbl <- build_table(lyt, adam_db$adae, alt_counts_df = adam_db$adsl)
Expand All @@ -76,7 +88,8 @@ aet01_aesi_1_main <- function(adam_db,
aet01_aesi_1_lyt <- function(arm_var,
aesi_vars,
deco,
lbl_aesi_vars) {
lbl_aesi_vars,
grade_groups) {
names(lbl_aesi_vars) <- aesi_vars
basic_table_deco(deco, show_colcounts = TRUE) %>%
split_cols_by(var = arm_var) %>%
Expand All @@ -96,7 +109,8 @@ aet01_aesi_1_lyt <- function(arm_var,
count_occurrences_by_grade(
var = "ATOXGR",
var_labels = "Total number of patients with at least one AE by worst grade",
show_labels = "visible"
show_labels = "visible",
grade_groups = grade_groups
) %>%
count_patients_with_flags(
"USUBJID",
Expand Down Expand Up @@ -206,16 +220,9 @@ aet01_aesi_1_pre <- function(adam_db,
)
) %>%
mutate(
ATOXGR = reformat(
ATOXGR = dunlin::reformat(
.data$ATOXGR,
dunlin::rule(
"Missing" = c("", NA_character_),
"Grade 1" = "1",
"Grade 2" = "2",
"Grade 3" = "3",
"Grade 4" = "4",
"Grade 5 (fatal outcome)" = "5"
)
dunlin::rule("Missing" = c("", NA_character_))
)
)

Expand Down
6 changes: 2 additions & 4 deletions R/aet02.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,9 @@ aet02_1_lyt <- function(arm_var,
#'
#' @export
#'
aet02_1_pre <- function(adam_db, arm_var, ...) {
checkmate::assert_class(adam_db, "dm")

aet02_1_pre <- function(adam_db, arm_var = "ACTARM", ...) {
assert_all_tablenames(adam_db, c("adsl", "adae"))
aet02_1_check(adam_db, arm_var = arm_var)

new_format <- list(
adae = list(
AEBODSYS = rule("No Coding available" = c("", NA, "<Missing>")),
Expand Down
4 changes: 1 addition & 3 deletions R/aet10.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ aet10_1_lyt <- function(arm_var,
#'
#' @export
#'
aet10_1_pre <- function(adam_db, arm_var, ...) {
checkmate::assert_class(adam_db, "dm")

aet10_1_pre <- function(adam_db, arm_var = "ACTARM", ...) {
aet10_1_check(adam_db, arm_var = arm_var)

adam_db$adae <- adam_db$adae %>%
Expand Down
1 change: 0 additions & 1 deletion R/assertions.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#' @param x (`character`) the names of the columns to be checked.
#' @param null_ok (`logical`) can `x` be NULL.
#'
#' @keywords internal
#' @export
#'
#' @examples
Expand Down
13 changes: 5 additions & 8 deletions R/ext01.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,15 @@ ext01_1_lyt <- function(arm_var,
ext01_1_pre <- function(adam_db,
paramcd_order = list("TNDOSE", "DOSE", "NDOSE", "TDOSE"),
...) {
checkmate::assert_class(adam_db, "dm")
checkmate::assert_list(paramcd_order)
paramcd_order <- unlist(paramcd_order)
db <- adam_db %>%
dm_zoom_to("adex") %>%
filter(.data$PARCAT1 == "OVERALL") %>%
dm_update_zoomed()
assert_all_tablenames(adam_db, c("adsl", "adex"))

adam_db$adex <- adam_db$adex %>%
filter(.data$PARCAT1 == "OVERALL")

if (nrow(adam_db$adex) > 0L) {
param_vars <- adam_db$adex %>%
dplyr::select("PARAM", "PARAMCD") %>%
dunlin::co_relevels("PARAMCD", "PARAM", paramcd_order)
dunlin::co_relevels("PARAMCD", "PARAM", unlist(paramcd_order))

adam_db$adex <- adam_db$adex %>%
mutate(PARAM = param_vars$PARAM, PARAMCD = param_vars$PARAMCD)
Expand Down
16 changes: 11 additions & 5 deletions man/aet01_2.Rd

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

5 changes: 4 additions & 1 deletion man/aet01_aesi_1.Rd

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

2 changes: 1 addition & 1 deletion man/aet02_1.Rd

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

2 changes: 1 addition & 1 deletion man/aet10_1.Rd

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

11 changes: 7 additions & 4 deletions tests/testthat/_snaps/aet01_aesi.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
Grade 2 6 (4.5%) 10 (7.5%) 7 (5.3%)
Grade 3 18 (13.4%) 14 (10.4%) 16 (12.1%)
Grade 4 15 (11.2%) 20 (14.9%) 18 (13.6%)
Grade 5 (fatal outcome) 76 (56.7%) 70 (52.2%) 75 (56.8%)
Grade 5 76 (56.7%) 70 (52.2%) 75 (56.8%)
Missing 0 0 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if missing should appear (I dont think so). What do you think @barnett11

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a difficult issue. We can expect Missing grade in adae.atoxgr, (even in cleaned adam). We should not filter this out because we also need to count the number of events. But this should not be counted in the count_occurrences_by_grade.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. for ATOXGR, those records are still using NA (not convertet to a level) and then will be discarded.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another update: ADAE.ATOXGR is a string (character in R's language). in rtables we require ATOXGR to be factor. Then we need to convert it into factor.
so here while we apply reformating, we need to populate NA for empty strings. Very different logic here!

reformat(c('1','2','3','4','5',''), rule('1'='1','2'='2','3'='3','4'='4','5'='5'))

my suggestion is to keep this missing level for now (as this solves many issues) and open another issue to deal with this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably need to evaluate the reformat when it is partial match of the whole set, option 1, keep the rest, option 2, discard the rest levels (use NA to replace) (but this is really contradictory to what we are doing to conver NA to "Missing"!) The logic/design behind this need to be further evaluated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue opened here: #492

Total number of patients with study drug withdrawn due to AE 0 0 0
Total number of patients with dose modified/interrupted due to AE 0 0 0
Total number of patients with treatment received for AE 0 0 0
Expand All @@ -37,7 +38,7 @@
Grade 2 6 (4.5%) 10 (7.5%) 7 (5.3%)
Grade 3 18 (13.4%) 14 (10.4%) 16 (12.1%)
Grade 4 15 (11.2%) 20 (14.9%) 18 (13.6%)
Grade 5 (fatal outcome) 76 (56.7%) 70 (52.2%) 75 (56.8%)
Grade 5 76 (56.7%) 70 (52.2%) 75 (56.8%)
Total number of patients with all non-fatal AEs resolved 84 (62.7%) 92 (68.7%) 97 (73.5%)
Total number of patients with at least one unresolved or ongoing non-fatal AE 102 (76.1%) 110 (82.1%) 107 (81.1%)
Total number of patients with at least one related AE 105 (78.4%) 108 (80.6%) 109 (82.6%)
Expand All @@ -57,7 +58,8 @@
Grade 2 6 (4.5%) 10 (7.5%) 7 (5.3%)
Grade 3 18 (13.4%) 14 (10.4%) 16 (12.1%)
Grade 4 15 (11.2%) 20 (14.9%) 18 (13.6%)
Grade 5 (fatal outcome) 76 (56.7%) 70 (52.2%) 75 (56.8%)
Grade 5 76 (56.7%) 70 (52.2%) 75 (56.8%)
Missing 0 0 0
Total number of patients with study drug withdrawn due to AE 27 (20.1%) 26 (19.4%) 30 (22.7%)
Total number of patients with dose modified/interrupted due to AE 66 (49.3%) 76 (56.7%) 74 (56.1%)
Total number of patients with treatment received for AE 98 (73.1%) 102 (76.1%) 103 (78.0%)
Expand All @@ -81,7 +83,8 @@
Grade 2 6 (4.5%) 10 (7.5%) 7 (5.3%)
Grade 3 18 (13.4%) 14 (10.4%) 16 (12.1%)
Grade 4 15 (11.2%) 20 (14.9%) 18 (13.6%)
Grade 5 (fatal outcome) 76 (56.7%) 70 (52.2%) 75 (56.8%)
Grade 5 76 (56.7%) 70 (52.2%) 75 (56.8%)
Missing 0 0 0
Total number of patients with study drug withdrawn due to AE 27 (20.1%) 26 (19.4%) 30 (22.7%)
Total number of patients with dose modified/interrupted due to AE 66 (49.3%) 76 (56.7%) 74 (56.1%)
Total number of patients with treatment received for AE 98 (73.1%) 102 (76.1%) 103 (78.0%)
Expand Down
32 changes: 0 additions & 32 deletions tests/testthat/_snaps/chevron_tlg-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,38 +131,6 @@

# script_funs works as expected in interactive mode

Code
res
Output
[1] "# Edit Preprocessing Function."
[2] "pre_fun <- function(adam_db, ...) {"
[3] " checkmate::assert_class(adam_db, \"dm\")"
[4] ""
[5] " new_format <- list("
[6] " adae = list("
[7] " AEBODSYS = rule(\"No Coding Available\" = c(\"\", NA, \"<Missing>\")),"
[8] " AEDECOD = rule(\"No Coding Available\" = c(\"\", NA, \"<Missing>\")),"
[9] " ATOXGR = rule(\"No Grading Available\" = c(\"\", NA, \"<Missing>\"))"
[10] " )"
[11] " )"
[12] ""
[13] " adam_db <- dunlin::reformat(adam_db, new_format, na_last = TRUE)"
[14] ""
[15] " adam_db %>%"
[16] " dm_zoom_to(\"adae\") %>%"
[17] " filter(.data$ANL01FL == \"Y\") %>%"
[18] " filter(.data$ATOXGR != \"No Grading Available\") %>%"
[19] " mutate(ATOXGR = factor(.data$ATOXGR,"
[20] " levels = setdiff(levels(.data$ATOXGR), \"No Grading Available\")"
[21] " )) %>%"
[22] " dm_update_zoomed()"
[23] "}"
[24] ""
[25] "# Create TLG"
[26] "tlg_output <- rlang::exec(.fn = pre_fun, adam_db = data, !!!args_ls) %>% \nrlang::exec(.fn = run, object = aet04_1, !!!args_ls, auto_pre = FALSE, check_arg = FALSE)"

# script_funs works as expected in non interactive mode

Code
res
Output
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/default_tlg.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
Grade 2 6 (4.5%) 10 (7.5%) 7 (5.3%)
Grade 3 18 (13.4%) 14 (10.4%) 16 (12.1%)
Grade 4 15 (11.2%) 20 (14.9%) 18 (13.6%)
Grade 5 (fatal outcome) 76 (56.7%) 70 (52.2%) 75 (56.8%)
Grade 5 76 (56.7%) 70 (52.2%) 75 (56.8%)
Missing 0 0 0
Total number of patients with study drug withdrawn due to AE 27 (20.1%) 26 (19.4%) 30 (22.7%)
Total number of patients with dose modified/interrupted due to AE 66 (49.3%) 76 (56.7%) 74 (56.1%)
Total number of patients with treatment received for AE 98 (73.1%) 102 (76.1%) 103 (78.0%)
Expand Down