Skip to content

Commit

Permalink
adapt to reformat function in dunlin (#409)
Browse files Browse the repository at this point in the history
adapt chevron preprocessing and tests to the new `reformat` method from
dunlin.
Sister PR to the `dunlin` PR:
insightsengineering/dunlin#58 please merge at
the same time.

thank you for the review

---------

Co-authored-by: benoit <[email protected]>
  • Loading branch information
BFalquet and benoit authored Mar 8, 2023
1 parent 300fd35 commit ded40c0
Show file tree
Hide file tree
Showing 32 changed files with 153 additions and 159 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ import(rtables)
import(scda.2022)
import(tern)
importFrom(checkmate,check_class)
importFrom(dunlin,poly_pivot_wider)
importFrom(dunlin,rule)
importFrom(forcats,fct_relevel)
importFrom(ggplot2,theme)
importFrom(magrittr,"%>%")
Expand Down
12 changes: 6 additions & 6 deletions R/aet01.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ aet01_1_pre <- function(adam_db,
) %>%
dm_update_zoomed()

missing_list <- list("<Missing>" = c("", NA))
missing_rule <- rule("<Missing>" = c("", NA))

new_format <- list(
adsl = list(
DCSREAS = missing_list
DCSREAS = missing_rule
)
)

db <- dunlin::apply_reformat(db, new_format)
db <- dunlin::reformat(db, new_format, na_last = TRUE)

db
}
Expand Down Expand Up @@ -482,15 +482,15 @@ aet01_2_pre <- function(adam_db) {
) %>%
dm_update_zoomed()

missing_list <- list("<Missing>" = c("", NA))
missing_rule <- rule("<Missing>" = c("", NA))

new_format <- list(
adsl = list(
DCSREAS = missing_list
DCSREAS = missing_rule
)
)

db <- dunlin::apply_reformat(db, new_format)
db <- dunlin::reformat(db, new_format, na_last = TRUE)

db
}
Expand Down
12 changes: 5 additions & 7 deletions R/aet03.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,18 @@ aet03_1_pre <- function(adam_db) {

new_format <- list(
adae = list(
AEBODSYS = list("No Coding available" = c("", NA, "<Missing>")),
AEDECOD = list("No Coding available" = c("", NA, "<Missing>")),
ASEV = list("<Missing>" = c("", NA, "<Missing>"))
AEBODSYS = rule("No Coding available" = c("", NA, "<Missing>")),
AEDECOD = rule("No Coding available" = c("", NA, "<Missing>")),
ASEV = rule("<Missing>" = c("", NA, "<Missing>"))
)
)
adam_db <- dunlin::apply_reformat(adam_db, new_format)
adam_db <- dunlin::reformat(adam_db, new_format, na_last = TRUE)

adam_db %>%
dm_zoom_to("adae") %>%
filter(.data$ANL01FL == "Y") %>%
filter(.data$ASEV != "<Missing>") %>%
mutate(ASEV = factor(.data$ASEV,
levels = setdiff(levels(.data$ASEV), "<Missing>")
)) %>%
mutate(ASEV = factor(.data$ASEV, levels = setdiff(levels(.data$ASEV), "<Missing>"))) %>%
dm_update_zoomed()
}

