Skip to content

Commit 1ac634c

Browse files
m7prllrs-rocheaverissimo
authored
introduce decorators for tm_g_barchart_simple (#1267)
Part of insightsengineering/teal#1371 #### Checklist for final review: - Has documentation: - [ ] `` #' @param decorators `r roxygen_decorators_param("tm_X_XXX")` `` - [ ] Section for decorators - [ ] In case of multiple decorators, section has pseudo code explaining how to limit calls - [ ] Code shows in "Show R code" - [ ] Reporter shows both the outputs and code - [ ] Fix order of `assert_decorators` function (`null.ok = TRUE` is sometimes second argument and looks better at 3^rd) #### Example <details><summary> Working Example </summary> ```r devtools::load_all("../teal.reporter") devtools::load_all("../teal") devtools::load_all(".") library(nestcolor) library(dplyr) data <- teal_data() data <- within(data, { ADSL <- tmc_ex_adsl %>% mutate(ITTFL = factor("Y") %>% with_label("Intent-To-Treat Population Flag")) ADAE <- tmc_ex_adae %>% filter(!((AETOXGR == 1) & (AESEV == "MILD") & (ARM == "A: Drug X"))) }) join_keys(data) <- default_cdisc_join_keys[names(data)] ADSL <- data[["ADSL"]] ADAE <- data[["ADAE"]] caption_decorator <- function(default_caption = "I am a good decorator", .var_to_replace = "plot") { teal_transform_module( label = "Caption", ui = function(id) shiny::textInput(shiny::NS(id, "footnote"), "Footnote", value = default_caption), server = make_teal_transform_server( substitute({ .var_to_replace <- .var_to_replace + ggplot2::labs(caption = footnote) }, env = list(.var_to_replace = as.name(.var_to_replace))) ) ) } head_decorator <- function(default_value = 6, .var_to_replace = "object") { teal_transform_module( label = "Head", ui = function(id) shiny::numericInput(shiny::NS(id, "n"), "N rows", value = default_value), server = make_teal_transform_server( substitute({ .var_to_replace <- utils::head(.var_to_replace, n = n) }, env = list(.var_to_replace = as.name(.var_to_replace))) ) ) } app <- init( data = data, modules = modules( tm_g_barchart_simple( label = "ADAE Analysis", x = data_extract_spec( dataname = "ADSL", select = select_spec( choices = variable_choices( ADSL, c( "ARM", "ACTARM", "SEX", "RACE", "ITTFL", "SAFFL", "STRATA2" ) ), selected = "ACTARM", multiple = FALSE ) ), fill = list( data_extract_spec( dataname = "ADSL", select = select_spec( choices = variable_choices( ADSL, c( "ARM", "ACTARM", "SEX", "RACE", "ITTFL", "SAFFL", "STRATA2" ) ), selected = "SEX", multiple = FALSE ) ), data_extract_spec( dataname = "ADAE", select = select_spec( choices = variable_choices(ADAE, c("AETOXGR", "AESEV", "AESER")), selected = NULL, multiple = FALSE ) ) ), x_facet = list( data_extract_spec( dataname = "ADAE", select = select_spec( choices = variable_choices(ADAE, c("AETOXGR", "AESEV", "AESER")), selected = "AETOXGR", multiple = FALSE ) ), data_extract_spec( dataname = "ADSL", select = select_spec( choices = variable_choices( ADSL, c( "ARM", "ACTARM", "SEX", "RACE", "ITTFL", "SAFFL", "STRATA2" ) ), selected = NULL, multiple = FALSE ) ) ), y_facet = list( data_extract_spec( dataname = "ADAE", select = select_spec( choices = variable_choices(ADAE, c("AETOXGR", "AESEV", "AESER")), selected = "AESEV", multiple = FALSE ) ), data_extract_spec( dataname = "ADSL", select = select_spec( choices = variable_choices( ADSL, c( "ARM", "ACTARM", "SEX", "RACE", "ITTFL", "SAFFL", "STRATA2" ) ), selected = NULL, multiple = FALSE ) ) ), decorators = list(plot = caption_decorator('Marcin', 'plot')) ) ) ) if (interactive()) { shinyApp(app$ui, app$server) } ``` </details> --------- Signed-off-by: André Veríssimo <[email protected]> Co-authored-by: Lluís Revilla <[email protected]> Co-authored-by: André Veríssimo <[email protected]> Co-authored-by: Lluís Revilla <[email protected]>
1 parent 5fdc43e commit 1ac634c

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

R/tm_g_barchart_simple.R

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,18 @@
1212
#' @param x_facet (`data_extract_spec`)\cr row-wise faceting groups.
1313
#' @param y_facet (`data_extract_spec`)\cr column-wise faceting groups.
1414
#' @param plot_options (`list`)\cr list of plot options.
15+
#' @param decorators `r roxygen_decorators_param("tm_g_barchart_simple")`
1516
#'
1617
#' @inherit module_arguments return seealso
1718
#'
19+
#' @section Decorating `tm_g_barchart_simple`:
20+
#'
21+
#' This module generates the following objects, which can be modified in place using decorators:
22+
#' - `plot` (`ggplot2`)
23+
#'
24+
#' For additional details and examples of decorators, refer to the vignette
25+
#' `vignette("decorate-modules-output", package = "teal")` or the [`teal_transform_module()`] documentation.
26+
#'
1827
#' @examplesShinylive
1928
#' library(teal.modules.clinical)
2029
#' interactive <- function() TRUE
@@ -146,7 +155,8 @@ tm_g_barchart_simple <- function(x = NULL,
146155
plot_width = NULL,
147156
pre_output = NULL,
148157
post_output = NULL,
149-
ggplot2_args = teal.widgets::ggplot2_args()) {
158+
ggplot2_args = teal.widgets::ggplot2_args(),
159+
decorators = NULL) {
150160
message("Initializing tm_g_barchart_simple")
151161
checkmate::assert_string(label)
152162
checkmate::assert_list(plot_options, null.ok = TRUE)
@@ -171,6 +181,8 @@ tm_g_barchart_simple <- function(x = NULL,
171181
checkmate::assert_class(pre_output, classes = "shiny.tag", null.ok = TRUE)
172182
checkmate::assert_class(post_output, classes = "shiny.tag", null.ok = TRUE)
173183
checkmate::assert_class(ggplot2_args, "ggplot2_args")
184+
decorators <- normalize_decorators(decorators)
185+
assert_decorators(decorators, names = "plot", null.ok = TRUE)
174186

175187
plot_options <- utils::modifyList(
176188
list(stacked = FALSE), # default
@@ -190,7 +202,8 @@ tm_g_barchart_simple <- function(x = NULL,
190202
y_facet = y_facet,
191203
plot_height = plot_height,
192204
plot_width = plot_width,
193-
ggplot2_args = ggplot2_args
205+
ggplot2_args = ggplot2_args,
206+
decorators = decorators
194207
),
195208
datanames = "all"
196209
)
@@ -249,6 +262,7 @@ ui_g_barchart_simple <- function(id, ...) {
249262
is_single_dataset = is_single_dataset_value
250263
)
251264
},
265+
ui_decorate_teal_data(ns("decorator"), decorators = select_decorators(args$decorators, "plot")),
252266
teal.widgets::panel_group(
253267
teal.widgets::panel_item(
254268
"Additional plot settings",
@@ -336,7 +350,8 @@ srv_g_barchart_simple <- function(id,
336350
y_facet,
337351
plot_height,
338352
plot_width,
339-
ggplot2_args) {
353+
ggplot2_args,
354+
decorators) {
340355
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
341356
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
342357
checkmate::assert_class(data, "reactive")
@@ -506,13 +521,16 @@ srv_g_barchart_simple <- function(id,
506521
)
507522
)) %>%
508523
teal.code::eval_code(code = plot_call)
509-
510-
# explicitly calling print on the plot inside the qenv evaluates
511-
# the ggplot call and therefore catches errors
512-
teal.code::eval_code(all_q, code = quote(print(plot)))
513524
})
514525

515-
plot_r <- reactive(all_q()[["plot"]])
526+
decorated_all_q_code <- srv_decorate_teal_data(
527+
"decorator",
528+
data = all_q,
529+
decorators = select_decorators(decorators, "plot"),
530+
expr = print(plot)
531+
)
532+
533+
plot_r <- reactive(decorated_all_q_code()[["plot"]])
516534

517535
output$table <- renderTable({
518536
req(iv_r()$is_valid())
@@ -550,7 +568,7 @@ srv_g_barchart_simple <- function(id,
550568

551569
teal.widgets::verbatim_popup_srv(
552570
id = "rcode",
553-
verbatim_content = reactive(teal.code::get_code(all_q())),
571+
verbatim_content = reactive(teal.code::get_code(req(decorated_all_q_code()))),
554572
title = "Bar Chart"
555573
)
556574

@@ -569,7 +587,7 @@ srv_g_barchart_simple <- function(id,
569587
card$append_text("Comment", "header3")
570588
card$append_text(comment)
571589
}
572-
card$append_src(teal.code::get_code(all_q()))
590+
card$append_src(teal.code::get_code(req(decorated_all_q_code())))
573591
card
574592
}
575593
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)

man/tm_g_barchart_simple.Rd

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)