Skip to content

Commit cf4371f

Browse files
m7praverissimo
andauthored
introduce decorators for tm_g_bivariate (#797)
Part of https://github.com/insightsengineering/teal/issues/1370 <details><summary> Working Example </summary> ```r devtools::load_all("../teal") devtools::load_all(".") library(ggplot2) interactive_decorator <- teal_transform_module( ui = function(id) { ns <- NS(id) div( textInput(ns("x_axis_title"), "X axis title", value = "x axis") ) }, server = function(id, data) { moduleServer(id, function(input, output, session) { reactive({ req(data()) within(data(), { plot <- plot + xlab(my_title) }, my_title = input$x_axis_title ) }) }) } ) # general data example data <- teal_data() data <- within(data, { require(nestcolor) CO2 <- data.frame(CO2) }) app <- init( data = data, modules = tm_g_bivariate( x = data_extract_spec( dataname = "CO2", select = select_spec( label = "Select variable:", choices = variable_choices(data[["CO2"]]), selected = "conc", fixed = FALSE ) ), y = data_extract_spec( dataname = "CO2", select = select_spec( label = "Select variable:", choices = variable_choices(data[["CO2"]]), selected = "uptake", multiple = FALSE, fixed = FALSE ) ), row_facet = data_extract_spec( dataname = "CO2", select = select_spec( label = "Select variable:", choices = variable_choices(data[["CO2"]]), selected = "Type", fixed = FALSE ) ), col_facet = data_extract_spec( dataname = "CO2", select = select_spec( label = "Select variable:", choices = variable_choices(data[["CO2"]]), selected = "Treatment", fixed = FALSE ) ), decorators = list(interactive_decorator) ) ) if (interactive()) { shinyApp(app$ui, app$server) } ``` </details> --------- Signed-off-by: Marcin <[email protected]> Co-authored-by: André Veríssimo <[email protected]>
1 parent 6dcb2ef commit cf4371f

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
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)

0 commit comments

Comments
 (0)