Skip to content

Commit b5bd1bf

Browse files
committed
introduce decorators for tm_g_response
1 parent b26a29d commit b5bd1bf

16 files changed

+101
-65
lines changed

R/tm_g_response.R

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@
3939
#' @note For more examples, please see the vignette "Using response plot" via
4040
#' `vignette("using-response-plot", package = "teal.modules.general")`.
4141
#'
42+
#' @section Decorating `tm_g_response`:
43+
#'
44+
#' This module generates the following objects, which can be modified in place using decorators:
45+
#' - `plot` (`ggplot2`)
46+
#'
47+
#' For additional details and examples of decorators, refer to the vignette
48+
#' `vignette("decorate-modules-output", package = "teal")` or the [`teal_transform_module()`] documentation.
49+
#'
4250
#' @examplesShinylive
4351
#' library(teal.modules.general)
4452
#' interactive <- function() TRUE
@@ -147,7 +155,8 @@ tm_g_response <- function(label = "Response Plot",
147155
ggtheme = c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void"),
148156
ggplot2_args = teal.widgets::ggplot2_args(),
149157
pre_output = NULL,
150-
post_output = NULL) {
158+
post_output = NULL,
159+
decorators = list(default = teal_transform_module())) {
151160
message("Initializing tm_g_response")
152161

153162
# Normalize the parameters
@@ -191,6 +200,8 @@ tm_g_response <- function(label = "Response Plot",
191200

192201
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
193202
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
203+
204+
checkmate::assert_list(decorators, "teal_transform_module")
194205
# End of assertions
195206

196207
# Make UI args
@@ -210,7 +221,12 @@ tm_g_response <- function(label = "Response Plot",
210221
ui_args = args,
211222
server_args = c(
212223
data_extract_list,
213-
list(plot_height = plot_height, plot_width = plot_width, ggplot2_args = ggplot2_args)
224+
list(
225+
plot_height = plot_height,
226+
plot_width = plot_width,
227+
ggplot2_args = ggplot2_args,
228+
decorators = decorators
229+
)
214230
),
215231
datanames = teal.transform::get_extract_datanames(data_extract_list)
216232
)
@@ -269,6 +285,7 @@ ui_g_response <- function(id, ...) {
269285
selected = ifelse(args$freq, "frequency", "density"),
270286
justified = TRUE
271287
),
288+
ui_teal_transform_data(ns("decorator"), transformators = args$decorators),
272289
teal.widgets::panel_group(
273290
teal.widgets::panel_item(
274291
title = "Plot settings",
@@ -304,7 +321,8 @@ srv_g_response <- function(id,
304321
col_facet,
305322
plot_height,
306323
plot_width,
307-
ggplot2_args) {
324+
ggplot2_args,
325+
decorators) {
308326
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
309327
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
310328
checkmate::assert_class(data, "reactive")
@@ -523,8 +541,7 @@ srv_g_response <- function(id,
523541
)
524542

525543
plot_call <- substitute(expr = {
526-
p <- plot_call + labs + ggthemes + themes
527-
print(p)
544+
plot <- plot_call + labs + ggthemes + themes
528545
}, env = list(
529546
plot_call = plot_call,
530547
labs = parsed_ggplot2_args$labs,
@@ -535,7 +552,10 @@ srv_g_response <- function(id,
535552
teal.code::eval_code(qenv, plot_call)
536553
})
537554

538-
plot_r <- reactive(output_q()[["p"]])
555+
decorated_output_q <- srv_teal_transform_data(id = "decorator", data = output_q, transformators = decorators)
556+
557+
decorated_output_plot_q <- reactive(within(decorated_output_q(), print(plot)))
558+
plot_r <- reactive(decorated_output_plot_q()[["plot"]])
539559

540560
# Insert the plot into a plot_with_settings module from teal.widgets
541561
pws <- teal.widgets::plot_with_settings_srv(
@@ -547,7 +567,7 @@ srv_g_response <- function(id,
547567

548568
teal.widgets::verbatim_popup_srv(
549569
id = "rcode",
550-
verbatim_content = reactive(teal.code::get_code(output_q())),
570+
verbatim_content = reactive(teal.code::get_code(req(decorated_output_plot_q()))),
551571
title = "Show R Code for Response"
552572
)
553573

@@ -566,7 +586,7 @@ srv_g_response <- function(id,
566586
card$append_text("Comment", "header3")
567587
card$append_text(comment)
568588
}
569-
card$append_src(teal.code::get_code(output_q()))
589+
card$append_src(teal.code::get_code(req(decorated_output_plot_q())))
570590
card
571591
}
572592
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)

man/tm_a_pca.Rd

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

man/tm_a_regression.Rd

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

0 commit comments

Comments
 (0)