Skip to content

Commit

Permalink
update strata var to group_var (#1246)
Browse files Browse the repository at this point in the history
to match `control_lineplot_vars()` in tern

closes #1245

---------

Signed-off-by: Abinaya Yogasekaram <[email protected]>
Co-authored-by: Emily de la Rua <[email protected]>
  • Loading branch information
ayogasekaram and edelarua authored Nov 26, 2024
1 parent b9268ca commit 789d06f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 41 deletions.
73 changes: 50 additions & 23 deletions R/tm_g_lineplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#' @inheritParams tern::g_lineplot
#' @inheritParams tern::control_lineplot_vars
#' @inheritParams template_arguments
#' @param strata (`string` or `NA`)\cr group variable name.
#' @param strata `r lifecycle::badge("deprecated")` Please use the `group_var` argument instead.
#' @param group_var (`string` or `NA`)\cr group variable name.
#' @param param (`character`)\cr parameter to filter the data by.
#' @param incl_screen (`logical`)\cr whether the screening visit should be included.
#' @param ggplot2_args (`ggplot2_args`) optional\cr object created by [teal.widgets::ggplot2_args()] with settings
Expand All @@ -22,7 +23,8 @@
#'
#' @keywords internal
template_g_lineplot <- function(dataname = "ANL",
strata = "ARM",
strata = lifecycle::deprecated(),
group_var = "ARM",
x = "AVISIT",
y = "AVAL",
y_unit = "AVALU",
Expand All @@ -40,8 +42,17 @@ template_g_lineplot <- function(dataname = "ANL",
title = "Line Plot",
y_lab = "",
ggplot2_args = teal.widgets::ggplot2_args()) {
if (lifecycle::is_present(strata)) {
warning(
"The `strata` argument of `template_g_lineplot()` is deprecated as of teal.modules.clinical 0.9.1. ",
"Please use the `group_var` argument instead.",
call. = FALSE
)
group_var <- strata
}

checkmate::assert_string(dataname)
checkmate::assert_string(strata)
checkmate::assert_string(group_var)
checkmate::assert_string(x)
checkmate::assert_string(y)
checkmate::assert_string(y_unit)
Expand Down Expand Up @@ -72,15 +83,15 @@ template_g_lineplot <- function(dataname = "ANL",
)
}

# droplevels for strata
# droplevels for group_var
data_list <- add_expr(
data_list,
substitute_names(
expr = dplyr::mutate(
arm_var = droplevels(arm_var)
),
names = list(
arm_var = as.name(strata)
arm_var = as.name(group_var)
)
)
)
Expand All @@ -96,7 +107,7 @@ template_g_lineplot <- function(dataname = "ANL",

z$variables <- substitute(
expr = variables <- control_lineplot_vars(x = x, y = y, group_var = arm, paramcd = paramcd, y_unit = y_unit),
env = list(x = x, y = y, arm = strata, paramcd = paramcd, y_unit = y_unit)
env = list(x = x, y = y, arm = group_var, paramcd = paramcd, y_unit = y_unit)
)

mid_choices <- c(
Expand Down Expand Up @@ -233,7 +244,7 @@ template_g_lineplot <- function(dataname = "ANL",
#' tm_g_lineplot(
#' label = "Line Plot",
#' dataname = "ADLB",
#' strata = choices_selected(
#' group_var = choices_selected(
#' variable_choices(ADSL, c("ARM", "ARMCD", "ACTARMCD")),
#' "ARM"
#' ),
Expand All @@ -255,12 +266,9 @@ template_g_lineplot <- function(dataname = "ANL",
#' @export
tm_g_lineplot <- function(label,
dataname,
parentname = ifelse(
inherits(strata, "data_extract_spec"),
teal.transform::datanames_input(strata),
"ADSL"
),
strata = teal.transform::choices_selected(
parentname = NULL,
strata = lifecycle::deprecated(),
group_var = teal.transform::choices_selected(
teal.transform::variable_choices(parentname, c("ARM", "ARMCD", "ACTARMCD")), "ARM"
),
x = teal.transform::choices_selected(
Expand Down Expand Up @@ -294,6 +302,25 @@ tm_g_lineplot <- function(label,
pre_output = NULL,
post_output = NULL,
ggplot2_args = teal.widgets::ggplot2_args()) {
if (lifecycle::is_present(strata)) {
warning(
"The `strata` argument of `tm_g_lineplot()` is deprecated as of teal.modules.clinical 0.9.1. ",
"Please use the `group_var` argument instead.",
call. = FALSE
)
group_var <- strata
} else {
strata <- group_var # resolves missing argument error
}

# Now handle 'parentname' calculation based on 'group_var'
if (is.null(parentname)) {
parentname <- ifelse(
inherits(group_var, "data_extract_spec"),
teal.transform::datanames_input(group_var),
"ADSL"
)
}
message("Initializing tm_g_lineplot")
checkmate::assert_string(label)
checkmate::assert_string(dataname)
Expand All @@ -316,7 +343,7 @@ tm_g_lineplot <- function(label,

args <- as.list(environment())
data_extract_list <- list(
strata = cs_to_des_select(strata, dataname = parentname),
group_var = cs_to_des_select(group_var, dataname = parentname),
param = cs_to_des_filter(param, dataname = dataname),
x = cs_to_des_select(x, dataname = dataname, multiple = FALSE),
y = cs_to_des_select(y, dataname = dataname, multiple = FALSE),
Expand Down Expand Up @@ -348,7 +375,7 @@ tm_g_lineplot <- function(label,
ui_g_lineplot <- function(id, ...) {
a <- list(...)
is_single_dataset_value <- teal.transform::is_single_dataset(
a$strata,
a$group_var,
a$paramcd,
a$x,
a$param,
Expand All @@ -369,17 +396,17 @@ ui_g_lineplot <- function(id, ...) {
teal.reporter::simple_reporter_ui(ns("simple_reporter")),
###
tags$label("Encodings", class = "text-primary"),
teal.transform::datanames_input(a[c("strata", "paramcd", "x", "y", "y_unit", "param")]),
teal.transform::datanames_input(a[c("group_var", "paramcd", "x", "y", "y_unit", "param")]),
teal.transform::data_extract_ui(
id = ns("param"),
label = "Select Biomarker",
data_extract_spec = a$param,
is_single_dataset = is_single_dataset_value
),
teal.transform::data_extract_ui(
id = ns("strata"),
id = ns("group_var"),
label = "Select Treatment Variable",
data_extract_spec = a$strata,
data_extract_spec = a$group_var,
is_single_dataset = is_single_dataset_value
),
teal.transform::data_extract_ui(
Expand Down Expand Up @@ -508,7 +535,7 @@ srv_g_lineplot <- function(id,
dataname,
parentname,
paramcd,
strata,
group_var,
x,
y,
param,
Expand All @@ -525,12 +552,12 @@ srv_g_lineplot <- function(id,
moduleServer(id, function(input, output, session) {
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.clinical")
selector_list <- teal.transform::data_extract_multiple_srv(
data_extract = list(x = x, y = y, strata = strata, paramcd = paramcd, y_unit = y_unit, param = param),
data_extract = list(x = x, y = y, group_var = group_var, paramcd = paramcd, y_unit = y_unit, param = param),
datasets = data,
select_validation_rule = list(
x = shinyvalidate::sv_required("Please select a time variable"),
y = shinyvalidate::sv_required("Please select an analysis variable"),
strata = shinyvalidate::sv_required("Please select a treatment variable")
group_var = shinyvalidate::sv_required("Please select a treatment variable")
),
filter_validation_rule = list(
param = shinyvalidate::sv_required(message = "Please select Biomarker filter.")
Expand Down Expand Up @@ -569,7 +596,7 @@ srv_g_lineplot <- function(id,
adsl_filtered <- merged$anl_q()[[parentname]]
anl_filtered <- merged$anl_q()[[dataname]]

input_strata <- names(merged$anl_input_r()$columns_source$strata)
input_strata <- names(merged$anl_input_r()$columns_source$group_var)
input_x_var <- names(merged$anl_input_r()$columns_source$x)
input_y <- names(merged$anl_input_r()$columns_source$y)
input_param <- unlist(param$filter)["vars_selected"]
Expand Down Expand Up @@ -612,7 +639,7 @@ srv_g_lineplot <- function(id,

my_calls <- template_g_lineplot(
dataname = "ANL",
strata = names(merged$anl_input_r()$columns_source$strata),
group_var = names(merged$anl_input_r()$columns_source$group_var),
y = names(merged$anl_input_r()$columns_source$y),
x = names(merged$anl_input_r()$columns_source$x),
paramcd = names(merged$anl_input_r()$columns_source$paramcd),
Expand Down
7 changes: 5 additions & 2 deletions man/template_g_lineplot.Rd

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

17 changes: 10 additions & 7 deletions man/tm_g_lineplot.Rd

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

16 changes: 8 additions & 8 deletions tests/testthat/test-shinytest2-tm_g_lineplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ app_driver_tm_g_lineplot <- function() {
label = "Line Plot",
dataname = "ADLB",
parentname = "ADSL",
strata = teal.transform::choices_selected(
group_var = teal.transform::choices_selected(
teal.transform::variable_choices("ADSL", c("ARM", "ARMCD", "ACTARMCD")),
"ARM"
),
Expand Down Expand Up @@ -76,7 +76,7 @@ testthat::test_that("e2e - tm_g_lineplot: Module initializes in teal without err
})

testthat::test_that(
"e2e - tm_g_lineplot: Starts with specified label, param, strata, y, x, mid, interval, incl_screen,
"e2e - tm_g_lineplot: Starts with specified label, param, group_var, y, x, mid, interval, incl_screen,
plot_settings and table_settings.",
{
skip_if_too_deep(5)
Expand All @@ -85,7 +85,7 @@ testthat::test_that(

testthat::expect_equal(trimws(app_driver$get_text("#teal-teal_modules-active_tab > li.active")), "Line Plot")
testthat::expect_equal(app_driver$get_active_module_input("param-dataset_ADLB_singleextract-filter1-vals"), "ALT")
testthat::expect_equal(app_driver$get_active_module_input("strata-dataset_ADSL_singleextract-select"), "ARM")
testthat::expect_equal(app_driver$get_active_module_input("group_var-dataset_ADSL_singleextract-select"), "ARM")
testthat::expect_equal(app_driver$get_active_module_input("y-dataset_ADLB_singleextract-select"), "AVAL")
testthat::expect_equal(app_driver$get_active_module_input("x-dataset_ADLB_singleextract-select"), "AVISIT")
testthat::expect_equal(app_driver$get_active_module_input("mid"), "mean")
Expand Down Expand Up @@ -137,12 +137,12 @@ testthat::test_that("e2e - tm_g_lineplot: Deselecting param throws validation er
})

testthat::test_that(
"e2e - tm_g_lineplot: Selecting strata changes plot and doesn't throw validation errors.",
"e2e - tm_g_lineplot: Selecting group_var changes plot and doesn't throw validation errors.",
{
skip_if_too_deep(5)
app_driver <- app_driver_tm_g_lineplot()
plot_before <- app_driver$get_active_module_plot_output("myplot")
app_driver$set_active_module_input("strata-dataset_ADSL_singleextract-select", "ARMCD")
app_driver$set_active_module_input("group_var-dataset_ADSL_singleextract-select", "ARMCD")
testthat::expect_false(
identical(
plot_before,
Expand All @@ -154,14 +154,14 @@ testthat::test_that(
}
)

testthat::test_that("e2e - tm_g_lineplot: Deselecting strata throws validation error.", {
testthat::test_that("e2e - tm_g_lineplot: Deselecting group_var throws validation error.", {
skip_if_too_deep(5)
app_driver <- app_driver_tm_g_lineplot()
app_driver$set_active_module_input("strata-dataset_ADSL_singleextract-select", NULL)
app_driver$set_active_module_input("group_var-dataset_ADSL_singleextract-select", NULL)
testthat::expect_identical(app_driver$get_active_module_plot_output("myplot"), character(0))
testthat::expect_identical(
app_driver$active_module_element_text(
"strata-dataset_ADSL_singleextract-select_input > div > span"
"group_var-dataset_ADSL_singleextract-select_input > div > span"
),
"Please select a treatment variable"
)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-tm_g_lineplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ testthat::test_that("template_g_lineplot works as expected with default argument

testthat::test_that("template_g_lineplot gives correct data expression with custom arguments", {
result <- template_g_lineplot(
strata = "ARMCD",
group_var = "ARMCD",
y = "CHG",
mid = "median",
interval = "median_ci",
Expand Down

0 comments on commit 789d06f

Please sign in to comment.