Skip to content

Commit 0870002

Browse files
m7praverissimo
andauthored
introduce decorators for tm_g_scatterplot (#807)
Part of https://github.com/insightsengineering/teal/issues/1370 <details><summary> Working Example </summary> ```r pkgload::load_all("../teal") pkgload::load_all(".") footnote_dec <- teal_transform_module( label = "Footnote", ui = function(id) shiny::textInput(shiny::NS(id, "footnote"), "Footnote", value = "I am a good decorator"), server = function(id, data) { moduleServer(id, function(input, output, session) { logger::log_info("🟢 Footnote called to action!", namespace = "teal.modules.general") reactive( within( data(), { footnote_str <- footnote plot <- plot + ggplot2::labs(caption = footnote_str) }, footnote = input$footnote ) ) }) } ) # CDISC data example data <- teal_data() data <- within(data, { require(nestcolor) ADSL <- rADSL }) join_keys(data) <- default_cdisc_join_keys[names(data)] app <- init( data = data, modules = modules( tm_g_scatterplot( label = "Scatterplot Choices", x = data_extract_spec( dataname = "ADSL", select = select_spec( label = "Select variable:", choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1", "BMRKR2")), selected = "AGE", multiple = FALSE, fixed = FALSE ) ), y = data_extract_spec( dataname = "ADSL", select = select_spec( label = "Select variable:", choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1", "BMRKR2")), selected = "BMRKR1", multiple = FALSE, fixed = FALSE ) ), color_by = data_extract_spec( dataname = "ADSL", select = select_spec( label = "Select variable:", choices = variable_choices( data[["ADSL"]], c("AGE", "BMRKR1", "BMRKR2", "RACE", "REGION1") ), selected = NULL, multiple = FALSE, fixed = FALSE ) ), size_by = data_extract_spec( dataname = "ADSL", select = select_spec( label = "Select variable:", choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1")), selected = "AGE", multiple = FALSE, fixed = FALSE ) ), row_facet = data_extract_spec( dataname = "ADSL", select = select_spec( label = "Select variable:", choices = variable_choices(data[["ADSL"]], c("BMRKR2", "RACE", "REGION1")), selected = NULL, multiple = FALSE, fixed = FALSE ) ), col_facet = data_extract_spec( dataname = "ADSL", select = select_spec( label = "Select variable:", choices = variable_choices(data[["ADSL"]], c("BMRKR2", "RACE", "REGION1")), selected = NULL, multiple = FALSE, fixed = FALSE ) ), decorators = list(footnote_dec) ) ) ) if (interactive()) { shinyApp(app$ui, app$server) } ``` </details> --------- Signed-off-by: Marcin <[email protected]> Co-authored-by: André Veríssimo <[email protected]>
1 parent 09968a8 commit 0870002

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

R/tm_g_scatterplot.R

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030
#'
3131
#' @inherit shared_params return
3232
#'
33+
#' @section Decorating `tm_g_scatterplot`:
34+
#'
35+
#' This module generates the following objects, which can be modified in place using decorators:
36+
#' - `plot` (`ggplot2`)
37+
#'
38+
#' For additional details and examples of decorators, refer to the vignette
39+
#' `vignette("decorate-modules-output", package = "teal")` or the [`teal_transform_module()`] documentation.
40+
#'
41+
#'
3342
#' @examplesShinylive
3443
#' library(teal.modules.general)
3544
#' interactive <- function() TRUE
@@ -229,7 +238,8 @@ tm_g_scatterplot <- function(label = "Scatterplot",
229238
pre_output = NULL,
230239
post_output = NULL,
231240
table_dec = 4,
232-
ggplot2_args = teal.widgets::ggplot2_args()) {
241+
ggplot2_args = teal.widgets::ggplot2_args(),
242+
decorators = NULL) {
233243
message("Initializing tm_g_scatterplot")
234244

235245
# Requires Suggested packages
@@ -297,6 +307,7 @@ tm_g_scatterplot <- function(label = "Scatterplot",
297307

298308
checkmate::assert_scalar(table_dec)
299309
checkmate::assert_class(ggplot2_args, "ggplot2_args")
310+
checkmate::assert_list(decorators, "teal_transform_module", null.ok = TRUE)
300311
# End of assertions
301312

302313
# Make UI args
@@ -318,7 +329,13 @@ tm_g_scatterplot <- function(label = "Scatterplot",
318329
ui_args = args,
319330
server_args = c(
320331
data_extract_list,
321-
list(plot_height = plot_height, plot_width = plot_width, table_dec = table_dec, ggplot2_args = ggplot2_args)
332+
list(
333+
plot_height = plot_height,
334+
plot_width = plot_width,
335+
table_dec = table_dec,
336+
ggplot2_args = ggplot2_args,
337+
decorators = decorators
338+
)
322339
),
323340
datanames = teal.transform::get_extract_datanames(data_extract_list)
324341
)
@@ -413,6 +430,7 @@ ui_g_scatterplot <- function(id, ...) {
413430
is_single_dataset = is_single_dataset_value
414431
)
415432
},
433+
ui_transform_teal_data(ns("decorator"), transformators = args$decorators),
416434
teal.widgets::panel_group(
417435
teal.widgets::panel_item(
418436
title = "Plot settings",
@@ -491,7 +509,8 @@ srv_g_scatterplot <- function(id,
491509
plot_height,
492510
plot_width,
493511
table_dec,
494-
ggplot2_args) {
512+
ggplot2_args,
513+
decorators) {
495514
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
496515
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
497516
checkmate::assert_class(data, "reactive")
@@ -981,13 +1000,17 @@ srv_g_scatterplot <- function(id,
9811000
)
9821001
}
9831002

984-
plot_call <- substitute(expr = p <- plot_call, env = list(plot_call = plot_call))
1003+
plot_call <- substitute(expr = plot <- plot_call, env = list(plot_call = plot_call))
9851004

986-
teal.code::eval_code(plot_q, plot_call) %>%
987-
teal.code::eval_code(quote(print(p)))
1005+
teal.code::eval_code(plot_q, plot_call)
9881006
})
9891007

990-
plot_r <- reactive(output_q()[["p"]])
1008+
decorated_output_q <- srv_transform_teal_data(id = "decorator", data = output_q, transformators = decorators)
1009+
decorated_output_plot_q <- reactive(within(decorated_output_q(), print(plot)))
1010+
plot_r <- reactive({
1011+
req(output_q()) # Ensure original errors are displayed
1012+
decorated_output_plot_q()[["plot"]]
1013+
})
9911014

9921015
# Insert the plot into a plot_with_settings module from teal.widgets
9931016
pws <- teal.widgets::plot_with_settings_srv(
@@ -1028,7 +1051,7 @@ srv_g_scatterplot <- function(id,
10281051

10291052
teal.widgets::verbatim_popup_srv(
10301053
id = "rcode",
1031-
verbatim_content = reactive(teal.code::get_code(output_q())),
1054+
verbatim_content = reactive(teal.code::get_code(req(decorated_output_plot_q()))),
10321055
title = "R Code for scatterplot"
10331056
)
10341057

@@ -1047,7 +1070,7 @@ srv_g_scatterplot <- function(id,
10471070
card$append_text("Comment", "header3")
10481071
card$append_text(comment)
10491072
}
1050-
card$append_src(teal.code::get_code(output_q()))
1073+
card$append_src(teal.code::get_code(req(decorated_output_plot_q())))
10511074
card
10521075
}
10531076
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)

man/tm_g_scatterplot.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)