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

Improvement on decorators #822

Merged
merged 17 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 13 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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Imports:
DT (>= 0.13),
forcats (>= 1.0.0),
grid,
lifecycle (>= 0.2.0),
rlistings (>= 0.2.8),
llrs-roche marked this conversation as resolved.
Show resolved Hide resolved
scales,
shinyjs,
shinyTree (>= 0.2.8),
Expand Down Expand Up @@ -66,6 +66,7 @@ Suggests:
jsonlite,
knitr (>= 1.42),
lattice (>= 0.18-4),
lifecycle (>= 0.2.0),
logger (>= 0.2.0),
MASS,
nestcolor (>= 0.1.0),
Expand Down
16 changes: 1 addition & 15 deletions R/roxygen2_templates.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
# nocov start
roxygen_decorators_param <- function(module_name) {
paste(
sep = " ",
lifecycle::badge("experimental"),
" (`list` of `teal_transform_module`, named `list` of `teal_transform_module` or",
"`NULL`) optional, if not `NULL`, decorator for tables or plots included in the module.",
"When a named list of `teal_transform_module`, the decorators are applied to the",
"respective output objects.\n\n",
"Otherwise, the decorators are applied to all objects, which is equivalent as using the name `default`.\n\n",
sprintf("See section \"Decorating `%s`\"", module_name),
"below for more details."
)
}

roxygen_ggplot2_args_param <- function(...) {
llrs-roche marked this conversation as resolved.
Show resolved Hide resolved
paste(
sep = " ",
Expand All @@ -21,7 +7,7 @@ roxygen_ggplot2_args_param <- function(...) {
"The argument is merged with options variable `teal.ggplot2_args` and default module setup.\n\n",
sprintf(
"List names should match the following: `c(\"default\", %s)`.\n\n",
paste("\"", unlist(rlang::list2(...)), "\"", collapse = ", ", sep = "")
paste("\"", unlist(list(...)), "\"", collapse = ", ", sep = "")
),
"For more details see the vignette: `vignette(\"custom-ggplot2-arguments\", package = \"teal.widgets\")`."
)
Expand Down
10 changes: 6 additions & 4 deletions R/tm_a_pca.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
#' - If vector of `length == 1` then the font sizes will have a fixed size.
#' - while vector of `value`, `min`, and `max` allows dynamic adjustment.
#' @param ggplot2_args `r roxygen_ggplot2_args_param("Elbow plot", "Circle plot", "Biplot", "Eigenvector plot")`
#' @param decorators `r roxygen_decorators_param("tm_a_pca")`
#'
#' @inherit shared_params return
#'
#' @section Decorating `tm_a_pca`:
#' @section Decorating Module:
#'
#' This module generates the following objects, which can be modified in place using decorators:
#' - `elbow_plot` (`ggplot2`)
Expand Down Expand Up @@ -1121,9 +1120,12 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
)
})

# Render R code.
source_code_r <- reactive(teal.code::get_code(req(decorated_output_q())))

teal.widgets::verbatim_popup_srv(
id = "rcode",
verbatim_content = reactive(teal.code::get_code(req(decorated_output_q()))),
verbatim_content = source_code_r,
title = "R Code for PCA"
)

Expand All @@ -1146,7 +1148,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
card$append_text("Comment", "header3")
card$append_text(comment)
}
card$append_src(teal.code::get_code(req(decorated_output_q())))
card$append_src(source_code_r())
card
}
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
Expand Down
10 changes: 6 additions & 4 deletions R/tm_a_regression.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@
# nolint start: line_length.
#' @param ggplot2_args `r roxygen_ggplot2_args_param("Response vs Regressor", "Residuals vs Fitted", "Scale-Location", "Cook's distance", "Residuals vs Leverage", "Cook's dist vs Leverage")`
# nolint end: line_length.
#' @param decorators `r roxygen_decorators_param("tm_a_regression")`
#'
#' @inherit shared_params return
#'
#' @section Decorating `tm_a_regression`:
#' @section Decorating Module:
#'
#' This module generates the following objects, which can be modified in place using decorators:
#' - `plot` (`ggplot2`)
Expand Down Expand Up @@ -1006,9 +1005,12 @@ srv_a_regression <- function(id,
)
})