Expand Down
8 changes: 4 additions & 4 deletions R/aet04.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ aet04_1_pre <- function(adam_db) {

new_format <- list(
adae = list(
AEBODSYS = list("No Coding Available" = c("", NA, "<Missing>")),
AEDECOD = list("No Coding Available" = c("", NA, "<Missing>")),
ATOXGR = list("No Grading Available" = c("", NA, "<Missing>"))
AEBODSYS = rule("No Coding Available" = c("", NA, "<Missing>")),
AEDECOD = rule("No Coding Available" = c("", NA, "<Missing>")),
ATOXGR = rule("No Grading Available" = c("", NA, "<Missing>"))
)
)

adam_db <- dunlin::apply_reformat(adam_db, new_format)
adam_db <- dunlin::reformat(adam_db, new_format, na_last = TRUE)

adam_db %>%
dm_zoom_to("adae") %>%
Expand Down
18 changes: 9 additions & 9 deletions R/cmt01a.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,18 @@ cmt01a_1_pre <- function(adam_db, medcat_var = "ATC2", medname_var = "CMDECOD")
dm_update_zoomed()

fmt_ls <- list(
medcat_var = list(
medcat_var = rule(
"No Coding available" = c("", NA)
),
medname_var = list(
medname_var = rule(
"No Coding available" = c("", NA)
)
)

names(fmt_ls) <- c(medcat_var, medname_var)
new_format <- list(adcm = fmt_ls)

dunlin::apply_reformat(adam_db, new_format)
dunlin::reformat(adam_db, new_format, na_last = TRUE)
}

#' @describeIn cmt01a_1 Postprocessing
Expand Down Expand Up @@ -255,18 +255,18 @@ cmt01a_2_pre <- function(adam_db, medcat_var = "ATC2", medname_var = "CMDECOD")
dm_update_zoomed()

fmt_ls <- list(
medcat_var = list(
medcat_var = rule(
"No Coding available" = c("", NA)
),
medname_var = list(
medname_var = rule(
"No Coding available" = c("", NA)
)
)

names(fmt_ls) <- c(medcat_var, medname_var)
new_format <- list(adcm = fmt_ls)

dunlin::apply_reformat(adam_db, new_format)
dunlin::reformat(adam_db, new_format, na_last = TRUE)
}

#' @describeIn cmt01a_2 Postprocessing
Expand Down Expand Up @@ -440,18 +440,18 @@ cmt01a_3_pre <- function(adam_db, medcat_var = "ATC2", medname_var = "CMDECOD")
dm_update_zoomed()

fmt_ls <- list(
medcat_var = list(
medcat_var = rule(
"No Coding available" = c("", NA)
),
medname_var = list(
medname_var = rule(
"No Coding available" = c("", NA)
)
)

names(fmt_ls) <- c(medcat_var, medname_var)
new_format <- list(adcm = fmt_ls)

dunlin::apply_reformat(adam_db, new_format)
dunlin::reformat(adam_db, new_format, na_last = TRUE)
}

#' @describeIn cmt01a_3 Preprocessing
Expand Down
6 changes: 3 additions & 3 deletions R/cmt02_pt.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ cmt02_pt_1_pre <- function(adam_db) {
dm_update_zoomed()

fmt_ls <- list(
CMDECOD = list(
CMDECOD = rule(
"No Coding available" = c("", NA)
),
CMSEQ = list(
CMSEQ = rule(
"<Missing>" = c("", NA)
)
)

new_format <- list(adcm = fmt_ls)

dunlin::apply_reformat(adam_db, new_format)
dunlin::reformat(adam_db, new_format, na_last = TRUE)
}

#' @describeIn cmt02_pt_1 Postprocessing
Expand Down
11 changes: 0 additions & 11 deletions R/dmt01.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,6 @@ dmt01_1_lyt <- function(arm_var,
dmt01_1_pre <- function(adam_db) {
checkmate::assert_class(adam_db, "dm")

new_format <- list(
adsl = list(
SEX = list(
"Female" = "F",
"Male" = "M"
)
)
)

adam_db <- dunlin::apply_reformat(adam_db, new_format)

adam_db <- dunlin::dm_explicit_na(adam_db)

db <- adam_db %>%
Expand Down
29 changes: 19 additions & 10 deletions R/dst01.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ dst01_1_pre <- function(adam_db,
disc_reason_var = "DCSREAS") {
new_format <- list(
adsl = list(
list("<Missing>" = c("", NA)),
list(
rule("<Missing>" = c("", NA)),
rule(
"COMPLETED" = "Completed",
"DISCONTINUED" = "Discontinued",
"ONGOING" = "Ongoing",
Expand All @@ -167,11 +167,14 @@ dst01_1_pre <- function(adam_db,

names(new_format$adsl) <- c(disc_reason_var, status_var)

adam_db <- dunlin::apply_reformat(adam_db, new_format)
adam_db <- dunlin::reformat(adam_db, new_format, na_last = TRUE)

adam_db %>%
dm_zoom_to("adsl") %>%
mutate(DOMAIN = "ADSL") %>%
mutate(
!!status_var := factor(.data[[status_var]], levels = c("COMPLETED", "DISCONTINUED", "ONGOING", "<Missing>"))
) %>%
dm_update_zoomed()
}

Expand Down Expand Up @@ -380,8 +383,8 @@ dst01_2_pre <- function(adam_db,

new_format <- list(
adsl = list(
list("<Missing>" = c("", NA)),
list(
rule("<Missing>" = c("", NA)),
rule(
"COMPLETED" = "Completed",
"DISCONTINUED" = "Discontinued",
"ONGOING" = "Ongoing",
Expand All @@ -392,10 +395,13 @@ dst01_2_pre <- function(adam_db,

names(new_format$adsl) <- c(disc_reason_var, status_var)

adam_db <- dunlin::apply_reformat(adam_db, new_format)
adam_db <- dunlin::reformat(adam_db, new_format, na_last = TRUE)

adam_db %>%
dm_zoom_to("adsl") %>%
mutate(
!!status_var := factor(.data[[status_var]], levels = c("COMPLETED", "DISCONTINUED", "ONGOING", "<Missing>"))
) %>%
mutate(reasonGP = case_when(
.data[[disc_reason_var]] %in% c("ADVERSE EVENT", "DEATH") ~ "Safety",
(.data[[disc_reason_var]] != "<Missing>" &
Expand Down Expand Up @@ -606,14 +612,14 @@ dst01_3_pre <- function(adam_db,

new_format <- list(
adsl = list(
list("<Missing>" = c("", NA)),
list(
rule("<Missing>" = c("", NA)),
rule(
"COMPLETED" = "Completed",
"DISCONTINUED" = "Discontinued",
"ONGOING" = "Ongoing",
"<Missing>" = c("", NA)
),
list(
rule(
"COMPLETED" = "Completed",
"DISCONTINUED" = "Discontinued",
"ONGOING" = "Ongoing",
Expand All @@ -624,10 +630,13 @@ dst01_3_pre <- function(adam_db,

names(new_format$adsl) <- c(disc_reason_var, status_var, status_treatment_var)

adam_db <- dunlin::apply_reformat(adam_db, new_format)
adam_db <- dunlin::reformat(adam_db, new_format, na_last = TRUE)

adam_db %>%
dm_zoom_to("adsl") %>%
mutate(
!!status_var := factor(.data[[status_var]], levels = c("COMPLETED", "DISCONTINUED", "ONGOING", "<Missing>"))
) %>%
mutate(reasonGP = case_when(
.data[[disc_reason_var]] %in% c("ADVERSE EVENT", "DEATH") ~ "Safety",
.data[[disc_reason_var]] == "<Missing>" ~ "<Missing>",
Expand Down
33 changes: 13 additions & 20 deletions R/dtht01.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,32 +148,25 @@ dtht01_1_opt_lyt <- function(arm_var,
dtht01_1_pre <- function(adam_db) {
checkmate::assert_class(adam_db, "dm")

adam_db <- adam_db %>%
dm_zoom_to("adsl") %>%
mutate(DTHFL = as.factor(.data$DTHFL)) %>%
dm_update_zoomed()

# Reorder factors to have "OTHER" last.
dthcat <- as.factor(adam_db$adsl$DTHCAT)
dthcat_lvl <- levels(dthcat)
dthcat_lvl <- setdiff(dthcat_lvl, c("OTHER", "", NA))
dthcat_lvl <- c(dthcat_lvl, "OTHER")

dthcat_lvl_order <- as.list(setNames(dthcat_lvl, dthcat_lvl))
na_lvl <- list("<Missing>" = c("", NA))
missing_rule <- rule("<Missing>" = c("", NA))

new_formats <- list(
adsl = list(
DTHCAT = c(
dthcat_lvl_order,
na_lvl
),
DTHCAUS = na_lvl,
LDDTHGR1 = na_lvl
DTHCAT = missing_rule,
DTHCAUS = missing_rule,
LDDTHGR1 = missing_rule
)
)

dunlin::apply_reformat(adam_db, new_formats)
# Reorder factors to have "OTHER" last.
adam_db <- adam_db %>%
dm_zoom_to("adsl") %>%
mutate(DTHFL = as.factor(.data$DTHFL)) %>%
mutate(DTHCAT = as.factor(DTHCAT)) %>%
mutate(DTHCAT = factor(DTHCAT, levels = c(setdiff(levels(DTHCAT), "OTHER"), "OTHER"))) %>%
dm_update_zoomed()

dunlin::reformat(adam_db, new_formats, na_last = TRUE)
}


Expand Down
4 changes: 2 additions & 2 deletions R/lbt04.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ lbt04_1_pre <- function(adam_db, req_tables = c("adsl", "adlb"), arm_var = "ACTA

new_format <- list(
adlb = list(
ANRIND = list("<Missing>" = c("", NA, "<Missing>"))
ANRIND = rule("<Missing>" = c("", NA, "<Missing>"))
)
)

adam_db <- dunlin::apply_reformat(adam_db, new_format)
adam_db <- dunlin::reformat(adam_db, new_format, na_last = TRUE)

adam_db %>%
dm_zoom_to("adlb") %>%
Expand Down
10 changes: 5 additions & 5 deletions R/lbt05.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,23 @@ lbt05_1_pre <- function(adam_db, arm_var = "ACTARM") {
.data$PARCAT2 == "LS",
!is.na(.data$AVAL)
) %>%
dm_update_zoomed() %>%
dm_zoom_to("adlb") %>%
mutate(abn_dir = factor(case_when(
ANRIND == "LOW LOW" ~ "Low",
ANRIND == "HIGH HIGH" ~ "High",
TRUE ~ ""
), levels = c("Low", "High"))) %>%
dm_update_zoomed()

missing_rule <- rule("<Missing>" = c("", NA, "<Missing>", "No Coding Available"))

new_format <- list(
adlb = list(
AVALCAT1 = list("<Missing>" = c("", NA, "<Missing>", "No Coding Available")),
abn_dir = list("<Missing>" = c("", NA, "<Missing>", "No Coding Available"))
AVALCAT1 = missing_rule,
abn_dir = missing_rule
)
)

db <- dunlin::apply_reformat(db, new_format)
db <- dunlin::reformat(db, new_format, na_last = TRUE)
}

#' @describeIn lbt05_1 Checks
Expand Down
4 changes: 2 additions & 2 deletions R/lbt07.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ lbt07_1_pre <- function(adam_db) {

new_format <- list(
adlb = list(
ATOXGR = list("<Missing>" = c("", NA, "<Missing>", "No Coding available"))
ATOXGR = rule("<Missing>" = c("", NA, "<Missing>", "No Coding available"))
)
)

adam_db <- dunlin::apply_reformat(adam_db, new_format)
adam_db <- dunlin::reformat(adam_db, new_format, na_last = TRUE)

adam_db %>%
dm_zoom_to("adlb") %>%
Expand Down
Loading

0 comments on commit ded40c0

Please sign in to comment.