diff --git a/R/modules.R b/R/modules.R index bacbbcb73e..e541a65f2d 100644 --- a/R/modules.R +++ b/R/modules.R @@ -329,6 +329,17 @@ modules <- function(..., label = "root") { #' format.teal_modules(). Determines whether to show "TEAL ROOT" header #' @param what (`character`) Specifes which metadata to display. #' Possible values: "datasets", "properties", "ui_args", "server_args", "transformers" +#' @examples +#' mod <- module( +#' label = "My Custom Module", +#' server = function(id, data, ...) {}, +#' ui = function(id, ...) {}, +#' datanames = c("ADSL", "ADTTE"), +#' transformers = list(), +#' ui_args = list(a = 1, b = "b"), +#' server_args = list(x = 5, y = list(p = 1)) +#' ) +#' cat(format(mod)) #' @export format.teal_module <- function( x, indent = 0, is_last = FALSE, parent_prefix = "", @@ -394,11 +405,107 @@ format.teal_module <- function( } #' @rdname teal_modules +#' @examples +#' custom_module <- function(label = "label", ui_args = NULL, server_args = NULL, datanames = "all", transformers = list(), bk = FALSE) { # nolint line_length +#' ans <- module( +#' label, +#' server = function(id, data, ...) {}, +#' ui = function(id, ...) { +#' }, +#' datanames = datanames, +#' transformers = transformers, +#' ui_args = ui_args, +#' server_args = server_args +#' ) +#' attr(ans, "teal_bookmarkable") <- bk +#' ans +#' } +#' +#' dummy_transformer <- teal_transform_module( +#' label = "Dummy Transform", +#' ui = function(id) div("(does nothing)"), +#' server = function(id, data) { +#' moduleServer(id, function(input, output, session) data) +#' } +#' ) +#' +#' plot_transformer <- teal_transform_module( +#' label = "Plot Settings", +#' ui = function(id) div("(does nothing)"), +#' server = function(id, data) { +#' moduleServer(id, function(input, output, session) data) +#' } +#' ) +#' +#' complete_modules <- modules( +#' custom_module( +#' label = "Data Overview", +#' datanames = c("ADSL", "ADAE", "ADVS"), +#' ui_args = list( +#' view_type = "table", +#' page_size = 10, +#' filters = c("ARM", "SEX", "RACE") +#' ), +#' server_args = list( +#' cache = TRUE, +#' debounce = 1000 +#' ), +#' transformers = list(dummy_transformer), +#' bk = TRUE +#' ), +#' modules( +#' label = "Nested 1", +#' custom_module( +#' label = "Interactive Plots", +#' datanames = c("ADSL", "ADVS"), +#' ui_args = list( +#' plot_type = c("scatter", "box", "line"), +#' height = 600, +#' width = 800, +#' color_scheme = "viridis" +#' ), +#' server_args = list( +#' render_type = "svg", +#' cache_plots = TRUE +#' ), +#' transformers = list(dummy_transformer, plot_transformer), +#' bk = TRUE +#' ), +#' modules( +#' label = "Nested 2", +#' custom_module( +#' label = "Summary Statistics", +#' datanames = "ADSL", +#' ui_args = list( +#' stats = c("mean", "median", "sd", "range"), +#' grouping = c("ARM", "SEX") +#' ) +#' ), +#' modules( +#' label = "Labeled nested modules", +#' custom_module( +#' label = "Subgroup Analysis", +#' datanames = c("ADSL", "ADAE"), +#' ui_args = list( +#' subgroups = c("AGE", "SEX", "RACE"), +#' analysis_type = "stratified" +#' ), +#' bk = TRUE +#' ) +#' ), +#' modules(custom_module(label = "Subgroup Analysis in non-labled modules")) +#' ) +#' ), +#' custom_module("Non-nested module") +#' ) +#' +#' cat(format(complete_modules)) +#' cat(format(complete_modules, what = c("ui_args", "server_args", "transformers"))) #' @export format.teal_modules <- function(x, indent = 0, is_root = TRUE, is_last = FALSE, parent_prefix = "", ...) { if (is_root) { header <- pasten(crayon::bold("TEAL ROOT")) - new_parent_prefix <- " " # Initial indent for root level + new_parent_prefix <- " " #' Initial indent for root level } else { if (!is.null(x$label)) { branch <- if (is_last) "└─" else "├─" diff --git a/man/teal_modules.Rd b/man/teal_modules.Rd index 840b140d47..acfcaf950c 100644 --- a/man/teal_modules.Rd +++ b/man/teal_modules.Rd @@ -224,6 +224,111 @@ app <- init( if (interactive()) { shinyApp(app$ui, app$server) } +mod <- module( + label = "My Custom Module", + server = function(id, data, ...) {}, + ui = function(id, ...) {}, + datanames = c("ADSL", "ADTTE"), + transformers = list(), + ui_args = list(a = 1, b = "b"), + server_args = list(x = 5, y = list(p = 1)) +) +cat(format(mod)) +custom_module <- function(label = "label", ui_args = NULL, server_args = NULL, datanames = "all", transformers = list(), bk = FALSE) { # nolint line_length + ans <- module( + label, + server = function(id, data, ...) {}, + ui = function(id, ...) { + }, + datanames = datanames, + transformers = transformers, + ui_args = ui_args, + server_args = server_args + ) + attr(ans, "teal_bookmarkable") <- bk + ans +} + +dummy_transformer <- teal_transform_module( + label = "Dummy Transform", + ui = function(id) div("(does nothing)"), + server = function(id, data) { + moduleServer(id, function(input, output, session) data) + } +) + +plot_transformer <- teal_transform_module( + label = "Plot Settings", + ui = function(id) div("(does nothing)"), + server = function(id, data) { + moduleServer(id, function(input, output, session) data) + } +) + +complete_modules <- modules( + custom_module( + label = "Data Overview", + datanames = c("ADSL", "ADAE", "ADVS"), + ui_args = list( + view_type = "table", + page_size = 10, + filters = c("ARM", "SEX", "RACE") + ), + server_args = list( + cache = TRUE, + debounce = 1000 + ), + transformers = list(dummy_transformer), + bk = TRUE + ), + modules( + label = "Nested 1", + custom_module( + label = "Interactive Plots", + datanames = c("ADSL", "ADVS"), + ui_args = list( + plot_type = c("scatter", "box", "line"), + height = 600, + width = 800, + color_scheme = "viridis" + ), + server_args = list( + render_type = "svg", + cache_plots = TRUE + ), + transformers = list(dummy_transformer, plot_transformer), + bk = TRUE + ), + modules( + label = "Nested 2", + custom_module( + label = "Summary Statistics", + datanames = "ADSL", + ui_args = list( + stats = c("mean", "median", "sd", "range"), + grouping = c("ARM", "SEX") + ) + ), + modules( + label = "Labeled nested modules", + custom_module( + label = "Subgroup Analysis", + datanames = c("ADSL", "ADAE"), + ui_args = list( + subgroups = c("AGE", "SEX", "RACE"), + analysis_type = "stratified" + ), + bk = TRUE + ) + ), + modules(custom_module(label = "Subgroup Analysis in non-labled modules")) + ) + ), + custom_module("Non-nested module") +) + +cat(format(complete_modules)) +cat(format(complete_modules, what = c("ui_args", "server_args", "transformers"))) # change the module's datanames set_datanames(module(datanames = "all"), "a")