Skip to content

Commit

Permalink
Adds decorators to tm_t_shift_by_grade (#1285)
Browse files Browse the repository at this point in the history
Part of insightsengineering/teal#1371

<details>
<summary>Working example</summary>

```r
pkgload::load_all("../teal.modules.clinical", export_all = FALSE)
# Example below

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)))
    )
  )
}

data <- teal_data()
data <- within(data, {
  ADSL <- tmc_ex_adsl
  ADLB <- tmc_ex_adlb
})
join_keys(data) <- default_cdisc_join_keys[names(data)]

ADSL <- data[["ADSL"]]
ADLB <- data[["ADLB"]]

init(
  data = data,
  modules = modules(
    tm_t_shift_by_grade(
      label = "Grade Laboratory Abnormality Table",
      dataname = "ADLB",
      arm_var = choices_selected(
        choices = variable_choices(ADSL, subset = c("ARM", "ARMCD")),
        selected = "ARM"
      ),
      paramcd = choices_selected(
        choices = value_choices(ADLB, "PARAMCD", "PARAM"),
        selected = "ALT"
      ),
      worst_flag_var = choices_selected(
        choices = variable_choices(ADLB, subset = c("WGRLOVFL", "WGRLOFL", "WGRHIVFL", "WGRHIFL")),
        selected = c("WGRLOVFL")
      ),
      worst_flag_indicator = choices_selected(
        value_choices(ADLB, "WGRLOVFL"),
        selected = "Y", fixed = TRUE
      ),
      anl_toxgrade_var = choices_selected(
        choices = variable_choices(ADLB, subset = c("ATOXGR")),
        selected = c("ATOXGR"),
        fixed = TRUE
      ),
      base_toxgrade_var = choices_selected(
        choices = variable_choices(ADLB, subset = c("BTOXGR")),
        selected = c("BTOXGR"),
        fixed = TRUE
      ),
      add_total = FALSE,
      decorators = list(insert_rrow_decorator())
    )
  ),
  filter = teal_slices(teal_slice("ADSL", "SAFFL", selected = "Y"))
) |> shiny::runApp()
```

</details>

---------

Co-authored-by: Marcin <[email protected]>
  • Loading branch information
averissimo and m7pr authored Dec 10, 2024
1 parent 015a964 commit d29d166
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
39 changes: 31 additions & 8 deletions R/tm_t_shift_by_grade.R
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,8 @@ template_shift_by_grade <- function(parentname,

y$table <- substitute(
expr = {
result <- rtables::build_table(lyt = lyt, df = anl, alt_counts_df = parent) %>%
table <- rtables::build_table(lyt = lyt, df = anl, alt_counts_df = parent) %>%
rtables::prune_table()
result
},
env = list(parent = as.name(parentname))
)
Expand All @@ -472,6 +471,14 @@ template_shift_by_grade <- function(parentname,
#'
#' @inherit module_arguments return seealso
#'
#' @section Decorating Module:
#'
#' This module generates the following objects, which can be modified in place using decorators:
#' - `table` (`TableTree` - 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.
#'
#' @examplesShinylive
#' library(teal.modules.clinical)
#' interactive <- function() TRUE
Expand Down Expand Up @@ -572,7 +579,8 @@ tm_t_shift_by_grade <- function(label,
post_output = NULL,
na_level = default_na_str(),
code_missing_baseline = FALSE,
basic_table_args = teal.widgets::basic_table_args()) {
basic_table_args = teal.widgets::basic_table_args(),
decorators = NULL) {
message("Initializing tm_t_shift_by_grade")
checkmate::assert_string(label)
checkmate::assert_string(dataname)
Expand All @@ -593,6 +601,8 @@ tm_t_shift_by_grade <- function(label,
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")

args <- as.list(environment())

Expand All @@ -619,7 +629,8 @@ tm_t_shift_by_grade <- function(label,
label = label,
total_label = total_label,
na_level = na_level,
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 @@ -704,6 +715,7 @@ ui_t_shift_by_grade <- function(id, ...) {
)
)
),
ui_decorate_teal_data(ns("decorator"), decorators = select_decorators(a$decorators, "table")),
teal.widgets::panel_group(
teal.widgets::panel_item(
"Additional Variables Info",
Expand Down Expand Up @@ -751,7 +763,8 @@ srv_t_shift_by_grade <- function(id,
drop_arm_levels,
na_level,
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 @@ -840,6 +853,7 @@ srv_t_shift_by_grade <- function(id,
)
})

# Generate r code for the analysis.
all_q <- reactive({
validate_checks()

Expand All @@ -865,18 +879,27 @@ srv_t_shift_by_grade <- function(id,
teal.code::eval_code(merged$anl_q(), as.expression(unlist(my_calls)))
})

# Decoration of table output.
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",
table_r = table_r
)

# Render R code.
source_code_r <- reactive(teal.code::get_code(req(decorated_table_q())))
teal.widgets::verbatim_popup_srv(
id = "rcode",
verbatim_content = reactive(teal.code::get_code(all_q())),
verbatim_content = source_code_r,
title = label
)

Expand All @@ -895,7 +918,7 @@ srv_t_shift_by_grade <- function(id,
card$append_text("Comment", "header3")
card$append_text(comment)
}
card$append_src(teal.code::get_code(all_q()))
card$append_src(source_code_r())
card
}
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
Expand Down
24 changes: 23 additions & 1 deletion man/tm_t_shift_by_grade.Rd

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

0 comments on commit d29d166

Please sign in to comment.