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

introduce decorators for tm_t_abnormality_by_worst_grade #1260

36 changes: 28 additions & 8 deletions R/tm_t_abnormality_by_worst_grade.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ template_abnormality_by_worst_grade <- function(parentname, # nolint: object_len

y$table <- substitute(
expr = {
result <- rtables::build_table(lyt = lyt, df = anl, alt_counts_df = parent)
result
table <- rtables::build_table(lyt = lyt, df = anl, alt_counts_df = parent)
},
env = list(parent = as.name(parentname))
)
Expand All @@ -249,6 +248,14 @@ template_abnormality_by_worst_grade <- function(parentname, # nolint: object_len
#'
#' @inherit module_arguments return seealso
#'
#' @section Decorating `tm_t_abnormality_by_worst_grade`:
#'
#' This module generates the following objects, which can be modified in place using decorators:
#' - `table` (`ElementaryTable` - output of `rtables::build_table`)
#'
#' For additional details and examples of decorators, refer to the vignette
#' `vignette("decorate-modules-output", package = "teal")` or the [`teal_transform_module()`] documentation.
#'
#' @export
#'
#' @examplesShinylive
Expand Down Expand Up @@ -339,7 +346,8 @@ tm_t_abnormality_by_worst_grade <- function(label, # nolint: object_length.
drop_arm_levels = TRUE,
pre_output = NULL,
post_output = NULL,
basic_table_args = teal.widgets::basic_table_args()) {
basic_table_args = teal.widgets::basic_table_args(),
decorators = NULL) {
message("Initializing tm_t_abnormality_by_worst_grade")
checkmate::assert_string(label)
checkmate::assert_string(dataname)
Expand All @@ -355,6 +363,8 @@ tm_t_abnormality_by_worst_grade <- function(label, # nolint: object_length.
checkmate::assert_class(pre_output, classes = "shiny.tag", null.ok = TRUE)
checkmate::assert_class(post_output, classes = "shiny.tag", null.ok = TRUE)
checkmate::assert_class(basic_table_args, "basic_table_args")
decorators <- normalize_decorators(decorators)
assert_decorators(decorators, null.ok = TRUE, "table")

data_extract_list <- list(
arm_var = cs_to_des_select(arm_var, dataname = parentname),
Expand All @@ -380,7 +390,8 @@ tm_t_abnormality_by_worst_grade <- function(label, # nolint: object_length.
label = label,
worst_flag_indicator = worst_flag_indicator,
total_label = total_label,
basic_table_args = basic_table_args
basic_table_args = basic_table_args,
decorators = decorators
)
),
datanames = teal.transform::get_extract_datanames(data_extract_list)
Expand Down Expand Up @@ -452,6 +463,7 @@ ui_t_abnormality_by_worst_grade <- function(id, ...) { # nolint: object_length.
data_extract_spec = a$worst_high_flag_var,
is_single_dataset = is_single_dataset_value
),
ui_decorate_teal_data(ns("decorator"), decorators = select_decorators(a$decorators, "table")),
teal.widgets::panel_group(
teal.widgets::panel_item(
"Additional table settings",
Expand Down Expand Up @@ -501,7 +513,8 @@ srv_t_abnormality_by_worst_grade <- function(id, # nolint: object_length.
total_label,
drop_arm_levels,
label,
basic_table_args) {
basic_table_args,
decorators) {
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
checkmate::assert_class(data, "reactive")
Expand Down Expand Up @@ -664,8 +677,15 @@ srv_t_abnormality_by_worst_grade <- function(id, # nolint: object_length.
teal.code::eval_code(merged$anl_q(), as.expression(unlist(my_calls)))
})

decorated_table_q <- srv_decorate_teal_data(
id = "decorator",
data = all_q,
decorators = select_decorators(decorators, "table"),
expr = table
)

# Outputs to render.
table_r <- reactive(all_q()[["result"]])
table_r <- reactive(decorated_table_q()[["table"]])

teal.widgets::table_with_settings_srv(
id = "table",
Expand All @@ -675,7 +695,7 @@ srv_t_abnormality_by_worst_grade <- function(id, # nolint: object_length.
# Render R code.
teal.widgets::verbatim_popup_srv(
id = "rcode",
verbatim_content = reactive(teal.code::get_code(all_q())),
verbatim_content = reactive(teal.code::get_code(req(decorated_table_q()))),
title = label
)

Expand All @@ -695,7 +715,7 @@ srv_t_abnormality_by_worst_grade <- function(id, # nolint: object_length.
card$append_text("Comment", "header3")
card$append_text(comment)
}
card$append_src(teal.code::get_code(all_q()))
card$append_src(teal.code::get_code(req(decorated_table_q())))
card
}
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
Expand Down