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

Teal refactor #598

Merged
merged 22 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Imports:
ggplot2,
ggrepel,
grid,
lifecycle,
logger (>= 0.2.0),
magrittr,
methods,
Expand Down
8 changes: 7 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# teal.modules.clinical 0.8.14.9002

### Breaking changes

* Replaced `chunks` with simpler `qenv` class.
* Replaced `datasets` argument containing `FilteredData` with the new arguments `data` (`tdata` object) and `filter_panel_api` (`FilterPanelAPI`).

### Enhancements
* Replaced `synthetic_cdisc_data` with refactored `synthetic_cdisc_dataset` function to speed up dataset loading in tests/examples.

Expand All @@ -10,6 +15,7 @@

### Enhancements
* Updated all synthetic data for tests to version `rcd_2022_02_28`.
* Updated all test files in `tests/testthat/` to `synthetic_cdisc_data("2022_02_28")`
* Reverted missing data checkbox in `tm_t_summary` (encoding and filtering should be separate).
* Implemented a new widget that allows dragging and dropping to select comparison groups.
* Added the `teal.reporter` functionality to all modules.
Expand All @@ -28,7 +34,7 @@

### Bug fixes

* Fixed bug in `tm_g_barchart_simple` which prevented graph from showing.
* Fixed bug in `tm_g_barchart_simple` which prevented graph from being shown.
* Fixed broken example for `tm_t_abnormality_by_worst_grade`.
* Fixed bug in `tm_a_mmrm` which prevented table headers from displaying.
* Fixed bug in `tm_g_forest_rsp` when deselecting endpoint.
Expand Down
3 changes: 2 additions & 1 deletion R/argument_convention.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ NULL
#' @param arm_var (`choices_selected` or `data_extract_spec`)\cr
#' object with all available choices
#' and preselected option for variable names that can be used as `arm_var`.
#' It defines the grouping variable(s) in the results table. If there are two elements selected for `arm_var`,
#' It defines the grouping variable(s) in the results table.
#' If there are two elements selected for `arm_var`,
#' second variable will be nested under the first variable.
#' @param arm_ref_comp optional, (`list`)\cr
#' If specified it must be a named list with each element corresponding to
Expand Down
109 changes: 45 additions & 64 deletions R/arm_ref_comp.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#' @param id_ref (`character`) id of reference Treatment input ui element
#' @param id_comp (`character`) id of comparison group input ui element
#' @param id_arm_var (`character`) id of Treatment variable input ui element
#' @param datasets (`FilteredData`) object from the module
#' @param dataname (`character`) dataset name
#' @param data (`reactive` or `data.frame`) dataset used to validate Treatment reference inputs and
#' set `id_ref` input.
#' @param arm_ref_comp (`unknown`) Treatment reference and compare variables provided as a
#' nested list where each Treatment variable corresponds a list specifying the default levels for the
#' reference and comparison treatments.
Expand All @@ -24,66 +24,60 @@
#' @keywords internal
#'
#' @examples
#' \dontrun{
#' ds <- teal:::get_dummy_datasets()
#'
#' arm_ref_comp <- list(ARMCD = list(ref = "ARM A", comp = c("ARM B")))
#' arm_var <- choices_selected(c("ARM", "ARMCD"), "ARM")
#' shinyApp(
#' ui = fluidPage(
#' teal.widgets::optionalSelectInput(
#' "arm",
#' "Treatment Variable",
#' choices = arm_var$choices,
#' selected = arm_var$selected
#' if (interactive()) {
#' shinyApp(
#' ui = fluidPage(
#' teal.widgets::optionalSelectInput(
#' "arm",
#' "Treatment Variable",
#' choices = arm_var$choices,
#' selected = arm_var$selected
#' ),
#' shiny::uiOutput("arms_buckets"),
#' ),
#' shiny::uiOutput("arms_buckets"),
#' ),
#' server = function(input, output, session) {
#' shiny::isolate({
#' teal.modules.clinical:::arm_ref_comp_observer(
#' session,
#' input,
#' output,
#' id_arm_var = "arm",
#' datasets = ds,
#' arm_ref_comp = arm_ref_comp,
#' module = "example"
#' )
#' })
#' }
#' )
#' server = function(input, output, session) {
#' shiny::isolate({
#' teal.modules.clinical:::arm_ref_comp_observer(
#' session,
#' input,
#' output,
#' id_arm_var = "arm",
#' datasets = ds,
#' arm_ref_comp = arm_ref_comp,
#' module = "example"
#' )
#' })
#' }
#' )
#' }
arm_ref_comp_observer <- function(session,
input,
output,
id_ref = "Ref",
id_comp = "Comp",
id_arm_var,
datasets,
dataname = "ADSL",
data,
arm_ref_comp,
module,
on_off = shiny::reactive(TRUE),
input_id = "buckets",
output_id = "arms_buckets") {
if (any(unlist(lapply(arm_ref_comp, lapply, inherits, "delayed_data")))) {
stopifnot(
all(vapply(arm_ref_comp, function(x) identical(sort(names(x)), c("comp", "ref")), logical(1)))
)
# when a delayed object is used for arm_ref_comp, the entire FilteredData
# object must be passed to resolve it
arm_ref_comp <- teal.transform::resolve_delayed(arm_ref_comp, datasets)
}

df <- datasets$get_data(dataname, filtered = FALSE)

check_arm_ref_comp(arm_ref_comp, df, module) ## throws an error if there are issues

# uses observe because observeEvent evaluates only when on_off() is switched
# not necessarily when variables are dropped
output[[output_id]] <- shiny::renderUI({
if (!is.null(on_off()) && on_off()) {
df <- if (shiny::is.reactive(data)) {
data()
} else {
data
}

check_arm_ref_comp(arm_ref_comp, df, module) ## throws an error if there are issues

arm_var <- input[[id_arm_var]]

# validations here don't produce nice UI message (it's observe and not render output) but it prevent red errors
Expand All @@ -97,7 +91,6 @@ arm_ref_comp_observer <- function(session,
} else {
unique(arm)
}

default_settings <- arm_ref_comp[[arm_var]]

if (is.null(default_settings)) {
Expand Down Expand Up @@ -150,29 +143,17 @@ check_arm_ref_comp <- function(x, df_to_check, module) {
}


Map(function(xi, var) {
if (!is.list(xi)) {
stop(
msg, "definition for Treatment variable ",
var, " list element needs to be lists with ref and comp elements"
)
}

rc <- names(xi)
if (is.null(rc) || !identical(sort(rc), c("comp", "ref"))) {
stop(msg, "definition for Treatment variable ", var, " nested list needs to have the elements ref and comp")
}


arm_levels <- unlist(xi)

if (!all(arm_levels %in% df_to_check[[var]])) {
stop(
msg, "definition for Treatment variable ",
var, " refers to treatment levels that do not exist in the data"
)
Map(
x, vars,
f = function(xi, var) {
if (!checkmate::check_list(xi) || !setequal(names(xi), c("comp", "ref"))) {
stop(
msg, "definition for Treatment variable ",
var, " list element needs to be lists with ref and comp elements"
)
}
}
}, x, vars)
)
}

invisible(TRUE)
Expand Down
Loading