# Render R code.
source_code_r <- reactive(teal.code::get_code(req(decorated_output_q())))

teal.widgets::verbatim_popup_srv(
id = "rcode",
verbatim_content = reactive(teal.code::get_code(req(decorated_output_q()))),
verbatim_content = source_code_r,
title = "R code for the regression plot",
)

Expand All @@ -1027,7 +1029,7 @@ srv_a_regression <- function(id,
card$append_text("Comment", "header3")
card$append_text(comment)
}
card$append_src(teal.code::get_code(req(decorated_output_q())))
card$append_src(source_code_r())
card
}
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
Expand Down
43 changes: 8 additions & 35 deletions R/tm_data_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,9 @@
#' `list(searching = FALSE, pageLength = 30, lengthMenu = c(5, 15, 30, 100), scrollX = TRUE)`
#' @param server_rendering (`logical`) should the data table be rendered server side
#' (see `server` argument of [DT::renderDataTable()])
#' @param decorators `r roxygen_decorators_param("tm_data_table")`
#'
#' @inherit shared_params return
#'
#' @section Decorating `tm_data_table`:
#'
#' This module generates the following objects, which can be modified in place using decorators:
#' - `table` ([DT::datatable()])
#'
#' For additional details and examples of decorators, refer to the vignette
#' `vignette("decorate-modules-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
#'
#' @examplesShinylive
#' library(teal.modules.general)
#' interactive <- function() TRUE
Expand Down Expand Up @@ -105,8 +96,7 @@ tm_data_table <- function(label = "Data Table",
),
server_rendering = FALSE,
pre_output = NULL,
post_output = NULL,
decorators = NULL) {
post_output = NULL) {
message("Initializing tm_data_table")

# Start of assertions
Expand All @@ -132,8 +122,6 @@ tm_data_table <- function(label = "Data Table",
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)

decorators <- normalize_decorators(decorators)
assert_decorators(decorators, null.ok = TRUE, "table")
# End of assertions

ans <- module(
Expand All @@ -146,8 +134,7 @@ tm_data_table <- function(label = "Data Table",
datasets_selected = datasets_selected,
dt_args = dt_args,
dt_options = dt_options,
server_rendering = server_rendering,
decorators = decorators
server_rendering = server_rendering
),
ui_args = list(
pre_output = pre_output,
Expand Down Expand Up @@ -197,8 +184,7 @@ srv_page_data_table <- function(id,
variables_selected,
dt_args,
dt_options,
server_rendering,
decorators) {
server_rendering) {
checkmate::assert_class(data, "reactive")
checkmate::assert_class(isolate(data()), "teal_data")
moduleServer(id, function(input, output, session) {
Expand Down Expand Up @@ -251,8 +237,7 @@ srv_page_data_table <- function(id,
ui_data_table(
id = session$ns(x),
choices = choices,
selected = variables_selected,
decorators = decorators
selected = variables_selected
)
)
)
Expand All @@ -274,19 +259,15 @@ srv_page_data_table <- function(id,
if_distinct = if_distinct,
dt_args = dt_args,
dt_options = dt_options,
server_rendering = server_rendering,
decorators = decorators
server_rendering = server_rendering
)
}
)
})
}

