Skip to content

Commit 6863da9

Browse files
authored
Merge branch '1187_decorate_output@main' into tm_g_distribution@1187_decorate_output@main
2 parents d028a57 + 612bb06 commit 6863da9

File tree

3 files changed

+87
-21
lines changed

3 files changed

+87
-21
lines changed

R/tm_g_bivariate.R

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@
4646
#'
4747
#' @inherit shared_params return
4848
#'
49+
#' @section Decorating `tm_outliers`:
50+
#'
51+
#' This module generates the following objects, which can be modified in place using decorators:
52+
#' - `plot` (`ggplot2`)
53+
#'
54+
#' For additional details and examples of decorators, refer to the vignette
55+
#' `vignette("decorate-modules-output", package = "teal")` or the [`teal_transform_module()`] documentation.
56+
#'
57+
#'
4958
#' @examplesShinylive
5059
#' library(teal.modules.general)
5160
#' interactive <- function() TRUE
@@ -185,7 +194,8 @@ tm_g_bivariate <- function(label = "Bivariate Plots",
185194
ggtheme = c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void"),
186195
ggplot2_args = teal.widgets::ggplot2_args(),
187196
pre_output = NULL,
188-
post_output = NULL) {
197+
post_output = NULL,
198+
decorators = NULL) {
189199
message("Initializing tm_g_bivariate")
190200

191201
# Normalize the parameters
@@ -265,6 +275,8 @@ tm_g_bivariate <- function(label = "Bivariate Plots",
265275

266276
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
267277
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
278+
279+
checkmate::assert_list(decorators, "teal_transform_module", null.ok = TRUE)
268280
# End of assertions
269281

270282
# Make UI args
@@ -288,7 +300,7 @@ tm_g_bivariate <- function(label = "Bivariate Plots",
288300
ui_args = args,
289301
server_args = c(
290302
data_extract_list,
291-
list(plot_height = plot_height, plot_width = plot_width, ggplot2_args = ggplot2_args)
303+
list(plot_height = plot_height, plot_width = plot_width, ggplot2_args = ggplot2_args, decorators = decorators)
292304
),
293305
datanames = teal.transform::get_extract_datanames(data_extract_list)
294306
)
@@ -338,6 +350,7 @@ ui_g_bivariate <- function(id, ...) {
338350
justified = TRUE
339351
)
340352
),
353+
ui_teal_transform_data(ns("decorate"), transformators = args$decorators),
341354
if (!is.null(args$row_facet) || !is.null(args$col_facet)) {
342355
tags$div(
343356
class = "data-extract-box",
@@ -451,7 +464,8 @@ srv_g_bivariate <- function(id,
451464
size,
452465
plot_height,
453466
plot_width,
454-
ggplot2_args) {
467+
ggplot2_args,
468+
decorators) {
455469
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
456470
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
457471
checkmate::assert_class(data, "reactive")
@@ -648,37 +662,50 @@ srv_g_bivariate <- function(id,
648662
}
649663
}
650664

665+
teal.code::eval_code(merged$anl_q_r(), substitute(expr = plot <- cl, env = list(cl = cl)))
666+
})
667+
668+
decorated_output_q <- srv_teal_transform_data("decorate", data = output_q, transformators = decorators)
669+
670+
decorated_output_q_facets <- reactive({
671+
672+
ANL <- merged$anl_q_r()[["ANL"]]
673+
row_facet_name <- as.vector(merged$anl_input_r()$columns_source$row_facet)
674+
col_facet_name <- as.vector(merged$anl_input_r()$columns_source$col_facet)
675+
651676
# Add labels to facets
652677
nulled_row_facet_name <- varname_w_label(row_facet_name, ANL)
653678
nulled_col_facet_name <- varname_w_label(col_facet_name, ANL)
679+
facetting <- (isTRUE(input$facetting) && (!is.null(row_facet_name) || !is.null(col_facet_name)))
654680
without_facet <- (is.null(nulled_row_facet_name) && is.null(nulled_col_facet_name)) || !facetting
655681

656682
print_call <- if (without_facet) {
657-
quote(print(p))
683+
quote(print(plot))
658684
} else {
659685
substitute(
660686
expr = {
661687
# Add facetting labels
662688
# optional: grid.newpage() # nolint: commented_code.
663689
# Prefixed with teal.modules.general as its usage will appear in "Show R code"
664-
p <- teal.modules.general::add_facet_labels(
665-
p,
690+
plot <- teal.modules.general::add_facet_labels(
691+
plot,
666692
xfacet_label = nulled_col_facet_name,
667693
yfacet_label = nulled_row_facet_name
668694
)
669695
grid::grid.newpage()
670-
grid::grid.draw(p)
696+
grid::grid.draw(plot)
671697
},
672698
env = list(nulled_col_facet_name = nulled_col_facet_name, nulled_row_facet_name = nulled_row_facet_name)
673699
)
674700
}
675-
676-
teal.code::eval_code(merged$anl_q_r(), substitute(expr = p <- cl, env = list(cl = cl))) %>%
701+
decorated_output_q() %>%
677702
teal.code::eval_code(print_call)
678703
})
679704

705+
680706
plot_r <- reactive({
681-
output_q()[["p"]]
707+
req(output_q())
708+
decorated_output_q_facets()[["plot"]]
682709
})
683710

684711
pws <- teal.widgets::plot_with_settings_srv(
@@ -690,7 +717,7 @@ srv_g_bivariate <- function(id,
690717

691718
teal.widgets::verbatim_popup_srv(
692719
id = "rcode",
693-
verbatim_content = reactive(teal.code::get_code(output_q())),
720+
verbatim_content = reactive(teal.code::get_code(req(decorated_output_q_facets()))),
694721
title = "Bivariate Plot"
695722
)
696723

@@ -709,7 +736,7 @@ srv_g_bivariate <- function(id,
709736
card$append_text("Comment", "header3")
710737
card$append_text(comment)
711738
}
712-
card$append_src(teal.code::get_code(output_q()))
739+
card$append_src(teal.code::get_code(req(decorated_output_q_facets)))
713740
card
714741
}
715742
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)

R/tm_g_response.R

Lines changed: 31 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 = NULL) {
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", null.ok = TRUE)
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,13 @@ 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({
559+
req(output_q()) # Ensure original errors are displayed
560+
decorated_output_plot_q()[["plot"]]
561+
})
539562

540563
# Insert the plot into a plot_with_settings module from teal.widgets
541564
pws <- teal.widgets::plot_with_settings_srv(
@@ -547,7 +570,7 @@ srv_g_response <- function(id,
547570

548571
teal.widgets::verbatim_popup_srv(
549572
id = "rcode",
550-
verbatim_content = reactive(teal.code::get_code(output_q())),
573+
verbatim_content = reactive(teal.code::get_code(req(decorated_output_plot_q()))),
551574
title = "Show R Code for Response"
552575
)
553576

@@ -566,7 +589,7 @@ srv_g_response <- function(id,
566589
card$append_text("Comment", "header3")
567590
card$append_text(comment)
568591
}
569-
card$append_src(teal.code::get_code(output_q()))
592+
card$append_src(teal.code::get_code(req(decorated_output_plot_q())))
570593
card
571594
}
572595
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)

man/tm_g_response.Rd

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