Skip to content

Commit b6ca759

Browse files
m7praverissimo
andauthored
introduce decorators for tm_g_association (#800)
Part of https://github.com/insightsengineering/teal/issues/1370 <details><summary> Working Example </summary> ```r devtools::load_all("../teal") devtools::load_all(".") interactive_decorator <- teal_transform_module( ui = function(id) { ns <- NS(id) div( textInput(ns("x_axis_title_top"), "X axis title plot top", value = "x axis top"), textInput(ns("x_axis_title_bottom"), "X axis title plot bottom", value = "x axis bottom") ) }, server = function(id, data) { moduleServer(id, function(input, output, session) { reactive({ req(data()) within(data(), { plot_bottom <- plot_bottom + xlab(my_title_bottom) plot_top <- plot_top + xlab(my_title_top) }, my_title_top = input$x_axis_title_top, my_title_bottom = input$x_axis_title_bottom ) }) }) } ) # general data example data <- teal_data() data <- within(data, { require(nestcolor) CO2 <- CO2 factors <- names(Filter(isTRUE, vapply(CO2, is.factor, logical(1L)))) CO2[factors] <- lapply(CO2[factors], as.character) }) app <- init( data = data, modules = modules( tm_g_association( ref = data_extract_spec( dataname = "CO2", select = select_spec( label = "Select variable:", choices = variable_choices(data[["CO2"]], c("Plant", "Type", "Treatment")), selected = "Plant", fixed = FALSE ) ), vars = data_extract_spec( dataname = "CO2", select = select_spec( label = "Select variables:", choices = variable_choices(data[["CO2"]], c("Plant", "Type", "Treatment")), selected = "Treatment", multiple = TRUE, fixed = FALSE ) ), decorators = list(interactive_decorator) ) ) ) if (interactive()) { shinyApp(app$ui, app$server) } ``` </details> --------- Co-authored-by: André Veríssimo <[email protected]>
1 parent 0870002 commit b6ca759

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

R/tm_g_association.R

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525
#'
2626
#' @inherit shared_params return
2727
#'
28+
#' @section Decorating `tm_g_association`:
29+
#'
30+
#' This module generates the following objects, which can be modified in place using decorators:
31+
#' - `plot_top` (`ggplot2`)
32+
#' - `plot_bottom` (`ggplot2`)
33+
#'
34+
#' For additional details and examples of decorators, refer to the vignette
35+
#' `vignette("decorate-modules-output", package = "teal")` or the [`teal_transform_module()`] documentation.
36+
#'
2837
#' @examplesShinylive
2938
#' library(teal.modules.general)
3039
#' interactive <- function() TRUE
@@ -130,7 +139,8 @@ tm_g_association <- function(label = "Association",
130139
association_theme = c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void"), # nolint: line_length.
131140
pre_output = NULL,
132141
post_output = NULL,
133-
ggplot2_args = teal.widgets::ggplot2_args()) {
142+
ggplot2_args = teal.widgets::ggplot2_args(),
143+
decorators = NULL) {
134144
message("Initializing tm_g_association")
135145

136146
# Normalize the parameters
@@ -166,6 +176,7 @@ tm_g_association <- function(label = "Association",
166176
plot_choices <- c("Bivariate1", "Bivariate2")
167177
checkmate::assert_list(ggplot2_args, types = "ggplot2_args")
168178
checkmate::assert_subset(names(ggplot2_args), c("default", plot_choices))
179+
checkmate::assert_list(decorators, "teal_transform_module", null.ok = TRUE)
169180
# End of assertions
170181

171182
# Make UI args
@@ -183,7 +194,7 @@ tm_g_association <- function(label = "Association",
183194
ui_args = args,
184195
server_args = c(
185196
data_extract_list,
186-
list(plot_height = plot_height, plot_width = plot_width, ggplot2_args = ggplot2_args)
197+
list(plot_height = plot_height, plot_width = plot_width, ggplot2_args = ggplot2_args, decorators = decorators)
187198
),
188199
datanames = teal.transform::get_extract_datanames(data_extract_list)
189200
)
@@ -236,6 +247,7 @@ ui_tm_g_association <- function(id, ...) {
236247
"Log transformed",
237248
value = FALSE
238249
),
250+
ui_transform_teal_data(ns("decorate"), transformators = args$decorators),
239251
teal.widgets::panel_group(
240252
teal.widgets::panel_item(
241253
title = "Plot settings",
@@ -277,7 +289,8 @@ srv_tm_g_association <- function(id,
277289
vars,
278290
plot_height,
279291
plot_width,
280-
ggplot2_args) {
292+
ggplot2_args,
293+
decorators) {
281294
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
282295
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
283296
checkmate::assert_class(data, "reactive")
@@ -463,7 +476,6 @@ srv_tm_g_association <- function(id,
463476
)
464477
)
465478
}
466-
467479
teal.code::eval_code(
468480
merged$anl_q_r(),
469481
substitute(
@@ -474,10 +486,8 @@ srv_tm_g_association <- function(id,
474486
teal.code::eval_code(
475487
substitute(
476488
expr = {
477-
plots <- plot_calls
478-
p <- tern::stack_grobs(grobs = lapply(plots, ggplotGrob))
479-
grid::grid.newpage()
480-
grid::grid.draw(p)
489+
plot_top <- plot_calls[[1]]
490+
plot_bottom <- plot_calls[[1]]
481491
},
482492
env = list(
483493
plot_calls = do.call(
@@ -490,9 +500,23 @@ srv_tm_g_association <- function(id,
490500
)
491501
})
492502

503+
decorated_output_q <- srv_transform_teal_data("decorate", data = output_q, transformators = decorators)
504+
decorated_output_grob_q <- reactive({
505+
within(
506+
decorated_output_q(),
507+
{
508+
plot <- tern::stack_grobs(grobs = lapply(list(plot_top, plot_bottom), ggplotGrob))
509+
grid::grid.newpage()
510+
grid::grid.draw(plot)
511+
}
512+
)
513+
})
514+
515+
493516
plot_r <- reactive({
494517
req(iv_r()$is_valid())
495-
output_q()[["p"]]
518+
req(output_q())
519+
decorated_output_grob_q()[["plot"]]
496520
})
497521

498522
pws <- teal.widgets::plot_with_settings_srv(
@@ -508,7 +532,7 @@ srv_tm_g_association <- function(id,
508532

509533
teal.widgets::verbatim_popup_srv(
510534
id = "rcode",
511-
verbatim_content = reactive(teal.code::get_code(output_q())),
535+
verbatim_content = reactive(teal.code::get_code(req(decorated_output_q()))),
512536
title = "Association Plot"
513537
)
514538

@@ -527,7 +551,7 @@ srv_tm_g_association <- function(id,
527551
card$append_text("Comment", "header3")
528552
card$append_text(comment)
529553
}
530-
card$append_src(teal.code::get_code(output_q()))
554+
card$append_src(teal.code::get_code(req(decorated_output_q())))
531555
card
532556
}
533557
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)

man/tm_g_association.Rd

Lines changed: 18 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)