Skip to content

Commit

Permalink
1441 Introduce transformators parameter in modules (#826)
Browse files Browse the repository at this point in the history
insightsengineering/teal#1441

Included `transformators` parameter in modules. This is passed to
`teal::module` that does assertions and checks on this parameter.

Did not introduce for `tm_data_table`, `tm_file_viewer` and
`tm_front_page` as we also didn't introduce `decorators` for those
modules. We can always introduce, if there is a user request, but I
don't think there will ever be.

Lastly, a food for thought: default `decorators` value is `NULL` where
default `transformators` value is `list()`. Maybe we can introduce a
check, that allows `transformators` to be `NULL` on default, and which
changes `NULL` to `list()`.

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Lluís Revilla <185338939+llrs-roche@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 20, 2025
1 parent 8ce4546 commit 798168a
Show file tree
Hide file tree
Showing 24 changed files with 129 additions and 69 deletions.
6 changes: 4 additions & 2 deletions R/tm_a_pca.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ tm_a_pca <- function(label = "Principal Component Analysis",
size = c(2, 1, 8),
pre_output = NULL,
post_output = NULL,
decorators = NULL) {
transformators = list(),
decorators = list()) {
message("Initializing tm_a_pca")

# Normalize the parameters
Expand Down Expand Up @@ -186,7 +187,7 @@ tm_a_pca <- function(label = "Principal Component Analysis",

available_decorators <- c("elbow_plot", "circle_plot", "biplot", "eigenvector_plot")
decorators <- normalize_decorators(decorators)
assert_decorators(decorators, null.ok = TRUE, available_decorators)
assert_decorators(decorators, available_decorators)
# End of assertions

# Make UI args
Expand All @@ -208,6 +209,7 @@ tm_a_pca <- function(label = "Principal Component Analysis",
decorators = decorators
)
),
transformators = transformators,
datanames = teal.transform::get_extract_datanames(data_extract_list)
)
attr(ans, "teal_bookmarkable") <- FALSE
Expand Down
8 changes: 5 additions & 3 deletions R/tm_a_regression.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ tm_a_regression <- function(label = "Regression Analysis",
default_plot_type = 1,
default_outlier_label = "USUBJID",
label_segment_threshold = c(0.5, 0, 10),
decorators = NULL) {
transformators = list(),
decorators = list()) {
message("Initializing tm_a_regression")

# Normalize the parameters
Expand Down Expand Up @@ -211,7 +212,7 @@ tm_a_regression <- function(label = "Regression Analysis",
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
checkmate::assert_choice(default_plot_type, seq.int(1L, length(plot_choices)))
checkmate::assert_string(default_outlier_label)
checkmate::assert_list(decorators, "teal_transform_module", null.ok = TRUE)
checkmate::assert_list(decorators, "teal_transform_module")

if (length(label_segment_threshold) == 1) {
checkmate::assert_numeric(label_segment_threshold, any.missing = FALSE, finite = TRUE)
Expand All @@ -225,7 +226,7 @@ tm_a_regression <- function(label = "Regression Analysis",
)
}
decorators <- normalize_decorators(decorators)
assert_decorators(decorators, "plot", null.ok = TRUE)
assert_decorators(decorators, "plot")
# End of assertions

# Make UI args
Expand All @@ -251,6 +252,7 @@ tm_a_regression <- function(label = "Regression Analysis",
decorators = decorators
)
),
transformators = transformators,
datanames = teal.transform::get_extract_datanames(data_extract_list)
)
attr(ans, "teal_bookmarkable") <- FALSE
Expand Down
6 changes: 4 additions & 2 deletions R/tm_g_association.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ tm_g_association <- function(label = "Association",
pre_output = NULL,
post_output = NULL,
ggplot2_args = teal.widgets::ggplot2_args(),
decorators = NULL) {
transformators = list(),
decorators = list()) {
message("Initializing tm_g_association")

# Normalize the parameters
Expand Down Expand Up @@ -176,7 +177,7 @@ tm_g_association <- function(label = "Association",
checkmate::assert_subset(names(ggplot2_args), c("default", plot_choices))

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

# Make UI args
Expand All @@ -196,6 +197,7 @@ tm_g_association <- function(label = "Association",
data_extract_list,
list(plot_height = plot_height, plot_width = plot_width, ggplot2_args = ggplot2_args, decorators = decorators)
),
transformators = transformators,
datanames = teal.transform::get_extract_datanames(data_extract_list)
)
attr(ans, "teal_bookmarkable") <- TRUE
Expand Down
6 changes: 4 additions & 2 deletions R/tm_g_bivariate.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ tm_g_bivariate <- function(label = "Bivariate Plots",
ggplot2_args = teal.widgets::ggplot2_args(),
pre_output = NULL,
post_output = NULL,
decorators = NULL) {
transformators = list(),
decorators = list()) {
message("Initializing tm_g_bivariate")

# Normalize the parameters
Expand Down Expand Up @@ -277,7 +278,7 @@ tm_g_bivariate <- function(label = "Bivariate Plots",
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, "plot")
assert_decorators(decorators, "plot")
# End of assertions

# Make UI args
Expand All @@ -303,6 +304,7 @@ tm_g_bivariate <- function(label = "Bivariate Plots",
data_extract_list,
list(plot_height = plot_height, plot_width = plot_width, ggplot2_args = ggplot2_args, decorators = decorators)
),
transformators = transformators,
datanames = teal.transform::get_extract_datanames(data_extract_list)
)
attr(ans, "teal_bookmarkable") <- TRUE
Expand Down
6 changes: 4 additions & 2 deletions R/tm_g_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ tm_g_distribution <- function(label = "Distribution Module",
plot_width = NULL,
pre_output = NULL,
post_output = NULL,
decorators = NULL) {
transformators = list(),
decorators = list()) {
message("Initializing tm_g_distribution")

# Normalize the parameters
Expand Down Expand Up @@ -194,7 +195,7 @@ tm_g_distribution <- function(label = "Distribution Module",

available_decorators <- c("histogram_plot", "qq_plot", "test_table", "summary_table")
decorators <- normalize_decorators(decorators)
assert_decorators(decorators, null.ok = TRUE, names = available_decorators)
assert_decorators(decorators, names = available_decorators)

# End of assertions

Expand All @@ -221,6 +222,7 @@ tm_g_distribution <- function(label = "Distribution Module",
),
ui = ui_distribution,
ui_args = args,
transformators = transformators,
datanames = teal.transform::get_extract_datanames(data_extract_list)
)
attr(ans, "teal_bookmarkable") <- TRUE
Expand Down
6 changes: 4 additions & 2 deletions R/tm_g_response.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ tm_g_response <- function(label = "Response Plot",
ggplot2_args = teal.widgets::ggplot2_args(),
pre_output = NULL,
post_output = NULL,
decorators = NULL) {
transformators = list(),
decorators = list()) {
message("Initializing tm_g_response")

# Normalize the parameters
Expand Down Expand Up @@ -202,7 +203,7 @@ tm_g_response <- function(label = "Response Plot",
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, "plot")
assert_decorators(decorators, "plot")
# End of assertions

# Make UI args
Expand All @@ -229,6 +230,7 @@ tm_g_response <- function(label = "Response Plot",
decorators = decorators
)
),
transformators = transformators,
datanames = teal.transform::get_extract_datanames(data_extract_list)
)
attr(ans, "teal_bookmarkable") <- TRUE
Expand Down
6 changes: 4 additions & 2 deletions R/tm_g_scatterplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ tm_g_scatterplot <- function(label = "Scatterplot",
post_output = NULL,
table_dec = 4,
ggplot2_args = teal.widgets::ggplot2_args(),
decorators = NULL) {
transformators = list(),
decorators = list()) {
message("Initializing tm_g_scatterplot")

# Normalize the parameters
Expand Down Expand Up @@ -299,7 +300,7 @@ tm_g_scatterplot <- function(label = "Scatterplot",
checkmate::assert_class(ggplot2_args, "ggplot2_args")

decorators <- normalize_decorators(decorators)
assert_decorators(decorators, null.ok = TRUE, "plot")
assert_decorators(decorators, "plot")

# End of assertions

Expand Down Expand Up @@ -330,6 +331,7 @@ tm_g_scatterplot <- function(label = "Scatterplot",
decorators = decorators
)
),
transformators = transformators,
datanames = teal.transform::get_extract_datanames(data_extract_list)
)
attr(ans, "teal_bookmarkable") <- TRUE
Expand Down
6 changes: 4 additions & 2 deletions R/tm_g_scatterplotmatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ tm_g_scatterplotmatrix <- function(label = "Scatterplot Matrix",
plot_width = NULL,
pre_output = NULL,
post_output = NULL,
decorators = NULL) {
transformators = list(),
decorators = list()) {
message("Initializing tm_g_scatterplotmatrix")

# Normalize the parameters
Expand All @@ -199,7 +200,7 @@ tm_g_scatterplotmatrix <- function(label = "Scatterplot Matrix",
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, "plot")
assert_decorators(decorators, "plot")
# End of assertions

# Make UI args
Expand All @@ -216,6 +217,7 @@ tm_g_scatterplotmatrix <- function(label = "Scatterplot Matrix",
plot_width = plot_width,
decorators = decorators
),
transformators = transformators,
datanames = teal.transform::get_extract_datanames(variables)
)
attr(ans, "teal_bookmarkable") <- TRUE
Expand Down
6 changes: 4 additions & 2 deletions R/tm_missing_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ tm_missing_data <- function(label = "Missing data",
),
pre_output = NULL,
post_output = NULL,
decorators = NULL) {
transformators = list(),
decorators = list()) {
message("Initializing tm_missing_data")

# Normalize the parameters
Expand Down Expand Up @@ -148,7 +149,7 @@ tm_missing_data <- function(label = "Missing data",

available_decorators <- c("summary_plot", "combination_plot", "by_subject_plot", "summary_table")
decorators <- normalize_decorators(decorators)
assert_decorators(decorators, null.ok = TRUE, names = available_decorators)
assert_decorators(decorators, names = available_decorators)
# End of assertions

ans <- module(
Expand All @@ -164,6 +165,7 @@ tm_missing_data <- function(label = "Missing data",
decorators = decorators
),
ui = ui_page_missing_data,
transformators = transformators,
ui_args = list(pre_output = pre_output, post_output = post_output)
)
attr(ans, "teal_bookmarkable") <- TRUE
Expand Down
6 changes: 4 additions & 2 deletions R/tm_outliers.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ tm_outliers <- function(label = "Outliers Module",
plot_width = NULL,
pre_output = NULL,
post_output = NULL,
decorators = NULL) {
transformators = list(),
decorators = list()) {
message("Initializing tm_outliers")

# Normalize the parameters
Expand Down Expand Up @@ -197,7 +198,7 @@ tm_outliers <- function(label = "Outliers Module",

available_decorators <- c("box_plot", "density_plot", "cumulative_plot", "table")
decorators <- normalize_decorators(decorators)
assert_decorators(decorators, null.ok = TRUE, names = available_decorators)
assert_decorators(decorators, names = available_decorators)
# End of assertions

# Make UI args
Expand All @@ -221,6 +222,7 @@ tm_outliers <- function(label = "Outliers Module",
),
ui = ui_outliers,
ui_args = args,
transformators = transformators,
datanames = teal.transform::get_extract_datanames(data_extract_list)
)
attr(ans, "teal_bookmarkable") <- TRUE
Expand Down
6 changes: 4 additions & 2 deletions R/tm_t_crosstable.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ tm_t_crosstable <- function(label = "Cross Table",
pre_output = NULL,
post_output = NULL,
basic_table_args = teal.widgets::basic_table_args(),
decorators = NULL) {
transformators = list(),
decorators = list()) {
message("Initializing tm_t_crosstable")

# Normalize the parameters
Expand All @@ -164,7 +165,7 @@ tm_t_crosstable <- function(label = "Cross Table",
checkmate::assert_class(basic_table_args, classes = "basic_table_args")

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

# Make UI args
Expand All @@ -184,6 +185,7 @@ tm_t_crosstable <- function(label = "Cross Table",
ui = ui_t_crosstable,
ui_args = ui_args,
server_args = server_args,
transformators = transformators,
datanames = teal.transform::get_extract_datanames(list(x = x, y = y))
)
attr(ans, "teal_bookmarkable") <- TRUE
Expand Down
16 changes: 5 additions & 11 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
#' - When the length of `size` is three: the plot points size are dynamically adjusted based on
#' vector of `value`, `min`, and `max`.
#' @param decorators `r 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.
#' (`list` of `teal_transform_module`, named `list` of `teal_transform_module`) optional,
#' decorator for tables or plots included in the module output reported.
#' When a named list of `teal_transform_module`, the decorators are applied to the respective output objects.
#'
#' Otherwise, the decorators are applied to all objects, which is equivalent as using the name `default`.
Expand Down Expand Up @@ -342,14 +342,9 @@ ui_decorate_teal_data <- function(id, decorators, ...) {

#' Internal function to check if decorators is a valid object
#' @noRd
check_decorators <- function(x, names = NULL, null.ok = FALSE) { # nolint: object_name.
checkmate::qassert(null.ok, "B1")
check_decorators <- function(x, names = NULL) { # nolint: object_name.

check_message <- checkmate::check_list(
x,
null.ok = null.ok,
names = "named"
)
check_message <- checkmate::check_list(x, names = "named")

if (!is.null(names)) {
check_message <- if (isTRUE(check_message)) {
Expand All @@ -373,7 +368,6 @@ check_decorators <- function(x, names = NULL, null.ok = FALSE) { # nolint: objec
x,
checkmate::test_list,
types = "teal_transform_module",
null.ok = TRUE,
FUN.VALUE = logical(1L)
)

Expand Down Expand Up @@ -411,7 +405,7 @@ select_decorators <- function(decorators, scope) {
#' @return A named list of lists with `teal_transform_module` objects.
#' @keywords internal
normalize_decorators <- function(decorators) {
if (checkmate::test_list(decorators, "teal_transform_module", null.ok = TRUE)) {
if (checkmate::test_list(decorators, "teal_transform_module")) {
if (checkmate::test_names(names(decorators))) {
lapply(decorators, list)
} else {
Expand Down
4 changes: 2 additions & 2 deletions man/shared_params.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions man/tm_a_pca.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 798168a

Please sign in to comment.