1313# ' It controls the font size for plot titles, axis labels, and legends.
1414# ' - If vector of `length == 1` then the font sizes will have a fixed size.
1515# ' - while vector of `value`, `min`, and `max` allows dynamic adjustment.
16- # ' @templateVar ggnames "Elbow plot", "Circle plot", "Biplot", "Eigenvector plot"
17- # ' @template ggplot2_args_multi
16+ # ' @param ggplot2_args `r roxygen_ggplot2_args_param( "Elbow plot", "Circle plot", "Biplot", "Eigenvector plot")`
17+ # ' @param decorators `r roxygen_decorators_param("tm_a_pca")`
1818# '
1919# ' @inherit shared_params return
2020# '
2121# ' @section Decorating `tm_a_pca`:
2222# '
2323# ' This module generates the following objects, which can be modified in place using decorators:
24- # ' - `plot` (`ggplot2`)
24+ # ' - `elbow_plot` (`ggplot2`)
25+ # ' - `circle_plot` (`ggplot2`)
26+ # ' - `biplot` (`ggplot2`)
27+ # ' - `eigenvector_plot` (`ggplot2`)
28+ # '
29+ # ' Decorators can be applied to all outputs or only to specific objects using a
30+ # ' named list of `teal_transform_module` objects.
31+ # ' The `"default"` name is reserved for decorators that are applied to all outputs.
32+ # ' See code snippet below:
33+ # '
34+ # ' ```
35+ # ' tm_a_pca(
36+ # ' ..., # arguments for module
37+ # ' decorators = list(
38+ # ' default = list(teal_transform_module(...)), # applied to all outputs
39+ # ' elbow_plot = list(teal_transform_module(...)), # applied only to `elbow_plot` output
40+ # ' circle_plot = list(teal_transform_module(...)) # applied only to `circle_plot` output
41+ # ' biplot = list(teal_transform_module(...)) # applied only to `biplot` output
42+ # ' eigenvector_plot = list(teal_transform_module(...)) # applied only to `eigenvector_plot` output
43+ # ' )
44+ # ' )
45+ # ' ```
2546# '
2647# ' For additional details and examples of decorators, refer to the vignette
2748# ' `vignette("decorate-modules-output", package = "teal")` or the [`teal_transform_module()`] documentation.
2849# '
29- # '
3050# ' @examplesShinylive
3151# ' library(teal.modules.general)
3252# ' interactive <- function() TRUE
@@ -165,7 +185,9 @@ tm_a_pca <- function(label = "Principal Component Analysis",
165185 checkmate :: assert_multi_class(pre_output , c(" shiny.tag" , " shiny.tag.list" , " html" ), null.ok = TRUE )
166186 checkmate :: assert_multi_class(post_output , c(" shiny.tag" , " shiny.tag.list" , " html" ), null.ok = TRUE )
167187
168- checkmate :: assert_list(decorators , " teal_transform_module" , null.ok = TRUE )
188+ available_decorators <- c(" elbow_plot" , " circle_plot" , " biplot" , " eigenvector_plot" )
189+ decorators <- normalize_decorators(decorators )
190+ assert_decorators(decorators , null.ok = TRUE , available_decorators )
169191 # End of assertions
170192
171193 # Make UI args
@@ -240,7 +262,34 @@ ui_a_pca <- function(id, ...) {
240262 choices = args $ plot_choices ,
241263 selected = args $ plot_choices [1 ]
242264 ),
243- ui_transform_teal_data(ns(" decorate" ), transformators = args $ decorators )
265+ conditionalPanel(
266+ condition = sprintf(" input['%s'] == 'Elbow plot'" , ns(" plot_type" )),
267+ ui_decorate_teal_data(
268+ ns(" d_elbow_plot" ),
269+ decorators = select_decorators(args $ decorators , " elbow_plot" )
270+ )
271+ ),
272+ conditionalPanel(
273+ condition = sprintf(" input['%s'] == 'Circle plot'" , ns(" plot_type" )),
274+ ui_decorate_teal_data(
275+ ns(" d_circle_plot" ),
276+ decorators = select_decorators(args $ decorators , " circle_plot" )
277+ )
278+ ),
279+ conditionalPanel(
280+ condition = sprintf(" input['%s'] == 'Biplot'" , ns(" plot_type" )),
281+ ui_decorate_teal_data(
282+ ns(" d_biplot" ),
283+ decorators = select_decorators(args $ decorators , " biplot" )
284+ )
285+ ),
286+ conditionalPanel(
287+ condition = sprintf(" input['%s'] == 'Eigenvector plot'" , ns(" plot_type" )),
288+ ui_decorate_teal_data(
289+ ns(" d_eigenvector_plot" ),
290+ decorators = select_decorators(args $ decorators , " eigenvector_plot" )
291+ )
292+ )
244293 ),
245294 teal.widgets :: panel_item(
246295 title = " Pre-processing" ,
@@ -565,7 +614,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
565614 )
566615
567616 cols <- c(getOption(" ggplot2.discrete.colour" ), c(" lightblue" , " darkred" , " black" ))[1 : 3 ]
568- plot <- ggplot(mapping = aes_string(x = " component" , y = " value" )) +
617+ elbow_plot <- ggplot(mapping = aes_string(x = " component" , y = " value" )) +
569618 geom_bar(
570619 aes(fill = " Single variance" ),
571620 data = dplyr :: filter(elb_dat , metric == " Proportion of Variance" ),
@@ -642,7 +691,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
642691 y = sin(seq(0 , 2 * pi , length.out = 100 ))
643692 )
644693
645- plot <- ggplot(pca_rot ) +
694+ circle_plot <- ggplot(pca_rot ) +
646695 geom_point(aes_string(x = x_axis , y = y_axis )) +
647696 geom_label(
648697 aes_string(x = x_axis , y = y_axis , label = " label" ),
@@ -874,7 +923,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
874923 qenv ,
875924 substitute(
876925 expr = {
877- plot <- plot_call
926+ biplot <- plot_call
878927 },
879928 env = list (
880929 plot_call = Reduce(function (x , y ) call(" +" , x , y ), pca_plot_biplot_expr )
@@ -883,8 +932,8 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
883932 )
884933 }
885934
886- # plot pc_var ----
887- plot_pc_var <- function (base_q ) {
935+ # plot eigenvector_plot ----
936+ plot_eigenvector <- function (base_q ) {
888937 pc <- input $ pc
889938 ggtheme <- input $ ggtheme
890939
@@ -950,7 +999,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
950999 expr = {
9511000 pca_rot <- pca $ rotation [, pc , drop = FALSE ] %> %
9521001 dplyr :: as_tibble(rownames = " Variable" )
953- plot <- plot_call
1002+ eigenvector_plot <- plot_call
9541003 },
9551004 env = list (
9561005 pc = pc ,
@@ -960,27 +1009,54 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
9601009 )
9611010 }
9621011
963- # plot final ----
964- output_q <- reactive({
965- req(computation())
966- teal :: validate_inputs(iv_r())
967- teal :: validate_inputs(iv_extra , header = " Plot settings are required" )
1012+ # qenvs ---
1013+ output_q <- lapply(
1014+ list (
1015+ elbow_plot = plot_elbow ,
1016+ circle_plot = plot_circle ,
1017+ biplot = plot_biplot ,
1018+ eigenvector_plot = plot_eigenvector
1019+ ),
1020+ function (fun ) {
1021+ reactive({
1022+ req(computation())
1023+ teal :: validate_inputs(iv_r())
1024+ teal :: validate_inputs(iv_extra , header = " Plot settings are required" )
1025+ fun(computation())
1026+ })
1027+ }
1028+ )
1029+
1030+ decorated_q <- mapply(
1031+ function (obj_name , q ) {
1032+ srv_decorate_teal_data(
1033+ id = sprintf(" d_%s" , obj_name ),
1034+ data = q ,
1035+ decorators = select_decorators(decorators , obj_name ),
1036+ expr = reactive({
1037+ substitute(print(.plot ), env = list (.plot = as.name(obj_name )))
1038+ }),
1039+ expr_is_reactive = TRUE
1040+ )
1041+ },
1042+ names(output_q ),
1043+ output_q
1044+ )
9681045
969- switch (input $ plot_type ,
970- " Elbow plot" = plot_elbow(computation()),
971- " Circle plot" = plot_circle(computation()),
972- " Biplot" = plot_biplot(computation()),
973- " Eigenvector plot" = plot_pc_var(computation()),
1046+ # plot final ----
1047+ decorated_output_q <- reactive({
1048+ switch (req(input $ plot_type ),
1049+ " Elbow plot" = decorated_q $ elbow_plot(),
1050+ " Circle plot" = decorated_q $ circle_plot(),
1051+ " Biplot" = decorated_q $ biplot(),
1052+ " Eigenvector plot" = decorated_q $ eigenvector_plot(),
9741053 stop(" Unknown plot" )
9751054 )
9761055 })
9771056
978- decorated_output_q_no_print <- srv_transform_teal_data(" decorate" , data = output_q , transformators = decorators )
979- decorated_output_q <- reactive(within(decorated_output_q_no_print(), expr = print(plot )))
980-
9811057 plot_r <- reactive({
982- req(output_q( ))
983- decorated_output_q()[[ " plot " ]]
1058+ plot_name <- gsub( " " , " _ " , tolower(req( input $ plot_type ) ))
1059+ req( decorated_output_q())[[ plot_name ]]
9841060 })
9851061
9861062 pws <- teal.widgets :: plot_with_settings_srv(
0 commit comments