|
| 1 | +setOldClass("teal_module") |
| 2 | +setOldClass("teal_modules") |
| 3 | + |
1 | 4 | #' Create `teal_module` and `teal_modules` objects |
2 | 5 | #' |
3 | 6 | #' @description |
|
39 | 42 | #' - `...` (optional) When provided, `ui_args` elements will be passed to the module named argument |
40 | 43 | #' or to the `...`. |
41 | 44 | #' @param filters (`character`) Deprecated. Use `datanames` instead. |
42 | | -#' @param datanames (`character`) A vector with `datanames` that are relevant for the item. The |
43 | | -#' filter panel will automatically update the shown filters to include only |
44 | | -#' filters in the listed datasets. `NULL` will hide the filter panel, |
45 | | -#' and the keyword `"all"` will show filters of all datasets. `datanames` also determines |
46 | | -#' a subset of datasets which are appended to the `data` argument in server function. |
| 45 | +#' @param datanames (`character`) Names of the datasets that are relevant for the item. |
| 46 | +#' The keyword `"all"` provides all datasets available in `data` passed to `teal` application. |
| 47 | +#' `NULL` will hide the filter panel. |
47 | 48 | #' @param server_args (named `list`) with additional arguments passed on to the server function. |
48 | 49 | #' @param ui_args (named `list`) with additional arguments passed on to the UI function. |
49 | 50 | #' @param x (`teal_module` or `teal_modules`) Object to format/print. |
50 | 51 | #' @param indent (`integer(1)`) Indention level; each nested element is indented one level more. |
51 | 52 | #' @param transformers (`list` of `teal_data_module`) that will be applied to transform the data. |
52 | 53 | #' Each transform module UI will appear in the `teal` application, unless the `custom_ui` attribute is set on the list. |
53 | | -#' If so, the module developer is responsible to display the UI in the module itself. |
| 54 | +#' If so, the module developer is responsible to display the UI in the module itself. `datanames` of the `transformers` |
| 55 | +#' will be added to the `datanames`. |
54 | 56 | #' |
55 | 57 | #' When the transformation does not have sufficient input data, the resulting data will fallback |
56 | 58 | #' to the last successful transform or, in case there are none, to the filtered data. |
57 | 59 | #' @param ... |
58 | 60 | #' - For `modules()`: (`teal_module` or `teal_modules`) Objects to wrap into a tab. |
59 | 61 | #' - For `format()` and `print()`: Arguments passed to other methods. |
60 | 62 | #' |
| 63 | +#' @section `datanames`: |
| 64 | +#' The module's `datanames` argument determines a subset of datasets from the `data` object, as specified in the |
| 65 | +#' server function argument, to be presented in the module. Datasets displayed in the filter panel will be limited |
| 66 | +#' to this subset. |
| 67 | +#' When `datanames` is set to `"all"`, all available datasets in the `data` object are considered relevant for the |
| 68 | +#' module. However, setting `datanames` argument to `"all"` might include datasets that are irrelevant for the module, |
| 69 | +#' for example: |
| 70 | +#' - Proxy variables used for modifying columns. |
| 71 | +#' - Modified copies of datasets used to create a final dataset. |
| 72 | +#' - Connection objects. |
| 73 | +#' To prevent these irrelevant datasets from appearing in the module, use the [set_datanames()] function on the |
| 74 | +#' [module] or [modules()] to change the `datanames` from `"all"` to specific dataset names. Attempting to change |
| 75 | +#' `datanames` values that was not set to `"all"` using [set_datanames()] will be ignored with a warning. |
| 76 | +#' |
| 77 | +#' Additionally, datasets with names starting with `.` are ignored when `datanames` is set to `"all"`. |
| 78 | +#' |
61 | 79 | #' @return |
62 | 80 | #' `module()` returns an object of class `teal_module`. |
63 | 81 | #' |
|
127 | 145 | #' @export |
128 | 146 | #' |
129 | 147 | module <- function(label = "module", |
130 | | - server = function(id, ...) moduleServer(id, function(input, output, session) NULL), |
| 148 | + server = function(id, data, ...) moduleServer(id, function(input, output, session) NULL), |
131 | 149 | ui = function(id, ...) tags$p(paste0("This module has no UI (id: ", id, " )")), |
132 | 150 | filters, |
133 | 151 | datanames = "all", |
@@ -241,14 +259,23 @@ module <- function(label = "module", |
241 | 259 | } |
242 | 260 |
|
243 | 261 | ## `transformers` |
244 | | - checkmate::assert_list(transformers, types = "teal_data_module") |
| 262 | + if (inherits(transformers, "teal_transform_module")) { |
| 263 | + transformers <- list(transformers) |
| 264 | + } |
| 265 | + checkmate::assert_list(transformers, types = "teal_transform_module") |
| 266 | + transformer_datanames <- unlist(lapply(transformers, attr, "datanames")) |
| 267 | + combined_datanames <- if (identical(datanames, "all") || identical(transformer_datanames, "all")) { |
| 268 | + "all" |
| 269 | + } else { |
| 270 | + union(datanames, transformer_datanames) |
| 271 | + } |
245 | 272 |
|
246 | 273 | structure( |
247 | 274 | list( |
248 | 275 | label = label, |
249 | 276 | server = server, |
250 | 277 | ui = ui, |
251 | | - datanames = unique(datanames), |
| 278 | + datanames = combined_datanames, |
252 | 279 | server_args = server_args, |
253 | 280 | ui_args = ui_args, |
254 | 281 | transformers = transformers |
@@ -313,6 +340,38 @@ format.teal_modules <- function(x, indent = 0, ...) { |
313 | 340 | ) |
314 | 341 | } |
315 | 342 |
|
| 343 | +#' @param modules (`teal_module` or `teal_modules`) |
| 344 | +#' @rdname teal_modules |
| 345 | +#' @examples |
| 346 | +#' # change the module's datanames |
| 347 | +#' set_datanames(module(datanames = "all"), "a") |
| 348 | +#' |
| 349 | +#' # change modules' datanames |
| 350 | +#' set_datanames( |
| 351 | +#' modules( |
| 352 | +#' module(datanames = "all"), |
| 353 | +#' module(datanames = "a") |
| 354 | +#' ), |
| 355 | +#' "b" |
| 356 | +#' ) |
| 357 | +#' @export |
| 358 | +set_datanames <- function(modules, datanames) { |
| 359 | + checkmate::assert_multi_class(modules, c("teal_modules", "teal_module")) |
| 360 | + if (inherits(modules, "teal_modules")) { |
| 361 | + modules$children <- lapply(modules$children, set_datanames, datanames) |
| 362 | + } else { |
| 363 | + if (identical(modules$datanames, "all")) { |
| 364 | + modules$datanames <- datanames |
| 365 | + } else { |
| 366 | + warning( |
| 367 | + "Not possible to modify datanames of the module ", modules$label, |
| 368 | + ". set_datanames() can only change datanames if it was set to \"all\".", |
| 369 | + call. = FALSE |
| 370 | + ) |
| 371 | + } |
| 372 | + } |
| 373 | + modules |
| 374 | +} |
316 | 375 |
|
317 | 376 | #' @rdname teal_modules |
318 | 377 | #' @export |
|
0 commit comments