Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce decorators for tm_g_ci #1265

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions R/tm_g_ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ template_g_ci <- function(dataname,

substitute(
expr = {
gg <- graph_expr
print(gg)
plot <- graph_expr
},
env = list(graph_expr = pipe_expr(graph_list, pipe_str = "+"))
)
Expand All @@ -189,6 +188,14 @@ template_g_ci <- function(dataname,
#'
#' @inherit module_arguments return seealso
#'
#' @section Decorating `tm_g_ci`:
#'
#' 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.clinical)
#' interactive <- function() TRUE
Expand Down Expand Up @@ -275,7 +282,8 @@ tm_g_ci <- function(label,
plot_width = NULL,
pre_output = NULL,
post_output = NULL,
ggplot2_args = teal.widgets::ggplot2_args()) {
ggplot2_args = teal.widgets::ggplot2_args(),
decorators = NULL) {
message("Initializing tm_g_ci")
checkmate::assert_string(label)
stat <- match.arg(stat)
Expand All @@ -293,6 +301,8 @@ tm_g_ci <- function(label,
checkmate::assert_class(pre_output, classes = "shiny.tag", null.ok = TRUE)
checkmate::assert_class(post_output, classes = "shiny.tag", null.ok = TRUE)
checkmate::assert_class(ggplot2_args, "ggplot2_args")
decorators <- normalize_decorators(decorators)
assert_decorators(decorators, null.ok = TRUE, "plot")

args <- as.list(environment())

Expand All @@ -306,7 +316,8 @@ tm_g_ci <- function(label,
label = label,
plot_height = plot_height,
plot_width = plot_width,
ggplot2_args = ggplot2_args
ggplot2_args = ggplot2_args,
decorators = decorators
),
ui = ui_g_ci,
ui_args = args,
Expand Down Expand Up @@ -355,7 +366,8 @@ ui_g_ci <- function(id, ...) {
label = "Statistic to use",
choices = c("mean", "median"),
selected = args$stat
)
),
ui_decorate_teal_data(ns("decorator"), decorators = subset_decorators("plot", args$decorators))
),
forms = tagList(
teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")
Expand All @@ -376,7 +388,8 @@ srv_g_ci <- function(id,
label,
plot_height,
plot_width,
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")
Expand Down Expand Up @@ -466,11 +479,18 @@ srv_g_ci <- function(id,
teal.code::eval_code(anl_q(), list_calls)
})

plot_r <- reactive(all_q()[["gg"]])
decorated_plot_q <- srv_decorate_teal_data(
id = "decorator",
data = all_q,
decorators = subset_decorators("plot", decorators),
expr = print(plot)
)
# Outputs to render.
plot_r <- reactive(decorated_plot_q()[["plot"]])

teal.widgets::verbatim_popup_srv(
id = "rcode",
verbatim_content = reactive(teal.code::get_code(all_q())),
verbatim_content = reactive(teal.code::get_code(req(decorated_plot_q()))),
title = label
)

Expand All @@ -497,7 +517,7 @@ srv_g_ci <- function(id,
card$append_text("Comment", "header3")
card$append_text(comment)
}
card$append_src(teal.code::get_code(all_q()))
card$append_src(teal.code::get_code(req(decorated_plot_q())))
card
}
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
Expand Down