Skip to content

Commit

Permalink
tdata to teal_data (#855)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: Aleksander Chlebowski <[email protected]>
Signed-off-by: Vedha Viyash <[email protected]>
Co-authored-by: go_gonzo <[email protected]>
Co-authored-by: 27856297+dependabot-preview[bot]@users.noreply.github.com <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: unknown <[email protected]>
Co-authored-by: Vedha Viyash <[email protected]>
Co-authored-by: André Veríssimo <[email protected]>
Co-authored-by: vedhav <[email protected]>
  • Loading branch information
7 people authored Dec 8, 2023
1 parent a2172bf commit 029c1cc
Show file tree
Hide file tree
Showing 39 changed files with 297 additions and 316 deletions.
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ BugReports:
https://github.com/insightsengineering/teal.modules.clinical/issues
Depends:
R (>= 4.0),
teal (>= 0.14.0.9019),
teal (>= 0.14.0.9027),
tern (>= 0.9.2.9017)
Imports:
assertthat,
Expand All @@ -53,11 +53,11 @@ Imports:
shinyWidgets,
stats,
styler,
teal.code (>= 0.4.0),
teal.data (>= 0.3.0.9010),
teal.code(>= 0.4.1.9009),
teal.data (>= 0.3.0.9018),
teal.logger (>= 0.1.1),
teal.reporter (>= 0.2.1),
teal.transform (>= 0.4.0.9007),
teal.transform (>= 0.4.0.9011),
teal.widgets (>= 0.4.0),
tern.gee (>= 0.1.0),
tern.mmrm (>= 0.2.1),
Expand Down
18 changes: 14 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# teal.modules.clinical 0.8.16.9018

### Breaking Changes
+ Adapted all modules to use `teal_data` objects.

### Enhancements
* Updated the documentation and vignettes to demonstrate method to pass `teal_data` object to `teal::init()`.
* Added parameter `sort_freq_col` to `tm_t_events` to allow the user to select column to use when sorting by decreasing frequency.
* Added parameter `incl_overall_sum` to `tm_t_events` to allow the user to choose whether overall summary rows are included at the top of the table.
* Updated the documentation and vignettes to demonstrate method to pass `teal_data` object to `teal::init()`.

### Bug fixes
* Fixed bug in `tm_g_lineplot` forcing module to initialize with a table.

# teal.modules.clinical 0.8.16.9010

### Enhancements
* Added more informative error message when grade mapping error occurs in `tm_t_abnormality_by_worst_grade`.
* Fixed label indentation in `tm_t_abnormality_by_worst_grade`.
Expand All @@ -14,13 +28,9 @@
* Updated `tm_t_pp_basic_info`, `tm_g_pp_therapy`, `tm_g_pp_adverse_events`, and `tm_t_pp_laboratory` to use `rlistings` to print data neatly in reports.
* Updated `tm_g_lineplot` to allow user to remove interval from plot.
* Updated the documentation and vignettes to demonstrate method to pass `teal_data` object to `teal::init()`.
* Added parameter `sort_freq_col` to `tm_t_events` to allow the user to select column to use when sorting by decreasing frequency.
* Added parameter `incl_overall_sum` to `tm_t_events` to allow the user to choose whether overall summary rows are included at the top of the table.
* Updated the documentation and vignettes to demonstrate method to pass `teal_data` object to `teal::init()`.

### Bug fixes
* Fixed bug in `tm_t_coxreg` preventing table from being displayed when no covariates are selected.
* Fixed bug in `tm_g_lineplot` forcing module to initialize with a table.

### Miscellaneous
* Updated `control_incidence_rate` parameter names in `tm_t_events_patyear` from `time_unit_input` and `time_unit_output` to `input_time_unit` and `num_pt_year`, respectively, after parameter names were changed in `tern`.
Expand Down
17 changes: 8 additions & 9 deletions R/tm_a_gee.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ template_a_gee <- function(output_table,
#' as.factor() %>%
#' as.numeric() %>%
#' as.factor(),
#' AVALBIN = AVAL < 50 #' Just as an example to get a binary endpoint.
#' AVALBIN = AVAL < 50 # Just as an example to get a binary endpoint.
#' ) %>%
#' droplevels()
#' })
Expand Down Expand Up @@ -369,7 +369,8 @@ srv_gee <- function(id,
basic_table_args) {
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
checkmate::assert_class(data, "tdata")
checkmate::assert_class(data, "reactive")
checkmate::assert_class(shiny::isolate(data()), "teal_data")

shiny::moduleServer(id, function(input, output, session) {
## split_covariates ----
Expand Down Expand Up @@ -401,7 +402,7 @@ srv_gee <- function(id,
input,
output,
id_arm_var = extract_input("arm_var", parentname),
data = data[[parentname]],
data = shiny::reactive(data()[[parentname]]),
arm_ref_comp = arm_ref_comp,
module = "tm_a_gee"
)
Expand Down Expand Up @@ -446,19 +447,17 @@ srv_gee <- function(id,
anl_inputs <- teal.transform::merge_expression_srv(
datasets = data,
selector_list = selector_list,
merge_function = "dplyr::inner_join",
join_keys = teal.data::join_keys(data)
merge_function = "dplyr::inner_join"
)

adsl_inputs <- teal.transform::merge_expression_module(
datasets = data,
data_extract = list(arm_var = arm_var),
anl_name = "ANL_ADSL",
join_keys = teal.data::join_keys(data)
anl_name = "ANL_ADSL"
)

anl_q <- shiny::reactive({
teal.code::new_qenv(tdata2env(data), code = get_code_tdata(data)) %>%
data() %>%
teal.code::eval_code(as.expression(anl_inputs()$expr)) %>%
teal.code::eval_code(as.expression(adsl_inputs()$expr))
})
Expand Down Expand Up @@ -570,7 +569,7 @@ srv_gee <- function(id,
card$append_text("Comment", "header3")
card$append_text(comment)
}
card$append_src(paste(teal.code::get_code(table_q()), collapse = "\n"))
card$append_src(teal.code::get_code(table_q()))
card
}
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
Expand Down
15 changes: 7 additions & 8 deletions R/tm_a_mmrm.R
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,8 @@ srv_mmrm <- function(id,
ggplot2_args) {
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
checkmate::assert_class(data, "tdata")
checkmate::assert_class(data, "reactive")
checkmate::assert_class(shiny::isolate(data()), "teal_data")

shiny::moduleServer(id, function(input, output, session) {
# Reactive responsible for sending a disable/enable signal
Expand Down Expand Up @@ -853,7 +854,7 @@ srv_mmrm <- function(id,
input,
output,
id_arm_var = extract_input("arm_var", parentname), # From UI.
data = data[[parentname]],
data = shiny::reactive(data()[[parentname]]),
arm_ref_comp = arm_ref_comp,
module = "tm_mmrm"
)
Expand Down Expand Up @@ -920,21 +921,19 @@ srv_mmrm <- function(id,
anl_inputs <- teal.transform::merge_expression_srv(
datasets = data,
selector_list = selector_list_without_cov,
join_keys = teal.data::join_keys(data),
merge_function = "dplyr::inner_join"
)

adsl_merge_inputs <- teal.transform::merge_expression_module(
datasets = data,
data_extract = list(arm_var = arm_var),
join_keys = teal.data::join_keys(data),
anl_name = "ANL_ADSL"
)

anl_q <- shiny::reactive({
qenv <- teal.code::new_qenv(tdata2env(data), code = get_code_tdata(data))
qenv2 <- teal.code::eval_code(qenv, as.expression(anl_inputs()$expr))
teal.code::eval_code(qenv2, as.expression(adsl_merge_inputs()$expr))
data() %>%
teal.code::eval_code(code = as.expression(anl_inputs()$expr)) %>%
teal.code::eval_code(code = as.expression(adsl_merge_inputs()$expr))
})

# Initially hide the output title because there is no output yet.
Expand Down Expand Up @@ -1457,7 +1456,7 @@ srv_mmrm <- function(id,
card$append_text("Comment", "header3")
card$append_text(comment)
}
card$append_src(paste(teal.code::get_code(all_q()), collapse = "\n"))
card$append_src(teal.code::get_code(all_q()))
card
}
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
Expand Down
87 changes: 46 additions & 41 deletions R/tm_g_barchart_simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ srv_g_barchart_simple <- function(id,
ggplot2_args) {
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
checkmate::assert_class(data, "tdata")
checkmate::assert_class(data, "reactive")
checkmate::assert_class(shiny::isolate(data()), "teal_data")

shiny::moduleServer(id, function(input, output, session) {
rule_dupl <- function(others) {
Expand Down Expand Up @@ -381,19 +382,17 @@ srv_g_barchart_simple <- function(id,

anl_inputs <- teal.transform::merge_expression_srv(
datasets = data,
join_keys = teal.data::join_keys(data),
selector_list = selector_list
)

anl_q <- shiny::reactive({
qenv <- teal.code::new_qenv(tdata2env(data), code = get_code_tdata(data))
qenv <- teal.code::eval_code(qenv, as.expression(anl_inputs()$expr))
qenv
data() %>%
teal.code::eval_code(as.expression(anl_inputs()$expr))
})

count_q <- shiny::reactive({
qenv <- anl_q()
teal::validate_has_data(qenv[["ANL"]], 2)
anl_q <- anl_q()
teal::validate_has_data(anl_q[["ANL"]], 2)
groupby_vars <- r_groupby_vars()

# count
Expand All @@ -409,51 +408,46 @@ srv_g_barchart_simple <- function(id,
count_str_to_col_exprs <- sapply(groupby_vars[-1], count_str_to_column_expr)
count_exprs <- c(count_exprs, count_exprs2, count_str_to_col_exprs)
}
qenv2 <- teal.code::eval_code(qenv, code = count_exprs)

data_list <- sapply(teal.data::datanames(data()), function(x) shiny::reactive(data()[[x]]),
simplify = FALSE
)

anl_q <- anl_q %>%
teal.code::eval_code(code = count_exprs)

# add label and slice(1) as all patients in the same subgroup have same n_'s
qenv3 <- teal.code::eval_code(
qenv2,
as.expression(
c(
bquote(attr(counts[[.(get_n_name(groupby_vars))]], "label") <- "Count"),
bquote(
counts <- counts %>%
dplyr::group_by_at(.(as.vector(groupby_vars))) %>%
dplyr::slice(1) %>%
dplyr::ungroup() %>%
dplyr::select(.(as.vector(groupby_vars)), dplyr::starts_with("n_"))
anl_q <- anl_q %>%
teal.code::eval_code(
as.expression(
c(
bquote(attr(counts[[.(get_n_name(groupby_vars))]], "label") <- "Count"),
bquote(
counts <- counts %>%
dplyr::group_by_at(.(as.vector(groupby_vars))) %>%
dplyr::slice(1) %>%
dplyr::ungroup() %>%
dplyr::select(.(as.vector(groupby_vars)), dplyr::starts_with("n_"))
)
)
)
)
)

# dplyr::select loses labels
teal.code::eval_code(
qenv3,
teal.transform::get_anl_relabel_call(
columns_source = anl_inputs()$columns_source,
datasets = data,
anl_name = "counts"
anl_q %>%
teal.code::eval_code(
teal.transform::get_anl_relabel_call(
columns_source = anl_inputs()$columns_source,
datasets = data_list,
anl_name = "counts"
)
)
)
})

all_q <- shiny::reactive({
teal::validate_inputs(iv_r())
groupby_vars <- as.list(r_groupby_vars()) # so $ access works below

ANL <- count_q()[["ANL"]] # nolint

qenv2 <- teal.code::eval_code(count_q(), substitute(
env = list(groupby_vars = paste(groupby_vars, collapse = ", ")),
plot_title <- sprintf(
"Number of patients (total N = %s) for each combination of (%s)",
nrow(ANL),
groupby_vars
)
))

y_lab <- substitute(
column_annotation_label(counts, y_name),
list(y_name = get_n_name(groupby_vars))
Expand Down Expand Up @@ -487,11 +481,22 @@ srv_g_barchart_simple <- function(id,
ggplot2_args = all_ggplot2_args
)

qenv3 <- teal.code::eval_code(qenv2, code = plot_call)
ANL <- count_q()[["ANL"]] # nolint

all_q <- count_q() %>%
teal.code::eval_code(substitute(
env = list(groupby_vars = paste(groupby_vars, collapse = ", ")),
plot_title <- sprintf(
"Number of patients (total N = %s) for each combination of (%s)",
nrow(ANL),
groupby_vars
)
)) %>%
teal.code::eval_code(code = plot_call)

# explicitly calling print on the plot inside the qenv evaluates
# the ggplot call and therefore catches errors
teal.code::eval_code(qenv3, code = quote(print(plot)))
teal.code::eval_code(all_q, code = quote(print(plot)))
})

plot_r <- shiny::reactive(all_q()[["plot"]])
Expand Down Expand Up @@ -558,7 +563,7 @@ srv_g_barchart_simple <- function(id,
card$append_text("Comment", "header3")
card$append_text(comment)
}
card$append_src(paste(teal.code::get_code(all_q()), collapse = "\n"))
card$append_src(teal.code::get_code(all_q()))
card
}
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
Expand Down
21 changes: 10 additions & 11 deletions R/tm_g_ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ srv_g_ci <- function(id, # nolint
ggplot2_args) {
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
checkmate::assert_class(data, "tdata")
checkmate::assert_class(data, "reactive")
checkmate::assert_class(shiny::isolate(data()), "teal_data")

shiny::moduleServer(id, function(input, output, session) {
selector_list <- teal.transform::data_extract_multiple_srv(
Expand Down Expand Up @@ -407,12 +408,10 @@ srv_g_ci <- function(id, # nolint
selector_list = selector_list
)

anl_q <- shiny::reactive(
teal.code::eval_code(
object = teal.code::new_qenv(tdata2env(data), code = get_code_tdata(data)),
code = as.expression(anl_inputs()$expr)
)
)
anl_q <- shiny::reactive({
data() %>%
teal.code::eval_code(as.expression(anl_inputs()$expr))
})

all_q <- shiny::reactive({
teal::validate_inputs(iv_r())
Expand All @@ -429,10 +428,10 @@ srv_g_ci <- function(id, # nolint
)
)

x_label <- column_annotation_label(data[[attr(x, "dataname")]](), x)
y_label <- column_annotation_label(data[[attr(y, "dataname")]](), y)
x_label <- column_annotation_label(data()[[attr(x, "dataname")]], x)
y_label <- column_annotation_label(data()[[attr(y, "dataname")]], y)
color_label <- if (length(color)) {
column_annotation_label(data[[attr(color, "dataname")]](), color)
column_annotation_label(data()[[attr(color, "dataname")]], color)
} else {
NULL
}
Expand Down Expand Up @@ -501,7 +500,7 @@ srv_g_ci <- function(id, # nolint
card$append_text("Comment", "header3")
card$append_text(comment)
}
card$append_src(paste(teal.code::get_code(all_q()), collapse = "\n"))
card$append_src(teal.code::get_code(all_q()))
card
}
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
Expand Down
Loading

0 comments on commit 029c1cc

Please sign in to comment.