From 7b997b3e315ed27eb5a131a0efa33ff066e44913 Mon Sep 17 00:00:00 2001 From: m7pr Date: Fri, 22 Nov 2024 11:35:06 +0100 Subject: [PATCH 1/3] decorate the plot --- R/tm_g_scatterplot.R | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/R/tm_g_scatterplot.R b/R/tm_g_scatterplot.R index 1f2d9d58a..344de3d51 100644 --- a/R/tm_g_scatterplot.R +++ b/R/tm_g_scatterplot.R @@ -30,6 +30,15 @@ #' #' @inherit shared_params return #' +#' @section Decorating `tm_g_response`: +#' +#' This module generates the following objects, which can be modified in place using decorators: +#' - `plot` (`ggplot2`) +#' +#' For additional details and examples of decorators, refer to the vignette +#' `vignette("decorate-modules-output", package = "teal")` or the [`teal_transform_module()`] documentation. +#' +#' #' @examplesShinylive #' library(teal.modules.general) #' interactive <- function() TRUE @@ -229,7 +238,8 @@ tm_g_scatterplot <- function(label = "Scatterplot", pre_output = NULL, post_output = NULL, table_dec = 4, - ggplot2_args = teal.widgets::ggplot2_args()) { + ggplot2_args = teal.widgets::ggplot2_args(), + decorators = NULL) { message("Initializing tm_g_scatterplot") # Requires Suggested packages @@ -297,6 +307,7 @@ tm_g_scatterplot <- function(label = "Scatterplot", checkmate::assert_scalar(table_dec) checkmate::assert_class(ggplot2_args, "ggplot2_args") + checkmate::assert_list(decorators, "teal_transform_module", null.ok = TRUE) # End of assertions # Make UI args @@ -318,7 +329,13 @@ tm_g_scatterplot <- function(label = "Scatterplot", ui_args = args, server_args = c( data_extract_list, - list(plot_height = plot_height, plot_width = plot_width, table_dec = table_dec, ggplot2_args = ggplot2_args) + list( + plot_height = plot_height, + plot_width = plot_width, + table_dec = table_dec, + ggplot2_args = ggplot2_args, + decorators = decorators + ) ), datanames = teal.transform::get_extract_datanames(data_extract_list) ) @@ -413,6 +430,7 @@ ui_g_scatterplot <- function(id, ...) { is_single_dataset = is_single_dataset_value ) }, + ui_teal_transform_data(ns("decorator"), transformators = args$decorators), teal.widgets::panel_group( teal.widgets::panel_item( title = "Plot settings", @@ -491,7 +509,8 @@ srv_g_scatterplot <- function(id, plot_height, plot_width, table_dec, - ggplot2_args) { + ggplot2_args, + decorators) { with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") checkmate::assert_class(data, "reactive") @@ -981,13 +1000,17 @@ srv_g_scatterplot <- function(id, ) } - plot_call <- substitute(expr = p <- plot_call, env = list(plot_call = plot_call)) + plot_call <- substitute(expr = plot <- plot_call, env = list(plot_call = plot_call)) - teal.code::eval_code(plot_q, plot_call) %>% - teal.code::eval_code(quote(print(p))) + teal.code::eval_code(plot_q, plot_call) }) - plot_r <- reactive(output_q()[["p"]]) + decorated_output_q <- srv_teal_transform_data(id = "decorator", data = output_q, transformators = decorators) + decorated_output_plot_q <- reactive(within(decorated_output_q(), print(plot))) + plot_r <- reactive({ + req(output_q()) # Ensure original errors are displayed + decorated_output_plot_q()[["plot"]] + }) # Insert the plot into a plot_with_settings module from teal.widgets pws <- teal.widgets::plot_with_settings_srv( @@ -1028,7 +1051,7 @@ srv_g_scatterplot <- function(id, teal.widgets::verbatim_popup_srv( id = "rcode", - verbatim_content = reactive(teal.code::get_code(output_q())), + verbatim_content = reactive(teal.code::get_code(req(decorated_output_plot_q()))), title = "R Code for scatterplot" ) @@ -1047,7 +1070,7 @@ srv_g_scatterplot <- function(id, card$append_text("Comment", "header3") card$append_text(comment) } - card$append_src(teal.code::get_code(output_q())) + card$append_src(teal.code::get_code(req(decorated_output_plot_q()))) card } teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun) From b98b2c2aa7bbd7884f91529225e94c0ba2f36efc Mon Sep 17 00:00:00 2001 From: Marcin <133694481+m7pr@users.noreply.github.com> Date: Fri, 22 Nov 2024 11:46:00 +0100 Subject: [PATCH 2/3] Update R/tm_g_scatterplot.R Signed-off-by: Marcin <133694481+m7pr@users.noreply.github.com> --- R/tm_g_scatterplot.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/tm_g_scatterplot.R b/R/tm_g_scatterplot.R index 344de3d51..f50f6cb7c 100644 --- a/R/tm_g_scatterplot.R +++ b/R/tm_g_scatterplot.R @@ -30,7 +30,7 @@ #' #' @inherit shared_params return #' -#' @section Decorating `tm_g_response`: +#' @section Decorating `tm_g_scatterplot`: #' #' This module generates the following objects, which can be modified in place using decorators: #' - `plot` (`ggplot2`) From e09b72a1a0bf5d848d85914998d64a13fc304277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Fri, 22 Nov 2024 15:39:50 +0000 Subject: [PATCH 3/3] chore: rename ui/srv_teal_transform_data to ui/srv_transform_teal_data --- R/tm_g_scatterplot.R | 4 ++-- man/tm_g_scatterplot.Rd | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/R/tm_g_scatterplot.R b/R/tm_g_scatterplot.R index f50f6cb7c..3e3f69de4 100644 --- a/R/tm_g_scatterplot.R +++ b/R/tm_g_scatterplot.R @@ -430,7 +430,7 @@ ui_g_scatterplot <- function(id, ...) { is_single_dataset = is_single_dataset_value ) }, - ui_teal_transform_data(ns("decorator"), transformators = args$decorators), + ui_transform_teal_data(ns("decorator"), transformators = args$decorators), teal.widgets::panel_group( teal.widgets::panel_item( title = "Plot settings", @@ -1005,7 +1005,7 @@ srv_g_scatterplot <- function(id, teal.code::eval_code(plot_q, plot_call) }) - decorated_output_q <- srv_teal_transform_data(id = "decorator", data = output_q, transformators = decorators) + decorated_output_q <- srv_transform_teal_data(id = "decorator", data = output_q, transformators = decorators) decorated_output_plot_q <- reactive(within(decorated_output_q(), print(plot))) plot_r <- reactive({ req(output_q()) # Ensure original errors are displayed diff --git a/man/tm_g_scatterplot.Rd b/man/tm_g_scatterplot.Rd index 5c1c306f7..43439b489 100644 --- a/man/tm_g_scatterplot.Rd +++ b/man/tm_g_scatterplot.Rd @@ -23,7 +23,8 @@ tm_g_scatterplot( pre_output = NULL, post_output = NULL, table_dec = 4, - ggplot2_args = teal.widgets::ggplot2_args() + ggplot2_args = teal.widgets::ggplot2_args(), + decorators = NULL ) } \arguments{ @@ -93,6 +94,9 @@ with settings for the module plot. The argument is merged with options variable \code{teal.ggplot2_args} and default module setup. For more details see the vignette: \code{vignette("custom-ggplot2-arguments", package = "teal.widgets")}} + +\item{decorators}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} (\code{list} of \code{teal_transform_module} or \code{NULL}) optional, +if not \code{NULL}, decorator for tables or plots included in the module.} } \value{ Object of class \code{teal_module} to be used in \code{teal} applications. @@ -107,6 +111,18 @@ trend line additions, and dynamic adjustments of point opacity and size through For more examples, please see the vignette "Using scatterplot" via \code{vignette("using-scatterplot", package = "teal.modules.general")}. } +\section{Decorating \code{tm_g_scatterplot}}{ + + +This module generates the following objects, which can be modified in place using decorators: +\itemize{ +\item \code{plot} (\code{ggplot2}) +} + +For additional details and examples of decorators, refer to the vignette +\code{vignette("decorate-modules-output", package = "teal")} or the \code{\link[=teal_transform_module]{teal_transform_module()}} documentation. +} + \examples{ \dontshow{if (require("ggpmisc", quietly = TRUE) && require("ggExtra", quietly = TRUE) && require("colourpicker", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} # general data example