Skip to content

Commit

Permalink
Introduce decorator for tm_t_events_patyear (#1277)
Browse files Browse the repository at this point in the history
Part of insightsengineering/teal#1371


<details>
<summary>Example with decorator</summary>

```r
devtools::load_all("../tern")
devtools::load_all("../teal")
devtools::load_all(".")
library(dplyr)

data <- teal_data()
data <- within(data, {
  ADSL <- tmc_ex_adsl
  ADAETTE <- tmc_ex_adaette %>%
    filter(PARAMCD %in% c("AETTE1", "AETTE2", "AETTE3")) %>%
    mutate(is_event = CNSR == 0) %>%
    mutate(n_events = as.integer(is_event))
})
join_keys(data) <- default_cdisc_join_keys[names(data)]

ADSL <- data[["ADSL"]]
ADAETTE <- data[["ADAETTE"]]

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

# 1. Basic Example

app <- init(
  data = data,
  modules = modules(
    tm_t_events_patyear(
      label = "AE Rate Adjusted for Patient-Years At Risk Table",
      dataname = "ADAETTE",
      arm_var = choices_selected(
        choices = variable_choices(ADSL, c("ARM", "ARMCD")),
        selected = "ARMCD"
      ),
      add_total = TRUE,
      events_var = choices_selected(
        choices = variable_choices(ADAETTE, "n_events"),
        selected = "n_events",
        fixed = TRUE
      ),
      paramcd = choices_selected(
        choices = value_choices(ADAETTE, "PARAMCD", "PARAM"),
        selected = "AETTE1"
      ),
    decorators = list(insert_rrow_decorator())
    )
  )
)
if (interactive()) {
  shinyApp(app$ui, app$server)
}

```

</details>
llrs-roche authored Dec 4, 2024
1 parent 37e42a2 commit 28ba4c4
Showing 2 changed files with 51 additions and 11 deletions.
41 changes: 31 additions & 10 deletions R/tm_t_events_patyear.R
Original file line number Diff line number Diff line change
@@ -168,8 +168,7 @@ template_events_patyear <- function(dataname,
# table
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))
)
@@ -190,9 +189,19 @@ template_events_patyear <- function(dataname,
#' second variable will be nested under the first variable.
#' @param events_var ([teal.transform::choices_selected()])\cr object with
#' all available choices and preselected option for the variable with all event counts.
#' @param decorators `r roxygen_decorators_param("tm_t_events_patyear")`
#'
#' @inherit module_arguments return seealso
#'
#' @section Decorating `tm_t_events_patyear`:
#'
#' This module generates the following objects, which can be modified in place using decorators:
#' - `table` (`TableTree` as created from `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.
#'
#'
#' @examples
#' library(dplyr)
#'
@@ -310,7 +319,8 @@ tm_t_events_patyear <- function(label,
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_events_patyear")
checkmate::assert_string(label)
checkmate::assert_string(dataname)
@@ -328,6 +338,8 @@ tm_t_events_patyear <- 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, "table", null.ok = TRUE)

args <- c(as.list(environment()))

@@ -352,7 +364,8 @@ tm_t_events_patyear <- 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)
@@ -422,6 +435,7 @@ ui_events_patyear <- function(id, ...) {
multiple = FALSE,
fixed = FALSE
),
ui_decorate_teal_data(ns("decorator"), decorators = select_decorators(a$decorators, "table")),
teal.widgets::panel_group(
teal.widgets::panel_item(
"Additional table settings",
@@ -473,7 +487,8 @@ srv_events_patyear <- function(id,
na_level,
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")
@@ -627,10 +642,16 @@ srv_events_patyear <- function(id,
teal.code::eval_code(merged$anl_q(), as.expression(unlist(my_calls)))
})


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

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

teal.widgets::table_with_settings_srv(
id = "patyear_table",
@@ -640,7 +661,7 @@ srv_events_patyear <- function(id,
# Render R code.
teal.widgets::verbatim_popup_srv(
id = "rcode",
verbatim_content = reactive(teal.code::get_code(table_q())),
verbatim_content = reactive(teal.code::get_code(req(decorated_table_q()))),
title = label
)

@@ -659,7 +680,7 @@ srv_events_patyear <- function(id,
card$append_text("Comment", "header3")
card$append_text(comment)
}
card$append_src(teal.code::get_code(table_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)
21 changes: 20 additions & 1 deletion man/tm_t_events_patyear.Rd

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

0 comments on commit 28ba4c4

Please sign in to comment.