# UI function for the data_table module
ui_data_table <- function(id,
choices,
selected,
decorators) {
ui_data_table <- function(id, choices, selected) {
ns <- NS(id)

if (!is.null(selected)) {
Expand All @@ -298,7 +279,6 @@ ui_data_table <- function(id,
tagList(
teal.widgets::get_dt_rows(ns("data_table"), ns("dt_rows")),
fluidRow(
ui_decorate_teal_data(ns("decorator"), decorators = select_decorators(decorators, "table")),
teal.widgets::optionalSelectInput(
ns("variables"),
"Select variables:",
Expand All @@ -322,8 +302,7 @@ srv_data_table <- function(id,
if_distinct,
dt_args,
dt_options,
server_rendering,
decorators) {
server_rendering) {
moduleServer(id, function(input, output, session) {
iv <- shinyvalidate::InputValidator$new()
iv$add_rule("variables", shinyvalidate::sv_required("Please select valid variable names"))
Expand Down Expand Up @@ -367,15 +346,9 @@ srv_data_table <- function(id,
)
})

decorated_data_table_data <- srv_decorate_teal_data(
id = "decorator",
data = data_table_data,
decorators = select_decorators(decorators, "table")
)

output$data_table <- DT::renderDataTable(server = server_rendering, {
teal::validate_inputs(iv)
req(decorated_data_table_data())[["table"]]
req(data_table_data())[["table"]]
})
})
}
10 changes: 6 additions & 4 deletions R/tm_g_association.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
#' Default to `"gray"`.
#'
#' @param ggplot2_args `r roxygen_ggplot2_args_param("Bivariate1", "Bivariate2")`
#' @param decorators `r roxygen_decorators_param("tm_g_association")`
#'
#' @inherit shared_params return
#'
#' @section Decorating `tm_g_association`:
#' @section Decorating Module:
#'
#' This module generates the following objects, which can be modified in place using decorators:
#' - `plot` (`grob` created with [ggplot2::ggplotGrob()])
Expand Down Expand Up @@ -526,9 +525,12 @@ srv_tm_g_association <- function(id,
teal.code::dev_suppress(output_q()[["title"]])
})

# Render R code.
source_code_r <- reactive(teal.code::get_code(req(decorated_output_grob_q())))

teal.widgets::verbatim_popup_srv(
id = "rcode",
verbatim_content = reactive(teal.code::get_code(req(decorated_output_grob_q()))),
verbatim_content = source_code_r,
title = "Association Plot"
)

Expand All @@ -547,7 +549,7 @@ srv_tm_g_association <- function(id,
card$append_text("Comment", "header3")
card$append_text(comment)
}
card$append_src(teal.code::get_code(req(decorated_output_grob_q())))
card$append_src(source_code_r())
card
}
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
Expand Down
10 changes: 6 additions & 4 deletions R/tm_g_bivariate.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@
#' @param free_y_scales (`logical`) optional, whether Y scaling shall be changeable.
#' Does not allow scaling to be changed by default (`FALSE`).
#' @param swap_axes (`logical`) optional, whether to swap X and Y axes. Defaults to `FALSE`.
#' @param decorators `r roxygen_decorators_param("tm_g_bivariate")`
#'
#' @inherit shared_params return
#'
#' @section Decorating `tm_g_bivariate`:
#' @section Decorating Module:
#'
#' This module generates the following objects, which can be modified in place using decorators:
#' - `plot` (`ggplot2`)
Expand Down Expand Up @@ -715,9 +714,12 @@ srv_g_bivariate <- function(id,
width = plot_width
)

# Render R code.
source_code_r <- reactive(teal.code::get_code(req(decorated_output_q_facets())))

teal.widgets::verbatim_popup_srv(
id = "rcode",
verbatim_content = reactive(teal.code::get_code(req(decorated_output_q_facets()))),
verbatim_content = source_code_r,
title = "Bivariate Plot"
)

Expand All @@ -736,7 +738,7 @@ srv_g_bivariate <- function(id,
card$append_text("Comment", "header3")
card$append_text(comment)
}
card$append_src(teal.code::get_code(req(decorated_output_q_facets)))
card$append_src(source_code_r())
card
}
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
Expand Down
Loading
Loading