Skip to content

Commit 1afa6e4

Browse files
committed
enable decorators for tm_a_pca
1 parent bf0af48 commit 1afa6e4

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

R/tm_a_pca.R

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,25 @@
1818
#'
1919
#' @inherit shared_params return
2020
#'
21+
#' @inheritSection tm_a_regression Decorating Module Outputs
22+
#' @section Decorating `tm_a_pca`:
23+
#'
24+
#' This module creates below objects that can be modified with decorators:
25+
#' - `plot` (`ggplot2`)
26+
#'
27+
#'
2128
#' @examplesShinylive
2229
#' library(teal.modules.general)
2330
#' interactive <- function() TRUE
2431
#' {{ next_example }}
2532
#' @examples
33+
#'
34+
#' plot_title <- teal_transform_module(
35+
#' server = make_teal_transform_server(expression(
36+
#' plot <- plot + ggtilte("Custom title")
37+
#' ))
38+
#' )
39+
#'
2640
#' # general data example
2741
#' data <- teal_data()
2842
#' data <- within(data, {
@@ -45,7 +59,8 @@
4559
#' multiple = TRUE
4660
#' ),
4761
#' filter = NULL
48-
#' )
62+
#' ),
63+
#' decorators = list(plot_title)
4964
#' )
5065
#' )
5166
#' )
@@ -58,6 +73,13 @@
5873
#' interactive <- function() TRUE
5974
#' {{ next_example }}
6075
#' @examples
76+
#'
77+
#' plot_title <- teal_transform_module(
78+
#' server = make_teal_transform_server(expression(
79+
#' plot <- plot + ggtilte("Custom title")
80+
#' ))
81+
#' )
82+
#'
6183
#' # CDISC data example
6284
#' data <- teal_data()
6385
#' data <- within(data, {
@@ -81,7 +103,8 @@
81103
#' multiple = TRUE
82104
#' ),
83105
#' filter = NULL
84-
#' )
106+
#' ),
107+
#' decorators = list(plot_title)
85108
#' )
86109
#' )
87110
#' )
@@ -102,7 +125,8 @@ tm_a_pca <- function(label = "Principal Component Analysis",
102125
alpha = c(1, 0, 1),
103126
size = c(2, 1, 8),
104127
pre_output = NULL,
105-
post_output = NULL) {
128+
post_output = NULL,
129+
decorators = list(default = teal_transform_module())) {
106130
message("Initializing tm_a_pca")
107131

108132
# Normalize the parameters
@@ -152,6 +176,8 @@ tm_a_pca <- function(label = "Principal Component Analysis",
152176

153177
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
154178
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
179+
180+
checkmate::assert_list(decorators, "teal_transform_module")
155181
# End of assertions
156182

157183
# Make UI args
@@ -169,7 +195,8 @@ tm_a_pca <- function(label = "Principal Component Analysis",
169195
list(
170196
plot_height = plot_height,
171197
plot_width = plot_width,
172-
ggplot2_args = ggplot2_args
198+
ggplot2_args = ggplot2_args,
199+
decorators = decorators
173200
)
174201
),
175202
datanames = teal.transform::get_extract_datanames(data_extract_list)
@@ -224,7 +251,8 @@ ui_a_pca <- function(id, ...) {
224251
label = "Plot type",
225252
choices = args$plot_choices,
226253
selected = args$plot_choices[1]
227-
)
254+
),
255+
ui_teal_transform_data(ns("decorator"), transformators = args$decorators)
228256
),
229257
teal.widgets::panel_item(
230258
title = "Pre-processing",
@@ -289,7 +317,7 @@ ui_a_pca <- function(id, ...) {
289317
}
290318

291319
# Server function for the PCA module
292-
srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, plot_width, ggplot2_args) {
320+
srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, plot_width, ggplot2_args, decorators) {
293321
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
294322
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
295323
checkmate::assert_class(data, "reactive")
@@ -549,7 +577,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
549577
)
550578

551579
cols <- c(getOption("ggplot2.discrete.colour"), c("lightblue", "darkred", "black"))[1:3]
552-
g <- ggplot(mapping = aes_string(x = "component", y = "value")) +
580+
plot <- ggplot(mapping = aes_string(x = "component", y = "value")) +
553581
geom_bar(
554582
aes(fill = "Single variance"),
555583
data = dplyr::filter(elb_dat, metric == "Proportion of Variance"),
@@ -570,7 +598,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
570598
ggthemes +
571599
themes
572600

573-
print(g)
601+
print(plot)
574602
},
575603
env = list(
576604
ggthemes = parsed_ggplot2_args$ggtheme,
@@ -628,7 +656,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
628656
y = sin(seq(0, 2 * pi, length.out = 100))
629657
)
630658

631-
g <- ggplot(pca_rot) +
659+
plot <- ggplot(pca_rot) +
632660
geom_point(aes_string(x = x_axis, y = y_axis)) +
633661
geom_label(
634662
aes_string(x = x_axis, y = y_axis, label = "label"),
@@ -640,7 +668,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
640668
labs +
641669
ggthemes +
642670
themes
643-
print(g)
671+
print(plot)
644672
},
645673
env = list(
646674
x_axis = x_axis,
@@ -861,8 +889,8 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
861889
qenv,
862890
substitute(
863891
expr = {
864-
g <- plot_call
865-
print(g)
892+
plot <- plot_call
893+
print(plot)
866894
},
867895
env = list(
868896
plot_call = Reduce(function(x, y) call("+", x, y), pca_plot_biplot_expr)
@@ -939,9 +967,9 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
939967
pca_rot <- pca$rotation[, pc, drop = FALSE] %>%
940968
dplyr::as_tibble(rownames = "Variable")
941969

942-
g <- plot_call
970+
plot <- plot_call
943971

944-
print(g)
972+
print(plot)
945973
},
946974
env = list(
947975
pc = pc,
@@ -966,8 +994,10 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
966994
)
967995
})
968996

997+
decorated_output_q <- srv_teal_transform_data("decorate", data = output_q, transformators = decorators)
998+
969999
plot_r <- reactive({
970-
output_q()[["g"]]
1000+
decorated_output_q()[["plot"]]
9711001
})
9721002

9731003
pws <- teal.widgets::plot_with_settings_srv(
@@ -1034,7 +1064,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
10341064

10351065
teal.widgets::verbatim_popup_srv(
10361066
id = "rcode",
1037-
verbatim_content = reactive(teal.code::get_code(output_q())),
1067+
verbatim_content = reactive(teal.code::get_code(req(decorated_output_q()))),
10381068
title = "R Code for PCA"
10391069
)
10401070

0 commit comments

Comments
 (0)