Skip to content

Commit 78c451d

Browse files
Introduce decorators for tm_t_binary_outcome (#1273)
Part of insightsengineering/teal#1371 <details> <summary>Working example of the decorators</summary> ````r load_all("../teal.code") load_all("../teal.data") load_all("../teal.reporter") load_all("../teal") load_all(".") library(dplyr) data <- within(teal_data(), { ADSL <- tmc_ex_adsl ADRS <- tmc_ex_adrs %>% mutate( AVALC = d_onco_rsp_label(AVALC) %>% with_label("Character Result/Finding") ) %>% filter(PARAMCD != "OVRINV" | AVISIT == "FOLLOW UP") }) join_keys(data) <- default_cdisc_join_keys[names(data)] ADSL <- data[["ADSL"]] ADRS <- data[["ADRS"]] insert_rrow_decorator <- function(default_caption = "I am a good new row", .var_to_replace = "table") { teal_transform_module( label = "New row", ui = function(id) shiny::textInput(shiny::NS(id, "new_row"), "New row", value = default_caption), server = make_teal_transform_server( substitute({ .var_to_replace <- rtables::insert_rrow(.var_to_replace, rtables::rrow(new_row)) }, env = list(.var_to_replace = as.name(.var_to_replace))) ) ) } arm_ref_comp <- list( ARMCD = list(ref = "ARM B", comp = c("ARM A", "ARM C")), ARM = list(ref = "B: Placebo", comp = c("A: Drug X", "C: Combination")) ) app <- init( data = data, modules = modules( tm_t_binary_outcome( label = "Responders", dataname = "ADRS", paramcd = choices_selected( choices = value_choices(ADRS, "PARAMCD", "PARAM"), selected = "BESRSPI" ), arm_var = choices_selected( choices = variable_choices(ADRS, c("ARM", "ARMCD", "ACTARMCD")), selected = "ARM" ), arm_ref_comp = arm_ref_comp, strata_var = choices_selected( choices = variable_choices(ADRS, c("SEX", "BMRKR2", "RACE")), selected = "RACE" ), default_responses = list( BESRSPI = list( rsp = c("Complete Response (CR)", "Partial Response (PR)"), levels = c( "Complete Response (CR)", "Partial Response (PR)", "Stable Disease (SD)", "Progressive Disease (PD)" ) ), INVET = list( rsp = c("Stable Disease (SD)", "Not Evaluable (NE)"), levels = c( "Complete Response (CR)", "Not Evaluable (NE)", "Partial Response (PR)", "Progressive Disease (PD)", "Stable Disease (SD)" ) ), OVRINV = list( rsp = c("Progressive Disease (PD)", "Stable Disease (SD)"), levels = c("Progressive Disease (PD)", "Stable Disease (SD)", "Not Evaluable (NE)") ) ), decorators = list(insert_rrow_decorator("I am a new row")) ) ) ) if (interactive()) { shinyApp(app$ui, app$server) } ``` </details> --------- Signed-off-by: Lluís Revilla <[email protected]> Co-authored-by: André Veríssimo <[email protected]>
1 parent 5b87fdc commit 78c451d

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

R/tm_t_binary_outcome.R

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,7 @@ template_binary_outcome <- function(dataname,
314314

315315
y$table <- substitute(
316316
expr = {
317-
result <- rtables::build_table(lyt = lyt, df = anl, alt_counts_df = parentname)
318-
result
317+
table <- rtables::build_table(lyt = lyt, df = anl, alt_counts_df = parentname)
319318
},
320319
env = list(parentname = as.name(parentname))
321320
)
@@ -348,9 +347,18 @@ template_binary_outcome <- function(dataname,
348347
#' `"Not Evaluable (NE)"`, or `"Missing or unevaluable"`, 95% confidence interval will not be calculated.
349348
#'
350349
#' * Reference arms are automatically combined if multiple arms selected as reference group.
350+
#' @param decorators `r roxygen_decorators_param("tm_t_binary_outcome")`
351351
#'
352352
#' @inherit module_arguments return seealso
353353
#'
354+
#' @section Decorating `tm_t_binary_outcome`:
355+
#'
356+
#' This module generates the following objects, which can be modified in place using decorators:
357+
#' - `table` (`TableTree` - output of `rtables::build_table`)
358+
#'
359+
#' For additional details and examples of decorators, refer to the vignette
360+
#' `vignette("decorate-modules-output", package = "teal")` or the [`teal_transform_module()`] documentation.
361+
#'
354362
#' @examplesShinylive
355363
#' library(teal.modules.clinical)
356364
#' interactive <- function() TRUE
@@ -464,7 +472,8 @@ tm_t_binary_outcome <- function(label,
464472
na_level = default_na_str(),
465473
pre_output = NULL,
466474
post_output = NULL,
467-
basic_table_args = teal.widgets::basic_table_args()) {
475+
basic_table_args = teal.widgets::basic_table_args(),
476+
decorators = NULL) {
468477
message("Initializing tm_t_binary_outcome")
469478
checkmate::assert_string(label)
470479
checkmate::assert_string(dataname)
@@ -504,6 +513,8 @@ tm_t_binary_outcome <- function(label,
504513
control$strat$method_ci, c("wald", "waldcc", "cmh", "ha", "strat_newcombe", "strat_newcombecc")
505514
)
506515
checkmate::assert_subset(control$strat$method_test, c("cmh"))
516+
decorators <- normalize_decorators(decorators)
517+
assert_decorators(decorators, "table", null.ok = TRUE)
507518

508519
args <- as.list(environment())
509520

@@ -531,7 +542,8 @@ tm_t_binary_outcome <- function(label,
531542
control = control,
532543
rsp_table = rsp_table,
533544
na_level = na_level,
534-
basic_table_args = basic_table_args
545+
basic_table_args = basic_table_args,
546+
decorators = decorators
535547
)
536548
),
537549
datanames = teal.transform::get_extract_datanames(data_extract_list)
@@ -678,6 +690,7 @@ ui_t_binary_outcome <- function(id, ...) {
678690
condition = paste0("!input['", ns("compare_arms"), "']"),
679691
checkboxInput(ns("add_total"), "Add All Patients column", value = a$add_total)
680692
),
693+
ui_decorate_teal_data(ns("decorator"), decorators = select_decorators(a$decorators, "table")),
681694
teal.widgets::panel_item(
682695
"Additional table settings",
683696
teal.widgets::optionalSelectInput(
@@ -745,7 +758,8 @@ srv_t_binary_outcome <- function(id,
745758
default_responses,
746759
rsp_table,
747760
na_level,
748-
basic_table_args) {
761+
basic_table_args,
762+
decorators) {
749763
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
750764
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
751765
checkmate::assert_class(data, "reactive")
@@ -995,8 +1009,16 @@ srv_t_binary_outcome <- function(id,
9951009
teal.code::eval_code(qenv, as.expression(unlist(my_calls)))
9961010
})
9971011

1012+
1013+
decorated_all_q <- srv_decorate_teal_data(
1014+
id = "decorator",
1015+
data = table_q,
1016+
decorators = select_decorators(decorators, "table"),
1017+
expr = table
1018+
)
1019+
9981020
# Outputs to render.
999-
table_r <- reactive(table_q()[["result"]])
1021+
table_r <- reactive(decorated_all_q()[["table"]])
10001022

10011023
teal.widgets::table_with_settings_srv(
10021024
id = "table",
@@ -1007,7 +1029,7 @@ srv_t_binary_outcome <- function(id,
10071029
teal.widgets::verbatim_popup_srv(
10081030
id = "rcode",
10091031
verbatim_content = reactive({
1010-
teal.code::get_code(table_q())
1032+
teal.code::get_code(req(decorated_all_q()))
10111033
}),
10121034
title = label
10131035
)
@@ -1027,7 +1049,7 @@ srv_t_binary_outcome <- function(id,
10271049
card$append_text("Comment", "header3")
10281050
card$append_text(comment)
10291051
}
1030-
card$append_src(teal.code::get_code(table_q()))
1052+
card$append_src(teal.code::get_code(req(decorated_all_q())))
10311053
card
10321054
}
10331055
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)

man/tm_t_binary_outcome.Rd

Lines changed: 23 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)