Skip to content

Commit

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

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

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

rlisting_footer <- function(default_footer = "I am a good footer", .var_to_replace = "table_listing") {
  teal_transform_module(
    label = "New row",
    ui = function(id) shiny::textInput(shiny::NS(id, "footer"), "footer", value = default_footer),
    server = make_teal_transform_server(
      substitute({
        rlistings::main_footer(.var_to_replace) <- footer
      }, env = list(.var_to_replace = as.name(.var_to_replace)))
    )
  )
}

library(dplyr)
data <- teal_data()
data <- within(data, {
  ADCM <- tmc_ex_adcm
  ADSL <- tmc_ex_adsl %>% filter(USUBJID %in% ADCM$USUBJID)
  ADCM$CMASTDTM <- ADCM$ASTDTM
  ADCM$CMAENDTM <- ADCM$AENDTM
})
join_keys(data) <- default_cdisc_join_keys[names(data)]
adcm_keys <- c("STUDYID", "USUBJID", "ASTDTM", "CMSEQ", "ATC1", "ATC2", "ATC3", "ATC4")
join_keys(data)["ADCM", "ADCM"] <- adcm_keys

ADSL <- data[["ADSL"]]
ADCM <- data[["ADCM"]]

init(
  data = data,
  modules = modules(
    tm_t_pp_prior_medication(
      label = "Prior Medication",
      dataname = "ADCM",
      parentname = "ADSL",
      patient_col = "USUBJID",
      atirel = choices_selected(
        choices = variable_choices(ADCM, "ATIREL"),
        selected = "ATIREL"
      ),
      cmdecod = choices_selected(
        choices = variable_choices(ADCM, "CMDECOD"),
        selected = "CMDECOD"
      ),
      cmindc = choices_selected(
        choices = variable_choices(ADCM, "CMINDC"),
        selected = "CMINDC"
      ),
      cmstdy = choices_selected(
        choices = variable_choices(ADCM, "ASTDY"),
        selected = "ASTDY"
      ),
      decorators = list(
        table = rlisting_footer(.var_to_replace = "table")
      )
    )
  )
) |> shiny::runApp()
```

</details>
  • Loading branch information
averissimo authored Dec 11, 2024
1 parent 95f11f3 commit b85523c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 15 deletions.
67 changes: 53 additions & 14 deletions R/tm_t_pp_prior_medication.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ template_prior_medication <- function(dataname = "ANL",
dplyr::filter(!is.na(cmdecod)) %>%
dplyr::distinct() %>%
`colnames<-`(col_labels(dataname, fill = TRUE)[c(cmindc_char, cmdecod_char, cmstdy_char)])
result

table <- result %>%
dplyr::mutate( # Exception for columns of type difftime that is not supported by as_listing
dplyr::across(
dplyr::where(~ inherits(., what = "difftime")), ~ as.double(., units = "auto")
)
) %>%
rlistings::as_listing()
}, env = list(
dataname = as.name(dataname),
atirel = as.name(atirel),
Expand Down Expand Up @@ -61,6 +68,14 @@ template_prior_medication <- function(dataname = "ANL",
#'
#' @inherit module_arguments return
#'
#' @section Decorating Module:
#'
#' This module generates the following objects, which can be modified in place using decorators:
#' - `table` (`listing_df` - output of `rlistings::as_listing`)
#'
#' 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 @@ -123,7 +138,8 @@ tm_t_pp_prior_medication <- function(label,
cmindc = NULL,
cmstdy = NULL,
pre_output = NULL,
post_output = NULL) {
post_output = NULL,
decorators = NULL) {
message("Initializing tm_t_pp_prior_medication")
checkmate::assert_string(label)
checkmate::assert_string(dataname)
Expand All @@ -135,6 +151,8 @@ tm_t_pp_prior_medication <- function(label,
checkmate::assert_class(cmstdy, "choices_selected", null.ok = TRUE)
checkmate::assert_class(pre_output, classes = "shiny.tag", null.ok = TRUE)
checkmate::assert_class(post_output, classes = "shiny.tag", null.ok = TRUE)
decorators <- normalize_decorators(decorators)
assert_decorators(decorators, null.ok = TRUE, "table")

args <- as.list(environment())
data_extract_list <- list(
Expand All @@ -155,7 +173,8 @@ tm_t_pp_prior_medication <- function(label,
dataname = dataname,
parentname = parentname,
label = label,
patient_col = patient_col
patient_col = patient_col,
decorators = decorators
)
),
datanames = c(dataname, parentname)
Expand Down Expand Up @@ -212,7 +231,8 @@ ui_t_prior_medication <- function(id, ...) {
label = "Select CMSTDY variable:",
data_extract_spec = ui_args$cmstdy,
is_single_dataset = is_single_dataset_value
)
),
ui_decorate_teal_data(ns("decorator"), decorators = select_decorators(ui_args$decorators, "table")),
),
forms = tagList(
teal.widgets::verbatim_popup_ui(ns("rcode"), button_label = "Show R code")
Expand All @@ -234,7 +254,8 @@ srv_t_prior_medication <- function(id,
cmdecod,
cmindc,
cmstdy,
label) {
label,
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 @@ -303,6 +324,7 @@ srv_t_prior_medication <- function(id,
teal.code::eval_code(as.expression(anl_inputs()$expr))
})

# Generate r code for the analysis.
all_q <- reactive({
teal::validate_inputs(iv_r())

Expand All @@ -328,18 +350,35 @@ srv_t_prior_medication <- function(id,
teal.code::eval_code(as.expression(unlist(my_calls)))
})

table_r <- reactive(all_q()[["result"]])
# Decoration of table output.
decorated_table_q <- srv_decorate_teal_data(
id = "decorator",
data = all_q,
decorators = select_decorators(decorators, "table"),
expr = table
)

output$prior_medication_table <- DT::renderDataTable(
expr = table_r(),
options = list(
lengthMenu = list(list(-1, 5, 10, 25), list("All", "5", "10", "25"))
# Outputs to render.
table_r <- reactive({
q <- decorated_table_q()
list(
html = DT::datatable(
q[["result"]],
options = list(
lengthMenu = list(list(-1, 5, 10, 25), list("All", "5", "10", "25"))
)
),
listing = q[["table"]]
)
)
})

output$prior_medication_table <- DT::renderDataTable(expr = table_r()$html)

# 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 @@ -352,12 +391,12 @@ srv_t_prior_medication <- function(id,
filter_panel_api = filter_panel_api
)
card$append_text("Table", "header3")
card$append_table(table_r())
card$append_table(table_r()$listing)
if (!comment == "") {
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_pp_prior_medication.Rd

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

0 comments on commit b85523c

Please sign in to comment.