Skip to content

Commit c7192de

Browse files
authored
Merge branch '1187_decorate_output@main' into tm_pca@1187_decorate_output@main
2 parents f9d93c0 + 7deda6d commit c7192de

File tree

6 files changed

+199
-57
lines changed

6 files changed

+199
-57
lines changed

R/tm_g_bivariate.R

Lines changed: 38 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,49 @@ 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+
ANL <- merged$anl_q_r()[["ANL"]]
672+
row_facet_name <- as.vector(merged$anl_input_r()$columns_source$row_facet)
673+
col_facet_name <- as.vector(merged$anl_input_r()$columns_source$col_facet)
674+
651675
# Add labels to facets
652676
nulled_row_facet_name <- varname_w_label(row_facet_name, ANL)
653677
nulled_col_facet_name <- varname_w_label(col_facet_name, ANL)
678+
facetting <- (isTRUE(input$facetting) && (!is.null(row_facet_name) || !is.null(col_facet_name)))
654679
without_facet <- (is.null(nulled_row_facet_name) && is.null(nulled_col_facet_name)) || !facetting
655680

656681
print_call <- if (without_facet) {
657-
quote(print(p))
682+
quote(print(plot))
658683
} else {
659684
substitute(
660685
expr = {
661686
# Add facetting labels
662687
# optional: grid.newpage() # nolint: commented_code.
663688
# Prefixed with teal.modules.general as its usage will appear in "Show R code"
664-
p <- teal.modules.general::add_facet_labels(
665-
p,
689+
plot <- teal.modules.general::add_facet_labels(
690+
plot,
666691
xfacet_label = nulled_col_facet_name,
667692
yfacet_label = nulled_row_facet_name
668693
)
669694
grid::grid.newpage()
670-
grid::grid.draw(p)
695+
grid::grid.draw(plot)
671696
},
672697
env = list(nulled_col_facet_name = nulled_col_facet_name, nulled_row_facet_name = nulled_row_facet_name)
673698
)
674699
}
675-
676-
teal.code::eval_code(merged$anl_q_r(), substitute(expr = p <- cl, env = list(cl = cl))) %>%
700+
decorated_output_q() %>%
677701
teal.code::eval_code(print_call)
678702
})
679703

704+
680705
plot_r <- reactive({
681-
output_q()[["p"]]
706+
req(output_q())
707+
decorated_output_q_facets()[["plot"]]
682708
})
683709

684710
pws <- teal.widgets::plot_with_settings_srv(
@@ -690,7 +716,7 @@ srv_g_bivariate <- function(id,
690716

691717
teal.widgets::verbatim_popup_srv(
692718
id = "rcode",
693-
verbatim_content = reactive(teal.code::get_code(output_q())),
719+
verbatim_content = reactive(teal.code::get_code(req(decorated_output_q_facets()))),
694720
title = "Bivariate Plot"
695721
)
696722

@@ -709,7 +735,7 @@ srv_g_bivariate <- function(id,
709735
card$append_text("Comment", "header3")
710736
card$append_text(comment)
711737
}
712-
card$append_src(teal.code::get_code(output_q()))
738+
card$append_src(teal.code::get_code(req(decorated_output_q_facets)))
713739
card
714740
}
715741
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)

0 commit comments

Comments
 (0)