|
| 1 | +#' Extract Non-Parent Module Labels to `YAML` |
| 2 | +#' |
| 3 | +#' @description `r lifecycle::badge("experimental")` |
| 4 | +#' Extracts module labels from a teal modules object, filters out parent modules |
| 5 | +#' (grouping containers), and generates a `YAML` file with the functional modules. |
| 6 | +#' |
| 7 | +#' @param mods (`teal_module` or `teal_modules`) a teal modules object |
| 8 | +#' containing the module structure. |
| 9 | +#' @param filepath (`character(1)`) character string specifying the output `YAML` file path. |
| 10 | +#' |
| 11 | +#' @return Character vector of non-parent module labels |
| 12 | +#' |
| 13 | +#' @import yaml |
| 14 | +#' @examples |
| 15 | +#' # Extract modules from mods object to YAML file |
| 16 | +#' mods <- teal::modules( |
| 17 | +#' teal::example_module("mod1"), |
| 18 | +#' teal::example_module("mod2") |
| 19 | +#' ) |
| 20 | +#' labels <- extract_modules_to_yaml(mods, "panel_str_modules.yml") |
| 21 | +#' |
| 22 | +#' @export |
| 23 | +extract_modules_to_yaml <- function(mods, filepath) { |
| 24 | + # Recursively extract module labels, excluding parent containers |
| 25 | + extract_labels <- function(mod_obj) { |
| 26 | + labels <- character(0) |
| 27 | + |
| 28 | + if (inherits(mod_obj, "teal_module")) { |
| 29 | + # This is a leaf module - extract its label |
| 30 | + return(mod_obj$label) |
| 31 | + } else if (inherits(mod_obj, "teal_modules")) { |
| 32 | + # This is a container - recurse into children |
| 33 | + for (child in mod_obj$children) { |
| 34 | + labels <- c(labels, extract_labels(child)) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + labels |
| 39 | + } |
| 40 | + |
| 41 | + # Extract all non-parent module labels |
| 42 | + non_parent_labels <- extract_labels(mods) |
| 43 | + |
| 44 | + # Create panel_str structure |
| 45 | + panel_str <- list( |
| 46 | + list(access_panel = "ADMIN"), |
| 47 | + list( |
| 48 | + access_panel = "Modules", |
| 49 | + access_units = lapply(non_parent_labels, function(label) list(unit = label)) |
| 50 | + ) |
| 51 | + ) |
| 52 | + |
| 53 | + # Write to YAML file |
| 54 | + writeLines(yaml::as.yaml(list(panel_str = panel_str)), filepath) |
| 55 | + |
| 56 | + cat("Generated", filepath, "with", length(non_parent_labels), "non-parent module labels\n") |
| 57 | + non_parent_labels |
| 58 | +} |
| 59 | + |
| 60 | +#' Filter Teal Modules by Label |
| 61 | +#' |
| 62 | +#' @description `r lifecycle::badge("experimental")` |
| 63 | +#' Recursively filters a teal modules object to keep only modules whose labels |
| 64 | +#' match the specified labels. Removes modules that don't match and empty |
| 65 | +#' parent containers. |
| 66 | +#' |
| 67 | +#' @param x (`teal_module` or `teal_modules`) the object to filter. |
| 68 | +#' @param label (`character(1)`) character vector of module labels to keep. |
| 69 | +#' |
| 70 | +#' @return Filtered `teal_modules` or `teal_module` object, or `NULL` if none matches. |
| 71 | +#' |
| 72 | +#' @import checkmate |
| 73 | +#' @examples |
| 74 | +#' # Keep only specific modules by label |
| 75 | +#' mods <- teal::modules( |
| 76 | +#' teal::example_module("mod1"), |
| 77 | +#' teal::example_module("mod2") |
| 78 | +#' ) |
| 79 | +#' filtered_mods <- keep_by_label(mods, c("Data Table", "Disposition")) |
| 80 | +#' |
| 81 | +#' @export |
| 82 | +keep_by_label <- function(x, label) { |
| 83 | + checkmate::assert_multi_class(x, c("teal_modules", "teal_module")) |
| 84 | + if (inherits(x, "teal_modules")) { |
| 85 | + x$children <- Filter( |
| 86 | + length, |
| 87 | + lapply(x$children, keep_by_label, label = label) |
| 88 | + ) |
| 89 | + if (length(x$children) == 0) { |
| 90 | + return(NULL) |
| 91 | + } |
| 92 | + return(x) |
| 93 | + } |
| 94 | + if (x$label %in% label) { |
| 95 | + return(x) |
| 96 | + } |
| 97 | + NULL |
| 98 | +} |
| 99 | + |
| 100 | +#' Remove Teal Modules by Label |
| 101 | +#' |
| 102 | +#' @description `r lifecycle::badge("experimental")` |
| 103 | +#' Recursively removes modules from a teal modules structure that match the specified labels. |
| 104 | +#' |
| 105 | +#' @param x (`teal_module` or `teal_modules`) The object to filter. |
| 106 | +#' @param label (`character(1)`) character vector of module labels to remove. |
| 107 | +#' |
| 108 | +#' @return The filtered teal modules object with matching modules removed, or `NULL` |
| 109 | +#' if all modules are removed. |
| 110 | +#' |
| 111 | +#' @import checkmate |
| 112 | +#' @examples |
| 113 | +#' mods <- teal::modules( |
| 114 | +#' teal::example_module("mod1"), |
| 115 | +#' teal::example_module("mod2") |
| 116 | +#' ) |
| 117 | +#' # Remove a single module |
| 118 | +#' filtered_mods <- remove_by_label(mods, "Deaths") |
| 119 | +#' |
| 120 | +#' # Remove multiple modules |
| 121 | +#' filtered_mods <- remove_by_label(mods, c("Deaths", "Lab Summary Table")) |
| 122 | +#' |
| 123 | +#' @export |
| 124 | +remove_by_label <- function(x, label) { |
| 125 | + checkmate::assert_multi_class(x, c("teal_modules", "teal_module")) |
| 126 | + |
| 127 | + # Check if label exists and matches |
| 128 | + if (!is.null(x$label) && length(x$label) > 0 && x$label %in% label) { |
| 129 | + return(NULL) |
| 130 | + } |
| 131 | + |
| 132 | + if (inherits(x, "teal_modules")) { |
| 133 | + x$children <- Filter( |
| 134 | + function(child) !is.null(child), |
| 135 | + lapply(x$children, remove_by_label, label = label) |
| 136 | + ) |
| 137 | + if (length(x$children) == 0) { |
| 138 | + return(NULL) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + x |
| 143 | +} |
0 commit comments