Skip to content

Commit

Permalink
Adapt filter argument to follow API conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkorb committed Sep 13, 2024
1 parent 09ce9ac commit 62b2510
Show file tree
Hide file tree
Showing 19 changed files with 177 additions and 171 deletions.
14 changes: 7 additions & 7 deletions R/data_prep.R
Original file line number Diff line number Diff line change
Expand Up @@ -577,26 +577,26 @@ complete_events <- function(combined_data, trt_start, trt_end) {
set_filter_dataset <- function(filter, data_list, mapping, subjid_var) {
# Check for inconsistencies on modul definition side
if (length(filter$ae_filter) < 3) stop("No ae_filter defined for Clinical Timelines!")
if (!filter$ae_filter$data_name %in% names(data_list)) {
if (!filter$ae_filter$dataset_name %in% names(data_list)) {
msg <- paste0(
"Clinical Timelines (dv.clinlines) cannot find ",
filter$ae_filter$data_name,
filter$ae_filter$dataset_name,
" in your data list."
)

stop(msg)
}

only_filters <- filter$ae_filter[!names(filter$ae_filter) %in% c("data_name", "label")]
only_filters <- filter$ae_filter[!names(filter$ae_filter) %in% c("dataset_name", "label")]

check_names(data_list[[filter$ae_filter$data_name]],
unlist(only_filters), # drop data_name
check_names(data_list[[filter$ae_filter$dataset_name]],
unlist(only_filters), # drop dataset_name
subjid_var = subjid_var
)

ae_info <- mapping[[filter$ae_filter$data_name]][[filter$ae_filter$label]]
ae_info <- mapping[[filter$ae_filter$dataset_name]][[filter$ae_filter$label]]

filter_dataset <- data_list[[filter$ae_filter$data_name]] %>%
filter_dataset <- data_list[[filter$ae_filter$dataset_name]] %>%
dplyr::select(
dplyr::all_of(
c(
Expand Down
8 changes: 4 additions & 4 deletions R/local_filter_setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#'
local_filters <- function(ns) {
ui_list <- list(
serious_AE = shiny::radioButtons(
ns("serious_AE"),
serious_ae = shiny::radioButtons(
ns("serious_ae"),
label = "Serious Adverse Events?",
choices = c("Yes" = "Y", "No" = "N", "All" = "all"),
selected = "all"
Expand All @@ -27,8 +27,8 @@ local_filters <- function(ns) {
multiple = TRUE,
options = list(placeholder = "All PT's selected")
),
drug_rel_AE = shiny::radioButtons(
ns("drug_rel_AE"),
drug_rel_ae = shiny::radioButtons(
ns("drug_rel_ae"),
label = "Drug related Adverse Events?",
choices = c("Yes" = "Y", "No" = "N", "All" = "all"),
selected = "all"
Expand Down
12 changes: 6 additions & 6 deletions R/mock_clinical_timelines.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mock_clinical_timelines_UI <- function(id = NULL) { # nolint
shiny::tags$h1("BI Clinical Timelines", class = "mod-title"),
mod_clinical_timelines_UI(
ns("clin_tl"),
list("serious_AE", "soc", "pref_term", "drug_rel_AE")
list("serious_ae", "soc", "pref_term", "drug_rel_ae")
)
)

Expand Down Expand Up @@ -79,12 +79,12 @@ mock_clinical_timelines_server <- function(input, output, session) {
subjid_var = "USUBJID",
filter = list(
ae_filter = list(
data_name = "adae",
dataset_name = "adae",
label = "Adverse Events",
soc = "AESOC",
serious_AE = "AESER",
pref_term = "AEDECOD",
drug_rel_AE = "AEREL"
soc_var = "AESOC",
serious_ae_var = "AESER",
pref_term_var = "AEDECOD",
drug_rel__var = "AEREL"
)
),
ms = 50
Expand Down
4 changes: 2 additions & 2 deletions R/mock_local_filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mock_local_filter_UI <- function(request) { # nolint
shiny::bookmarkButton(),
mod_local_filter_UI(
"filter",
list("soc", "pref_term", "drug_rel_AE", "serious_AE")
list("soc", "pref_term", "drug_rel_ae", "serious_ae")
),
shiny::tableOutput("table")
)
Expand Down Expand Up @@ -69,7 +69,7 @@ mock_local_filter_server <- function(input, output, session) {
filtered_data <- mod_local_filter_server(
"filter",
filter = list(ae_filter = list(
data_name = "adae", soc = "AESOC", pref_term = "AEDECOD", drug_rel_AE = "AEREL", serious_AE = "AESER"
dataset_name = "adae", soc = "AESOC", pref_term = "AEDECOD", drug_rel_ae = "AEREL", serious_ae = "AESER"
)),
joined_data = initial_data,
changed = shiny::reactive(1)
Expand Down
10 changes: 5 additions & 5 deletions R/mock_with_mm.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ mock_with_mm_app <- function() {
subjid_var = "USUBJID",
filter = list(
ae_filter = list(
data_name = "adae",
dataset_name = "adae",
label = "Adverse Events",
soc = "AESOC",
serious_AE = "AESER",
pref_term = "AEDECOD",
drug_rel_AE = "AERELFLG"
soc_var = "AESOC",
serious_ae_var = "AESER",
pref_term_var = "AEDECOD",
drug_rel_ae_var = "AERELFLG"
)
),
ms = 50,
Expand Down
28 changes: 16 additions & 12 deletions R/mod_clinical_timelines.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod_clinical_timelines_UI <- function(module_id, # nolint
ac <- checkmate::makeAssertCollection()
checkmate::assert_string(module_id, min.chars = 1, add = ac)
checkmate::assert_list(filter_list, types = "character", null.ok = TRUE, add = ac)
checkmate::assert_subset(unlist(filter_list), choices = c("soc", "serious_AE", "pref_term", "drug_rel_AE"), add = ac)
checkmate::assert_subset(unlist(filter_list), choices = c("soc_var", "serious_ae_var", "pref_term_var", "drug_rel_ae_var"), add = ac)

Check warning on line 35 in R/mod_clinical_timelines.R

View workflow job for this annotation

GitHub Actions / Shared / Lintr 🔍 / ghcr.io/boehringer-ingelheim/r_4.3.2_cran_2024.01.12:latest

file=R/mod_clinical_timelines.R,line=35,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 135 characters.
checkmate::assert_string(x_param, null.ok = TRUE, add = ac)
checkmate::assert_subset(x_param, choices = c("date", "day"), add = ac)
checkmate::reportAssertions(ac)
Expand Down Expand Up @@ -141,7 +141,7 @@ mod_clinical_timelines_server <- function(module_id,
lapply(filter, function(x) {
checkmate::assert_subset(
names(unlist(x)),
choices = c("data_name", "label", "soc", "serious_AE", "pref_term", "drug_rel_AE")
choices = c("dataset_name", "label", "soc_var", "serious_ae_var", "pref_term_var", "drug_rel_ae_var")
)
})
checkmate::assert_string(subjid_var, min.chars = 1, add = ac)
Expand Down Expand Up @@ -394,25 +394,25 @@ mod_clinical_timelines_server <- function(module_id,
#' If not \code{NULL}, \code{filter} defines local filters.
#' So far, the following filters for adverse events are available:
#' \itemize{
#' \item{\code{serious_AE}: Filter for serious adverse events.}
#' \item{\code{soc}: Filter for system organ classes.}
#' \item{\code{pref_term}: Filter for preferred terms.}
#' \item{\code{drug_rel_AE}: Filter for drug related adverse events.}
#' \item{\code{serious_ae_var}: Filter for serious adverse events.}
#' \item{\code{soc_var}: Filter for system organ classes.}
#' \item{\code{pref_term_var}: Filter for preferred terms.}
#' \item{\code{drug_rel_ae_var}: Filter for drug related adverse events.}
#' }
#' The \code{filter} parameter must be a list that contains yet another list for adverse
#' events filters, that is named \code{ae_filter}, and that holds the following elements:
#' \itemize{
#' \item{\code{data_name}: Character name of the adverse events dataset. Must be
#' \item{\code{dataset_name}: Character name of the adverse events dataset. Must be
#' available in in the datalist that is provided to the \pkg{modulemanager}.}
#' \item{\code{label}: Character value which is exactly the same as the name for the
#' adverse events event defined in \code{mapping}.}
#' \item{\code{serious_AE}: Character name of the adverse events variable that contains
#' \item{\code{serious_ae_var}: Character name of the adverse events variable that contains
#' serious adverse events flags (Y/N).}
#' \item{\code{soc}: Character name of the adverse events variable that contains
#' \item{\code{soc_var}: Character name of the adverse events variable that contains
#' system organ classes.}
#' \item{\code{pref_term}: Character name of the adverse events variable that contains
#' \item{\code{pref_term_var}: Character name of the adverse events variable that contains
#' preferred terms.}
#' \item{\code{drug_rel_AE}: Character name of the adverse events variable that contains
#' \item{\code{drug_rel_ae_var}: Character name of the adverse events variable that contains
#' drug related (causality) flags (Y/N).}
#' }
#'
Expand Down Expand Up @@ -447,10 +447,14 @@ mod_clinical_timelines <- function(module_id,
checkmate::assert_subset(names(default_plot_settings), choices = c("x_param", "start_day", "boxheight_val"), add = ac)
checkmate::reportAssertions(ac)

# if (!is.null(filter$ae_filter)) {
# names(filter$ae_filter) <- gsub("_var", "", names(filter$ae_filter))

Check warning on line 451 in R/mod_clinical_timelines.R

View workflow job for this annotation

GitHub Actions / Shared / Lintr 🔍 / ghcr.io/boehringer-ingelheim/r_4.3.2_cran_2024.01.12:latest

file=R/mod_clinical_timelines.R,line=451,col=7,[commented_code_linter] Commented code should be removed.
# }

mod <- list(
ui = function(id) {
# Extract available adverse event filter names
ae_filter <- !names(filter$ae_filter) %in% c("data_name", "label")
ae_filter <- !names(filter$ae_filter) %in% c("dataset_name", "label")

# Select requested filters
selected <- as.list(names(filter$ae_filter[ae_filter]))
Expand Down
28 changes: 14 additions & 14 deletions R/mod_local_filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod_local_filter_UI <- function(module_id, filter_list = NULL) { # nolint

ui <- shiny::conditionalPanel(
condition = cond,
all_filters[which(names(all_filters) %in% filter_list)],
all_filters[which(paste0(names(all_filters), "_var") %in% filter_list)],
ns = ns
)

Expand Down Expand Up @@ -53,7 +53,7 @@ mod_local_filter_server <- function(module_id, filter, joined_data, changed) {
restored = FALSE,
pt_input = NULL
)
only_filters <- filter$ae_filter[!names(filter$ae_filter) %in% c("data_name", "label")]
only_filters <- filter$ae_filter[!names(filter$ae_filter) %in% c("dataset_name", "label")]
# No need for reactivity as this never changes during a session
status_filters <- get_filter_status(
names(local_filters(session$ns)),
Expand Down Expand Up @@ -87,11 +87,11 @@ mod_local_filter_server <- function(module_id, filter, joined_data, changed) {
selected_rel <- "all"
}

shiny::updateRadioButtons(session, "serious_AE", selected = selected_sae)
shiny::updateRadioButtons(session, "drug_rel_AE", selected = selected_rel)
shiny::updateRadioButtons(session, "serious_ae", selected = selected_sae)
shiny::updateRadioButtons(session, "drug_rel_ae", selected = selected_rel)

if (status_filters[["soc"]]) {
choices <- sort(unique(joined_data()[[filter$ae_filter$soc]]))
choices <- sort(unique(joined_data()[[filter$ae_filter$soc_var]]))

shiny::updateSelectizeInput(
inputId = "soc",
Expand Down Expand Up @@ -134,7 +134,7 @@ mod_local_filter_server <- function(module_id, filter, joined_data, changed) {
)


# Choices need to be saved explicitely
# Choices need to be saved explicitly
shiny::onBookmark(function(state) {
state$values$soc_choices <- cache$soc_choices
state$values$pt_choices <- cache$pt_choices
Expand All @@ -146,8 +146,8 @@ mod_local_filter_server <- function(module_id, filter, joined_data, changed) {
cache$soc_choices <<- state$values$soc_choices
cache$pt_choices <<- state$values$pt_choices
cache$pt_input <<- state$input$pref_term
cache$sae_choice <<- state$input$serious_AE
cache$rel_choice <<- state$input$drug_rel_AE
cache$sae_choice <<- state$input$serious_ae
cache$rel_choice <<- state$input$drug_rel_ae
})


Expand All @@ -169,22 +169,22 @@ mod_local_filter_server <- function(module_id, filter, joined_data, changed) {


filtered_data <- shiny::reactive({
if (status_filters[["serious_AE"]]) {
shiny::req(input$serious_AE)
if (status_filters[["serious_ae"]]) {
shiny::req(input$serious_ae)
}

if (status_filters[["drug_rel_AE"]]) {
shiny::req(input$drug_rel_AE)
if (status_filters[["drug_rel_ae"]]) {
shiny::req(input$drug_rel_ae)
}

filter_data(
data = joined_data(),
status_filters = status_filters,
input_filters = list(
serious_AE = input$serious_AE,
serious_ae = input$serious_ae,
soc = input$soc,
pref_term = input$pref_term,
drug_rel_AE = input$drug_rel_AE
drug_rel_ae = input$drug_rel_ae
),
filter = filter
)
Expand Down
34 changes: 18 additions & 16 deletions R/server_functions_local_filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
#' chosen by the user to be displayed (TRUE) or not (FALSE).
#' @keywords internal
get_filter_status <- function(all_filters, chosen_filters) {
all_filters <- paste0(all_filters, "_var")
check_filters(filter_list = chosen_filters, filter_names = all_filters)
status <- all_filters %in% chosen_filters
names(status) <- all_filters
names(status) <- gsub("_var", "", all_filters)

return(status)
}
Expand All @@ -31,12 +32,12 @@ get_filter_status <- function(all_filters, chosen_filters) {
set_pts <- function(status_filters, soc, prepped_data, filter) {
if (status_filters[["soc"]] && !is.null(soc)) {
pt_choices <- prepped_data %>%
dplyr::filter(.data[[filter$soc]] %in% soc) %>%
dplyr::select(tidyselect::all_of(filter$pref_term)) %>%
dplyr::filter(.data[[filter$soc_var]] %in% soc) %>%
dplyr::select(tidyselect::all_of(filter$pref_term_var)) %>%
dplyr::distinct() %>%
dplyr::arrange()
} else {
pt_choices <- sort(unique(prepped_data[[filter$pref_term]]))
pt_choices <- sort(unique(prepped_data[[filter$pref_term_var]]))
}
}

Expand All @@ -51,39 +52,40 @@ set_pts <- function(status_filters, soc, prepped_data, filter) {
#'
#' @keywords internal
filter_data <- function(data, status_filters, input_filters, filter) {

# .data$set != info_ae$name ensures to also display non-AE data
if (status_filters[["serious_AE"]]) {
if (input_filters$serious_AE != "all") {
if (status_filters[["serious_ae"]]) {
if (input_filters$serious_ae != "all") {
data <- data %>% dplyr::filter(
.data[[filter$ae_filter$serious_AE]] == input_filters$serious |
.data$set != filter$ae_filter$data_name
.data[[filter$ae_filter$serious_ae_var]] == input_filters$serious |
.data$set != filter$ae_filter$dataset_name
)
}
}

if (status_filters[["soc"]]) {
if (!is.null(input_filters$soc)) {
data <- data %>% dplyr::filter(
.data[[filter$ae_filter$soc]] %in% input_filters$soc |
.data$set != filter$ae_filter$data_name
.data[[filter$ae_filter$soc_var]] %in% input_filters$soc |
.data$set != filter$ae_filter$dataset_name
)
}
}

if (status_filters[["pref_term"]]) {
if (!is.null(input_filters$pref_term)) {
data <- data %>% dplyr::filter(
.data[[filter$ae_filter$pref_term]] %in% input_filters$pref_term |
.data$set != filter$ae_filter$data_name
.data[[filter$ae_filter$pref_term_var]] %in% input_filters$pref_term |
.data$set != filter$ae_filter$dataset_name
)
}
}

if (status_filters[["drug_rel_AE"]]) {
if (input_filters$drug_rel_AE != "all") {
if (status_filters[["drug_rel_ae"]]) {
if (input_filters$drug_rel_ae != "all") {
data <- data %>% dplyr::filter(
.data[[filter$ae_filter$drug_rel_AE]] == input_filters$drug_rel_AE |
.data$set != filter$ae_filter$data_name
.data[[filter$ae_filter$drug_rel_ae_var]] == input_filters$drug_rel_ae |
.data$set != filter$ae_filter$dataset_name
)
}
}
Expand Down
18 changes: 9 additions & 9 deletions man/mod_clinical_timelines.Rd

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

Loading

0 comments on commit 62b2510

Please sign in to comment.