diff --git a/main/coverage-report/index.html b/main/coverage-report/index.html index 0213cd200b..db771a7eb9 100644 --- a/main/coverage-report/index.html +++ b/main/coverage-report/index.html @@ -1,22 +1,23 @@ +
- + - + - + - - + + - + - + - - - + + + @@ -94,7 +95,7 @@ font-size: 11px; }1 |
- # FilteredData ------+ #' Create a `teal` module for previewing a report |
||
2 |
-
+ #' |
||
3 |
- #' Drive a `teal` application+ #' @description `r lifecycle::badge("experimental")` |
||
5 |
- #' Extension of the `shinytest2::AppDriver` class with methods for+ #' This function wraps [teal.reporter::reporter_previewer_ui()] and |
||
6 |
- #' driving a teal application for performing interactions for `shinytest2` tests.+ #' [teal.reporter::reporter_previewer_srv()] into a `teal_module` to be |
||
7 |
- #'+ #' used in `teal` applications. |
||
8 |
- #' @keywords internal+ #' |
||
9 |
- #'+ #' If you are creating a `teal` application using [init()] then this |
||
10 |
- TealAppDriver <- R6::R6Class( # nolint: object_name.+ #' module will be added to your application automatically if any of your `teal_modules` |
||
11 |
- "TealAppDriver",+ #' support report generation. |
||
12 |
- inherit = {+ #' |
||
13 |
- if (!requireNamespace("shinytest2", quietly = TRUE)) {+ #' @inheritParams teal_modules |
||
14 |
- stop("Please install 'shinytest2' package to use this class.")+ #' @param server_args (named `list`) |
||
15 |
- }+ #' Arguments passed to [teal.reporter::reporter_previewer_srv()]. |
||
16 |
- if (!requireNamespace("rvest", quietly = TRUE)) {+ #' |
||
17 |
- stop("Please install 'rvest' package to use this class.")+ #' @return |
||
18 |
- }+ #' `teal_module` (extended with `teal_module_previewer` class) containing the `teal.reporter` previewer functionality. |
||
19 |
- shinytest2::AppDriver+ #' |
||
20 |
- },+ #' @export |
||
21 |
- # public methods ----+ #' |
||
22 |
- public = list(+ reporter_previewer_module <- function(label = "Report previewer", server_args = list()) { |
||
23 | -+ | 7x |
- #' @description+ checkmate::assert_string(label) |
24 | -+ | 5x |
- #' Initialize a `TealAppDriver` object for testing a `teal` application.+ checkmate::assert_list(server_args, names = "named") |
25 | -+ | 5x |
- #'+ checkmate::assert_true(all(names(server_args) %in% names(formals(teal.reporter::reporter_previewer_srv)))) |
26 |
- #' @param data,modules,filter,title,header,footer,landing_popup arguments passed to `init`+ |
||
27 | -+ | 3x |
- #' @param timeout (`numeric`) Default number of milliseconds for any timeout or+ message("Initializing reporter_previewer_module") |
28 |
- #' timeout_ parameter in the `TealAppDriver` class.+ |
||
29 | -+ | 3x |
- #' Defaults to 20s.+ srv <- function(id, reporter, ...) { |
30 | -+ | ! |
- #'+ teal.reporter::reporter_previewer_srv(id, reporter, ...) |
31 |
- #' See [`shinytest2::AppDriver`] `new` method for more details on how to change it+ } |
||
32 |
- #' via options or environment variables.+ |
||
33 | -+ | 3x |
- #' @param load_timeout (`numeric`) How long to wait for the app to load, in ms.+ ui <- function(id, ...) { |
34 | -+ | ! |
- #' This includes the time to start R. Defaults to 100s.+ teal.reporter::reporter_previewer_ui(id, ...) |
35 |
- #'+ } |
||
36 |
- #' See [`shinytest2::AppDriver`] `new` method for more details on how to change it+ |
||
37 | -+ | 3x |
- #' via options or environment variables+ module <- module( |
38 | -+ | 3x |
- #' @param ... Additional arguments to be passed to `shinytest2::AppDriver$new`+ label = "temporary label", |
39 | -+ | 3x |
- #'+ server = srv, ui = ui, |
40 | -+ | 3x |
- #'+ server_args = server_args, ui_args = list(), datanames = NULL |
41 |
- #' @return Object of class `TealAppDriver`+ ) |
||
42 |
- initialize = function(data,+ # Module is created with a placeholder label and the label is changed later. |
||
43 |
- modules,+ # This is to prevent another module being labeled "Report previewer". |
||
44 | -+ | 3x |
- filter = teal_slices(),+ class(module) <- c(class(module), "teal_module_previewer") |
45 | -+ | 3x |
- title = build_app_title(),+ module$label <- label |
46 | -+ | 3x |
- header = tags$p(),+ attr(module, "teal_bookmarkable") <- TRUE |
47 | -+ | 3x |
- footer = tags$p(),+ module |
48 |
- landing_popup = NULL,+ } |
49 | +1 |
- timeout = rlang::missing_arg(),+ #' Get client timezone |
|
50 | +2 |
- load_timeout = rlang::missing_arg(),+ #' |
|
51 | +3 |
- ...) {- |
- |
52 | -! | -
- private$data <- data- |
- |
53 | -! | -
- private$modules <- modules- |
- |
54 | -! | -
- private$filter <- filter- |
- |
55 | -! | -
- app <- init(- |
- |
56 | -! | -
- data = data,- |
- |
57 | -! | -
- modules = modules,+ #' User timezone in the browser may be different to the one on the server. |
|
58 | -! | +||
4 | +
- filter = filter,+ #' This script can be run to register a `shiny` input which contains information about the timezone in the browser. |
||
59 | -! | +||
5 | +
- title = title,+ #' |
||
60 | -! | +||
6 | +
- header = header,+ #' @param ns (`function`) namespace function passed from the `session` object in the `shiny` server. |
||
61 | -! | +||
7 | +
- footer = footer,+ #' For `shiny` modules this will allow for proper name spacing of the registered input. |
||
62 | -! | +||
8 | +
- landing_popup = landing_popup,+ #' |
||
63 | +9 |
- )+ #' @return `NULL`, invisibly. |
|
64 | +10 |
-
+ #' |
|
65 | +11 |
- # Default timeout is hardcoded to 4s in shinytest2:::resolve_timeout+ #' @keywords internal |
|
66 | +12 |
- # It must be set as parameter to the AppDriver+ #' |
|
67 | -! | +||
13 | +
- suppressWarnings(+ get_client_timezone <- function(ns) { |
||
68 | -! | +||
14 | +81x |
- super$initialize(+ script <- sprintf( |
|
69 | -! | +||
15 | +81x |
- app_dir = shinyApp(app$ui, app$server),+ "Shiny.setInputValue(`%s`, Intl.DateTimeFormat().resolvedOptions().timeZone)", |
|
70 | -! | +||
16 | +81x |
- name = "teal",+ ns("timezone") |
|
71 | -! | +||
17 | +
- variant = shinytest2::platform_variant(),+ ) |
||
72 | -! | +||
18 | +81x |
- timeout = rlang::maybe_missing(timeout, 20 * 1000),+ shinyjs::runjs(script) # function does not return anything |
|
73 | -! | +||
19 | +81x |
- load_timeout = rlang::maybe_missing(load_timeout, 100 * 1000),+ invisible(NULL) |
|
74 | +20 |
- ...+ } |
|
75 | +21 |
- )+ |
|
76 | +22 |
- )+ #' Resolve the expected bootstrap theme |
|
77 | +23 |
-
+ #' @noRd |
|
78 | +24 |
- # Check for minimum version of Chrome that supports the tests+ #' @keywords internal |
|
79 | +25 |
- # - Element.checkVisibility was added on 105- |
- |
80 | -! | -
- chrome_version <- numeric_version(- |
- |
81 | -! | -
- gsub(+ get_teal_bs_theme <- function() { |
|
82 | -! | +||
26 | +4x |
- "[[:alnum:]_]+/", # Prefix that ends with forward slash+ bs_theme <- getOption("teal.bs_theme") |
|
83 | +27 |
- "",- |
- |
84 | -! | -
- self$get_chromote_session()$Browser$getVersion()$product+ |
|
85 | -+ | ||
28 | +4x |
- ),+ if (is.null(bs_theme)) { |
|
86 | -! | +||
29 | +1x |
- strict = FALSE+ return(NULL) |
|
87 | +30 |
- )+ } |
|
88 | +31 | ||
89 | -! | +||
32 | +3x |
- required_version <- "121"+ if (!checkmate::test_class(bs_theme, "bs_theme")) { |
|
90 | -+ | ||
33 | +2x |
-
+ warning( |
|
91 | -! | +||
34 | +2x |
- testthat::skip_if(+ "Assertion on 'teal.bs_theme' option value failed: ", |
|
92 | -! | +||
35 | +2x |
- is.na(chrome_version),+ checkmate::check_class(bs_theme, "bs_theme"), |
|
93 | -! | +||
36 | +2x |
- "Problem getting Chrome version, please contact the developers."+ ". The default Shiny Bootstrap theme will be used." |
|
94 | +37 |
- )+ ) |
|
95 | -! | +||
38 | +2x |
- testthat::skip_if(+ return(NULL) |
|
96 | -! | +||
39 | +
- chrome_version < required_version,+ } |
||
97 | -! | +||
40 | +
- sprintf(+ |
||
98 | -! | +||
41 | +1x |
- "Chrome version '%s' is not supported, please upgrade to '%s' or higher",+ bs_theme |
|
99 | -! | +||
42 | +
- chrome_version,+ } |
||
100 | -! | +||
43 | +
- required_version+ |
||
101 | +44 |
- )+ #' Return parentnames along with datanames. |
|
102 | +45 |
- )+ #' @noRd |
|
103 | +46 |
- # end od check+ #' @keywords internal |
|
104 | +47 |
-
+ .include_parent_datanames <- function(datanames, join_keys) { |
|
105 | -! | +||
48 | +163x |
- private$set_active_ns()+ ordered_datanames <- datanames |
|
106 | -! | +||
49 | +163x |
- self$wait_for_idle()+ for (i in datanames) { |
|
107 | -+ | ||
50 | +293x |
- },+ parents <- character(0) |
|
108 | -+ | ||
51 | +293x |
- #' @description+ while (length(i) > 0) { |
|
109 | -+ | ||
52 | +306x |
- #' Append parent [`shinytest2::AppDriver`] `click` method with a call to `waif_for_idle()` method.+ parent_i <- teal.data::parent(join_keys, i) |
|
110 | -+ | ||
53 | +306x |
- #' @param ... arguments passed to parent [`shinytest2::AppDriver`] `click()` method.+ parents <- c(parent_i, parents) |
|
111 | -+ | ||
54 | +306x |
- click = function(...) {+ i <- parent_i |
|
112 | -! | +||
55 | +
- super$click(...)+ } |
||
113 | -! | +||
56 | +293x |
- private$wait_for_page_stability()+ ordered_datanames <- c(parents, ordered_datanames) |
|
114 | +57 |
- },+ } |
|
115 | -+ | ||
58 | +163x |
- #' @description+ unique(ordered_datanames) |
|
116 | +59 |
- #' Check if the app has shiny errors. This checks for global shiny errors.+ } |
|
117 | +60 |
- #' Note that any shiny errors dependent on shiny server render will only be captured after the teal module tab+ |
|
118 | +61 |
- #' is visited because shiny will not trigger server computations when the tab is invisible.+ #' Return topologicaly sorted datanames |
|
119 | +62 |
- #' So, navigate to the module tab you want to test before calling this function.+ #' @noRd |
|
120 | +63 |
- #' Although, this catches errors hidden in the other module tabs if they are already rendered.+ #' @keywords internal |
|
121 | +64 |
- expect_no_shiny_error = function() {+ .topologically_sort_datanames <- function(datanames, join_keys) { |
|
122 | -! | +||
65 | +131x |
- testthat::expect_null(+ datanames_with_parents <- .include_parent_datanames(datanames, join_keys) |
|
123 | -! | +||
66 | +131x |
- self$get_html(".shiny-output-error:not(.shiny-output-error-validation)"),+ intersect(datanames, datanames_with_parents) |
|
124 | -! | +||
67 | +
- info = "Shiny error is observed"+ } |
||
125 | +68 |
- )+ |
|
126 | +69 |
- },+ #' Create a `FilteredData` |
|
127 | +70 |
- #' @description+ #' |
|
128 | +71 |
- #' Check if the app has no validation errors. This checks for global shiny validation errors.+ #' Create a `FilteredData` object from a `teal_data` object. |
|
129 | +72 |
- expect_no_validation_error = function() {+ #' |
|
130 | -! | +||
73 | +
- testthat::expect_null(+ #' @param x (`teal_data`) object |
||
131 | -! | +||
74 | +
- self$get_html(".shiny-output-error-validation"),+ #' @param datanames (`character`) vector of data set names to include; must be subset of `datanames(x)` |
||
132 | -! | +||
75 | +
- info = "No validation error is observed"+ #' @return A `FilteredData` object. |
||
133 | +76 |
- )+ #' @keywords internal |
|
134 | +77 |
- },+ teal_data_to_filtered_data <- function(x, datanames = ls(teal.code::get_env(x))) { |
|
135 | -+ | ||
78 | +76x |
- #' @description+ checkmate::assert_class(x, "teal_data") |
|
136 | -+ | ||
79 | +76x |
- #' Check if the app has validation errors. This checks for global shiny validation errors.+ checkmate::assert_character(datanames, min.chars = 1L, any.missing = FALSE) |
|
137 | +80 |
- expect_validation_error = function() {+ # Otherwise, FilteredData will be created in the modules' scope later |
|
138 | -! | +||
81 | +76x |
- testthat::expect_false(+ teal.slice::init_filtered_data( |
|
139 | -! | +||
82 | +76x |
- is.null(self$get_html(".shiny-output-error-validation")),+ x = Filter( |
|
140 | -! | +||
83 | +76x |
- info = "Validation error is not observed"+ length, |
|
141 | -+ | ||
84 | +76x |
- )+ sapply(datanames, function(dn) x[[dn]], simplify = FALSE) |
|
142 | +85 |
- },+ ), |
|
143 | -+ | ||
86 | +76x |
- #' @description+ join_keys = teal.data::join_keys(x) |
|
144 | +87 |
- #' Set the input in the `teal` app.+ ) |
|
145 | +88 |
- #'+ } |
|
146 | +89 |
- #' @param input_id (character) The shiny input id with it's complete name space.+ |
|
147 | +90 |
- #' @param value The value to set the input to.+ |
|
148 | +91 |
- #' @param ... Additional arguments to be passed to `shinytest2::AppDriver$set_inputs`+ #' Template function for `TealReportCard` creation and customization |
|
149 | +92 |
- #'+ #' |
|
150 | +93 |
- #' @return The `TealAppDriver` object invisibly.+ #' This function generates a report card with a title, |
|
151 | +94 |
- set_input = function(input_id, value, ...) {- |
- |
152 | -! | -
- do.call(- |
- |
153 | -! | -
- self$set_inputs,+ #' an optional description, and the option to append the filter state list. |
|
154 | -! | +||
95 | +
- c(setNames(list(value), input_id), list(...))+ #' |
||
155 | +96 |
- )+ #' @param title (`character(1)`) title of the card (unless overwritten by label) |
|
156 | -! | +||
97 | +
- invisible(self)+ #' @param label (`character(1)`) label provided by the user when adding the card |
||
157 | +98 |
- },+ #' @param description (`character(1)`) optional, additional description |
|
158 | +99 |
- #' @description+ #' @param with_filter (`logical(1)`) flag indicating to add filter state |
|
159 | +100 |
- #' Navigate the teal tabs in the `teal` app.+ #' @param filter_panel_api (`FilterPanelAPI`) object with API that allows the generation |
|
160 | +101 |
- #'+ #' of the filter state in the report |
|
161 | +102 |
- #' @param tabs (character) Labels of tabs to navigate to. The order of the tabs is important,+ #' |
|
162 | +103 |
- #' and it should start with the most parent level tab.+ #' @return (`TealReportCard`) populated with a title, description and filter state. |
|
163 | +104 |
- #' Note: In case the teal tab group has duplicate names, the first tab will be selected,+ #' |
|
164 | +105 |
- #' If you wish to select the second tab with the same name, use the suffix "_1".+ #' @export |
|
165 | +106 |
- #' If you wish to select the third tab with the same name, use the suffix "_2" and so on.+ report_card_template <- function(title, label, description = NULL, with_filter, filter_panel_api) { |
|
166 | -+ | ||
107 | +2x |
- #'+ checkmate::assert_string(title) |
|
167 | -+ | ||
108 | +2x |
- #' @return The `TealAppDriver` object invisibly.+ checkmate::assert_string(label) |
|
168 | -+ | ||
109 | +2x |
- navigate_teal_tab = function(tabs) {+ checkmate::assert_string(description, null.ok = TRUE) |
|
169 | -! | +||
110 | +2x |
- checkmate::check_character(tabs, min.len = 1)+ checkmate::assert_flag(with_filter) |
|
170 | -! | +||
111 | +2x |
- for (tab in tabs) {+ checkmate::assert_class(filter_panel_api, classes = "FilterPanelAPI") |
|
171 | -! | +||
112 | +
- self$set_input(+ |
||
172 | -! | +||
113 | +2x |
- "teal-teal_modules-active_tab",+ card <- teal::TealReportCard$new() |
|
173 | -! | +||
114 | +2x |
- get_unique_labels(tab),+ title <- if (label == "") title else label |
|
174 | -! | +||
115 | +2x |
- wait_ = FALSE+ card$set_name(title) |
|
175 | -+ | ||
116 | +2x |
- )+ card$append_text(title, "header2") |
|
176 | -+ | ||
117 | +1x |
- }+ if (!is.null(description)) card$append_text(description, "header3") |
|
177 | -! | +||
118 | +1x |
- self$wait_for_idle()+ if (with_filter) card$append_fs(filter_panel_api$get_filter_state()) |
|
178 | -! | +||
119 | +2x |
- private$set_active_ns()+ card |
|
179 | -! | +||
120 | +
- invisible(self)+ } |
||
180 | +121 |
- },+ |
|
181 | +122 |
- #' @description+ |
|
182 | +123 |
- #' Get the active shiny name space for different components of the teal app.+ #' Check `datanames` in modules |
|
183 | +124 |
- #'+ #' |
|
184 | +125 |
- #' @return (`list`) The list of active shiny name space of the teal components.+ #' These functions check if specified `datanames` in modules match those in the data object, |
|
185 | +126 |
- active_ns = function() {+ #' returning error messages or `TRUE` for successful validation. Two functions return error message |
|
186 | -! | +||
127 | +
- if (identical(private$ns$module, character(0))) {+ #' in different forms: |
||
187 | -! | +||
128 | +
- private$set_active_ns()+ #' - `check_modules_datanames` returns `character(1)` for basic assertion usage |
||
188 | +129 |
- }+ #' - `check_modules_datanames_html` returns `shiny.tag.list` to display it in the app. |
|
189 | -! | +||
130 | +
- private$ns+ #' |
||
190 | +131 |
- },+ #' @param modules (`teal_modules`) object |
|
191 | +132 |
- #' @description+ #' @param datanames (`character`) names of datasets available in the `data` object |
|
192 | +133 |
- #' Get the active shiny name space for interacting with the module content.+ #' |
|
193 | +134 |
- #'+ #' @return `TRUE` if validation passes, otherwise `character(1)` or `shiny.tag.list` |
|
194 | +135 |
- #' @return (`string`) The active shiny name space of the component.+ #' @keywords internal |
|
195 | +136 |
- active_module_ns = function() {+ check_modules_datanames <- function(modules, datanames) { |
|
196 | -! | +||
137 | +9x |
- if (identical(private$ns$module, character(0))) {+ out <- check_modules_datanames_html(modules, datanames) |
|
197 | -! | +||
138 | +9x |
- private$set_active_ns()+ if (inherits(out, "shiny.tag.list")) { |
|
198 | -+ | ||
139 | +3x |
- }+ out_with_ticks <- gsub("<code>|</code>", "`", toString(out)) |
|
199 | -! | +||
140 | +3x |
- private$ns$module+ out_text <- gsub("<[^<>]+>", "", toString(out_with_ticks)) |
|
200 | -+ | ||
141 | +3x |
- },+ trimws(gsub("[[:space:]]+", " ", out_text)) |
|
201 | +142 |
- #' @description- |
- |
202 | -- |
- #' Get the active shiny name space bound with a custom `element` name.- |
- |
203 | -- |
- #'+ } else { |
|
204 | -+ | ||
143 | +6x |
- #' @param element `character(1)` custom element name.+ out |
|
205 | +144 |
- #'+ } |
|
206 | +145 |
- #' @return (`string`) The active shiny name space of the component bound with the input `element`.+ } |
|
207 | +146 |
- active_module_element = function(element) {- |
- |
208 | -! | -
- checkmate::assert_string(element)- |
- |
209 | -! | -
- sprintf("#%s-%s", self$active_module_ns(), element)+ |
|
210 | +147 |
- },+ #' @rdname check_modules_datanames |
|
211 | +148 |
- #' @description+ check_modules_datanames_html <- function(modules, |
|
212 | +149 |
- #' Get the text of the active shiny name space bound with a custom `element` name.+ datanames) { |
|
213 | -+ | ||
150 | +173x |
- #'+ check_datanames <- check_modules_datanames_recursive(modules, datanames) |
|
214 | -+ | ||
151 | +173x |
- #' @param element `character(1)` the text of the custom element name.+ show_module_info <- inherits(modules, "teal_modules") # used in two contexts - module and app |
|
215 | -+ | ||
152 | +173x |
- #'+ if (!length(check_datanames)) { |
|
216 | -+ | ||
153 | +155x |
- #' @return (`string`) The text of the active shiny name space of the component bound with the input `element`.+ return(TRUE) |
|
217 | +154 |
- active_module_element_text = function(element) {+ } |
|
218 | -! | +||
155 | +18x |
- checkmate::assert_string(element)+ shiny::tagList( |
|
219 | -! | +||
156 | +18x |
- self$get_text(self$active_module_element(element))+ lapply( |
|
220 | -+ | ||
157 | +18x |
- },+ check_datanames, |
|
221 | -+ | ||
158 | +18x |
- #' @description+ function(mod) { |
|
222 | -+ | ||
159 | +18x |
- #' Get the active shiny name space for interacting with the filter panel.+ tagList( |
|
223 | -+ | ||
160 | +18x |
- #'+ tags$span( |
|
224 | -+ | ||
161 | +18x |
- #' @return (`string`) The active shiny name space of the component.+ tags$span(if (length(mod$missing_datanames) == 1) "Dataset" else "Datasets"), |
|
225 | -+ | ||
162 | +18x |
- active_filters_ns = function() {+ to_html_code_list(mod$missing_datanames), |
|
226 | -! | +||
163 | +18x |
- if (identical(private$ns$filter_panel, character(0))) {+ tags$span( |
|
227 | -! | +||
164 | +18x |
- private$set_active_ns()+ paste0( |
|
228 | -+ | ||
165 | +18x |
- }+ if (length(mod$missing_datanames) > 1) "are missing" else "is missing", |
|
229 | -! | +||
166 | +18x |
- private$ns$filter_panel+ if (show_module_info) sprintf(" for module '%s'.", mod$label) else "." |
|
230 | +167 |
- },+ ) |
|
231 | +168 |
- #' @description+ ) |
|
232 | +169 |
- #' Get the active shiny name space for interacting with the data-summary panel.+ ), |
|
233 | -+ | ||
170 | +18x |
- #'+ if (length(datanames) >= 1) { |
|
234 | -+ | ||
171 | +16x |
- #' @return (`string`) The active shiny name space of the data-summary component.+ tagList( |
|
235 | -+ | ||
172 | +16x |
- active_data_summary_ns = function() {+ tags$span(if (length(datanames) == 1) "Dataset" else "Datasets"), |
|
236 | -! | +||
173 | +16x |
- if (identical(private$ns$data_summary, character(0))) {+ tags$span("available in data:"), |
|
237 | -! | +||
174 | +16x |
- private$set_active_ns()+ tagList( |
|
238 | -+ | ||
175 | +16x |
- }+ tags$span( |
|
239 | -! | +||
176 | +16x |
- private$ns$data_summary+ to_html_code_list(datanames), |
|
240 | -+ | ||
177 | +16x |
- },+ tags$span(".", .noWS = "outside"), |
|
241 | -+ | ||
178 | +16x |
- #' @description+ .noWS = c("outside") |
|
242 | +179 |
- #' Get the active shiny name space bound with a custom `element` name.+ ) |
|
243 | +180 |
- #'+ ) |
|
244 | +181 |
- #' @param element `character(1)` custom element name.+ ) |
|
245 | +182 |
- #'+ } else { |
|
246 | -+ | ||
183 | +2x |
- #' @return (`string`) The active shiny name space of the component bound with the input `element`.+ tags$span("No datasets are available in data.") |
|
247 | +184 |
- active_data_summary_element = function(element) {- |
- |
248 | -! | -
- checkmate::assert_string(element)+ }, |
|
249 | -! | +||
185 | +18x |
- sprintf("#%s-%s", self$active_data_summary_ns(), element)+ tags$br(.noWS = "before") |
|
250 | +186 |
- },+ ) |
|
251 | +187 |
- #' @description+ } |
|
252 | +188 |
- #' Get the input from the module in the `teal` app.+ ) |
|
253 | +189 |
- #' This function will only access inputs from the name space of the current active teal module.+ ) |
|
254 | +190 |
- #'+ } |
|
255 | +191 |
- #' @param input_id (character) The shiny input id to get the value from.+ |
|
256 | +192 |
- #'+ #' Recursively checks modules and returns list for every datanames mismatch between module and data |
|
257 | +193 |
- #' @return The value of the shiny input.+ #' @noRd |
|
258 | +194 |
- get_active_module_input = function(input_id) {+ check_modules_datanames_recursive <- function(modules, datanames) { # nolint: object_name_length |
|
259 | -! | +||
195 | +270x |
- checkmate::check_string(input_id)+ checkmate::assert_multi_class(modules, c("teal_module", "teal_modules")) |
|
260 | -! | +||
196 | +270x |
- self$get_value(input = sprintf("%s-%s", self$active_module_ns(), input_id))+ checkmate::assert_character(datanames) |
|
261 | -+ | ||
197 | +270x |
- },+ if (inherits(modules, "teal_modules")) { |
|
262 | -+ | ||
198 | +77x |
- #' @description+ unlist( |
|
263 | -+ | ||
199 | +77x |
- #' Get the output from the module in the `teal` app.+ lapply(modules$children, check_modules_datanames_recursive, datanames = datanames), |
|
264 | -+ | ||
200 | +77x |
- #' This function will only access outputs from the name space of the current active teal module.+ recursive = FALSE |
|
265 | +201 |
- #'+ ) |
|
266 | +202 |
- #' @param output_id (character) The shiny output id to get the value from.+ } else { |
|
267 | -+ | ||
203 | +193x |
- #'+ missing_datanames <- setdiff(modules$datanames, c("all", datanames)) |
|
268 | -+ | ||
204 | +193x |
- #' @return The value of the shiny output.+ if (length(missing_datanames)) { |
|
269 | -+ | ||
205 | +18x |
- get_active_module_output = function(output_id) {+ list(list( |
|
270 | -! | +||
206 | +18x |
- checkmate::check_string(output_id)+ label = modules$label, |
|
271 | -! | +||
207 | +18x |
- self$get_value(output = sprintf("%s-%s", self$active_module_ns(), output_id))+ missing_datanames = missing_datanames |
|
272 | +208 |
- },+ )) |
|
273 | +209 |
- #' @description+ } |
|
274 | +210 |
- #' Get the output from the module's `teal.widgets::table_with_settings` or `DT::DTOutput` in the `teal` app.+ } |
|
275 | +211 |
- #' This function will only access outputs from the name space of the current active teal module.+ } |
|
276 | +212 |
- #'+ |
|
277 | +213 |
- #' @param table_id (`character(1)`) The id of the table in the active teal module's name space.+ #' Convert character vector to html code separated with commas and "and" |
|
278 | +214 |
- #' @param which (integer) If there is more than one table, which should be extracted.+ #' @noRd |
|
279 | +215 |
- #' By default it will look for a table that is built using `teal.widgets::table_with_settings`.+ to_html_code_list <- function(x) { |
|
280 | -+ | ||
216 | +34x |
- #'+ checkmate::assert_character(x) |
|
281 | -+ | ||
217 | +34x |
- #' @return The data.frame with table contents.+ do.call( |
|
282 | -+ | ||
218 | +34x |
- get_active_module_table_output = function(table_id, which = 1) {+ tagList, |
|
283 | -! | +||
219 | +34x |
- checkmate::check_number(which, lower = 1)+ lapply(seq_along(x), function(.ix) { |
|
284 | -! | +||
220 | +47x |
- checkmate::check_string(table_id)+ tagList( |
|
285 | -! | +||
221 | +47x |
- table <- rvest::html_table(+ tags$code(x[.ix]), |
|
286 | -! | +||
222 | +47x |
- self$get_html_rvest(self$active_module_element(table_id)),+ if (.ix != length(x)) { |
|
287 | -! | +||
223 | +1x |
- fill = TRUE+ if (.ix == length(x) - 1) tags$span(" and ") else tags$span(", ", .noWS = "before") |
|
288 | +224 |
- )+ } |
|
289 | -! | +||
225 | +
- if (length(table) == 0) {+ ) |
||
290 | -! | +||
226 | +
- data.frame()+ }) |
||
291 | +227 |
- } else {+ ) |
|
292 | -! | +||
228 | +
- table[[which]]+ } |
||
293 | +229 |
- }+ |
|
294 | +230 |
- },+ |
|
295 | +231 |
- #' @description+ #' Check `datanames` in filters |
|
296 | +232 |
- #' Get the output from the module's `teal.widgets::plot_with_settings` in the `teal` app.+ #' |
|
297 | +233 |
- #' This function will only access plots from the name space of the current active teal module.- |
- |
298 | -- |
- #'+ #' This function checks whether `datanames` in filters correspond to those in `data`, |
|
299 | +234 |
- #' @param plot_id (`character(1)`) The id of the plot in the active teal module's name space.+ #' returning character vector with error messages or `TRUE` if all checks pass. |
|
300 | +235 |
- #'+ #' |
|
301 | +236 |
- #' @return The `src` attribute as `character(1)` vector.+ #' @param filters (`teal_slices`) object |
|
302 | +237 |
- get_active_module_plot_output = function(plot_id) {- |
- |
303 | -! | -
- checkmate::check_string(plot_id)- |
- |
304 | -! | -
- self$get_attr(- |
- |
305 | -! | -
- self$active_module_element(sprintf("%s-plot_main > img", plot_id)),- |
- |
306 | -! | -
- "src"+ #' @param datanames (`character`) names of datasets available in the `data` object |
|
307 | +238 |
- )+ #' |
|
308 | +239 |
- },+ #' @return A `character(1)` containing error message or TRUE if validation passes. |
|
309 | +240 |
- #' @description+ #' @keywords internal |
|
310 | +241 |
- #' Set the input in the module in the `teal` app.+ check_filter_datanames <- function(filters, datanames) { |
|
311 | -+ | ||
242 | +77x |
- #' This function will only set inputs in the name space of the current active teal module.+ checkmate::assert_class(filters, "teal_slices") |
|
312 | -+ | ||
243 | +77x |
- #'+ checkmate::assert_character(datanames) |
|
313 | +244 |
- #' @param input_id (character) The shiny input id to get the value from.+ |
|
314 | +245 |
- #' @param value The value to set the input to.+ # check teal_slices against datanames |
|
315 | -+ | ||
246 | +77x |
- #' @param ... Additional arguments to be passed to `shinytest2::AppDriver$set_inputs`+ out <- unlist(sapply( |
|
316 | -+ | ||
247 | +77x |
- #'+ filters, function(filter) { |
|
317 | -+ | ||
248 | +24x |
- #' @return The `TealAppDriver` object invisibly.+ dataname <- shiny::isolate(filter$dataname) |
|
318 | -+ | ||
249 | +24x |
- set_active_module_input = function(input_id, value, ...) {+ if (!dataname %in% datanames) { |
|
319 | -! | +||
250 | +3x |
- checkmate::check_string(input_id)+ sprintf( |
|
320 | -! | +||
251 | +3x |
- checkmate::check_string(value)+ "- Filter '%s' refers to dataname not available in 'data':\n %s not in (%s)", |
|
321 | -! | +||
252 | +3x |
- self$set_input(+ shiny::isolate(filter$id), |
|
322 | -! | +||
253 | +3x |
- sprintf("%s-%s", self$active_module_ns(), input_id),+ dQuote(dataname, q = FALSE), |
|
323 | -! | +||
254 | +3x |
- value,+ toString(dQuote(datanames, q = FALSE)) |
|
324 | +255 |
- ...+ ) |
|
325 | +256 |
- )- |
- |
326 | -! | -
- dots <- rlang::list2(...)- |
- |
327 | -! | -
- if (!isFALSE(dots[["wait"]])) self$wait_for_idle() # Default behavior is to wait- |
- |
328 | -! | -
- invisible(self)+ } |
|
329 | +257 |
- },+ } |
|
330 | +258 |
- #' @description+ )) |
|
331 | +259 |
- #' Get the active datasets that can be accessed via the filter panel of the current active teal module.+ |
|
332 | +260 |
- get_active_filter_vars = function() {- |
- |
333 | -! | -
- displayed_datasets_index <- self$is_visible(+ |
|
334 | -! | +||
261 | +77x |
- sprintf("#%s-filters-filter_active_vars_contents > span", self$active_filters_ns())+ if (length(out)) { |
|
335 | -+ | ||
262 | +3x |
- )+ paste(out, collapse = "\n") |
|
336 | +263 | - - | -|
337 | -! | -
- available_datasets <- self$get_text(- |
- |
338 | -! | -
- sprintf(- |
- |
339 | -! | -
- "#%s-filters-filter_active_vars_contents .filter_panel_dataname",+ } else { |
|
340 | -! | +||
264 | +74x |
- self$active_filters_ns()+ TRUE |
|
341 | +265 |
- )+ } |
|
342 | +266 |
- )+ } |
|
343 | +267 | ||
344 | -! | +||
268 | +
- available_datasets[displayed_datasets_index]+ #' Function for validating the title parameter of `teal::init` |
||
345 | +269 |
- },+ #' |
|
346 | +270 |
- #' @description+ #' Checks if the input of the title from `teal::init` will create a valid title and favicon tag. |
|
347 | +271 |
- #' Get the active data summary table+ #' @param shiny_tag (`shiny.tag`) Object to validate for a valid title. |
|
348 | +272 |
- #' @return `data.frame`+ #' @keywords internal |
|
349 | +273 |
- get_active_data_summary_table = function() {+ validate_app_title_tag <- function(shiny_tag) { |
|
350 | -! | +||
274 | +7x |
- summary_table <- rvest::html_table(+ checkmate::assert_class(shiny_tag, "shiny.tag") |
|
351 | -! | +||
275 | +7x |
- self$get_html_rvest(self$active_data_summary_element("table")),+ checkmate::assert_true(shiny_tag$name == "head") |
|
352 | -! | +||
276 | +6x |
- fill = TRUE+ child_names <- vapply(shiny_tag$children, `[[`, character(1L), "name") |
|
353 | -! | +||
277 | +6x |
- )[[1]]+ checkmate::assert_subset(c("title", "link"), child_names, .var.name = "child tags") |
|
354 | -+ | ||
278 | +4x |
-
+ rel_attr <- shiny_tag$children[[which(child_names == "link")]]$attribs$rel |
|
355 | -! | +||
279 | +4x |
- col_names <- unlist(summary_table[1, ], use.names = FALSE)+ checkmate::assert_subset( |
|
356 | -! | +||
280 | +4x |
- summary_table <- summary_table[-1, ]+ rel_attr, |
|
357 | -! | +||
281 | +4x |
- colnames(summary_table) <- col_names+ c("icon", "shortcut icon"), |
|
358 | -! | +||
282 | +4x |
- if (nrow(summary_table) > 0) {+ .var.name = "Link tag's rel attribute", |
|
359 | -! | +||
283 | +4x |
- summary_table+ empty.ok = FALSE |
|
360 | +284 |
- } else {+ ) |
|
361 | -! | +||
285 | +
- NULL+ } |
||
362 | +286 |
- }+ |
|
363 | +287 |
- },+ #' Build app title with favicon |
|
364 | +288 |
- #' @description+ #' |
|
365 | +289 |
- #' Test if `DOM` elements are visible on the page with a JavaScript call.+ #' A helper function to create the browser title along with a logo. |
|
366 | +290 |
- #' @param selector (`character(1)`) `CSS` selector to check visibility.+ #' |
|
367 | +291 |
- #' A `CSS` id will return only one element if the UI is well formed.+ #' @param title (`character`) The browser title for the `teal` app. |
|
368 | +292 |
- #' @param content_visibility_auto,opacity_property,visibility_property (`logical(1)`) See more information+ #' @param favicon (`character`) The path for the icon for the title. |
|
369 | +293 |
- #' on <https://developer.mozilla.org/en-US/docs/Web/API/Element/checkVisibility>.+ #' The image/icon path can be remote or the static path accessible by `shiny`, like the `www/` |
|
370 | +294 |
- #'+ #' |
|
371 | +295 |
- #' @return Logical vector with all occurrences of the selector.+ #' @return A `shiny.tag` containing the element that adds the title and logo to the `shiny` app. |
|
372 | +296 |
- is_visible = function(selector,+ #' @export |
|
373 | +297 |
- content_visibility_auto = FALSE,+ build_app_title <- function( |
|
374 | +298 |
- opacity_property = FALSE,+ title = "teal app", |
|
375 | +299 |
- visibility_property = FALSE) {+ favicon = "https://raw.githubusercontent.com/insightsengineering/hex-stickers/main/PNG/nest.png") { |
|
376 | -! | +||
300 | +13x |
- checkmate::assert_string(selector)+ checkmate::assert_string(title, null.ok = TRUE) |
|
377 | -! | +||
301 | +13x |
- checkmate::assert_flag(content_visibility_auto)+ checkmate::assert_string(favicon, null.ok = TRUE) |
|
378 | -! | +||
302 | +13x |
- checkmate::assert_flag(opacity_property)+ tags$head( |
|
379 | -! | +||
303 | +13x |
- checkmate::assert_flag(visibility_property)+ tags$title(title), |
|
380 | -+ | ||
304 | +13x |
-
+ tags$link( |
|
381 | -! | +||
305 | +13x |
- private$wait_for_page_stability()+ rel = "icon", |
|
382 | -+ | ||
306 | +13x |
-
+ href = favicon, |
|
383 | -! | +||
307 | +13x |
- testthat::skip_if_not(+ sizes = "any" |
|
384 | -! | +||
308 | +
- self$get_js("typeof Element.prototype.checkVisibility === 'function'"),+ ) |
||
385 | -! | +||
309 | +
- "Element.prototype.checkVisibility is not supported in the current browser."+ ) |
||
386 | +310 |
- )+ } |
|
387 | +311 | ||
388 | -! | +||
312 | +
- unlist(- |
- ||
389 | -! | -
- self$get_js(- |
- |
390 | -! | -
- sprintf(- |
- |
391 | -! | -
- "Array.from(document.querySelectorAll('%s')).map(el => el.checkVisibility({%s, %s, %s}))",- |
- |
392 | -! | -
- selector,+ #' Application ID |
|
393 | +313 |
- # Extra parameters- |
- |
394 | -! | -
- sprintf("contentVisibilityAuto: %s", tolower(content_visibility_auto)),- |
- |
395 | -! | -
- sprintf("opacityProperty: %s", tolower(opacity_property)),- |
- |
396 | -! | -
- sprintf("visibilityProperty: %s", tolower(visibility_property))+ #' |
|
397 | +314 |
- )+ #' Creates App ID used to match filter snapshots to application. |
|
398 | +315 |
- )+ #' |
|
399 | +316 |
- )+ #' Calculate app ID that will be used to stamp filter state snapshots. |
|
400 | +317 |
- },+ #' App ID is a hash of the app's data and modules. |
|
401 | +318 |
- #' @description+ #' See "transferring snapshots" section in ?snapshot. |
|
402 | +319 |
- #' Get the active filter variables from a dataset in the `teal` app.+ #' |
|
403 | +320 |
- #'+ #' @param data (`teal_data` or `teal_data_module`) as accepted by `init` |
|
404 | +321 |
- #' @param dataset_name (character) The name of the dataset to get the filter variables from.+ #' @param modules (`teal_modules`) object as accepted by `init` |
|
405 | +322 |
- #' If `NULL`, the filter variables for all the datasets will be returned in a list.+ #' |
|
406 | +323 |
- get_active_data_filters = function(dataset_name = NULL) {+ #' @return A single character string. |
|
407 | -! | +||
324 | +
- checkmate::check_string(dataset_name, null.ok = TRUE)+ #' |
||
408 | -! | +||
325 | +
- datasets <- self$get_active_filter_vars()+ #' @keywords internal |
||
409 | -! | +||
326 | +
- checkmate::assert_subset(dataset_name, datasets)+ create_app_id <- function(data, modules) { |
||
410 | -! | +||
327 | +21x |
- active_filters <- lapply(+ checkmate::assert_multi_class(data, c("teal_data", "teal_data_module")) |
|
411 | -! | +||
328 | +20x |
- datasets,+ checkmate::assert_class(modules, "teal_modules") |
|
412 | -! | +||
329 | +
- function(x) {+ |
||
413 | -! | +||
330 | +19x |
- var_names <- gsub(+ data <- if (inherits(data, "teal_data")) { |
|
414 | -! | +||
331 | +17x |
- pattern = "\\s",+ as.list(teal.code::get_env(data)) |
|
415 | -! | +||
332 | +19x |
- replacement = "",+ } else if (inherits(data, "teal_data_module")) { |
|
416 | -! | +||
333 | +2x |
- self$get_text(+ deparse1(body(data$server)) |
|
417 | -! | +||
334 | +
- sprintf(+ } |
||
418 | -! | +||
335 | +19x |
- "#%s-filters-%s .filter-card-varname",+ modules <- lapply(modules, defunction) |
|
419 | -! | +||
336 | +
- self$active_filters_ns(),+ |
||
420 | -! | +||
337 | +19x |
- x+ rlang::hash(list(data = data, modules = modules)) |
|
421 | +338 |
- )+ } |
|
422 | +339 |
- )+ |
|
423 | +340 |
- )- |
- |
424 | -! | -
- structure(- |
- |
425 | -! | -
- lapply(var_names, private$get_active_filter_selection, dataset_name = x),+ #' Go through list and extract bodies of encountered functions as string, recursively. |
|
426 | -! | +||
341 | +
- names = var_names+ #' @keywords internal |
||
427 | +342 |
- )+ #' @noRd |
|
428 | +343 |
- }+ defunction <- function(x) { |
|
429 | -+ | ||
344 | +229x |
- )+ if (is.list(x)) { |
|
430 | -! | +||
345 | +67x |
- names(active_filters) <- datasets+ lapply(x, defunction) |
|
431 | -! | +||
346 | +162x |
- if (is.null(dataset_name)) {+ } else if (is.function(x)) { |
|
432 | -! | +||
347 | +50x |
- return(active_filters)+ deparse1(body(x)) |
|
433 | +348 |
- }+ } else { |
|
434 | -! | +||
349 | +112x |
- active_filters[[dataset_name]]+ x |
|
435 | +350 |
- },+ } |
|
436 | +351 |
- #' @description+ } |
|
437 | +352 |
- #' Add a new variable from the dataset to be filtered.+ |
|
438 | +353 |
- #'+ #' Get unique labels |
|
439 | +354 |
- #' @param dataset_name (character) The name of the dataset to add the filter variable to.+ #' |
|
440 | +355 |
- #' @param var_name (character) The name of the variable to add to the filter panel.+ #' Get unique labels for the modules to avoid namespace conflicts. |
|
441 | +356 |
- #' @param ... Additional arguments to be passed to `shinytest2::AppDriver$set_inputs`+ #' |
|
442 | +357 |
- #'+ #' @param labels (`character`) vector of labels |
|
443 | +358 |
- #' @return The `TealAppDriver` object invisibly.+ #' |
|
444 | +359 |
- add_filter_var = function(dataset_name, var_name, ...) {- |
- |
445 | -! | -
- checkmate::check_string(dataset_name)+ #' @return (`character`) vector of unique labels |
|
446 | -! | +||
360 | +
- checkmate::check_string(var_name)+ #' |
||
447 | -! | +||
361 | +
- private$set_active_ns()+ #' @keywords internal |
||
448 | -! | +||
362 | +
- self$click(+ get_unique_labels <- function(labels) { |
||
449 | -! | +||
363 | +211x |
- selector = sprintf(+ make.unique(gsub("[^[:alnum:]]", "_", tolower(labels)), sep = "_") |
|
450 | -! | +||
364 | +
- "#%s-filters-%s-add_filter_icon",+ } |
||
451 | -! | +||
365 | +
- private$ns$filter_panel,+ |
||
452 | -! | +||
366 | +
- dataset_name+ #' Remove ANSI escape sequences from a string |
||
453 | +367 |
- )+ #' @noRd |
|
454 | +368 |
- )+ strip_style <- function(string) { |
|
455 | -! | +||
369 | +2x |
- self$set_input(+ checkmate::assert_string(string) |
|
456 | -! | +||
370 | +
- sprintf(+ |
||
457 | -! | +||
371 | +2x |
- "%s-filters-%s-%s-filter-var_to_add",+ gsub( |
|
458 | -! | +||
372 | +2x |
- private$ns$filter_panel,+ "(?:(?:\\x{001b}\\[)|\\x{009b})(?:(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m])|\\x{001b}[A-M]", |
|
459 | -! | +||
373 | +
- dataset_name,+ "", |
||
460 | -! | +||
374 | +2x |
- dataset_name+ string, |
|
461 | -+ | ||
375 | +2x |
- ),+ perl = TRUE, |
|
462 | -! | +||
376 | +2x |
- var_name,+ useBytes = TRUE |
|
463 | +377 |
- ...+ ) |
|
464 | +378 |
- )+ } |
|
465 | -! | +
1 | +
- invisible(self)+ # FilteredData ------ |
||
466 | +2 |
- },+ |
|
467 | +3 |
- #' @description+ #' Drive a `teal` application |
|
468 | +4 |
- #' Remove an active filter variable of a dataset from the active filter variables panel.+ #' |
|
469 | +5 |
- #'+ #' Extension of the `shinytest2::AppDriver` class with methods for |
|
470 | +6 |
- #' @param dataset_name (character) The name of the dataset to remove the filter variable from.+ #' driving a teal application for performing interactions for `shinytest2` tests. |
|
471 | +7 |
- #' If `NULL`, all the filter variables will be removed.+ #' |
|
472 | +8 |
- #' @param var_name (character) The name of the variable to remove from the filter panel.+ #' @keywords internal |
|
473 | +9 |
- #' If `NULL`, all the filter variables of the dataset will be removed.+ #' |
|
474 | +10 |
- #'+ TealAppDriver <- R6::R6Class( # nolint: object_name. |
|
475 | +11 |
- #' @return The `TealAppDriver` object invisibly.+ "TealAppDriver", |
|
476 | +12 |
- remove_filter_var = function(dataset_name = NULL, var_name = NULL) {+ inherit = { |
|
477 | -! | +||
13 | +
- checkmate::check_string(dataset_name, null.ok = TRUE)+ if (!requireNamespace("shinytest2", quietly = TRUE)) { |
||
478 | -! | +||
14 | +
- checkmate::check_string(var_name, null.ok = TRUE)+ stop("Please install 'shinytest2' package to use this class.") |
||
479 | -! | +||
15 | +
- if (is.null(dataset_name)) {+ } |
||
480 | -! | +||
16 | +
- remove_selector <- sprintf(+ if (!requireNamespace("rvest", quietly = TRUE)) { |
||
481 | -! | +||
17 | +
- "#%s-active-remove_all_filters",+ stop("Please install 'rvest' package to use this class.") |
||
482 | -! | +||
18 | +
- self$active_filters_ns()+ } |
||
483 | +19 |
- )+ shinytest2::AppDriver |
|
484 | -! | +||
20 | +
- } else if (is.null(var_name)) {+ }, |
||
485 | -! | +||
21 | +
- remove_selector <- sprintf(+ # public methods ---- |
||
486 | -! | +||
22 | +
- "#%s-active-%s-remove_filters",+ public = list( |
||
487 | -! | +||
23 | +
- self$active_filters_ns(),+ #' @description |
||
488 | -! | +||
24 | +
- dataset_name+ #' Initialize a `TealAppDriver` object for testing a `teal` application. |
||
489 | +25 |
- )+ #' |
|
490 | +26 |
- } else {+ #' @param data,modules,filter,title,header,footer,landing_popup arguments passed to `init` |
|
491 | -! | +||
27 | +
- remove_selector <- sprintf(+ #' @param timeout (`numeric`) Default number of milliseconds for any timeout or |
||
492 | -! | +||
28 | +
- "#%s-active-%s-filter-%s_%s-remove",+ #' timeout_ parameter in the `TealAppDriver` class. |
||
493 | -! | +||
29 | +
- self$active_filters_ns(),+ #' Defaults to 20s. |
||
494 | -! | +||
30 | +
- dataset_name,+ #' |
||
495 | -! | +||
31 | +
- dataset_name,+ #' See [`shinytest2::AppDriver`] `new` method for more details on how to change it |
||
496 | -! | +||
32 | +
- var_name+ #' via options or environment variables. |
||
497 | +33 |
- )+ #' @param load_timeout (`numeric`) How long to wait for the app to load, in ms. |
|
498 | +34 |
- }+ #' This includes the time to start R. Defaults to 100s. |
|
499 | -! | +||
35 | +
- self$click(+ #' |
||
500 | -! | +||
36 | +
- selector = remove_selector+ #' See [`shinytest2::AppDriver`] `new` method for more details on how to change it |
||
501 | +37 |
- )+ #' via options or environment variables |
|
502 | -! | +||
38 | +
- invisible(self)+ #' @param ... Additional arguments to be passed to `shinytest2::AppDriver$new` |
||
503 | +39 |
- },+ #' |
|
504 | +40 |
- #' @description+ #' |
|
505 | +41 |
- #' Set the active filter values for a variable of a dataset in the active filter variable panel.+ #' @return Object of class `TealAppDriver` |
|
506 | +42 |
- #'+ initialize = function(data, |
|
507 | +43 |
- #' @param dataset_name (character) The name of the dataset to set the filter value for.+ modules, |
|
508 | +44 |
- #' @param var_name (character) The name of the variable to set the filter value for.+ filter = teal_slices(), |
|
509 | +45 |
- #' @param input The value to set the filter to.+ title = build_app_title(), |
|
510 | +46 |
- #' @param ... Additional arguments to be passed to `shinytest2::AppDriver$set_inputs`+ header = tags$p(), |
|
511 | +47 |
- #'+ footer = tags$p(), |
|
512 | +48 |
- #' @return The `TealAppDriver` object invisibly.+ landing_popup = NULL, |
|
513 | +49 |
- set_active_filter_selection = function(dataset_name,+ timeout = rlang::missing_arg(), |
|
514 | +50 |
- var_name,+ load_timeout = rlang::missing_arg(), |
|
515 | +51 |
- input,+ ...) { |
|
516 | -+ | ||
52 | +! |
- ...) {+ private$data <- data |
|
517 | +53 | ! |
- checkmate::check_string(dataset_name)+ private$modules <- modules |
518 | +54 | ! |
- checkmate::check_string(var_name)+ private$filter <- filter |
519 | +55 | ! |
- checkmate::check_string(input)+ app <- init( |
520 | -+ | ||
56 | +! |
-
+ data = data, |
|
521 | +57 | ! |
- input_id_prefix <- sprintf(+ modules = modules, |
522 | +58 | ! |
- "%s-filters-%s-filter-%s_%s-inputs",+ filter = filter, |
523 | +59 | ! |
- self$active_filters_ns(),+ title = title, |
524 | +60 | ! |
- dataset_name,+ header = header, |
525 | +61 | ! |
- dataset_name,+ footer = footer, |
526 | +62 | ! |
- var_name+ landing_popup = landing_popup, |
527 | +63 |
) |
|
528 | +64 | ||
529 | +65 |
- # Find the type of filter (based on filter panel)+ # Default timeout is hardcoded to 4s in shinytest2:::resolve_timeout |
|
530 | -! | +||
66 | +
- supported_suffix <- c("selection", "selection_manual")+ # It must be set as parameter to the AppDriver |
||
531 | +67 | ! |
- slices_suffix <- supported_suffix[+ suppressWarnings( |
532 | +68 | ! |
- match(+ super$initialize( |
533 | +69 | ! |
- TRUE,+ app_dir = shinyApp(app$ui, app$server), |
534 | +70 | ! |
- vapply(+ name = "teal", |
535 | +71 | ! |
- supported_suffix,+ variant = shinytest2::platform_variant(), |
536 | +72 | ! |
- function(suffix) {+ timeout = rlang::maybe_missing(timeout, 20 * 1000), |
537 | +73 | ! |
- !is.null(self$get_html(sprintf("#%s-%s", input_id_prefix, suffix)))+ load_timeout = rlang::maybe_missing(load_timeout, 100 * 1000), |
538 | +74 |
- },- |
- |
539 | -! | -
- logical(1)+ ... |
|
540 | +75 |
- )+ ) |
|
541 | +76 |
- )+ ) |
|
542 | +77 |
- ]+ |
|
543 | +78 |
-
+ # Check for minimum version of Chrome that supports the tests |
|
544 | +79 |
- # Generate correct namespace+ # - Element.checkVisibility was added on 105 |
|
545 | +80 | ! |
- slices_input_id <- sprintf(+ chrome_version <- numeric_version( |
546 | +81 | ! |
- "%s-filters-%s-filter-%s_%s-inputs-%s",+ gsub( |
547 | +82 | ! |
- self$active_filters_ns(),+ "[[:alnum:]_]+/", # Prefix that ends with forward slash |
548 | -! | +||
83 | +
- dataset_name,+ "", |
||
549 | +84 | ! |
- dataset_name,+ self$get_chromote_session()$Browser$getVersion()$product |
550 | -! | +||
85 | +
- var_name,+ ), |
||
551 | +86 | ! |
- slices_suffix+ strict = FALSE |
552 | +87 |
) |
|
553 | +88 | ||
554 | -! | -
- if (identical(slices_suffix, "selection_manual")) {- |
- |
555 | +89 | ! |
- checkmate::assert_numeric(input, len = 2)+ required_version <- "121" |
556 | +90 | ||
557 | +91 | ! |
- dots <- rlang::list2(...)+ testthat::skip_if( |
558 | +92 | ! |
- checkmate::assert_choice(dots$priority_, formals(self$set_inputs)[["priority_"]], null.ok = TRUE)+ is.na(chrome_version), |
559 | +93 | ! |
- checkmate::assert_flag(dots$wait_, null.ok = TRUE)+ "Problem getting Chrome version, please contact the developers." |
560 | +94 |
-
+ ) |
|
561 | +95 | ! |
- self$run_js(+ testthat::skip_if( |
562 | +96 | ! |
- sprintf(+ chrome_version < required_version, |
563 | +97 | ! |
- "Shiny.setInputValue('%s:sw.numericRange', [%f, %f], {priority: '%s'})",+ sprintf( |
564 | +98 | ! |
- slices_input_id,+ "Chrome version '%s' is not supported, please upgrade to '%s' or higher", |
565 | +99 | ! |
- input[[1]],+ chrome_version, |
566 | +100 | ! |
- input[[2]],+ required_version |
567 | -! | +||
101 | +
- priority_ = ifelse(is.null(dots$priority_), "input", dots$priority_)+ ) |
||
568 | +102 |
- )+ ) |
|
569 | +103 |
- )+ # end od check |
|
570 | +104 | ||
571 | +105 | ! |
- if (isTRUE(dots$wait_) || is.null(dots$wait_)) {+ private$set_active_ns() |
572 | +106 | ! |
- self$wait_for_idle(+ self$wait_for_idle() |
573 | -! | +||
107 | +
- timeout = if (is.null(dots$timeout_)) rlang::missing_arg() else dots$timeout_+ }, |
||
574 | +108 |
- )+ #' @description |
|
575 | +109 |
- }+ #' Append parent [`shinytest2::AppDriver`] `click` method with a call to `waif_for_idle()` method. |
|
576 | -! | +||
110 | +
- } else if (identical(slices_suffix, "selection")) {+ #' @param ... arguments passed to parent [`shinytest2::AppDriver`] `click()` method. |
||
577 | -! | -
- self$set_input(+ | |
111 | ++ |
+ click = function(...) { |
|
578 | +112 | ! |
- slices_input_id,+ super$click(...) |
579 | +113 | ! |
- input,+ private$wait_for_page_stability() |
580 | +114 |
- ...+ }, |
|
581 | +115 |
- )+ #' @description |
|
582 | +116 |
- } else {+ #' Check if the app has shiny errors. This checks for global shiny errors. |
|
583 | -! | +||
117 | +
- stop("Filter selection set not supported for this slice.")+ #' Note that any shiny errors dependent on shiny server render will only be captured after the teal module tab |
||
584 | +118 |
- }+ #' is visited because shiny will not trigger server computations when the tab is invisible. |
|
585 | +119 |
-
+ #' So, navigate to the module tab you want to test before calling this function.+ |
+ |
120 | ++ |
+ #' Although, this catches errors hidden in the other module tabs if they are already rendered.+ |
+ |
121 | ++ |
+ expect_no_shiny_error = function() { |
|
586 | +122 | ! |
- invisible(self)+ testthat::expect_null(+ |
+
123 | +! | +
+ self$get_html(".shiny-output-error:not(.shiny-output-error-validation)"),+ |
+ |
124 | +! | +
+ info = "Shiny error is observed" |
|
587 | +125 | ++ |
+ )+ |
+
126 |
}, |
||
588 | +127 |
#' @description |
|
589 | +128 |
- #' Extract `html` attribute (found by a `selector`).+ #' Check if the app has no validation errors. This checks for global shiny validation errors. |
|
590 | +129 |
- #'+ expect_no_validation_error = function() {+ |
+ |
130 | +! | +
+ testthat::expect_null(+ |
+ |
131 | +! | +
+ self$get_html(".shiny-output-error-validation"),+ |
+ |
132 | +! | +
+ info = "No validation error is observed" |
|
591 | +133 |
- #' @param selector (`character(1)`) specifying the selector to be used to get the content of a specific node.+ ) |
|
592 | +134 |
- #' @param attribute (`character(1)`) name of an attribute to retrieve from a node specified by `selector`.+ }, |
|
593 | +135 |
- #'+ #' @description |
|
594 | +136 |
- #' @return The `character` vector.+ #' Check if the app has validation errors. This checks for global shiny validation errors. |
|
595 | +137 |
- get_attr = function(selector, attribute) {+ expect_validation_error = function() { |
|
596 | +138 | ! |
- rvest::html_attr(+ testthat::expect_false( |
597 | +139 | ! |
- rvest::html_nodes(self$get_html_rvest("html"), selector),+ is.null(self$get_html(".shiny-output-error-validation")), |
598 | +140 | ! |
- attribute+ info = "Validation error is not observed" |
599 | +141 |
) |
|
600 | +142 |
}, |
|
601 | +143 |
#' @description |
|
602 | +144 |
- #' Wrapper around `get_html` that passes the output directly to `rvest::read_html`.+ #' Set the input in the `teal` app. |
|
603 | +145 |
#' |
|
604 | +146 |
- #' @param selector `(character(1))` passed to `get_html`.+ #' @param input_id (character) The shiny input id with it's complete name space. |
|
605 | +147 |
- #'+ #' @param value The value to set the input to. |
|
606 | +148 |
- #' @return An XML document.+ #' @param ... Additional arguments to be passed to `shinytest2::AppDriver$set_inputs` |
|
607 | +149 |
- get_html_rvest = function(selector) {+ #' |
|
608 | -! | +||
150 | +
- rvest::read_html(self$get_html(selector))+ #' @return The `TealAppDriver` object invisibly. |
||
609 | +151 |
- },+ set_input = function(input_id, value, ...) { |
|
610 | -+ | ||
152 | +! |
- #' Wrapper around `get_url()` method that opens the app in the browser.+ do.call( |
|
611 | -+ | ||
153 | +! |
- #'+ self$set_inputs, |
|
612 | -+ | ||
154 | +! |
- #' @return Nothing. Opens the underlying teal app in the browser.+ c(setNames(list(value), input_id), list(...)) |
|
613 | +155 |
- open_url = function() {+ ) |
|
614 | +156 | ! |
- browseURL(self$get_url())+ invisible(self) |
615 | +157 |
}, |
|
616 | +158 |
#' @description |
|
617 | +159 |
- #' Waits until a specified input, output, or export value.+ #' Navigate the teal tabs in the `teal` app. |
|
618 | +160 |
- #' This function serves as a wrapper around the `wait_for_value` method,+ #' |
|
619 | +161 |
- #' providing a more flexible interface for waiting on different types of values within the active module namespace.+ #' @param tabs (character) Labels of tabs to navigate to. The order of the tabs is important, |
|
620 | +162 |
- #' @param input,output,export A name of an input, output, or export value.+ #' and it should start with the most parent level tab. |
|
621 | +163 |
- #' Only one of these parameters may be used.+ #' Note: In case the teal tab group has duplicate names, the first tab will be selected, |
|
622 | +164 |
- #' @param ... Must be empty. Allows for parameter expansion.+ #' If you wish to select the second tab with the same name, use the suffix "_1". |
|
623 | +165 |
- #' Parameter with additional value to passed in `wait_for_value`.+ #' If you wish to select the third tab with the same name, use the suffix "_2" and so on. |
|
624 | +166 |
- wait_for_active_module_value = function(input = rlang::missing_arg(),+ #' |
|
625 | +167 |
- output = rlang::missing_arg(),+ #' @return The `TealAppDriver` object invisibly. |
|
626 | +168 |
- export = rlang::missing_arg(),+ navigate_teal_tab = function(tabs) { |
|
627 | -+ | ||
169 | +! |
- ...) {+ checkmate::check_character(tabs, min.len = 1) |
|
628 | +170 | ! |
- ns <- shiny::NS(self$active_module_ns())+ for (tab in tabs) { |
629 | -+ | ||
171 | +! |
-
+ self$set_input( |
|
630 | +172 | ! |
- if (!rlang::is_missing(input) && checkmate::test_string(input, min.chars = 1)) input <- ns(input)+ "teal-teal_modules-active_tab", |
631 | +173 | ! |
- if (!rlang::is_missing(output) && checkmate::test_string(output, min.chars = 1)) output <- ns(output)+ get_unique_labels(tab), |
632 | +174 | ! |
- if (!rlang::is_missing(export) && checkmate::test_string(export, min.chars = 1)) export <- ns(export)+ wait_ = FALSE |
633 | +175 |
-
+ ) |
|
634 | -! | +||
176 | +
- self$wait_for_value(+ } |
||
635 | +177 | ! |
- input = input,+ self$wait_for_idle() |
636 | +178 | ! |
- output = output,+ private$set_active_ns() |
637 | +179 | ! |
- export = export,+ invisible(self) |
638 | +180 |
- ...+ }, |
|
639 | +181 |
- )+ #' @description |
|
640 | +182 |
- }+ #' Get the active shiny name space for different components of the teal app. |
|
641 | +183 |
- ),+ #' |
|
642 | +184 |
- # private members ----+ #' @return (`list`) The list of active shiny name space of the teal components. |
|
643 | +185 |
- private = list(+ active_ns = function() { |
|
644 | -+ | ||
186 | +! |
- # private attributes ----+ if (identical(private$ns$module, character(0))) { |
|
645 | -+ | ||
187 | +! |
- data = NULL,+ private$set_active_ns() |
|
646 | +188 |
- modules = NULL,+ } |
|
647 | -+ | ||
189 | +! |
- filter = teal_slices(),+ private$ns |
|
648 | +190 |
- ns = list(+ }, |
|
649 | +191 |
- module = character(0),+ #' @description |
|
650 | +192 |
- filter_panel = character(0)+ #' Get the active shiny name space for interacting with the module content. |
|
651 | +193 |
- ),+ #' |
|
652 | +194 |
- # private methods ----+ #' @return (`string`) The active shiny name space of the component. |
|
653 | +195 |
- set_active_ns = function() {+ active_module_ns = function() { |
|
654 | +196 | ! |
- all_inputs <- self$get_values()$input+ if (identical(private$ns$module, character(0))) { |
655 | +197 | ! |
- active_tab_inputs <- all_inputs[grepl("-active_tab$", names(all_inputs))]+ private$set_active_ns() |
656 | +198 |
-
+ } |
|
657 | +199 | ! |
- tab_ns <- unlist(lapply(names(active_tab_inputs), function(name) {+ private$ns$module |
658 | -! | +||
200 | +
- gsub(+ }, |
||
659 | -! | +||
201 | +
- pattern = "-active_tab$",+ #' @description |
||
660 | -! | +||
202 | +
- replacement = sprintf("-%s", active_tab_inputs[[name]]),+ #' Get the active shiny name space bound with a custom `element` name. |
||
661 | -! | +||
203 | +
- name+ #' |
||
662 | +204 |
- )+ #' @param element `character(1)` custom element name. |
|
663 | +205 |
- }))+ #' |
|
664 | -! | +||
206 | +
- active_ns <- tab_ns[1]+ #' @return (`string`) The active shiny name space of the component bound with the input `element`. |
||
665 | -! | +||
207 | +
- if (length(tab_ns) > 1) {+ active_module_element = function(element) { |
||
666 | +208 | ! |
- for (i in 2:length(tab_ns)) {+ checkmate::assert_string(element) |
667 | +209 | ! |
- next_ns <- tab_ns[i]+ sprintf("#%s-%s", self$active_module_ns(), element) |
668 | -! | +||
210 | +
- if (grepl(pattern = active_ns, next_ns)) {+ }, |
||
669 | -! | +||
211 | +
- active_ns <- next_ns+ #' @description |
||
670 | +212 |
- }+ #' Get the text of the active shiny name space bound with a custom `element` name. |
|
671 | +213 |
- }+ #' |
|
672 | +214 |
- }+ #' @param element `character(1)` the text of the custom element name. |
|
673 | -! | +||
215 | +
- private$ns$module <- sprintf("%s-%s", active_ns, "module")+ #' |
||
674 | +216 | - - | -|
675 | -! | -
- components <- c("filter_panel", "data_summary")- |
- |
676 | -! | -
- for (component in components) {+ #' @return (`string`) The text of the active shiny name space of the component bound with the input `element`. |
|
677 | +217 |
- if (+ active_module_element_text = function(element) { |
|
678 | +218 | ! |
- !is.null(self$get_html(sprintf("#%s-%s-panel", active_ns, component))) ||+ checkmate::assert_string(element) |
679 | +219 | ! |
- !is.null(self$get_html(sprintf("#%s-%s-table", active_ns, component)))+ self$get_text(self$active_module_element(element)) |
680 | +220 |
- ) {+ }, |
|
681 | -! | +||
221 | +
- private$ns[[component]] <- sprintf("%s-%s", active_ns, component)+ #' @description |
||
682 | +222 |
- } else {+ #' Get the active shiny name space for interacting with the filter panel. |
|
683 | -! | +||
223 | +
- private$ns[[component]] <- sprintf("%s-module_%s", active_ns, component)+ #' |
||
684 | +224 |
- }+ #' @return (`string`) The active shiny name space of the component. |
|
685 | +225 |
- }+ active_filters_ns = function() { |
|
686 | -+ | ||
226 | +! |
- },+ if (identical(private$ns$filter_panel, character(0))) { |
|
687 | -+ | ||
227 | +! |
- # @description+ private$set_active_ns() |
|
688 | +228 |
- # Get the active filter values from the active filter selection of dataset from the filter panel.+ } |
|
689 | -+ | ||
229 | +! |
- #+ private$ns$filter_panel |
|
690 | +230 |
- # @param dataset_name (character) The name of the dataset to get the filter values from.+ }, |
|
691 | +231 |
- # @param var_name (character) The name of the variable to get the filter values from.+ #' @description |
|
692 | +232 |
- #+ #' Get the active shiny name space for interacting with the data-summary panel. |
|
693 | +233 |
- # @return The value of the active filter selection.+ #' |
|
694 | +234 |
- get_active_filter_selection = function(dataset_name, var_name) {+ #' @return (`string`) The active shiny name space of the data-summary component. |
|
695 | -! | +||
235 | +
- checkmate::check_string(dataset_name)+ active_data_summary_ns = function() { |
||
696 | +236 | ! |
- checkmate::check_string(var_name)+ if (identical(private$ns$data_summary, character(0))) { |
697 | +237 | ! |
- input_id_prefix <- sprintf(+ private$set_active_ns() |
698 | -! | +||
238 | +
- "%s-filters-%s-filter-%s_%s-inputs",+ } |
||
699 | +239 | ! |
- self$active_filters_ns(),+ private$ns$data_summary |
700 | -! | +||
240 | +
- dataset_name,+ }, |
||
701 | -! | +||
241 | +
- dataset_name,+ #' @description |
||
702 | -! | +||
242 | +
- var_name+ #' Get the active shiny name space bound with a custom `element` name. |
||
703 | +243 |
- )+ #' |
|
704 | +244 |
-
+ #' @param element `character(1)` custom element name. |
|
705 | +245 |
- # Find the type of filter (categorical or range)+ #' |
|
706 | -! | +||
246 | +
- supported_suffix <- c("selection", "selection_manual")+ #' @return (`string`) The active shiny name space of the component bound with the input `element`. |
||
707 | -! | +||
247 | +
- for (suffix in supported_suffix) {+ active_data_summary_element = function(element) { |
||
708 | +248 | ! |
- if (!is.null(self$get_html(sprintf("#%s-%s", input_id_prefix, suffix)))) {+ checkmate::assert_string(element) |
709 | +249 | ! |
- return(self$get_value(input = sprintf("%s-%s", input_id_prefix, suffix)))- |
-
710 | -- |
- }+ sprintf("#%s-%s", self$active_data_summary_ns(), element) |
|
711 | +250 |
- }+ }, |
|
712 | +251 | - - | -|
713 | -! | -
- NULL # If there are not any supported filters+ #' @description |
|
714 | +252 |
- },+ #' Get the input from the module in the `teal` app. |
|
715 | +253 |
- # @description+ #' This function will only access inputs from the name space of the current active teal module. |
|
716 | +254 |
- # Check if the page is stable without any `DOM` updates in the body of the app.+ #' |
|
717 | +255 |
- # This is achieved by blocing the R process by sleeping until the page is unchanged till the `stability_period`.+ #' @param input_id (character) The shiny input id to get the value from. |
|
718 | +256 |
- # @param stability_period (`numeric(1)`) The time in milliseconds to wait till the page to be stable.+ #' |
|
719 | +257 |
- # @param check_interval (`numeric(1)`) The time in milliseconds to check for changes in the page.+ #' @return The value of the shiny input. |
|
720 | +258 |
- # The stability check is reset when a change is detected in the page after sleeping for check_interval.+ get_active_module_input = function(input_id) { |
|
721 | -+ | ||
259 | +! |
- wait_for_page_stability = function(stability_period = 2000, check_interval = 200) {+ checkmate::check_string(input_id) |
|
722 | +260 | ! |
- previous_content <- self$get_html("body")+ self$get_value(input = sprintf("%s-%s", self$active_module_ns(), input_id)) |
723 | -! | +||
261 | +
- end_time <- Sys.time() + (stability_period / 1000)+ }, |
||
724 | +262 |
-
+ #' @description |
|
725 | -! | +||
263 | +
- repeat {+ #' Get the output from the module in the `teal` app. |
||
726 | -! | +||
264 | +
- Sys.sleep(check_interval / 1000)+ #' This function will only access outputs from the name space of the current active teal module. |
||
727 | -! | +||
265 | +
- current_content <- self$get_html("body")+ #' |
||
728 | +266 |
-
+ #' @param output_id (character) The shiny output id to get the value from. |
|
729 | -! | +||
267 | +
- if (!identical(previous_content, current_content)) {+ #' |
||
730 | -! | +||
268 | +
- previous_content <- current_content+ #' @return The value of the shiny output. |
||
731 | -! | +||
269 | +
- end_time <- Sys.time() + (stability_period / 1000)+ get_active_module_output = function(output_id) { |
||
732 | +270 | ! |
- } else if (Sys.time() >= end_time) {+ checkmate::check_string(output_id) |
733 | +271 | ! |
- break+ self$get_value(output = sprintf("%s-%s", self$active_module_ns(), output_id)) |
734 | +272 |
- }+ }, |
|
735 | +273 |
- }+ #' @description |
|
736 | +274 |
- }+ #' Get the output from the module's `teal.widgets::table_with_settings` or `DT::DTOutput` in the `teal` app. |
|
737 | +275 |
- )+ #' This function will only access outputs from the name space of the current active teal module. |
|
738 | +276 |
- )+ #' |
1 | +277 |
- setOldClass("teal_module")+ #' @param table_id (`character(1)`) The id of the table in the active teal module's name space. |
|
2 | +278 |
- setOldClass("teal_modules")+ #' @param which (integer) If there is more than one table, which should be extracted. |
|
3 | +279 |
-
+ #' By default it will look for a table that is built using `teal.widgets::table_with_settings`. |
|
4 | +280 |
- #' Create `teal_module` and `teal_modules` objects+ #' |
|
5 | +281 |
- #'+ #' @return The data.frame with table contents. |
|
6 | +282 |
- #' @description+ get_active_module_table_output = function(table_id, which = 1) { |
|
7 | -+ | ||
283 | +! |
- #' `r lifecycle::badge("stable")`+ checkmate::check_number(which, lower = 1) |
|
8 | -+ | ||
284 | +! |
- #' Create a nested tab structure to embed modules in a `teal` application.+ checkmate::check_string(table_id) |
|
9 | -+ | ||
285 | +! |
- #'+ table <- rvest::html_table( |
|
10 | -+ | ||
286 | +! |
- #' @details+ self$get_html_rvest(self$active_module_element(table_id)), |
|
11 | -+ | ||
287 | +! |
- #' `module()` creates an instance of a `teal_module` that can be placed in a `teal` application.+ fill = TRUE |
|
12 | +288 |
- #' `modules()` shapes the structure of a the application by organizing `teal_module` within the navigation panel.+ ) |
|
13 | -+ | ||
289 | +! |
- #' It wraps `teal_module` and `teal_modules` objects in a `teal_modules` object,+ if (length(table) == 0) { |
|
14 | -+ | ||
290 | +! |
- #' which results in a nested structure corresponding to the nested tabs in the final application.+ data.frame() |
|
15 | +291 |
- #'+ } else { |
|
16 | -+ | ||
292 | +! |
- #' Note that for `modules()` `label` comes after `...`, so it must be passed as a named argument,+ table[[which]] |
|
17 | +293 |
- #' otherwise it will be captured by `...`.+ } |
|
18 | +294 |
- #'+ }, |
|
19 | +295 |
- #' The labels `"global_filters"` and `"Report previewer"` are reserved+ #' @description |
|
20 | +296 |
- #' because they are used by the `mapping` argument of [teal_slices()]+ #' Get the output from the module's `teal.widgets::plot_with_settings` in the `teal` app. |
|
21 | +297 |
- #' and the report previewer module [reporter_previewer_module()], respectively.+ #' This function will only access plots from the name space of the current active teal module. |
|
22 | +298 |
- #'+ #' |
|
23 | +299 |
- #' # Restricting datasets used by `teal_module`:+ #' @param plot_id (`character(1)`) The id of the plot in the active teal module's name space. |
|
24 | +300 |
- #' The `datanames` argument controls which datasets are used by the module’s server. These datasets,+ #' |
|
25 | +301 |
- #' passed via server's `data` argument, are the only ones shown in the module's tab.+ #' @return The `src` attribute as `character(1)` vector. |
|
26 | +302 |
- #'+ get_active_module_plot_output = function(plot_id) { |
|
27 | -+ | ||
303 | +! |
- #' When `datanames` is set to `"all"`, all datasets in the data object are treated as relevant.+ checkmate::check_string(plot_id) |
|
28 | -+ | ||
304 | +! |
- #' However, this may include unnecessary datasets, such as:+ self$get_attr( |
|
29 | -- |
- #' - Proxy variables for column modifications+ | |
305 | +! | +
+ self$active_module_element(sprintf("%s-plot_main > img", plot_id)), |
|
30 | -+ | ||
306 | +! |
- #' - Temporary datasets used to create final versions+ "src" |
|
31 | +307 |
- #' - Connection objects+ ) |
|
32 | +308 |
- #'+ }, |
|
33 | +309 |
- #' To exclude irrelevant datasets, use the [set_datanames()] function to change `datanames` from+ #' @description |
|
34 | +310 |
- #' `"all"` to specific names. Trying to modify non-`"all"` values with [set_datanames()] will result+ #' Set the input in the module in the `teal` app. |
|
35 | +311 |
- #' in a warning. Datasets with names starting with . are ignored globally unless explicitly listed+ #' This function will only set inputs in the name space of the current active teal module. |
|
36 | +312 |
- #' in `datanames`.+ #' |
|
37 | +313 |
- #'+ #' @param input_id (character) The shiny input id to get the value from. |
|
38 | +314 |
- #' # `datanames` with `transformers`+ #' @param value The value to set the input to. |
|
39 | +315 |
- #' When transformers are specified, their `datanames` are added to the module’s `datanames`, which+ #' @param ... Additional arguments to be passed to `shinytest2::AppDriver$set_inputs` |
|
40 | +316 |
- #' changes the behavior as follows:+ #' |
|
41 | +317 |
- #' - If `module(datanames)` is `NULL` and the `transformers` have defined `datanames`, the sidebar+ #' @return The `TealAppDriver` object invisibly. |
|
42 | +318 |
- #' will appear showing the `transformers`' datasets, instead of being hidden.+ set_active_module_input = function(input_id, value, ...) { |
|
43 | -+ | ||
319 | +! |
- #' - If `module(datanames)` is set to specific values and any `transformer` has `datanames = "all"`,+ checkmate::check_string(input_id) |
|
44 | -+ | ||
320 | +! |
- #' the module may receive extra datasets that could be unnecessary+ checkmate::check_string(value) |
|
45 | -+ | ||
321 | +! |
- #'+ self$set_input( |
|
46 | -+ | ||
322 | +! |
- #' @param label (`character(1)`) Label shown in the navigation item for the module or module group.+ sprintf("%s-%s", self$active_module_ns(), input_id), |
|
47 | -+ | ||
323 | +! |
- #' For `modules()` defaults to `"root"`. See `Details`.+ value, |
|
48 | +324 |
- #' @param server (`function`) `shiny` module with following arguments:+ ... |
|
49 | +325 |
- #' - `id` - `teal` will set proper `shiny` namespace for this module (see [shiny::moduleServer()]).+ ) |
|
50 | -+ | ||
326 | +! |
- #' - `input`, `output`, `session` - (optional; not recommended) When provided, then [shiny::callModule()]+ dots <- rlang::list2(...) |
|
51 | -+ | ||
327 | +! |
- #' will be used to call a module. From `shiny` 1.5.0, the recommended way is to use+ if (!isFALSE(dots[["wait"]])) self$wait_for_idle() # Default behavior is to wait |
|
52 | -+ | ||
328 | +! |
- #' [shiny::moduleServer()] instead which doesn't require these arguments.+ invisible(self) |
|
53 | +329 |
- #' - `data` (optional) When provided, the module will be called with `teal_data` object (i.e. a list of+ }, |
|
54 | +330 |
- #' reactive (filtered) data specified in the `filters` argument) as the value of this argument.+ #' @description |
|
55 | +331 |
- #' - `datasets` (optional) When provided, the module will be called with `FilteredData` object as the+ #' Get the active datasets that can be accessed via the filter panel of the current active teal module. |
|
56 | +332 |
- #' value of this argument. (See [`teal.slice::FilteredData`]).+ get_active_filter_vars = function() { |
|
57 | -+ | ||
333 | +! |
- #' - `reporter` (optional) When provided, the module will be called with `Reporter` object as the value+ displayed_datasets_index <- self$is_visible( |
|
58 | -+ | ||
334 | +! |
- #' of this argument. (See [`teal.reporter::Reporter`]).+ sprintf("#%s-filters-filter_active_vars_contents > span", self$active_filters_ns()) |
|
59 | +335 |
- #' - `filter_panel_api` (optional) When provided, the module will be called with `FilterPanelAPI` object+ ) |
|
60 | +336 |
- #' as the value of this argument. (See [`teal.slice::FilterPanelAPI`]).+ |
|
61 | -+ | ||
337 | +! |
- #' - `...` (optional) When provided, `server_args` elements will be passed to the module named argument+ available_datasets <- self$get_text( |
|
62 | -+ | ||
338 | +! |
- #' or to the `...`.+ sprintf( |
|
63 | -+ | ||
339 | +! |
- #' @param ui (`function`) `shiny` UI module function with following arguments:+ "#%s-filters-filter_active_vars_contents .filter_panel_dataname", |
|
64 | -+ | ||
340 | +! |
- #' - `id` - `teal` will set proper `shiny` namespace for this module.+ self$active_filters_ns() |
|
65 | +341 |
- #' - `...` (optional) When provided, `ui_args` elements will be passed to the module named argument+ ) |
|
66 | +342 |
- #' or to the `...`.+ ) |
|
67 | +343 |
- #' @param filters (`character`) Deprecated. Use `datanames` instead.+ |
|
68 | -+ | ||
344 | +! |
- #' @param datanames (`character`) Names of the datasets relevant to the item.+ available_datasets[displayed_datasets_index] |
|
69 | +345 |
- #' There are 2 reserved values that have specific behaviors:+ }, |
|
70 | +346 |
- #' - The keyword `"all"` includes all datasets available in the data passed to the teal application.+ #' @description |
|
71 | +347 |
- #' - `NULL` hides the sidebar panel completely.+ #' Get the active data summary table |
|
72 | +348 |
- #' - If `transformers` are specified, their `datanames` are automatically added to this `datanames`+ #' @return `data.frame` |
|
73 | +349 |
- #' argument.+ get_active_data_summary_table = function() { |
|
74 | -+ | ||
350 | +! |
- #' @param server_args (named `list`) with additional arguments passed on to the server function.+ summary_table <- rvest::html_table( |
|
75 | -+ | ||
351 | +! |
- #' @param ui_args (named `list`) with additional arguments passed on to the UI function.+ self$get_html_rvest(self$active_data_summary_element("table")), |
|
76 | -+ | ||
352 | +! |
- #' @param x (`teal_module` or `teal_modules`) Object to format/print.+ fill = TRUE |
|
77 | -+ | ||
353 | +! |
- #' @param indent (`integer(1)`) Indention level; each nested element is indented one level more.+ )[[1]] |
|
78 | +354 |
- #' @param transformers (`list` of `teal_data_module`) that will be applied to transform the data.+ |
|
79 | -+ | ||
355 | +! |
- #' Each transform module UI will appear in the `teal`'s sidebar panel.+ col_names <- unlist(summary_table[1, ], use.names = FALSE) |
|
80 | -+ | ||
356 | +! |
- #' Transformers' `datanames` are added to the `datanames`. See [teal_transform_module()].+ summary_table <- summary_table[-1, ] |
|
81 | -+ | ||
357 | +! |
- #'+ colnames(summary_table) <- col_names |
|
82 | -+ | ||
358 | +! |
- #' @param ...+ if (nrow(summary_table) > 0) { |
|
83 | -+ | ||
359 | +! |
- #' - For `modules()`: (`teal_module` or `teal_modules`) Objects to wrap into a tab.+ summary_table |
|
84 | +360 |
- #' - For `format()` and `print()`: Arguments passed to other methods.+ } else { |
|
85 | -+ | ||
361 | +! |
- #'+ NULL |
|
86 | +362 |
- #' @return+ } |
|
87 | +363 |
- #' `module()` returns an object of class `teal_module`.+ }, |
|
88 | +364 |
- #'+ #' @description |
|
89 | +365 |
- #' `modules()` returns a `teal_modules` object which contains following fields:+ #' Test if `DOM` elements are visible on the page with a JavaScript call. |
|
90 | +366 |
- #' - `label`: taken from the `label` argument.+ #' @param selector (`character(1)`) `CSS` selector to check visibility. |
|
91 | +367 |
- #' - `children`: a list containing objects passed in `...`. List elements are named after+ #' A `CSS` id will return only one element if the UI is well formed. |
|
92 | +368 |
- #' their `label` attribute converted to a valid `shiny` id.+ #' @param content_visibility_auto,opacity_property,visibility_property (`logical(1)`) See more information |
|
93 | +369 |
- #'+ #' on <https://developer.mozilla.org/en-US/docs/Web/API/Element/checkVisibility>. |
|
94 | +370 |
- #' @name teal_modules+ #' |
|
95 | +371 |
- #' @aliases teal_module+ #' @return Logical vector with all occurrences of the selector. |
|
96 | +372 |
- #'+ is_visible = function(selector, |
|
97 | +373 |
- #' @examples+ content_visibility_auto = FALSE, |
|
98 | +374 |
- #' library(shiny)+ opacity_property = FALSE, |
|
99 | +375 |
- #'+ visibility_property = FALSE) { |
|
100 | -+ | ||
376 | +! |
- #' module_1 <- module(+ checkmate::assert_string(selector) |
|
101 | -+ | ||
377 | +! |
- #' label = "a module",+ checkmate::assert_flag(content_visibility_auto) |
|
102 | -+ | ||
378 | +! |
- #' server = function(id, data) {+ checkmate::assert_flag(opacity_property) |
|
103 | -+ | ||
379 | +! |
- #' moduleServer(+ checkmate::assert_flag(visibility_property) |
|
104 | +380 |
- #' id,+ |
|
105 | -+ | ||
381 | +! |
- #' module = function(input, output, session) {+ private$wait_for_page_stability() |
|
106 | +382 |
- #' output$data <- renderDataTable(data()[["iris"]])+ |
|
107 | -+ | ||
383 | +! |
- #' }+ testthat::skip_if_not( |
|
108 | -+ | ||
384 | +! |
- #' )+ self$get_js("typeof Element.prototype.checkVisibility === 'function'"), |
|
109 | -+ | ||
385 | +! |
- #' },+ "Element.prototype.checkVisibility is not supported in the current browser." |
|
110 | +386 |
- #' ui = function(id) {+ ) |
|
111 | +387 |
- #' ns <- NS(id)+ |
|
112 | -+ | ||
388 | +! |
- #' tagList(dataTableOutput(ns("data")))+ unlist( |
|
113 | -+ | ||
389 | +! |
- #' },+ self$get_js( |
|
114 | -+ | ||
390 | +! |
- #' datanames = "all"+ sprintf( |
|
115 | -+ | ||
391 | +! |
- #' )+ "Array.from(document.querySelectorAll('%s')).map(el => el.checkVisibility({%s, %s, %s}))", |
|
116 | -+ | ||
392 | +! |
- #'+ selector, |
|
117 | +393 |
- #' module_2 <- module(+ # Extra parameters |
|
118 | -+ | ||
394 | +! |
- #' label = "another module",+ sprintf("contentVisibilityAuto: %s", tolower(content_visibility_auto)), |
|
119 | -+ | ||
395 | +! |
- #' server = function(id) {+ sprintf("opacityProperty: %s", tolower(opacity_property)), |
|
120 | -+ | ||
396 | +! |
- #' moduleServer(+ sprintf("visibilityProperty: %s", tolower(visibility_property)) |
|
121 | +397 |
- #' id,+ ) |
|
122 | +398 |
- #' module = function(input, output, session) {+ ) |
|
123 | +399 |
- #' output$text <- renderText("Another Module")+ ) |
|
124 | +400 |
- #' }+ }, |
|
125 | +401 |
- #' )+ #' @description |
|
126 | +402 |
- #' },+ #' Get the active filter variables from a dataset in the `teal` app. |
|
127 | +403 |
- #' ui = function(id) {+ #' |
|
128 | +404 |
- #' ns <- NS(id)+ #' @param dataset_name (character) The name of the dataset to get the filter variables from. |
|
129 | +405 |
- #' tagList(textOutput(ns("text")))+ #' If `NULL`, the filter variables for all the datasets will be returned in a list. |
|
130 | +406 |
- #' },+ get_active_data_filters = function(dataset_name = NULL) { |
|
131 | -+ | ||
407 | +! |
- #' datanames = NULL+ checkmate::check_string(dataset_name, null.ok = TRUE) |
|
132 | -+ | ||
408 | +! |
- #' )+ datasets <- self$get_active_filter_vars() |
|
133 | -+ | ||
409 | +! |
- #'+ checkmate::assert_subset(dataset_name, datasets) |
|
134 | -+ | ||
410 | +! |
- #' modules <- modules(+ active_filters <- lapply( |
|
135 | -+ | ||
411 | +! |
- #' label = "modules",+ datasets, |
|
136 | -+ | ||
412 | +! |
- #' modules(+ function(x) { |
|
137 | -+ | ||
413 | +! |
- #' label = "nested modules",+ var_names <- gsub( |
|
138 | -+ | ||
414 | +! |
- #' module_1+ pattern = "\\s", |
|
139 | -+ | ||
415 | +! |
- #' ),+ replacement = "", |
|
140 | -+ | ||
416 | +! |
- #' module_2+ self$get_text( |
|
141 | -+ | ||
417 | +! |
- #' )+ sprintf( |
|
142 | -+ | ||
418 | +! |
- #'+ "#%s-filters-%s .filter-card-varname", |
|
143 | -+ | ||
419 | +! |
- #' app <- init(+ self$active_filters_ns(), |
|
144 | -+ | ||
420 | +! |
- #' data = teal_data(iris = iris),+ x |
|
145 | +421 |
- #' modules = modules+ ) |
|
146 | +422 |
- #' )+ ) |
|
147 | +423 |
- #'+ ) |
|
148 | -+ | ||
424 | +! |
- #' if (interactive()) {+ structure( |
|
149 | -+ | ||
425 | +! |
- #' shinyApp(app$ui, app$server)+ lapply(var_names, private$get_active_filter_selection, dataset_name = x),+ |
+ |
426 | +! | +
+ names = var_names |
|
150 | +427 |
- #' }+ ) |
|
151 | +428 |
- #' @rdname teal_modules+ } |
|
152 | +429 |
- #' @export+ )+ |
+ |
430 | +! | +
+ names(active_filters) <- datasets+ |
+ |
431 | +! | +
+ if (is.null(dataset_name)) {+ |
+ |
432 | +! | +
+ return(active_filters) |
|
153 | +433 |
- #'+ }+ |
+ |
434 | +! | +
+ active_filters[[dataset_name]] |
|
154 | +435 |
- module <- function(label = "module",+ }, |
|
155 | +436 |
- server = function(id, data, ...) moduleServer(id, function(input, output, session) NULL),+ #' @description |
|
156 | +437 |
- ui = function(id, ...) tags$p(paste0("This module has no UI (id: ", id, " )")),+ #' Add a new variable from the dataset to be filtered. |
|
157 | +438 |
- filters,+ #' |
|
158 | +439 |
- datanames = "all",+ #' @param dataset_name (character) The name of the dataset to add the filter variable to. |
|
159 | +440 |
- server_args = NULL,+ #' @param var_name (character) The name of the variable to add to the filter panel. |
|
160 | +441 |
- ui_args = NULL,+ #' @param ... Additional arguments to be passed to `shinytest2::AppDriver$set_inputs` |
|
161 | +442 |
- transformers = list()) {+ #' |
|
162 | +443 |
- # argument checking (independent)+ #' @return The `TealAppDriver` object invisibly. |
|
163 | +444 |
- ## `label`+ add_filter_var = function(dataset_name, var_name, ...) { |
|
164 | -207x - | +||
445 | +! |
- checkmate::assert_string(label)+ checkmate::check_string(dataset_name) |
|
165 | -204x - | +||
446 | +! |
- if (label == "global_filters") {+ checkmate::check_string(var_name) |
|
166 | -1x - | +||
447 | +! |
- stop(+ private$set_active_ns() |
|
167 | -1x - | +||
448 | +! |
- sprintf("module(label = \"%s\", ...\n ", label),+ self$click( |
|
168 | -1x - | +||
449 | +! |
- "Label 'global_filters' is reserved in teal. Please change to something else.",+ selector = sprintf( |
|
169 | -1x - | +||
450 | +! |
- call. = FALSE+ "#%s-filters-%s-add_filter_icon",+ |
+ |
451 | +! | +
+ private$ns$filter_panel,+ |
+ |
452 | +! | +
+ dataset_name |
|
170 | +453 |
- )+ ) |
|
171 | +454 |
- }+ ) |
|
172 | -203x - | +||
455 | +! |
- if (label == "Report previewer") {+ self$set_input( |
|
173 | +456 | ! |
- stop(+ sprintf( |
174 | +457 | ! |
- sprintf("module(label = \"%s\", ...\n ", label),+ "%s-filters-%s-%s-filter-var_to_add", |
175 | +458 | ! |
- "Label 'Report previewer' is reserved in teal. Please change to something else.",+ private$ns$filter_panel, |
176 | +459 | ! |
- call. = FALSE+ dataset_name, |
177 | -+ | ||
460 | +! |
- )+ dataset_name |
|
178 | +461 |
- }+ ), |
|
179 | -+ | ||
462 | +! |
-
+ var_name, |
|
180 | +463 |
- ## server+ ... |
|
181 | -203x - | +||
464 | +
- checkmate::assert_function(server)+ ) |
||
182 | -203x - | +||
465 | +! |
- server_formals <- names(formals(server))+ invisible(self) |
|
183 | -203x - | +||
466 | +
- if (!(+ }, |
||
184 | -203x - | +||
467 | +
- "id" %in% server_formals ||+ #' @description |
||
185 | -203x - | +||
468 | +
- all(c("input", "output", "session") %in% server_formals)+ #' Remove an active filter variable of a dataset from the active filter variables panel. |
||
186 | +469 |
- )) {+ #' |
|
187 | -2x - | +||
470 | +
- stop(+ #' @param dataset_name (character) The name of the dataset to remove the filter variable from. |
||
188 | -2x - | +||
471 | +
- "\nmodule() `server` argument requires a function with following arguments:",+ #' If `NULL`, all the filter variables will be removed. |
||
189 | -2x - | +||
472 | +
- "\n - id - `teal` will set proper `shiny` namespace for this module.",+ #' @param var_name (character) The name of the variable to remove from the filter panel. |
||
190 | -2x - | +||
473 | +
- "\n - input, output, session (not recommended) - then `shiny::callModule` will be used to call a module.",+ #' If `NULL`, all the filter variables of the dataset will be removed. |
||
191 | -2x - | +||
474 | +
- "\n\nFollowing arguments can be used optionaly:",+ #' |
||
192 | -2x - | +||
475 | +
- "\n - `data` - module will receive list of reactive (filtered) data specified in the `filters` argument",+ #' @return The `TealAppDriver` object invisibly. |
||
193 | -2x - | +||
476 | +
- "\n - `datasets` - module will receive `FilteredData`. See `help(teal.slice::FilteredData)`",+ remove_filter_var = function(dataset_name = NULL, var_name = NULL) { |
||
194 | -2x - | +||
477 | +! |
- "\n - `reporter` - module will receive `Reporter`. See `help(teal.reporter::Reporter)`",+ checkmate::check_string(dataset_name, null.ok = TRUE) |
|
195 | -2x - | +||
478 | +! |
- "\n - `filter_panel_api` - module will receive `FilterPanelAPI`. (See [teal.slice::FilterPanelAPI]).",+ checkmate::check_string(var_name, null.ok = TRUE) |
|
196 | -2x - | +||
479 | +! |
- "\n - `...` server_args elements will be passed to the module named argument or to the `...`"+ if (is.null(dataset_name)) { |
|
197 | -+ | ||
480 | +! |
- )+ remove_selector <- sprintf( |
|
198 | -+ | ||
481 | +! |
- }+ "#%s-active-remove_all_filters", |
|
199 | -201x - | +||
482 | +! |
- if ("datasets" %in% server_formals) {+ self$active_filters_ns() |
|
200 | -2x - | +||
483 | +
- warning(+ ) |
||
201 | -2x - | +||
484 | +! |
- sprintf("Called from module(label = \"%s\", ...)\n ", label),+ } else if (is.null(var_name)) { |
|
202 | -2x - | +||
485 | +! |
- "`datasets` argument in the server is deprecated and will be removed in the next release. ",+ remove_selector <- sprintf( |
|
203 | -2x - | +||
486 | +! |
- "Please use `data` instead.",+ "#%s-active-%s-remove_filters", |
|
204 | -2x - | +||
487 | +! |
- call. = FALSE+ self$active_filters_ns(), |
|
205 | -+ | ||
488 | +! |
- )+ dataset_name |
|
206 | +489 |
- }+ ) |
|
207 | +490 |
-
+ } else { |
|
208 | -+ | ||
491 | +! |
-
+ remove_selector <- sprintf( |
|
209 | -+ | ||
492 | +! |
- ## UI+ "#%s-active-%s-filter-%s_%s-remove", |
|
210 | -201x - | +||
493 | +! |
- checkmate::assert_function(ui)+ self$active_filters_ns(), |
|
211 | -201x - | +||
494 | +! |
- ui_formals <- names(formals(ui))+ dataset_name, |
|
212 | -201x - | +||
495 | +! |
- if (!"id" %in% ui_formals) {+ dataset_name, |
|
213 | -1x - | +||
496 | +! |
- stop(+ var_name |
|
214 | -1x - | +||
497 | +
- "\nmodule() `ui` argument requires a function with following arguments:",+ ) |
||
215 | -1x - | +||
498 | +
- "\n - id - `teal` will set proper `shiny` namespace for this module.",+ } |
||
216 | -1x - | +||
499 | +! |
- "\n\nFollowing arguments can be used optionally:",+ self$click( |
|
217 | -1x - | +||
500 | +! |
- "\n - `...` ui_args elements will be passed to the module argument of the same name or to the `...`"+ selector = remove_selector |
|
218 | +501 |
- )+ )+ |
+ |
502 | +! | +
+ invisible(self) |
|
219 | +503 |
- }+ }, |
|
220 | -200x - | +||
504 | +
- if (any(c("data", "datasets") %in% ui_formals)) {+ #' @description |
||
221 | -2x - | +||
505 | +
- stop(+ #' Set the active filter values for a variable of a dataset in the active filter variable panel. |
||
222 | -2x - | +||
506 | +
- sprintf("Called from module(label = \"%s\", ...)\n ", label),+ #' |
||
223 | -2x - | +||
507 | +
- "UI with `data` or `datasets` argument is no longer accepted.\n ",+ #' @param dataset_name (character) The name of the dataset to set the filter value for. |
||
224 | -2x - | +||
508 | +
- "If some UI inputs depend on data, please move the logic to your server instead.\n ",+ #' @param var_name (character) The name of the variable to set the filter value for. |
||
225 | -2x - | +||
509 | +
- "Possible solutions are renderUI() or updateXyzInput() functions."+ #' @param input The value to set the filter to. |
||
226 | +510 |
- )+ #' @param ... Additional arguments to be passed to `shinytest2::AppDriver$set_inputs` |
|
227 | +511 |
- }+ #' |
|
228 | +512 |
-
+ #' @return The `TealAppDriver` object invisibly. |
|
229 | +513 |
-
+ set_active_filter_selection = function(dataset_name, |
|
230 | +514 |
- ## `filters`+ var_name, |
|
231 | -198x - | +||
515 | +
- if (!missing(filters)) {+ input, |
||
232 | -! | +||
516 | +
- datanames <- filters+ ...) { |
||
233 | +517 | ! |
- msg <-+ checkmate::check_string(dataset_name) |
234 | +518 | ! |
- "The `filters` argument is deprecated and will be removed in the next release. Please use `datanames` instead."+ checkmate::check_string(var_name) |
235 | +519 | ! |
- warning(msg)+ checkmate::check_string(input) |
236 | +520 |
- }+ |
|
237 | -+ | ||
521 | +! |
-
+ input_id_prefix <- sprintf( |
|
238 | -+ | ||
522 | +! |
- ## `datanames` (also including deprecated `filters`)+ "%s-filters-%s-filter-%s_%s-inputs", |
|
239 | -+ | ||
523 | +! |
- # please note a race condition between datanames set when filters is not missing and data arg in server function+ self$active_filters_ns(), |
|
240 | -198x - | +||
524 | +! |
- if (!is.element("data", server_formals) && !is.null(datanames)) {+ dataset_name, |
|
241 | -12x - | +||
525 | +! |
- message(sprintf("module \"%s\" server function takes no data so \"datanames\" will be ignored", label))+ dataset_name, |
|
242 | -12x - | +||
526 | +! |
- datanames <- NULL+ var_name |
|
243 | +527 |
- }- |
- |
244 | -198x - | -
- checkmate::assert_character(datanames, min.len = 1, null.ok = TRUE, any.missing = FALSE)+ ) |
|
245 | +528 | ||
246 | +529 |
- ## `server_args`+ # Find the type of filter (based on filter panel) |
|
247 | -197x - | +||
530 | +! |
- checkmate::assert_list(server_args, null.ok = TRUE, names = "named")+ supported_suffix <- c("selection", "selection_manual") |
|
248 | -195x - | +||
531 | +! |
- srv_extra_args <- setdiff(names(server_args), server_formals)+ slices_suffix <- supported_suffix[ |
|
249 | -195x - | +||
532 | +! |
- if (length(srv_extra_args) > 0 && !"..." %in% server_formals) {+ match( |
|
250 | -1x - | +||
533 | +! |
- stop(+ TRUE, |
|
251 | -1x - | +||
534 | +! |
- "\nFollowing `server_args` elements have no equivalent in the formals of the server:\n",+ vapply( |
|
252 | -1x - | +||
535 | +! |
- paste(paste(" -", srv_extra_args), collapse = "\n"),+ supported_suffix, |
|
253 | -1x - | +||
536 | +! |
- "\n\nUpdate the server arguments by including above or add `...`"+ function(suffix) {+ |
+ |
537 | +! | +
+ !is.null(self$get_html(sprintf("#%s-%s", input_id_prefix, suffix))) |
|
254 | +538 |
- )+ },+ |
+ |
539 | +! | +
+ logical(1) |
|
255 | +540 |
- }+ ) |
|
256 | +541 |
-
+ ) |
|
257 | +542 |
- ## `ui_args`+ ] |
|
258 | -194x - | +||
543 | +
- checkmate::assert_list(ui_args, null.ok = TRUE, names = "named")+ |
||
259 | -192x - | +||
544 | +
- ui_extra_args <- setdiff(names(ui_args), ui_formals)+ # Generate correct namespace |
||
260 | -192x - | +||
545 | +! |
- if (length(ui_extra_args) > 0 && !"..." %in% ui_formals) {+ slices_input_id <- sprintf( |
|
261 | -1x - | +||
546 | +! |
- stop(+ "%s-filters-%s-filter-%s_%s-inputs-%s", |
|
262 | -1x - | +||
547 | +! |
- "\nFollowing `ui_args` elements have no equivalent in the formals of UI:\n",+ self$active_filters_ns(), |
|
263 | -1x - | +||
548 | +! |
- paste(paste(" -", ui_extra_args), collapse = "\n"),+ dataset_name, |
|
264 | -1x - | +||
549 | +! |
- "\n\nUpdate the UI arguments by including above or add `...`"+ dataset_name, |
|
265 | -+ | ||
550 | +! |
- )+ var_name, |
|
266 | -+ | ||
551 | +! |
- }+ slices_suffix |
|
267 | +552 |
-
+ ) |
|
268 | +553 |
- ## `transformers`+ |
|
269 | -191x - | +||
554 | +! |
- if (inherits(transformers, "teal_transform_module")) {+ if (identical(slices_suffix, "selection_manual")) { |
|
270 | -1x - | +||
555 | +! |
- transformers <- list(transformers)+ checkmate::assert_numeric(input, len = 2) |
|
271 | +556 |
- }+ |
|
272 | -191x - | +||
557 | +! |
- checkmate::assert_list(transformers, types = "teal_transform_module")+ dots <- rlang::list2(...) |
|
273 | -191x - | +||
558 | +! |
- transformer_datanames <- unlist(lapply(transformers, attr, "datanames"))+ checkmate::assert_choice(dots$priority_, formals(self$set_inputs)[["priority_"]], null.ok = TRUE) |
|
274 | -191x - | +||
559 | +! |
- combined_datanames <- if (identical(datanames, "all")) {+ checkmate::assert_flag(dots$wait_, null.ok = TRUE) |
|
275 | -142x - | +||
560 | +
- "all"+ |
||
276 | -+ | ||
561 | +! |
- } else {+ self$run_js( |
|
277 | -49x - | +||
562 | +! |
- union(datanames, transformer_datanames)+ sprintf( |
|
278 | -+ | ||
563 | +! |
- }+ "Shiny.setInputValue('%s:sw.numericRange', [%f, %f], {priority: '%s'})", |
|
279 | -+ | ||
564 | +! |
-
+ slices_input_id, |
|
280 | -191x - | +||
565 | +! |
- structure(+ input[[1]], |
|
281 | -191x - | +||
566 | +! |
- list(+ input[[2]], |
|
282 | -191x - | +||
567 | +! |
- label = label,+ priority_ = ifelse(is.null(dots$priority_), "input", dots$priority_) |
|
283 | -191x - | +||
568 | +
- server = server,+ ) |
||
284 | -191x - | +||
569 | +
- ui = ui,+ ) |
||
285 | -191x - | +||
570 | +
- datanames = combined_datanames,+ |
||
286 | -191x - | +||
571 | +! |
- server_args = server_args,+ if (isTRUE(dots$wait_) || is.null(dots$wait_)) { |
|
287 | -191x - | +||
572 | +! |
- ui_args = ui_args,+ self$wait_for_idle( |
|
288 | -191x - | +||
573 | +! |
- transformers = transformers+ timeout = if (is.null(dots$timeout_)) rlang::missing_arg() else dots$timeout_ |
|
289 | +574 |
- ),+ ) |
|
290 | -191x - | +||
575 | +
- class = "teal_module"+ } |
||
291 | -+ | ||
576 | +! |
- )+ } else if (identical(slices_suffix, "selection")) { |
|
292 | -+ | ||
577 | +! |
- }+ self$set_input( |
|
293 | -+ | ||
578 | +! |
-
+ slices_input_id, |
|
294 | -+ | ||
579 | +! |
- #' @rdname teal_modules+ input, |
|
295 | +580 |
- #' @export+ ... |
|
296 | +581 |
- #'+ ) |
|
297 | +582 |
- modules <- function(..., label = "root") {+ } else { |
|
298 | -131x - | +||
583 | +! |
- checkmate::assert_string(label)+ stop("Filter selection set not supported for this slice.") |
|
299 | -129x - | +||
584 | +
- submodules <- list(...)+ } |
||
300 | -129x - | +||
585 | +
- if (any(vapply(submodules, is.character, FUN.VALUE = logical(1)))) {+ |
||
301 | -2x - | +||
586 | +! |
- stop(+ invisible(self) |
|
302 | -2x - | +||
587 | +
- "The only character argument to modules() must be 'label' and it must be named, ",+ }, |
||
303 | -2x - | +||
588 | +
- "change modules('lab', ...) to modules(label = 'lab', ...)"+ #' @description |
||
304 | +589 |
- )+ #' Extract `html` attribute (found by a `selector`). |
|
305 | +590 |
- }+ #' |
|
306 | +591 |
-
+ #' @param selector (`character(1)`) specifying the selector to be used to get the content of a specific node. |
|
307 | -127x - | +||
592 | +
- checkmate::assert_list(submodules, min.len = 1, any.missing = FALSE, types = c("teal_module", "teal_modules"))+ #' @param attribute (`character(1)`) name of an attribute to retrieve from a node specified by `selector`. |
||
308 | +593 |
- # name them so we can more easily access the children+ #' |
|
309 | +594 |
- # beware however that the label of the submodules should not be changed as it must be kept synced+ #' @return The `character` vector. |
|
310 | -124x - | +||
595 | +
- labels <- vapply(submodules, function(submodule) submodule$label, character(1))+ get_attr = function(selector, attribute) { |
||
311 | -124x - | +||
596 | +! |
- names(submodules) <- get_unique_labels(labels)+ rvest::html_attr( |
|
312 | -124x - | +||
597 | +! |
- structure(+ rvest::html_nodes(self$get_html_rvest("html"), selector), |
|
313 | -124x - | +||
598 | +! |
- list(+ attribute |
|
314 | -124x - | +||
599 | +
- label = label,+ ) |
||
315 | -124x - | +||
600 | +
- children = submodules+ }, |
||
316 | +601 |
- ),+ #' @description |
|
317 | -124x - | +||
602 | +
- class = "teal_modules"+ #' Wrapper around `get_html` that passes the output directly to `rvest::read_html`. |
||
318 | +603 |
- )+ #' |
|
319 | +604 |
- }+ #' @param selector `(character(1))` passed to `get_html`. |
|
320 | +605 |
-
+ #' |
|
321 | +606 |
- # printing methods ----+ #' @return An XML document. |
|
322 | +607 |
-
+ get_html_rvest = function(selector) {+ |
+ |
608 | +! | +
+ rvest::read_html(self$get_html(selector)) |
|
323 | +609 |
- #' @rdname teal_modules+ }, |
|
324 | +610 |
- #' @export+ #' Wrapper around `get_url()` method that opens the app in the browser. |
|
325 | +611 |
- format.teal_module <- function(x, indent = 0, ...) {+ #' |
|
326 | -3x - | +||
612 | +
- paste0(paste(rep(" ", indent), collapse = ""), "+ ", x$label, "\n", collapse = "")+ #' @return Nothing. Opens the underlying teal app in the browser. |
||
327 | +613 |
- }+ open_url = function() {+ |
+ |
614 | +! | +
+ browseURL(self$get_url()) |
|
328 | +615 |
-
+ }, |
|
329 | +616 |
-
+ #' @description |
|
330 | +617 |
- #' @rdname teal_modules+ #' Waits until a specified input, output, or export value. |
|
331 | +618 |
- #' @export+ #' This function serves as a wrapper around the `wait_for_value` method, |
|
332 | +619 |
- print.teal_module <- function(x, ...) {+ #' providing a more flexible interface for waiting on different types of values within the active module namespace. |
|
333 | -! | +||
620 | +
- cat(format(x, ...))+ #' @param input,output,export A name of an input, output, or export value. |
||
334 | -! | +||
621 | +
- invisible(x)+ #' Only one of these parameters may be used. |
||
335 | +622 |
- }+ #' @param ... Must be empty. Allows for parameter expansion. |
|
336 | +623 |
-
+ #' Parameter with additional value to passed in `wait_for_value`. |
|
337 | +624 |
-
+ wait_for_active_module_value = function(input = rlang::missing_arg(), |
|
338 | +625 |
- #' @rdname teal_modules+ output = rlang::missing_arg(), |
|
339 | +626 |
- #' @export+ export = rlang::missing_arg(), |
|
340 | +627 |
- format.teal_modules <- function(x, indent = 0, ...) {+ ...) { |
|
341 | -1x - | +||
628 | +! |
- paste(+ ns <- shiny::NS(self$active_module_ns()) |
|
342 | -1x - | +||
629 | +
- c(+ |
||
343 | -1x - | +||
630 | +! |
- paste0(rep(" ", indent), "+ ", x$label, "\n"),+ if (!rlang::is_missing(input) && checkmate::test_string(input, min.chars = 1)) input <- ns(input) |
|
344 | -1x - | +||
631 | +! |
- unlist(lapply(x$children, format, indent = indent + 1, ...))+ if (!rlang::is_missing(output) && checkmate::test_string(output, min.chars = 1)) output <- ns(output)+ |
+ |
632 | +! | +
+ if (!rlang::is_missing(export) && checkmate::test_string(export, min.chars = 1)) export <- ns(export) |
|
345 | +633 |
- ),+ |
|
346 | -1x - | +||
634 | +! |
- collapse = ""+ self$wait_for_value( |
|
347 | -+ | ||
635 | +! |
- )+ input = input, |
|
348 | -+ | ||
636 | +! |
- }+ output = output, |
|
349 | -+ | ||
637 | +! |
-
+ export = export, |
|
350 | +638 |
- #' @param modules (`teal_module` or `teal_modules`)+ ... |
|
351 | +639 |
- #' @rdname teal_modules+ ) |
|
352 | +640 |
- #' @examples+ } |
|
353 | +641 |
- #' # change the module's datanames+ ), |
|
354 | +642 |
- #' set_datanames(module(datanames = "all"), "a")+ # private members ---- |
|
355 | +643 |
- #'+ private = list( |
|
356 | +644 |
- #' # change modules' datanames+ # private attributes ---- |
|
357 | +645 |
- #' set_datanames(+ data = NULL, |
|
358 | +646 |
- #' modules(+ modules = NULL, |
|
359 | +647 |
- #' module(datanames = "all"),+ filter = teal_slices(), |
|
360 | +648 |
- #' module(datanames = "a")+ ns = list( |
|
361 | +649 |
- #' ),+ module = character(0), |
|
362 | +650 |
- #' "b"+ filter_panel = character(0) |
|
363 | +651 |
- #' )+ ), |
|
364 | +652 |
- #' @export+ # private methods ---- |
|
365 | +653 |
- set_datanames <- function(modules, datanames) {+ set_active_ns = function() { |
|
366 | +654 | ! |
- checkmate::assert_multi_class(modules, c("teal_modules", "teal_module"))+ all_inputs <- self$get_values()$input |
367 | +655 | ! |
- if (inherits(modules, "teal_modules")) {+ active_tab_inputs <- all_inputs[grepl("-active_tab$", names(all_inputs))]+ |
+
656 | ++ | + | |
368 | +657 | ! |
- modules$children <- lapply(modules$children, set_datanames, datanames)+ tab_ns <- unlist(lapply(names(active_tab_inputs), function(name) { |
369 | -+ | ||
658 | +! |
- } else {+ gsub( |
|
370 | +659 | ! |
- if (identical(modules$datanames, "all")) {+ pattern = "-active_tab$", |
371 | +660 | ! |
- modules$datanames <- datanames+ replacement = sprintf("-%s", active_tab_inputs[[name]]),+ |
+
661 | +! | +
+ name |
|
372 | +662 |
- } else {+ )+ |
+ |
663 | ++ |
+ })) |
|
373 | +664 | ! |
- warning(+ active_ns <- tab_ns[1] |
374 | +665 | ! |
- "Not possible to modify datanames of the module ", modules$label,+ if (length(tab_ns) > 1) { |
375 | +666 | ! |
- ". set_datanames() can only change datanames if it was set to \"all\".",+ for (i in 2:length(tab_ns)) { |
376 | +667 | ! |
- call. = FALSE+ next_ns <- tab_ns[i]+ |
+
668 | +! | +
+ if (grepl(pattern = active_ns, next_ns)) {+ |
+ |
669 | +! | +
+ active_ns <- next_ns |
|
377 | +670 |
- )+ } |
|
378 | +671 |
- }+ } |
|
379 | +672 |
- }+ } |
|
380 | +673 | ! |
- modules+ private$ns$module <- sprintf("%s-%s", active_ns, "module") |
381 | +674 |
- }+ |
|
382 | -+ | ||
675 | +! |
-
+ components <- c("filter_panel", "data_summary") |
|
383 | -+ | ||
676 | +! |
- #' @rdname teal_modules+ for (component in components) { |
|
384 | +677 |
- #' @export+ if ( |
|
385 | -+ | ||
678 | +! |
- print.teal_modules <- print.teal_module+ !is.null(self$get_html(sprintf("#%s-%s-panel", active_ns, component))) ||+ |
+ |
679 | +! | +
+ !is.null(self$get_html(sprintf("#%s-%s-table", active_ns, component))) |
|
386 | +680 |
-
+ ) {+ |
+ |
681 | +! | +
+ private$ns[[component]] <- sprintf("%s-%s", active_ns, component) |
|
387 | +682 |
-
+ } else {+ |
+ |
683 | +! | +
+ private$ns[[component]] <- sprintf("%s-module_%s", active_ns, component) |
|
388 | +684 |
- # utilities ----+ } |
|
389 | +685 |
- ## subset or modify modules ----+ } |
|
390 | +686 |
-
+ }, |
|
391 | +687 |
- #' Append a `teal_module` to `children` of a `teal_modules` object+ # @description |
|
392 | +688 |
- #' @keywords internal+ # Get the active filter values from the active filter selection of dataset from the filter panel. |
|
393 | +689 |
- #' @param modules (`teal_modules`)+ # |
|
394 | +690 |
- #' @param module (`teal_module`) object to be appended onto the children of `modules`+ # @param dataset_name (character) The name of the dataset to get the filter values from. |
|
395 | +691 |
- #' @return A `teal_modules` object with `module` appended.+ # @param var_name (character) The name of the variable to get the filter values from. |
|
396 | +692 |
- append_module <- function(modules, module) {+ # |
|
397 | -8x - | +||
693 | +
- checkmate::assert_class(modules, "teal_modules")+ # @return The value of the active filter selection. |
||
398 | -6x - | +||
694 | +
- checkmate::assert_class(module, "teal_module")+ get_active_filter_selection = function(dataset_name, var_name) { |
||
399 | -4x - | +||
695 | +! |
- modules$children <- c(modules$children, list(module))+ checkmate::check_string(dataset_name) |
|
400 | -4x - | +||
696 | +! |
- labels <- vapply(modules$children, function(submodule) submodule$label, character(1))+ checkmate::check_string(var_name) |
|
401 | -4x - | +||
697 | +! |
- names(modules$children) <- get_unique_labels(labels)+ input_id_prefix <- sprintf( |
|
402 | -4x - | +||
698 | +! |
- modules+ "%s-filters-%s-filter-%s_%s-inputs", |
|
403 | -+ | ||
699 | +! |
- }+ self$active_filters_ns(), |
|
404 | -+ | ||
700 | +! |
-
+ dataset_name, |
|
405 | -+ | ||
701 | +! |
- #' Extract/Remove module(s) of specific class+ dataset_name, |
|
406 | -+ | ||
702 | +! |
- #'+ var_name |
|
407 | +703 |
- #' Given a `teal_module` or a `teal_modules`, return the elements of the structure according to `class`.+ ) |
|
408 | +704 |
- #'+ |
|
409 | +705 |
- #' @param modules (`teal_modules`)+ # Find the type of filter (categorical or range) |
|
410 | -+ | ||
706 | +! |
- #' @param class The class name of `teal_module` to be extracted or dropped.+ supported_suffix <- c("selection", "selection_manual") |
|
411 | -+ | ||
707 | +! |
- #' @keywords internal+ for (suffix in supported_suffix) { |
|
412 | -+ | ||
708 | +! |
- #' @return+ if (!is.null(self$get_html(sprintf("#%s-%s", input_id_prefix, suffix)))) { |
|
413 | -+ | ||
709 | +! |
- #' - For `extract_module`, a `teal_module` of class `class` or `teal_modules` containing modules of class `class`.+ return(self$get_value(input = sprintf("%s-%s", input_id_prefix, suffix))) |
|
414 | +710 |
- #' - For `drop_module`, the opposite, which is all `teal_modules` of class other than `class`.+ } |
|
415 | +711 |
- #' @rdname module_management+ } |
|
416 | +712 |
- extract_module <- function(modules, class) {- |
- |
417 | -22x - | -
- if (inherits(modules, class)) {+ |
|
418 | +713 | ! |
- modules+ NULL # If there are not any supported filters |
419 | -22x - | +||
714 | +
- } else if (inherits(modules, "teal_module")) {+ }, |
||
420 | -12x - | +||
715 | +
- NULL+ # @description |
||
421 | -10x - | +||
716 | +
- } else if (inherits(modules, "teal_modules")) {+ # Check if the page is stable without any `DOM` updates in the body of the app. |
||
422 | -10x - | +||
717 | +
- Filter(function(x) length(x) > 0L, lapply(modules$children, extract_module, class))+ # This is achieved by blocing the R process by sleeping until the page is unchanged till the `stability_period`. |
||
423 | +718 |
- }+ # @param stability_period (`numeric(1)`) The time in milliseconds to wait till the page to be stable. |
|
424 | +719 |
- }+ # @param check_interval (`numeric(1)`) The time in milliseconds to check for changes in the page. |
|
425 | +720 |
-
+ # The stability check is reset when a change is detected in the page after sleeping for check_interval. |
|
426 | +721 |
- #' @keywords internal+ wait_for_page_stability = function(stability_period = 2000, check_interval = 200) { |
|
427 | -+ | ||
722 | +! |
- #' @return `teal_modules`+ previous_content <- self$get_html("body") |
|
428 | -+ | ||
723 | +! |
- #' @rdname module_management+ end_time <- Sys.time() + (stability_period / 1000) |
|
429 | +724 |
- drop_module <- function(modules, class) {+ |
|
430 | +725 | ! |
- if (inherits(modules, class)) {+ repeat { |
431 | +726 | ! |
- NULL+ Sys.sleep(check_interval / 1000) |
432 | +727 | ! |
- } else if (inherits(modules, "teal_module")) {+ current_content <- self$get_html("body")+ |
+
728 | ++ | + | |
433 | +729 | ! |
- modules+ if (!identical(previous_content, current_content)) { |
434 | +730 | ! |
- } else if (inherits(modules, "teal_modules")) {+ previous_content <- current_content |
435 | +731 | ! |
- do.call(+ end_time <- Sys.time() + (stability_period / 1000) |
436 | +732 | ! |
- "modules",+ } else if (Sys.time() >= end_time) { |
437 | +733 | ! |
- c(Filter(function(x) length(x) > 0L, lapply(modules$children, drop_module, class)), label = modules$label)+ break |
438 | +734 |
- )+ } |
|
439 | +735 |
- }+ } |
|
440 | +736 |
- }+ } |
|
441 | +737 |
-
+ ) |
|
442 | +738 |
- ## read modules ----+ ) |
443 | +1 |
-
+ setOldClass("teal_module") |
|
444 | +2 |
- #' Does the object make use of the `arg`+ setOldClass("teal_modules") |
|
445 | +3 |
- #'+ |
|
446 | +4 |
- #' @param modules (`teal_module` or `teal_modules`) object+ #' Create `teal_module` and `teal_modules` objects |
|
447 | +5 |
- #' @param arg (`character(1)`) names of the arguments to be checked against formals of `teal` modules.+ #' |
|
448 | +6 |
- #' @return `logical` whether the object makes use of `arg`.+ #' @description |
|
449 | +7 |
- #' @rdname is_arg_used+ #' `r lifecycle::badge("stable")` |
|
450 | +8 |
- #' @keywords internal+ #' Create a nested tab structure to embed modules in a `teal` application. |
|
451 | +9 |
- is_arg_used <- function(modules, arg) {- |
- |
452 | -457x - | -
- checkmate::assert_string(arg)+ #' |
|
453 | -454x - | +||
10 | +
- if (inherits(modules, "teal_modules")) {+ #' @details |
||
454 | -17x - | +||
11 | +
- any(unlist(lapply(modules$children, is_arg_used, arg)))+ #' `module()` creates an instance of a `teal_module` that can be placed in a `teal` application. |
||
455 | -437x - | +||
12 | +
- } else if (inherits(modules, "teal_module")) {+ #' `modules()` shapes the structure of a the application by organizing `teal_module` within the navigation panel. |
||
456 | -29x - | +||
13 | +
- is_arg_used(modules$server, arg) || is_arg_used(modules$ui, arg)+ #' It wraps `teal_module` and `teal_modules` objects in a `teal_modules` object, |
||
457 | -408x - | +||
14 | +
- } else if (is.function(modules)) {+ #' which results in a nested structure corresponding to the nested tabs in the final application. |
||
458 | -406x - | +||
15 | +
- isTRUE(arg %in% names(formals(modules)))+ #' |
||
459 | +16 |
- } else {+ #' Note that for `modules()` `label` comes after `...`, so it must be passed as a named argument, |
|
460 | -2x - | +||
17 | +
- stop("is_arg_used function not implemented for this object")+ #' otherwise it will be captured by `...`. |
||
461 | +18 |
- }+ #' |
|
462 | +19 |
- }+ #' The labels `"global_filters"` and `"Report previewer"` are reserved |
|
463 | +20 |
-
+ #' because they are used by the `mapping` argument of [teal_slices()] |
|
464 | +21 |
-
+ #' and the report previewer module [reporter_previewer_module()], respectively. |
|
465 | +22 |
- #' Get module depth+ #' |
|
466 | +23 |
- #'+ #' # Restricting datasets used by `teal_module`: |
|
467 | +24 |
- #' Depth starts at 0, so a single `teal.module` has depth 0.+ #' The `datanames` argument controls which datasets are used by the module’s server. These datasets, |
|
468 | +25 |
- #' Nesting it increases overall depth by 1.+ #' passed via server's `data` argument, are the only ones shown in the module's tab. |
|
469 | +26 |
#' |
|
470 | +27 |
- #' @inheritParams init+ #' When `datanames` is set to `"all"`, all datasets in the data object are treated as relevant. |
|
471 | +28 |
- #' @param depth optional integer determining current depth level+ #' However, this may include unnecessary datasets, such as: |
|
472 | +29 |
- #'+ #' - Proxy variables for column modifications |
|
473 | +30 |
- #' @return Depth level for given module.+ #' - Temporary datasets used to create final versions |
|
474 | +31 |
- #' @keywords internal+ #' - Connection objects |
|
475 | +32 |
- modules_depth <- function(modules, depth = 0L) {+ #' |
|
476 | -12x - | +||
33 | +
- checkmate::assert_multi_class(modules, c("teal_module", "teal_modules"))+ #' To exclude irrelevant datasets, use the [set_datanames()] function to change `datanames` from |
||
477 | -12x - | +||
34 | +
- checkmate::assert_int(depth, lower = 0)+ #' `"all"` to specific names. Trying to modify non-`"all"` values with [set_datanames()] will result |
||
478 | -11x - | +||
35 | +
- if (inherits(modules, "teal_modules")) {+ #' in a warning. Datasets with names starting with . are ignored globally unless explicitly listed |
||
479 | -4x - | +||
36 | +
- max(vapply(modules$children, modules_depth, integer(1), depth = depth + 1L))+ #' in `datanames`. |
||
480 | +37 |
- } else {+ #' |
|
481 | -7x - | +||
38 | +
- depth+ #' # `datanames` with `transformers` |
||
482 | +39 |
- }+ #' When transformers are specified, their `datanames` are added to the module’s `datanames`, which |
|
483 | +40 |
- }+ #' changes the behavior as follows: |
|
484 | +41 |
-
+ #' - If `module(datanames)` is `NULL` and the `transformers` have defined `datanames`, the sidebar |
|
485 | +42 |
- #' Retrieve labels from `teal_modules`+ #' will appear showing the `transformers`' datasets, instead of being hidden. |
|
486 | +43 |
- #'+ #' - If `module(datanames)` is set to specific values and any `transformer` has `datanames = "all"`, |
|
487 | +44 |
- #' @param modules (`teal_modules`)+ #' the module may receive extra datasets that could be unnecessary |
|
488 | +45 |
- #' @return A `list` containing the labels of the modules. If the modules are nested,+ #' |
|
489 | +46 |
- #' the function returns a nested `list` of labels.+ #' @param label (`character(1)`) Label shown in the navigation item for the module or module group. |
|
490 | +47 |
- #' @keywords internal+ #' For `modules()` defaults to `"root"`. See `Details`. |
|
491 | +48 |
- module_labels <- function(modules) {+ #' @param server (`function`) `shiny` module with following arguments: |
|
492 | -179x - | +||
49 | +
- if (inherits(modules, "teal_modules")) {+ #' - `id` - `teal` will set proper `shiny` namespace for this module (see [shiny::moduleServer()]). |
||
493 | -77x - | +||
50 | +
- lapply(modules$children, module_labels)+ #' - `input`, `output`, `session` - (optional; not recommended) When provided, then [shiny::callModule()] |
||
494 | +51 |
- } else {+ #' will be used to call a module. From `shiny` 1.5.0, the recommended way is to use |
|
495 | -102x - | +||
52 | +
- modules$label+ #' [shiny::moduleServer()] instead which doesn't require these arguments. |
||
496 | +53 |
- }+ #' - `data` (optional) When provided, the module will be called with `teal_data` object (i.e. a list of |
|
497 | +54 |
- }+ #' reactive (filtered) data specified in the `filters` argument) as the value of this argument. |
|
498 | +55 |
-
+ #' - `datasets` (optional) When provided, the module will be called with `FilteredData` object as the |
|
499 | +56 |
- #' Retrieve `teal_bookmarkable` attribute from `teal_modules`+ #' value of this argument. (See [`teal.slice::FilteredData`]). |
|
500 | +57 |
- #'+ #' - `reporter` (optional) When provided, the module will be called with `Reporter` object as the value |
|
501 | +58 |
- #' @param modules (`teal_modules` or `teal_module`) object+ #' of this argument. (See [`teal.reporter::Reporter`]). |
|
502 | +59 |
- #' @return named list of the same structure as `modules` with `TRUE` or `FALSE` values indicating+ #' - `filter_panel_api` (optional) When provided, the module will be called with `FilterPanelAPI` object |
|
503 | +60 |
- #' whether the module is bookmarkable.+ #' as the value of this argument. (See [`teal.slice::FilterPanelAPI`]). |
|
504 | +61 |
- #' @keywords internal+ #' - `...` (optional) When provided, `server_args` elements will be passed to the module named argument |
|
505 | +62 |
- modules_bookmarkable <- function(modules) {+ #' or to the `...`. |
|
506 | -179x - | +||
63 | +
- checkmate::assert_multi_class(modules, c("teal_modules", "teal_module"))+ #' @param ui (`function`) `shiny` UI module function with following arguments: |
||
507 | -179x - | +||
64 | +
- if (inherits(modules, "teal_modules")) {+ #' - `id` - `teal` will set proper `shiny` namespace for this module. |
||
508 | -77x - | +||
65 | +
- setNames(+ #' - `...` (optional) When provided, `ui_args` elements will be passed to the module named argument |
||
509 | -77x - | +||
66 | +
- lapply(modules$children, modules_bookmarkable),+ #' or to the `...`. |
||
510 | -77x - | +||
67 | +
- vapply(modules$children, `[[`, "label", FUN.VALUE = character(1))+ #' @param filters (`character`) Deprecated. Use `datanames` instead. |
||
511 | +68 |
- )+ #' @param datanames (`character`) Names of the datasets relevant to the item. |
|
512 | +69 |
- } else {+ #' There are 2 reserved values that have specific behaviors: |
|
513 | -102x - | +||
70 | +
- attr(modules, "teal_bookmarkable", exact = TRUE)+ #' - The keyword `"all"` includes all datasets available in the data passed to the teal application. |
||
514 | +71 |
- }+ #' - `NULL` hides the sidebar panel completely. |
|
515 | +72 |
- }+ #' - If `transformers` are specified, their `datanames` are automatically added to this `datanames` |
1 | +73 |
- #' Landing popup module+ #' argument. |
|
2 | +74 |
- #'+ #' @param server_args (named `list`) with additional arguments passed on to the server function. |
|
3 | +75 |
- #' @description Creates a landing welcome popup for `teal` applications.+ #' @param ui_args (named `list`) with additional arguments passed on to the UI function. |
|
4 | +76 |
- #'+ #' @param x (`teal_module` or `teal_modules`) Object to format/print. |
|
5 | +77 |
- #' This module is used to display a popup dialog when the application starts.+ #' @param indent (`integer(1)`) Indention level; each nested element is indented one level more. |
|
6 | +78 |
- #' The dialog blocks access to the application and must be closed with a button before the application can be viewed.+ #' @param transformers (`list` of `teal_data_module`) that will be applied to transform the data. |
|
7 | +79 |
- #'+ #' Each transform module UI will appear in the `teal`'s sidebar panel. |
|
8 | +80 |
- #' @param label (`character(1)`) Label of the module.+ #' Transformers' `datanames` are added to the `datanames`. See [teal_transform_module()]. |
|
9 | +81 |
- #' @param title (`character(1)`) Text to be displayed as popup title.+ #' |
|
10 | +82 |
- #' @param content (`character(1)`, `shiny.tag` or `shiny.tag.list`) with the content of the popup.+ #' @param ... |
|
11 | +83 |
- #' Passed to `...` of `shiny::modalDialog`. See examples.+ #' - For `modules()`: (`teal_module` or `teal_modules`) Objects to wrap into a tab. |
|
12 | +84 |
- #' @param buttons (`shiny.tag` or `shiny.tag.list`) Typically a `modalButton` or `actionButton`. See examples.+ #' - For `format()` and `print()`: Arguments passed to other methods. |
|
13 | +85 |
#' |
|
14 | +86 |
- #' @return A `teal_module` (extended with `teal_landing_module` class) to be used in `teal` applications.+ #' @return |
|
15 | +87 |
- #'+ #' `module()` returns an object of class `teal_module`. |
|
16 | +88 |
- #' @examples+ #' |
|
17 | +89 |
- #' app1 <- init(+ #' `modules()` returns a `teal_modules` object which contains following fields: |
|
18 | +90 |
- #' data = teal_data(iris = iris),+ #' - `label`: taken from the `label` argument. |
|
19 | +91 |
- #' modules = modules(+ #' - `children`: a list containing objects passed in `...`. List elements are named after |
|
20 | +92 |
- #' example_module()+ #' their `label` attribute converted to a valid `shiny` id. |
|
21 | +93 |
- #' ),+ #' |
|
22 | +94 |
- #' landing_popup = landing_popup_module(+ #' @name teal_modules |
|
23 | +95 |
- #' content = "A place for the welcome message or a disclaimer statement.",+ #' @aliases teal_module |
|
24 | +96 |
- #' buttons = modalButton("Proceed")+ #' |
|
25 | +97 |
- #' )+ #' @examples |
|
26 | +98 |
- #' )+ #' library(shiny) |
|
27 | +99 |
- #' if (interactive()) {+ #' |
|
28 | +100 |
- #' shinyApp(app1$ui, app1$server)+ #' module_1 <- module( |
|
29 | +101 |
- #' }+ #' label = "a module", |
|
30 | +102 |
- #'+ #' server = function(id, data) { |
|
31 | +103 |
- #' app2 <- init(+ #' moduleServer( |
|
32 | +104 |
- #' data = teal_data(iris = iris),+ #' id, |
|
33 | +105 |
- #' modules = modules(+ #' module = function(input, output, session) { |
|
34 | +106 |
- #' example_module()+ #' output$data <- renderDataTable(data()[["iris"]]) |
|
35 | +107 |
- #' ),+ #' } |
|
36 | +108 |
- #' landing_popup = landing_popup_module(+ #' ) |
|
37 | +109 |
- #' title = "Welcome",+ #' }, |
|
38 | +110 |
- #' content = tags$b(+ #' ui = function(id) { |
|
39 | +111 |
- #' "A place for the welcome message or a disclaimer statement.",+ #' ns <- NS(id) |
|
40 | +112 |
- #' style = "color: red;"+ #' tagList(dataTableOutput(ns("data"))) |
|
41 | +113 |
- #' ),+ #' }, |
|
42 | +114 |
- #' buttons = tagList(+ #' datanames = "all" |
|
43 | +115 |
- #' modalButton("Proceed"),+ #' ) |
|
44 | +116 |
- #' actionButton("read", "Read more",+ #' |
|
45 | +117 |
- #' onclick = "window.open('http://google.com', '_blank')"+ #' module_2 <- module( |
|
46 | +118 |
- #' ),+ #' label = "another module", |
|
47 | +119 |
- #' actionButton("close", "Reject", onclick = "window.close()")+ #' server = function(id) { |
|
48 | +120 |
- #' )+ #' moduleServer( |
|
49 | +121 |
- #' )+ #' id, |
|
50 | +122 |
- #' )+ #' module = function(input, output, session) { |
|
51 | +123 |
- #'+ #' output$text <- renderText("Another Module") |
|
52 | +124 |
- #' if (interactive()) {+ #' } |
|
53 | +125 |
- #' shinyApp(app2$ui, app2$server)+ #' ) |
|
54 | +126 |
- #' }+ #' }, |
|
55 | +127 |
- #'+ #' ui = function(id) { |
|
56 | +128 |
- #' @export+ #' ns <- NS(id) |
|
57 | +129 |
- landing_popup_module <- function(label = "Landing Popup",+ #' tagList(textOutput(ns("text"))) |
|
58 | +130 |
- title = NULL,+ #' }, |
|
59 | +131 |
- content = NULL,+ #' datanames = NULL |
|
60 | +132 |
- buttons = modalButton("Accept")) {- |
- |
61 | -! | -
- checkmate::assert_string(label)- |
- |
62 | -! | -
- checkmate::assert_string(title, null.ok = TRUE)- |
- |
63 | -! | -
- checkmate::assert_multi_class(- |
- |
64 | -! | -
- content,- |
- |
65 | -! | -
- classes = c("character", "shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE+ #' ) |
|
66 | +133 |
- )- |
- |
67 | -! | -
- checkmate::assert_multi_class(buttons, classes = c("shiny.tag", "shiny.tag.list"))+ #' |
|
68 | +134 | - - | -|
69 | -! | -
- message("Initializing landing_popup_module")+ #' modules <- modules( |
|
70 | +135 | - - | -|
71 | -! | -
- module <- module(- |
- |
72 | -! | -
- label = label,- |
- |
73 | -! | -
- server = function(id) {- |
- |
74 | -! | -
- moduleServer(id, function(input, output, session) {- |
- |
75 | -! | -
- showModal(- |
- |
76 | -! | -
- modalDialog(- |
- |
77 | -! | -
- id = "landingpopup",- |
- |
78 | -! | -
- title = title,- |
- |
79 | -! | -
- content,- |
- |
80 | -! | -
- footer = buttons+ #' label = "modules", |
|
81 | +136 |
- )+ #' modules( |
|
82 | +137 |
- )+ #' label = "nested modules", |
|
83 | +138 |
- })+ #' module_1 |
|
84 | +139 |
- }+ #' ), |
|
85 | +140 |
- )- |
- |
86 | -! | -
- class(module) <- c("teal_module_landing", class(module))- |
- |
87 | -! | -
- module+ #' module_2 |
|
88 | +141 |
- }+ #' ) |
1 | +142 |
- #' Manage multiple `FilteredData` objects+ #' |
||
2 | +143 |
- #'+ #' app <- init( |
||
3 | +144 |
- #' @description+ #' data = teal_data(iris = iris), |
||
4 | +145 |
- #' Oversee filter states across the entire application.+ #' modules = modules |
||
5 | +146 |
- #'+ #' ) |
||
6 | +147 |
- #' @section Slices global:+ #' |
||
7 | +148 |
- #' The key role in maintaining the module-specific filter states is played by the `.slicesGlobal`+ #' if (interactive()) { |
||
8 | +149 |
- #' object. It is a reference class that holds the following fields:+ #' shinyApp(app$ui, app$server) |
||
9 | +150 |
- #' - `all_slices` (`reactiveVal`) - reactive value containing all filters registered in an app.+ #' } |
||
10 | +151 |
- #' - `module_slices_api` (`reactiveValues`) - reactive field containing references to each modules'+ #' @rdname teal_modules |
||
11 | +152 |
- #' `FilteredData` object methods. At this moment it is used only in `srv_filter_manager` to display+ #' @export |
||
12 | +153 |
- #' the filter states in a table combining informations from `all_slices` and from+ #' |
||
13 | +154 |
- #' `FilteredData$get_available_teal_slices()`.+ module <- function(label = "module", |
||
14 | +155 |
- #'+ server = function(id, data, ...) moduleServer(id, function(input, output, session) NULL), |
||
15 | +156 |
- #' During a session only new filters are added to `all_slices` unless [`module_snapshot_manager`] is+ ui = function(id, ...) tags$p(paste0("This module has no UI (id: ", id, " )")), |
||
16 | +157 |
- #' used to restore previous state. Filters from `all_slices` can be activated or deactivated in a+ filters, |
||
17 | +158 |
- #' module which is linked (both ways) by `attr(, "mapping")` so that:+ datanames = "all", |
||
18 | +159 |
- #' - If module's filter is added or removed in its `FilteredData` object, this information is passed+ server_args = NULL, |
||
19 | +160 |
- #' to `SlicesGlobal` which updates `attr(, "mapping")` accordingly.+ ui_args = NULL, |
||
20 | +161 |
- #' - When mapping changes in a `SlicesGlobal`, filters are set or removed from module's+ transformers = list()) { |
||
21 | +162 |
- #' `FilteredData`.+ # argument checking (independent) |
||
22 | +163 |
- #'+ ## `label` |
||
23 | -+ | |||
164 | +211x |
- #' @section Filter manager:+ checkmate::assert_string(label) |
||
24 | -+ | |||
165 | +208x |
- #' Filter-manager is split into two parts:+ if (label == "global_filters") { |
||
25 | -+ | |||
166 | +1x |
- #' 1. `ui/srv_filter_manager_panel` - Called once for the whole app. This module observes changes in+ stop( |
||
26 | -+ | |||
167 | +1x |
- #' the filters in `slices_global` and displays them in a table utilizing information from `mapping`:+ sprintf("module(label = \"%s\", ...\n ", label), |
||
27 | -+ | |||
168 | +1x |
- #' - (`TRUE`) - filter is active in the module+ "Label 'global_filters' is reserved in teal. Please change to something else.", |
||
28 | -+ | |||
169 | +1x |
- #' - (`FALSE`) - filter is inactive in the module+ call. = FALSE |
||
29 | +170 |
- #' - (`NA`) - filter is not available in the module+ ) |
||
30 | +171 |
- #' 2. `ui/srv_module_filter_manager` - Called once for each `teal_module`. Handling filter states+ } |
||
31 | -+ | |||
172 | +207x |
- #' for of single module and keeping module `FilteredData` consistent with `slices_global`, so that+ if (label == "Report previewer") { |
||
32 | -+ | |||
173 | +! |
- #' local filters are always reflected in the `slices_global` and its mapping and vice versa.+ stop( |
||
33 | -+ | |||
174 | +! |
- #'+ sprintf("module(label = \"%s\", ...\n ", label), |
||
34 | -+ | |||
175 | +! |
- #'+ "Label 'Report previewer' is reserved in teal. Please change to something else.", |
||
35 | -+ | |||
176 | +! |
- #' @param id (`character(1)`)+ call. = FALSE |
||
36 | +177 |
- #' `shiny` module instance id.+ ) |
||
37 | +178 |
- #'+ } |
||
38 | +179 |
- #' @param slices_global (`reactiveVal`)+ |
||
39 | +180 |
- #' containing `teal_slices`.+ ## server |
||
40 | -+ | |||
181 | +207x |
- #'+ checkmate::assert_function(server) |
||
41 | -+ | |||
182 | +207x |
- #' @param module_fd (`FilteredData`)+ server_formals <- names(formals(server)) |
||
42 | -+ | |||
183 | +207x |
- #' Object containing the data to be filtered in a single `teal` module.+ if (!( |
||
43 | -+ | |||
184 | +207x |
- #'+ "id" %in% server_formals || |
||
44 | -+ | |||
185 | +207x |
- #' @return+ all(c("input", "output", "session") %in% server_formals) |
||
45 | +186 |
- #' Module returns a `slices_global` (`reactiveVal`) containing a `teal_slices` object with mapping.+ )) { |
||
46 | -+ | |||
187 | +2x |
- #'+ stop( |
||
47 | -+ | |||
188 | +2x |
- #' @encoding UTF-8+ "\nmodule() `server` argument requires a function with following arguments:", |
||
48 | -+ | |||
189 | +2x |
- #'+ "\n - id - `teal` will set proper `shiny` namespace for this module.", |
||
49 | -+ | |||
190 | +2x |
- #' @name module_filter_manager+ "\n - input, output, session (not recommended) - then `shiny::callModule` will be used to call a module.", |
||
50 | -+ | |||
191 | +2x |
- #' @rdname module_filter_manager+ "\n\nFollowing arguments can be used optionaly:", |
||
51 | -+ | |||
192 | +2x |
- #'+ "\n - `data` - module will receive list of reactive (filtered) data specified in the `filters` argument", |
||
52 | -+ | |||
193 | +2x |
- NULL+ "\n - `datasets` - module will receive `FilteredData`. See `help(teal.slice::FilteredData)`", |
||
53 | -+ | |||
194 | +2x |
-
+ "\n - `reporter` - module will receive `Reporter`. See `help(teal.reporter::Reporter)`", |
||
54 | -+ | |||
195 | +2x |
- #' @rdname module_filter_manager+ "\n - `filter_panel_api` - module will receive `FilterPanelAPI`. (See [teal.slice::FilterPanelAPI]).",+ |
+ ||
196 | +2x | +
+ "\n - `...` server_args elements will be passed to the module named argument or to the `...`" |
||
55 | +197 |
- ui_filter_manager_panel <- function(id) {+ ) |
||
56 | -! | +|||
198 | +
- ns <- NS(id)+ } |
|||
57 | -! | +|||
199 | +205x |
- tags$button(+ if ("datasets" %in% server_formals) { |
||
58 | -! | +|||
200 | +2x |
- id = ns("show_filter_manager"),+ warning( |
||
59 | -! | +|||
201 | +2x |
- class = "btn action-button wunder_bar_button",+ sprintf("Called from module(label = \"%s\", ...)\n ", label), |
||
60 | -! | +|||
202 | +2x |
- title = "View filter mapping",+ "`datasets` argument in the server is deprecated and will be removed in the next release. ", |
||
61 | -! | +|||
203 | +2x |
- suppressMessages(icon("fas fa-grip"))+ "Please use `data` instead.", |
||
62 | -+ | |||
204 | +2x |
- )+ call. = FALSE |
||
63 | +205 |
- }+ ) |
||
64 | +206 |
-
+ } |
||
65 | +207 |
- #' @rdname module_filter_manager+ |
||
66 | +208 |
- #' @keywords internal+ |
||
67 | +209 |
- srv_filter_manager_panel <- function(id, slices_global) {+ ## UI |
||
68 | -77x - | +210 | +205x |
- checkmate::assert_string(id)+ checkmate::assert_function(ui) |
69 | -77x - | +211 | +205x |
- checkmate::assert_class(slices_global, ".slicesGlobal")+ ui_formals <- names(formals(ui)) |
70 | -77x - | +212 | +205x |
- moduleServer(id, function(input, output, session) {+ if (!"id" %in% ui_formals) { |
71 | -77x - | +213 | +1x |
- setBookmarkExclude(c("show_filter_manager"))+ stop( |
72 | -77x - | -
- observeEvent(input$show_filter_manager, {- |
- ||
73 | -! | +214 | +1x |
- logger::log_debug("srv_filter_manager_panel@1 show_filter_manager button has been clicked.")+ "\nmodule() `ui` argument requires a function with following arguments:", |
74 | -! | +|||
215 | +1x |
- showModal(+ "\n - id - `teal` will set proper `shiny` namespace for this module.", |
||
75 | -! | +|||
216 | +1x |
- modalDialog(+ "\n\nFollowing arguments can be used optionally:", |
||
76 | -! | +|||
217 | +1x |
- ui_filter_manager(session$ns("filter_manager")),+ "\n - `...` ui_args elements will be passed to the module argument of the same name or to the `...`" |
||
77 | -! | +|||
218 | +
- class = "filter_manager_modal",+ ) |
|||
78 | -! | +|||
219 | +
- size = "l",+ } |
|||
79 | -! | +|||
220 | +204x |
- footer = NULL,+ if (any(c("data", "datasets") %in% ui_formals)) { |
||
80 | -! | +|||
221 | +2x |
- easyClose = TRUE+ stop( |
||
81 | -+ | |||
222 | +2x |
- )+ sprintf("Called from module(label = \"%s\", ...)\n ", label), |
||
82 | -+ | |||
223 | +2x |
- )+ "UI with `data` or `datasets` argument is no longer accepted.\n ", |
||
83 | -+ | |||
224 | +2x |
- })+ "If some UI inputs depend on data, please move the logic to your server instead.\n ", |
||
84 | -77x - | +225 | +2x |
- srv_filter_manager("filter_manager", slices_global = slices_global)+ "Possible solutions are renderUI() or updateXyzInput() functions." |
85 | +226 |
- })+ ) |
||
86 | +227 |
- }+ } |
||
87 | +228 | |||
88 | +229 |
- #' @rdname module_filter_manager+ |
||
89 | +230 |
- ui_filter_manager <- function(id) {+ ## `filters` |
||
90 | -! | +|||
231 | +202x |
- ns <- NS(id)+ if (!missing(filters)) { |
||
91 | +232 | ! |
- actionButton(ns("filter_manager"), NULL, icon = icon("fas fa-filter"))+ datanames <- filters |
|
92 | +233 | ! |
- tags$div(+ msg <- |
|
93 | +234 | ! |
- class = "filter_manager_content",+ "The `filters` argument is deprecated and will be removed in the next release. Please use `datanames` instead." |
|
94 | +235 | ! |
- tableOutput(ns("slices_table"))+ warning(msg) |
|
95 | +236 |
- )+ } |
||
96 | +237 |
- }+ |
||
97 | +238 |
-
+ ## `datanames` (also including deprecated `filters`) |
||
98 | +239 |
- #' @rdname module_filter_manager+ # please note a race condition between datanames set when filters is not missing and data arg in server function |
||
99 | -+ | |||
240 | +202x |
- srv_filter_manager <- function(id, slices_global) {+ if (!is.element("data", server_formals) && !is.null(datanames)) { |
||
100 | -77x - | +241 | +12x |
- checkmate::assert_string(id)+ message(sprintf("module \"%s\" server function takes no data so \"datanames\" will be ignored", label)) |
101 | -77x - | +242 | +12x |
- checkmate::assert_class(slices_global, ".slicesGlobal")+ datanames <- NULL |
102 | +243 | - - | -||
103 | -77x - | -
- moduleServer(id, function(input, output, session) {+ } |
||
104 | -77x - | +244 | +202x |
- logger::log_debug("filter_manager_srv initializing.")+ checkmate::assert_character(datanames, min.len = 1, null.ok = TRUE, any.missing = FALSE) |
105 | +245 | |||
106 | +246 |
- # Bookmark slices global with mapping.+ ## `server_args` |
||
107 | -77x - | +247 | +201x |
- session$onBookmark(function(state) {+ checkmate::assert_list(server_args, null.ok = TRUE, names = "named") |
108 | -! | +|||
248 | +199x |
- logger::log_debug("filter_manager_srv@onBookmark: storing filter state")+ srv_extra_args <- setdiff(names(server_args), server_formals) |
||
109 | -! | +|||
249 | +199x |
- state$values$filter_state_on_bookmark <- as.list(+ if (length(srv_extra_args) > 0 && !"..." %in% server_formals) { |
||
110 | -! | +|||
250 | +1x |
- slices_global$all_slices(),+ stop( |
||
111 | -! | +|||
251 | +1x |
- recursive = TRUE+ "\nFollowing `server_args` elements have no equivalent in the formals of the server:\n",+ |
+ ||
252 | +1x | +
+ paste(paste(" -", srv_extra_args), collapse = "\n"),+ |
+ ||
253 | +1x | +
+ "\n\nUpdate the server arguments by including above or add `...`" |
||
112 | +254 |
- )+ ) |
||
113 | +255 |
- })+ } |
||
114 | +256 | |||
257 | ++ |
+ ## `ui_args`+ |
+ ||
115 | -77x - | +258 | +198x |
- bookmarked_slices <- restoreValue(session$ns("filter_state_on_bookmark"), NULL)+ checkmate::assert_list(ui_args, null.ok = TRUE, names = "named") |
116 | -77x - | +259 | +196x |
- if (!is.null(bookmarked_slices)) {+ ui_extra_args <- setdiff(names(ui_args), ui_formals) |
117 | -! | +|||
260 | +196x |
- logger::log_debug("filter_manager_srv: restoring filter state from bookmark.")+ if (length(ui_extra_args) > 0 && !"..." %in% ui_formals) { |
||
118 | -! | +|||
261 | +1x |
- slices_global$slices_set(bookmarked_slices)+ stop( |
||
119 | -+ | |||
262 | +1x |
- }+ "\nFollowing `ui_args` elements have no equivalent in the formals of UI:\n", |
||
120 | -+ | |||
263 | +1x |
-
+ paste(paste(" -", ui_extra_args), collapse = "\n"), |
||
121 | -77x - | +264 | +1x |
- mapping_table <- reactive({+ "\n\nUpdate the UI arguments by including above or add `...`" |
122 | +265 |
- # We want this to be reactive on slices_global$all_slices() only as get_available_teal_slices()+ ) |
||
123 | +266 |
- # is dependent on slices_global$all_slices().+ } |
||
124 | -86x - | +|||
267 | +
- module_labels <- setdiff(+ + |
+ |||
268 | ++ |
+ ## `transformers` |
||
125 | -86x - | +269 | +195x |
- names(attr(slices_global$all_slices(), "mapping")),+ if (inherits(transformers, "teal_transform_module")) { |
126 | -86x - | +270 | +1x |
- "Report previewer"+ transformers <- list(transformers) |
127 | +271 |
- )+ } |
||
128 | -86x - | +272 | +195x |
- isolate({+ checkmate::assert_list(transformers, types = "teal_transform_module") |
129 | -86x - | +273 | +195x |
- mm <- as.data.frame(+ transformer_datanames <- unlist(lapply(transformers, attr, "datanames")) |
130 | -86x - | +274 | +195x |
- sapply(+ combined_datanames <- if (identical(datanames, "all")) { |
131 | -86x - | +275 | +142x |
- module_labels,+ "all" |
132 | -86x - | +|||
276 | +
- simplify = FALSE,+ } else { |
|||
133 | -86x - | +277 | +53x |
- function(module_label) {+ union(datanames, transformer_datanames) |
134 | -99x - | +|||
278 | +
- available_slices <- slices_global$module_slices_api[[module_label]]$get_available_teal_slices()+ } |
|||
135 | -91x - | +|||
279 | +
- global_ids <- sapply(slices_global$all_slices(), `[[`, "id", simplify = FALSE)+ |
|||
136 | -91x - | +280 | +195x |
- module_ids <- sapply(slices_global$slices_get(module_label), `[[`, "id", simplify = FALSE)+ structure( |
137 | -91x - | +281 | +195x |
- allowed_ids <- vapply(available_slices, `[[`, character(1L), "id")+ list( |
138 | -91x - | +282 | +195x |
- active_ids <- global_ids %in% module_ids+ label = label, |
139 | -91x - | +283 | +195x |
- setNames(nm = global_ids, ifelse(global_ids %in% allowed_ids, active_ids, NA))+ server = server, |
140 | -+ | |||
284 | +195x |
- }+ ui = ui, |
||
141 | -+ | |||
285 | +195x |
- ),+ datanames = combined_datanames, |
||
142 | -86x - | +286 | +195x |
- check.names = FALSE+ server_args = server_args, |
143 | -+ | |||
287 | +195x |
- )+ ui_args = ui_args, |
||
144 | -78x - | +288 | +195x |
- colnames(mm)[colnames(mm) == "global_filters"] <- "Global filters"+ transformers = transformers |
145 | +289 |
-
+ ), |
||
146 | -78x - | +290 | +195x |
- mm+ class = "teal_module" |
147 | +291 |
- })+ ) |
||
148 | +292 |
- })+ } |
||
149 | +293 | |||
294 | ++ |
+ #' @rdname teal_modules+ |
+ ||
295 | ++ |
+ #' @export+ |
+ ||
296 | ++ |
+ #'+ |
+ ||
297 | ++ |
+ modules <- function(..., label = "root") {+ |
+ ||
150 | -77x - | +298 | +135x |
- output$slices_table <- renderTable(+ checkmate::assert_string(label) |
151 | -77x - | +299 | +133x |
- expr = {+ submodules <- list(...) |
152 | -86x - | +300 | +133x |
- logger::log_debug("filter_manager_srv@1 rendering slices_table.")+ if (any(vapply(submodules, is.character, FUN.VALUE = logical(1)))) { |
153 | -86x - | +301 | +2x |
- mm <- mapping_table()+ stop(+ |
+
302 | +2x | +
+ "The only character argument to modules() must be 'label' and it must be named, ",+ |
+ ||
303 | +2x | +
+ "change modules('lab', ...) to modules(label = 'lab', ...)" |
||
154 | +304 |
-
+ ) |
||
155 | +305 |
- # Display logical values as UTF characters.+ } |
||
156 | -78x - | +|||
306 | +
- mm[] <- lapply(mm, ifelse, yes = intToUtf8(9989), no = intToUtf8(10060))+ |
|||
157 | -78x - | +307 | +131x |
- mm[] <- lapply(mm, function(x) ifelse(is.na(x), intToUtf8(128306), x))+ checkmate::assert_list(submodules, min.len = 1, any.missing = FALSE, types = c("teal_module", "teal_modules")) |
158 | +308 |
-
+ # name them so we can more easily access the children |
||
159 | +309 |
- # Display placeholder if no filters defined.+ # beware however that the label of the submodules should not be changed as it must be kept synced |
||
160 | -78x - | +310 | +128x |
- if (nrow(mm) == 0L) {+ labels <- vapply(submodules, function(submodule) submodule$label, character(1)) |
161 | -54x - | +311 | +128x |
- mm <- data.frame(`Filter manager` = "No filters specified.", check.names = FALSE)+ names(submodules) <- get_unique_labels(labels) |
162 | -54x - | +312 | +128x |
- rownames(mm) <- ""+ structure( |
163 | -+ | |||
313 | +128x |
- }+ list( |
||
164 | -78x - | +314 | +128x |
- mm+ label = label,+ |
+
315 | +128x | +
+ children = submodules |
||
165 | +316 |
- },+ ), |
||
166 | -77x - | +317 | +128x |
- rownames = TRUE+ class = "teal_modules" |
167 | +318 |
- )+ ) |
||
168 | +319 |
-
+ } |
||
169 | -77x - | +|||
320 | +
- mapping_table # for testing purpose+ |
|||
170 | +321 |
- })+ # printing methods ---- |
||
171 | +322 |
- }+ |
||
172 | +323 |
-
+ #' @rdname teal_modules |
||
173 | +324 |
- #' @rdname module_filter_manager+ #' @export |
||
174 | +325 |
- srv_module_filter_manager <- function(id, module_fd, slices_global) {+ format.teal_module <- function(x, indent = 0, ...) { |
||
175 | -102x - | +326 | +3x |
- checkmate::assert_string(id)+ paste0(paste(rep(" ", indent), collapse = ""), "+ ", x$label, "\n", collapse = "") |
176 | -102x - | +|||
327 | +
- assert_reactive(module_fd)+ } |
|||
177 | -102x - | +|||
328 | +
- checkmate::assert_class(slices_global, ".slicesGlobal")+ |
|||
178 | +329 | |||
179 | -102x - | +|||
330 | +
- moduleServer(id, function(input, output, session) {+ #' @rdname teal_modules |
|||
180 | -102x - | +|||
331 | +
- logger::log_debug("srv_module_filter_manager initializing for module: { id }.")+ #' @export |
|||
181 | +332 |
- # Track filter global and local states.+ print.teal_module <- function(x, ...) { |
||
182 | -102x - | +|||
333 | +! |
- slices_global_module <- reactive({+ cat(format(x, ...)) |
||
183 | -183x - | +|||
334 | +! |
- slices_global$slices_get(module_label = id)+ invisible(x) |
||
184 | +335 |
- })+ } |
||
185 | -102x - | +|||
336 | +
- slices_module <- reactive(req(module_fd())$get_filter_state())+ |
|||
186 | +337 | |||
187 | -102x - | +|||
338 | +
- module_fd_previous <- reactiveVal(NULL)+ #' @rdname teal_modules |
|||
188 | +339 |
-
+ #' @export |
||
189 | +340 |
- # Set (reactively) available filters for the module.+ format.teal_modules <- function(x, indent = 0, ...) { |
||
190 | -102x - | +341 | +1x |
- obs1 <- observeEvent(module_fd(), priority = 1, {+ paste( |
191 | -83x - | +342 | +1x |
- logger::log_debug("srv_module_filter_manager@1 setting initial slices for module: { id }.")+ c( |
192 | -+ | |||
343 | +1x |
- # Filters relevant for the module in module-specific app.+ paste0(rep(" ", indent), "+ ", x$label, "\n"), |
||
193 | -83x - | +344 | +1x |
- slices <- slices_global_module()+ unlist(lapply(x$children, format, indent = indent + 1, ...)) |
194 | +345 |
-
+ ),+ |
+ ||
346 | +1x | +
+ collapse = "" |
||
195 | +347 |
- # Clean up previous filter states and refresh cache of previous module_fd with current+ ) |
||
196 | -3x - | +|||
348 | +
- if (!is.null(module_fd_previous())) module_fd_previous()$finalize()+ } |
|||
197 | -83x - | +|||
349 | +
- module_fd_previous(module_fd())+ |
|||
198 | +350 |
-
+ #' @param modules (`teal_module` or `teal_modules`) |
||
199 | +351 |
- # Setting filter states from slices_global:+ #' @rdname teal_modules |
||
200 | +352 |
- # 1. when app initializes slices_global set to initial filters (specified by app developer)+ #' @examples |
||
201 | +353 |
- # 2. when data reinitializes slices_global reflects latest filter states+ #' # change the module's datanames |
||
202 | +354 |
-
+ #' set_datanames(module(datanames = "all"), "a") |
||
203 | -83x - | +|||
355 | +
- module_fd()$set_filter_state(slices)+ #' |
|||
204 | +356 |
-
+ #' # change modules' datanames |
||
205 | +357 |
- # irrelevant filters are discarded in FilteredData$set_available_teal_slices+ #' set_datanames( |
||
206 | +358 |
- # it means we don't need to subset slices_global$all_slices() from filters refering to irrelevant datasets+ #' modules( |
||
207 | -83x - | +|||
359 | +
- module_fd()$set_available_teal_slices(slices_global$all_slices)+ #' module(datanames = "all"), |
|||
208 | +360 |
-
+ #' module(datanames = "a") |
||
209 | +361 |
- # this needed in filter_manager_srv+ #' ), |
||
210 | -83x - | +|||
362 | +
- slices_global$module_slices_api_set(+ #' "b" |
|||
211 | -83x - | +|||
363 | +
- id,+ #' ) |
|||
212 | -83x - | +|||
364 | +
- list(+ #' @export |
|||
213 | -83x - | +|||
365 | +
- get_available_teal_slices = module_fd()$get_available_teal_slices(),+ set_datanames <- function(modules, datanames) { |
|||
214 | -83x - | +|||
366 | +! |
- set_filter_state = module_fd()$set_filter_state, # for testing purpose+ checkmate::assert_multi_class(modules, c("teal_modules", "teal_module")) |
||
215 | -83x - | +|||
367 | +! |
- get_filter_state = module_fd()$get_filter_state # for testing purpose+ if (inherits(modules, "teal_modules")) { |
||
216 | -+ | |||
368 | +! |
- )+ modules$children <- lapply(modules$children, set_datanames, datanames) |
||
217 | +369 |
- )+ } else { |
||
218 | -+ | |||
370 | +! |
- })+ if (identical(modules$datanames, "all")) { |
||
219 | -+ | |||
371 | +! |
-
+ modules$datanames <- datanames |
||
220 | +372 |
- # Update global state and mapping matrix when module filters change.+ } else { |
||
221 | -102x - | +|||
373 | +! |
- obs2 <- observeEvent(slices_module(), priority = 0, {+ warning( |
||
222 | -105x - | +|||
374 | +! |
- this_slices <- slices_module()+ "Not possible to modify datanames of the module ", modules$label, |
||
223 | -105x - | +|||
375 | +! |
- slices_global$slices_append(this_slices) # append new slices to the all_slices list+ ". set_datanames() can only change datanames if it was set to \"all\".", |
||
224 | -105x - | +|||
376 | +! |
- mapping_elem <- setNames(nm = id, list(vapply(this_slices, `[[`, character(1L), "id")))+ call. = FALSE |
||
225 | -105x - | +|||
377 | +
- slices_global$slices_active(mapping_elem)+ ) |
|||
226 | +378 |
- })+ } |
||
227 | +379 |
-
+ } |
||
228 | -102x - | +|||
380 | +! |
- obs3 <- observeEvent(slices_global_module(), {+ modules |
||
229 | -125x - | +|||
381 | +
- global_vs_module <- setdiff_teal_slices(slices_global_module(), slices_module())+ } |
|||
230 | -125x - | +|||
382 | +
- module_vs_global <- setdiff_teal_slices(slices_module(), slices_global_module())+ |
|||
231 | -116x - | +|||
383 | +
- if (length(global_vs_module) || length(module_vs_global)) {+ #' @rdname teal_modules |
|||
232 | +384 |
- # Comment: (Nota Bene) Normally new filters for a module are added through module-filter-panel, and slices+ #' @export |
||
233 | +385 |
- # global are updated automatically so slices_module -> slices_global_module are equal.+ print.teal_modules <- print.teal_module |
||
234 | +386 |
- # this if is valid only when a change is made on the global level so the change needs to be propagated down+ |
||
235 | +387 |
- # to the module (for example through snapshot manager). If it happens both slices are different- |
- ||
236 | -13x - | -
- logger::log_debug("srv_module_filter_manager@3 (N.B.) global state has changed for a module:{ id }.")- |
- ||
237 | -13x - | -
- module_fd()$clear_filter_states()- |
- ||
238 | -13x - | -
- module_fd()$set_filter_state(slices_global_module())+ |
||
239 | +388 |
- }+ # utilities ---- |
||
240 | +389 |
- })+ ## subset or modify modules ---- |
||
241 | +390 | |||
242 | -102x - | -
- slices_module # returned for testing purpose- |
- ||
243 | +391 |
- })+ #' Append a `teal_module` to `children` of a `teal_modules` object |
||
244 | +392 |
- }+ #' @keywords internal |
||
245 | +393 |
-
+ #' @param modules (`teal_modules`) |
||
246 | +394 |
- #' @importFrom shiny reactiveVal reactiveValues+ #' @param module (`teal_module`) object to be appended onto the children of `modules` |
||
247 | +395 |
- methods::setOldClass("reactiveVal")+ #' @return A `teal_modules` object with `module` appended. |
||
248 | +396 |
- methods::setOldClass("reactivevalues")+ append_module <- function(modules, module) { |
||
249 | -+ | |||
397 | +8x |
-
+ checkmate::assert_class(modules, "teal_modules") |
||
250 | -+ | |||
398 | +6x |
- #' @importFrom methods new+ checkmate::assert_class(module, "teal_module") |
||
251 | -+ | |||
399 | +4x |
- #' @rdname module_filter_manager+ modules$children <- c(modules$children, list(module)) |
||
252 | -+ | |||
400 | +4x |
- .slicesGlobal <- methods::setRefClass(".slicesGlobal", # nolint: object_name.+ labels <- vapply(modules$children, function(submodule) submodule$label, character(1)) |
||
253 | -+ | |||
401 | +4x |
- fields = list(+ names(modules$children) <- get_unique_labels(labels) |
||
254 | -+ | |||
402 | +4x |
- all_slices = "reactiveVal",+ modules |
||
255 | +403 |
- module_slices_api = "reactivevalues"+ } |
||
256 | +404 |
- ),+ |
||
257 | +405 |
- methods = list(+ #' Extract/Remove module(s) of specific class |
||
258 | +406 |
- initialize = function(slices = teal_slices(), module_labels) {- |
- ||
259 | -77x - | -
- shiny::isolate({- |
- ||
260 | -77x - | -
- checkmate::assert_class(slices, "teal_slices")+ #' |
||
261 | +407 |
- # needed on init to not mix "global_filters" with module-specific-slots- |
- ||
262 | -77x - | -
- if (isTRUE(attr(slices, "module_specific"))) {- |
- ||
263 | -11x - | -
- old_mapping <- attr(slices, "mapping")- |
- ||
264 | -11x - | -
- new_mapping <- sapply(module_labels, simplify = FALSE, function(module_label) {- |
- ||
265 | -20x - | -
- unique(unlist(old_mapping[c(module_label, "global_filters")]))+ #' Given a `teal_module` or a `teal_modules`, return the elements of the structure according to `class`. |
||
266 | +408 |
- })- |
- ||
267 | -11x - | -
- attr(slices, "mapping") <- new_mapping+ #' |
||
268 | +409 |
- }- |
- ||
269 | -77x - | -
- .self$all_slices <<- shiny::reactiveVal(slices)- |
- ||
270 | -77x - | -
- .self$module_slices_api <<- shiny::reactiveValues()- |
- ||
271 | -77x - | -
- .self$slices_append(slices)- |
- ||
272 | -77x - | -
- .self$slices_active(attr(slices, "mapping"))+ #' @param modules (`teal_modules`) |
||
273 | -77x - | +|||
410 | +
- invisible(.self)+ #' @param class The class name of `teal_module` to be extracted or dropped. |
|||
274 | +411 |
- })+ #' @keywords internal |
||
275 | +412 |
- },+ #' @return |
||
276 | +413 |
- is_module_specific = function() {+ #' - For `extract_module`, a `teal_module` of class `class` or `teal_modules` containing modules of class `class`. |
||
277 | -268x - | +|||
414 | +
- isTRUE(attr(.self$all_slices(), "module_specific"))+ #' - For `drop_module`, the opposite, which is all `teal_modules` of class other than `class`. |
|||
278 | +415 |
- },+ #' @rdname module_management |
||
279 | +416 |
- module_slices_api_set = function(module_label, functions_list) {+ extract_module <- function(modules, class) { |
||
280 | -83x - | +417 | +24x |
- shiny::isolate({+ if (inherits(modules, class)) { |
281 | -83x - | +|||
418 | +! |
- if (!.self$is_module_specific()) {+ modules |
||
282 | -67x - | +419 | +24x |
- module_label <- "global_filters"+ } else if (inherits(modules, "teal_module")) { |
283 | -+ | |||
420 | +13x |
- }+ NULL |
||
284 | -83x - | +421 | +11x |
- if (!identical(.self$module_slices_api[[module_label]], functions_list)) {+ } else if (inherits(modules, "teal_modules")) { |
285 | -83x - | +422 | +11x |
- .self$module_slices_api[[module_label]] <- functions_list+ Filter(function(x) length(x) > 0L, lapply(modules$children, extract_module, class)) |
286 | +423 |
- }+ } |
||
287 | -83x - | +|||
424 | +
- invisible(.self)+ } |
|||
288 | +425 |
- })+ |
||
289 | +426 |
- },+ #' @keywords internal |
||
290 | +427 |
- slices_deactivate_all = function(module_label) {+ #' @return `teal_modules` |
||
291 | -! | +|||
428 | +
- shiny::isolate({+ #' @rdname module_management |
|||
292 | -! | +|||
429 | +
- new_slices <- .self$all_slices()+ drop_module <- function(modules, class) { |
|||
293 | +430 | ! |
- old_mapping <- attr(new_slices, "mapping")- |
- |
294 | -- |
-
+ if (inherits(modules, class)) { |
||
295 | +431 | ! |
- new_mapping <- if (.self$is_module_specific()) {+ NULL |
|
296 | +432 | ! |
- new_module_mapping <- setNames(nm = module_label, list(character(0)))+ } else if (inherits(modules, "teal_module")) { |
|
297 | +433 | ! |
- modifyList(old_mapping, new_module_mapping)+ modules |
|
298 | +434 | ! |
- } else if (missing(module_label)) {+ } else if (inherits(modules, "teal_modules")) { |
|
299 | +435 | ! |
- lapply(+ do.call( |
|
300 | +436 | ! |
- attr(.self$all_slices(), "mapping"),+ "modules", |
|
301 | +437 | ! |
- function(x) character(0)+ c(Filter(function(x) length(x) > 0L, lapply(modules$children, drop_module, class)), label = modules$label) |
|
302 | +438 |
- )+ ) |
||
303 | +439 |
- } else {- |
- ||
304 | -! | -
- old_mapping[[module_label]] <- character(0)- |
- ||
305 | -! | -
- old_mapping+ } |
||
306 | +440 |
- }+ } |
||
307 | +441 | |||
308 | -! | -
- if (!identical(new_mapping, old_mapping)) {- |
- ||
309 | -! | -
- logger::log_debug(".slicesGlobal@slices_deactivate_all: deactivating all slices.")- |
- ||
310 | -! | -
- attr(new_slices, "mapping") <- new_mapping- |
- ||
311 | -! | -
- .self$all_slices(new_slices)- |
- ||
312 | +442 |
- }- |
- ||
313 | -! | -
- invisible(.self)+ ## read modules ---- |
||
314 | +443 |
- })+ |
||
315 | +444 |
- },+ #' Does the object make use of the `arg` |
||
316 | +445 |
- slices_active = function(mapping_elem) {- |
- ||
317 | -185x - | -
- shiny::isolate({+ #' |
||
318 | -185x - | +|||
446 | +
- if (.self$is_module_specific()) {+ #' @param modules (`teal_module` or `teal_modules`) object |
|||
319 | -36x - | +|||
447 | +
- new_mapping <- modifyList(attr(.self$all_slices(), "mapping"), mapping_elem)+ #' @param arg (`character(1)`) names of the arguments to be checked against formals of `teal` modules. |
|||
320 | +448 |
- } else {+ #' @return `logical` whether the object makes use of `arg`. |
||
321 | -149x - | +|||
449 | +
- new_mapping <- setNames(nm = "global_filters", list(unique(unlist(mapping_elem))))+ #' @rdname is_arg_used |
|||
322 | +450 |
- }+ #' @keywords internal |
||
323 | +451 |
-
+ is_arg_used <- function(modules, arg) { |
||
324 | -185x - | +452 | +476x |
- if (!identical(new_mapping, attr(.self$all_slices(), "mapping"))) {+ checkmate::assert_string(arg) |
325 | -128x - | +453 | +473x |
- mapping_modules <- toString(names(new_mapping))+ if (inherits(modules, "teal_modules")) { |
326 | -128x - | +454 | +18x |
- logger::log_debug(".slicesGlobal@slices_active: changing mapping for module(s): { mapping_modules }.")+ any(unlist(lapply(modules$children, is_arg_used, arg))) |
327 | -128x - | +455 | +455x |
- new_slices <- .self$all_slices()+ } else if (inherits(modules, "teal_module")) { |
328 | -128x - | +456 | +30x |
- attr(new_slices, "mapping") <- new_mapping+ is_arg_used(modules$server, arg) || is_arg_used(modules$ui, arg) |
329 | -128x - | +457 | +425x |
- .self$all_slices(new_slices)+ } else if (is.function(modules)) { |
330 | -+ | |||
458 | +423x |
- }+ isTRUE(arg %in% names(formals(modules))) |
||
331 | +459 |
-
+ } else { |
||
332 | -185x - | +460 | +2x |
- invisible(.self)+ stop("is_arg_used function not implemented for this object") |
333 | +461 |
- })+ } |
||
334 | +462 |
- },+ } |
||
335 | +463 |
- # - only new filters are appended to the $all_slices+ |
||
336 | +464 |
- # - mapping is not updated here+ |
||
337 | +465 |
- slices_append = function(slices, activate = FALSE) {+ #' Get module depth |
||
338 | -185x - | +|||
466 | +
- shiny::isolate({+ #' |
|||
339 | -185x - | +|||
467 | +
- if (!is.teal_slices(slices)) {+ #' Depth starts at 0, so a single `teal.module` has depth 0. |
|||
340 | -! | +|||
468 | +
- slices <- as.teal_slices(slices)+ #' Nesting it increases overall depth by 1. |
|||
341 | +469 |
- }+ #' |
||
342 | +470 |
-
+ #' @inheritParams init |
||
343 | +471 |
- # to make sure that we don't unnecessary trigger $all_slices <reactiveVal>+ #' @param depth optional integer determining current depth level |
||
344 | -185x - | +|||
472 | +
- new_slices <- setdiff_teal_slices(slices, .self$all_slices())+ #' |
|||
345 | -185x - | +|||
473 | +
- old_mapping <- attr(.self$all_slices(), "mapping")+ #' @return Depth level for given module. |
|||
346 | -185x - | +|||
474 | +
- if (length(new_slices)) {+ #' @keywords internal |
|||
347 | -6x - | +|||
475 | +
- new_ids <- vapply(new_slices, `[[`, character(1L), "id")+ modules_depth <- function(modules, depth = 0L) { |
|||
348 | -6x - | +476 | +12x |
- logger::log_debug(".slicesGlobal@slices_append: appending new slice(s): { new_ids }.")+ checkmate::assert_multi_class(modules, c("teal_module", "teal_modules")) |
349 | -6x - | +477 | +12x |
- slices_ids <- vapply(.self$all_slices(), `[[`, character(1L), "id")+ checkmate::assert_int(depth, lower = 0) |
350 | -6x - | +478 | +11x |
- lapply(new_slices, function(slice) {+ if (inherits(modules, "teal_modules")) { |
351 | -+ | |||
479 | +4x |
- # In case the new state has the same id as an existing one, add a suffix+ max(vapply(modules$children, modules_depth, integer(1), depth = depth + 1L)) |
||
352 | -6x - | +|||
480 | +
- if (slice$id %in% slices_ids) {+ } else { |
|||
353 | -1x - | +481 | +7x |
- slice$id <- utils::tail(make.unique(c(slices_ids, slice$id), sep = "_"), 1)+ depth |
354 | +482 |
- }+ } |
||
355 | +483 |
- })+ } |
||
356 | +484 | |||
357 | -6x - | -
- new_slices_all <- c(.self$all_slices(), new_slices)- |
- ||
358 | -6x - | -
- attr(new_slices_all, "mapping") <- old_mapping- |
- ||
359 | -6x - | +|||
485 | +
- .self$all_slices(new_slices_all)+ #' Retrieve labels from `teal_modules` |
|||
360 | +486 |
- }+ #' |
||
361 | +487 |
-
+ #' @param modules (`teal_modules`) |
||
362 | -185x - | +|||
488 | +
- invisible(.self)+ #' @return A `list` containing the labels of the modules. If the modules are nested, |
|||
363 | +489 |
- })+ #' the function returns a nested `list` of labels. |
||
364 | +490 |
- },+ #' @keywords internal |
||
365 | +491 |
- slices_get = function(module_label) {+ module_labels <- function(modules) { |
||
366 | -274x - | +492 | +185x |
- if (missing(module_label)) {+ if (inherits(modules, "teal_modules")) { |
367 | -! | +|||
493 | +80x |
- .self$all_slices()+ lapply(modules$children, module_labels) |
||
368 | +494 |
- } else {- |
- ||
369 | -274x - | -
- module_ids <- unlist(attr(.self$all_slices(), "mapping")[c(module_label, "global_filters")])+ } else { |
||
370 | -274x - | +495 | +105x |
- Filter(+ modules$label |
371 | -274x - | +|||
496 | +
- function(slice) slice$id %in% module_ids,+ } |
|||
372 | -274x - | +|||
497 | +
- .self$all_slices()+ } |
|||
373 | +498 |
- )+ |
||
374 | +499 |
- }+ #' Retrieve `teal_bookmarkable` attribute from `teal_modules` |
||
375 | +500 |
- },+ #' |
||
376 | +501 |
- slices_set = function(slices) {+ #' @param modules (`teal_modules` or `teal_module`) object |
||
377 | -7x - | +|||
502 | +
- shiny::isolate({+ #' @return named list of the same structure as `modules` with `TRUE` or `FALSE` values indicating |
|||
378 | -7x - | +|||
503 | +
- if (!is.teal_slices(slices)) {+ #' whether the module is bookmarkable. |
|||
379 | -! | +|||
504 | +
- slices <- as.teal_slices(slices)+ #' @keywords internal |
|||
380 | +505 |
- }+ modules_bookmarkable <- function(modules) { |
||
381 | -7x - | +506 | +185x |
- .self$all_slices(slices)+ checkmate::assert_multi_class(modules, c("teal_modules", "teal_module")) |
382 | -7x - | -
- invisible(.self)- |
- ||
383 | -+ | 507 | +185x |
- })+ if (inherits(modules, "teal_modules")) { |
384 | -+ | |||
508 | +80x |
- },+ setNames( |
||
385 | -+ | |||
509 | +80x |
- show = function() {+ lapply(modules$children, modules_bookmarkable), |
||
386 | -! | +|||
510 | +80x |
- shiny::isolate(print(.self$all_slices()))+ vapply(modules$children, `[[`, "label", FUN.VALUE = character(1)) |
||
387 | -! | +|||
511 | +
- invisible(.self)+ ) |
|||
388 | +512 |
- }+ } else { |
||
389 | -+ | |||
513 | +105x |
- )+ attr(modules, "teal_bookmarkable", exact = TRUE) |
||
390 | +514 |
- )+ } |
||
391 | +515 |
- # todo: prevent any teal_slices attribute except mapping+ } |
1 |
- #' Calls all `modules`+ #' Landing popup module |
||
3 |
- #' On the UI side each `teal_modules` is translated to a `tabsetPanel` and each `teal_module` is a+ #' @description Creates a landing welcome popup for `teal` applications. |
||
4 |
- #' `tabPanel`. Both, UI and server are called recursively so that each tab is a separate module and+ #' |
||
5 |
- #' reflect nested structure of `modules` argument.+ #' This module is used to display a popup dialog when the application starts. |
||
6 |
- #'+ #' The dialog blocks access to the application and must be closed with a button before the application can be viewed. |
||
7 |
- #' @name module_teal_module+ #' |
||
8 |
- #'+ #' @param label (`character(1)`) Label of the module. |
||
9 |
- #' @inheritParams module_teal+ #' @param title (`character(1)`) Text to be displayed as popup title. |
||
10 |
- #'+ #' @param content (`character(1)`, `shiny.tag` or `shiny.tag.list`) with the content of the popup. |
||
11 |
- #' @param data_rv (`reactive` returning `teal_data`)+ #' Passed to `...` of `shiny::modalDialog`. See examples. |
||
12 |
- #'+ #' @param buttons (`shiny.tag` or `shiny.tag.list`) Typically a `modalButton` or `actionButton`. See examples. |
||
13 |
- #' @param slices_global (`reactiveVal` returning `modules_teal_slices`)+ #' |
||
14 |
- #' see [`module_filter_manager`]+ #' @return A `teal_module` (extended with `teal_landing_module` class) to be used in `teal` applications. |
||
16 |
- #' @param depth (`integer(1)`)+ #' @examples |
||
17 |
- #' number which helps to determine depth of the modules nesting.+ #' app1 <- init( |
||
18 |
- #'+ #' data = teal_data(iris = iris), |
||
19 |
- #' @param datasets (`reactive` returning `FilteredData` or `NULL`)+ #' modules = modules( |
||
20 |
- #' When `datasets` is passed from the parent module (`srv_teal`) then `dataset` is a singleton+ #' example_module() |
||
21 |
- #' which implies in filter-panel to be "global". When `NULL` then filter-panel is "module-specific".+ #' ), |
||
22 |
- #'+ #' landing_popup = landing_popup_module( |
||
23 |
- #' @param data_load_status (`reactive` returning `character`)+ #' content = "A place for the welcome message or a disclaimer statement.", |
||
24 |
- #' Determines action dependent on a data loading status:+ #' buttons = modalButton("Proceed") |
||
25 |
- #' - `"ok"` when `teal_data` is returned from the data loading.+ #' ) |
||
26 |
- #' - `"teal_data_module failed"` when [teal_data_module()] didn't return `teal_data`. Disables tabs buttons.+ #' ) |
||
27 |
- #' - `"external failed"` when a `reactive` passed to `srv_teal(data)` didn't return `teal_data`. Hides the whole tab+ #' if (interactive()) { |
||
28 |
- #' panel.+ #' shinyApp(app1$ui, app1$server) |
||
29 |
- #'+ #' } |
||
30 |
- #' @return+ #' |
||
31 |
- #' output of currently active module.+ #' app2 <- init( |
||
32 |
- #' - `srv_teal_module.teal_module` returns `reactiveVal` containing output of the called module.+ #' data = teal_data(iris = iris), |
||
33 |
- #' - `srv_teal_module.teal_modules` returns output of module selected by `input$active_tab`.+ #' modules = modules( |
||
34 |
- #'+ #' example_module() |
||
35 |
- #' @keywords internal+ #' ), |
||
36 |
- NULL+ #' landing_popup = landing_popup_module( |
||
37 |
-
+ #' title = "Welcome", |
||
38 |
- #' @rdname module_teal_module+ #' content = tags$b( |
||
39 |
- ui_teal_module <- function(id, modules, depth = 0L) {+ #' "A place for the welcome message or a disclaimer statement.", |
||
40 | -! | +
- checkmate::assert_multi_class(modules, c("teal_modules", "teal_module", "shiny.tag"))+ #' style = "color: red;" |
|
41 | -! | +
- checkmate::assert_count(depth)+ #' ), |
|
42 | -! | +
- UseMethod("ui_teal_module", modules)+ #' buttons = tagList( |
|
43 |
- }+ #' modalButton("Proceed"), |
||
44 |
-
+ #' actionButton("read", "Read more", |
||
45 |
- #' @rdname module_teal_module+ #' onclick = "window.open('http://google.com', '_blank')" |
||
46 |
- #' @export+ #' ), |
||
47 |
- ui_teal_module.default <- function(id, modules, depth = 0L) {+ #' actionButton("close", "Reject", onclick = "window.close()") |
||
48 | -! | +
- stop("Modules class not supported: ", paste(class(modules), collapse = " "))+ #' ) |
|
49 |
- }+ #' ) |
||
50 |
-
+ #' ) |
||
51 |
- #' @rdname module_teal_module+ #' |
||
52 |
- #' @export+ #' if (interactive()) { |
||
53 |
- ui_teal_module.teal_modules <- function(id, modules, depth = 0L) {+ #' shinyApp(app2$ui, app2$server) |
||
54 | -! | +
- ns <- NS(id)+ #' } |
|
55 | -! | +
- tags$div(+ #' |
|
56 | -! | +
- id = ns("wrapper"),+ #' @export |
|
57 | -! | +
- do.call(+ landing_popup_module <- function(label = "Landing Popup", |
|
58 | -! | +
- tabsetPanel,+ title = NULL, |
|
59 | -! | +
- c(+ content = NULL, |
|
60 |
- # by giving an id, we can reactively respond to tab changes+ buttons = modalButton("Accept")) { |
||
61 | ! |
- list(+ checkmate::assert_string(label) |
|
62 | ! |
- id = ns("active_tab"),+ checkmate::assert_string(title, null.ok = TRUE) |
|
63 | ! |
- type = if (modules$label == "root") "pills" else "tabs"+ checkmate::assert_multi_class( |
|
64 | -+ | ! |
- ),+ content, |
65 | ! |
- lapply(+ classes = c("character", "shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE |
|
66 | -! | +
- names(modules$children),+ ) |
|
67 | ! |
- function(module_id) {+ checkmate::assert_multi_class(buttons, classes = c("shiny.tag", "shiny.tag.list")) |
|
68 | -! | +
- module_label <- modules$children[[module_id]]$label+ |
|
69 | ! |
- if (is.null(module_label)) {+ message("Initializing landing_popup_module") |
|
70 | -! | +
- module_label <- icon("fas fa-database")+ |
|
71 | -+ | ! |
- }+ module <- module( |
72 | ! |
- tabPanel(+ label = label, |
|
73 | ! |
- title = module_label,+ server = function(id) { |
|
74 | ! |
- value = module_id, # when clicked this tab value changes input$<tabset panel id>+ moduleServer(id, function(input, output, session) { |
|
75 | ! |
- ui_teal_module(+ showModal( |
|
76 | ! |
- id = ns(module_id),+ modalDialog( |
|
77 | ! |
- modules = modules$children[[module_id]],+ id = "landingpopup", |
|
78 | ! |
- depth = depth + 1L+ title = title, |
|
79 | -+ | ! |
- )+ content, |
80 | -+ | ! |
- )+ footer = buttons |
81 |
- }+ ) |
||
83 |
- )+ }) |
||
84 |
- )+ } |
||
86 | -+ | ! |
- }+ class(module) <- c("teal_module_landing", class(module)) |
87 | -+ | ! |
-
+ module |
88 |
- #' @rdname module_teal_module+ } |
89 | +1 |
- #' @export+ #' Validate that dataset has a minimum number of observations |
||
90 | +2 |
- ui_teal_module.teal_module <- function(id, modules, depth = 0L) {- |
- ||
91 | -! | -
- ns <- NS(id)- |
- ||
92 | -! | -
- args <- c(list(id = ns("module")), modules$ui_args)+ #' |
||
93 | +3 | - - | -||
94 | -! | -
- ui_teal <- tagList(- |
- ||
95 | -! | -
- div(- |
- ||
96 | -! | -
- id = ns("validate_datanames"),- |
- ||
97 | -! | -
- ui_validate_reactive_teal_data(ns("validate_datanames"))+ #' `r lifecycle::badge("stable")` |
||
98 | +4 |
- ),- |
- ||
99 | -! | -
- shinyjs::hidden(- |
- ||
100 | -! | -
- tags$div(- |
- ||
101 | -! | -
- id = ns("transformer_failure_info"),- |
- ||
102 | -! | -
- class = "teal_validated",- |
- ||
103 | -! | -
- div(- |
- ||
104 | -! | -
- class = "teal-output-warning",- |
- ||
105 | -! | -
- "One of transformers failed. Please fix and continue."+ #' |
||
106 | +5 |
- )+ #' This function is a wrapper for `shiny::validate`. |
||
107 | +6 |
- )+ #' |
||
108 | +7 |
- ),- |
- ||
109 | -! | -
- tags$div(- |
- ||
110 | -! | -
- id = ns("teal_module_ui"),- |
- ||
111 | -! | -
- do.call(modules$ui, args)+ #' @param x (`data.frame`) |
||
112 | +8 |
- )+ #' @param min_nrow (`numeric(1)`) Minimum allowed number of rows in `x`. |
||
113 | +9 |
- )+ #' @param complete (`logical(1)`) Flag specifying whether to check only complete cases. Defaults to `FALSE`. |
||
114 | +10 | - - | -||
115 | -! | -
- div(- |
- ||
116 | -! | -
- id = id,- |
- ||
117 | -! | -
- class = "teal_module",- |
- ||
118 | -! | -
- uiOutput(ns("data_reactive"), inline = TRUE),+ #' @param allow_inf (`logical(1)`) Flag specifying whether to allow infinite values. Defaults to `TRUE`. |
||
119 | -! | +|||
11 | +
- tagList(+ #' @param msg (`character(1)`) Additional message to display alongside the default message. |
|||
120 | -! | +|||
12 | +
- if (depth >= 2L) tags$div(style = "mt-6"),+ #' |
|||
121 | -! | +|||
13 | +
- if (!is.null(modules$datanames)) {+ #' @export |
|||
122 | -! | +|||
14 | +
- fluidRow(+ #' |
|||
123 | -! | +|||
15 | +
- column(width = 9, ui_teal, class = "teal_primary_col"),+ #' @examples |
|||
124 | -! | +|||
16 | +
- column(+ #' library(teal) |
|||
125 | -! | +|||
17 | +
- width = 3,+ #' ui <- fluidPage( |
|||
126 | -! | +|||
18 | +
- ui_data_summary(ns("data_summary")),+ #' sliderInput("len", "Max Length of Sepal", |
|||
127 | -! | +|||
19 | +
- ui_filter_data(ns("filter_panel")),+ #' min = 4.3, max = 7.9, value = 5 |
|||
128 | -! | +|||
20 | +
- ui_transform_data(ns("data_transform"), transformers = modules$transformers, class = "well"),+ #' ), |
|||
129 | -! | +|||
21 | +
- class = "teal_secondary_col"+ #' plotOutput("plot") |
|||
130 | +22 |
- )+ #' ) |
||
131 | +23 |
- )+ #' |
||
132 | +24 |
- } else {+ #' server <- function(input, output) { |
||
133 | -! | +|||
25 | +
- div(+ #' output$plot <- renderPlot({ |
|||
134 | -! | +|||
26 | +
- div(+ #' iris_df <- iris[iris$Sepal.Length <= input$len, ] |
|||
135 | -! | +|||
27 | +
- class = "teal_validated",+ #' validate_has_data( |
|||
136 | -! | +|||
28 | +
- uiOutput(ns("data_input_error"))+ #' iris_df, |
|||
137 | +29 |
- ),+ #' min_nrow = 10, |
||
138 | -! | +|||
30 | +
- ui_teal+ #' complete = FALSE, |
|||
139 | +31 |
- )+ #' msg = "Please adjust Max Length of Sepal" |
||
140 | +32 |
- }+ #' ) |
||
141 | +33 |
- )+ #' |
||
142 | +34 |
- )+ #' hist(iris_df$Sepal.Length, breaks = 5) |
||
143 | +35 |
- }+ #' }) |
||
144 | +36 |
-
+ #' } |
||
145 | +37 |
- #' @rdname module_teal_module+ #' if (interactive()) { |
||
146 | +38 |
- srv_teal_module <- function(id,+ #' shinyApp(ui, server) |
||
147 | +39 |
- data_rv,+ #' } |
||
148 | +40 |
- modules,+ #' |
||
149 | +41 |
- datasets = NULL,+ validate_has_data <- function(x, |
||
150 | +42 |
- slices_global,+ min_nrow = NULL, |
||
151 | +43 |
- reporter = teal.reporter::Reporter$new(),+ complete = FALSE, |
||
152 | +44 |
- data_load_status = reactive("ok"),+ allow_inf = TRUE, |
||
153 | +45 |
- is_active = reactive(TRUE)) {+ msg = NULL) { |
||
154 | -179x - | +46 | +17x |
- checkmate::assert_string(id)+ checkmate::assert_string(msg, null.ok = TRUE) |
155 | -179x - | +47 | +15x |
- assert_reactive(data_rv)+ checkmate::assert_data_frame(x) |
156 | -179x - | +48 | +15x |
- checkmate::assert_multi_class(modules, c("teal_modules", "teal_module"))+ if (!is.null(min_nrow)) { |
157 | -179x - | +49 | +15x |
- assert_reactive(datasets, null.ok = TRUE)+ if (complete) { |
158 | -179x - | +50 | +5x |
- checkmate::assert_class(slices_global, ".slicesGlobal")+ complete_index <- stats::complete.cases(x) |
159 | -179x - | +51 | +5x |
- checkmate::assert_class(reporter, "Reporter")+ validate(need( |
160 | -179x - | +52 | +5x |
- assert_reactive(data_load_status)+ sum(complete_index) > 0 && nrow(x[complete_index, , drop = FALSE]) >= min_nrow, |
161 | -179x - | +53 | +5x |
- UseMethod("srv_teal_module", modules)+ paste(c(paste("Number of complete cases is less than:", min_nrow), msg), collapse = "\n") |
162 | +54 |
- }+ )) |
||
163 | +55 |
-
+ } else { |
||
164 | -+ | |||
56 | +10x |
- #' @rdname module_teal_module+ validate(need( |
||
165 | -+ | |||
57 | +10x |
- #' @export+ nrow(x) >= min_nrow, |
||
166 | -+ | |||
58 | +10x |
- srv_teal_module.default <- function(id,+ paste( |
||
167 | -+ | |||
59 | +10x |
- data_rv,+ c(paste("Minimum number of records not met: >=", min_nrow, "records required."), msg), |
||
168 | -+ | |||
60 | +10x |
- modules,+ collapse = "\n" |
||
169 | +61 |
- datasets = NULL,+ ) |
||
170 | +62 |
- slices_global,+ )) |
||
171 | +63 |
- reporter = teal.reporter::Reporter$new(),+ } |
||
172 | +64 |
- data_load_status = reactive("ok"),+ |
||
173 | -+ | |||
65 | +10x |
- is_active = reactive(TRUE)) {+ if (!allow_inf) { |
||
174 | -! | +|||
66 | +6x |
- stop("Modules class not supported: ", paste(class(modules), collapse = " "))+ validate(need( |
||
175 | -+ | |||
67 | +6x |
- }+ all(vapply(x, function(col) !is.numeric(col) || !any(is.infinite(col)), logical(1))),+ |
+ ||
68 | +6x | +
+ "Dataframe contains Inf values which is not allowed." |
||
176 | +69 |
-
+ )) |
||
177 | +70 |
- #' @rdname module_teal_module+ } |
||
178 | +71 |
- #' @export+ } |
||
179 | +72 |
- srv_teal_module.teal_modules <- function(id,+ } |
||
180 | +73 |
- data_rv,+ |
||
181 | +74 |
- modules,+ #' Validate that dataset has unique rows for key variables |
||
182 | +75 |
- datasets = NULL,+ #' |
||
183 | +76 |
- slices_global,+ #' `r lifecycle::badge("stable")` |
||
184 | +77 |
- reporter = teal.reporter::Reporter$new(),+ #' |
||
185 | +78 |
- data_load_status = reactive("ok"),+ #' This function is a wrapper for `shiny::validate`. |
||
186 | +79 |
- is_active = reactive(TRUE)) {+ #' |
||
187 | -77x - | +|||
80 | +
- moduleServer(id = id, module = function(input, output, session) {+ #' @param x (`data.frame`) |
|||
188 | -77x - | +|||
81 | +
- logger::log_debug("srv_teal_module.teal_modules initializing the module { deparse1(modules$label) }.")+ #' @param key (`character`) Vector of ID variables from `x` that identify unique records. |
|||
189 | +82 |
-
+ #' |
||
190 | -77x - | +|||
83 | +
- observeEvent(data_load_status(), {+ #' @export |
|||
191 | -70x - | +|||
84 | +
- tabs_selector <- sprintf("#%s li a", session$ns("active_tab"))+ #' |
|||
192 | -70x - | +|||
85 | +
- if (identical(data_load_status(), "ok")) {+ #' @examples |
|||
193 | -65x - | +|||
86 | +
- logger::log_debug("srv_teal_module@1 enabling modules tabs.")+ #' iris$id <- rep(1:50, times = 3) |
|||
194 | -65x - | +|||
87 | +
- shinyjs::show("wrapper")+ #' ui <- fluidPage( |
|||
195 | -65x - | +|||
88 | +
- shinyjs::enable(selector = tabs_selector)+ #' selectInput( |
|||
196 | -5x - | +|||
89 | +
- } else if (identical(data_load_status(), "teal_data_module failed")) {+ #' inputId = "species", |
|||
197 | -5x - | +|||
90 | +
- logger::log_debug("srv_teal_module@1 disabling modules tabs.")+ #' label = "Select species", |
|||
198 | -5x - | +|||
91 | +
- shinyjs::disable(selector = tabs_selector)+ #' choices = c("setosa", "versicolor", "virginica"), |
|||
199 | -! | +|||
92 | +
- } else if (identical(data_load_status(), "external failed")) {+ #' selected = "setosa", |
|||
200 | -! | +|||
93 | +
- logger::log_debug("srv_teal_module@1 hiding modules tabs.")+ #' multiple = TRUE |
|||
201 | -! | +|||
94 | +
- shinyjs::hide("wrapper")+ #' ), |
|||
202 | +95 |
- }+ #' plotOutput("plot") |
||
203 | +96 |
- })+ #' ) |
||
204 | +97 |
-
+ #' server <- function(input, output) { |
||
205 | -77x - | +|||
98 | +
- modules_output <- sapply(+ #' output$plot <- renderPlot({ |
|||
206 | -77x - | +|||
99 | +
- names(modules$children),+ #' iris_f <- iris[iris$Species %in% input$species, ] |
|||
207 | -77x - | +|||
100 | +
- function(module_id) {+ #' validate_one_row_per_id(iris_f, key = c("id")) |
|||
208 | -102x - | +|||
101 | +
- srv_teal_module(+ #' |
|||
209 | -102x - | +|||
102 | +
- id = module_id,+ #' hist(iris_f$Sepal.Length, breaks = 5) |
|||
210 | -102x - | +|||
103 | +
- data_rv = data_rv,+ #' }) |
|||
211 | -102x - | +|||
104 | +
- modules = modules$children[[module_id]],+ #' } |
|||
212 | -102x - | +|||
105 | +
- datasets = datasets,+ #' if (interactive()) { |
|||
213 | -102x - | +|||
106 | +
- slices_global = slices_global,+ #' shinyApp(ui, server) |
|||
214 | -102x - | +|||
107 | +
- reporter = reporter,+ #' } |
|||
215 | -102x - | +|||
108 | +
- is_active = reactive(+ #' |
|||
216 | -102x - | +|||
109 | +
- is_active() &&+ validate_one_row_per_id <- function(x, key = c("USUBJID", "STUDYID")) { |
|||
217 | -102x - | +|||
110 | +! |
- input$active_tab == module_id &&+ validate(need(!any(duplicated(x[key])), paste("Found more than one row per id."))) |
||
218 | -102x - | +|||
111 | +
- identical(data_load_status(), "ok")+ } |
|||
219 | +112 |
- )+ |
||
220 | +113 |
- )+ #' Validates that vector includes all expected values |
||
221 | +114 |
- },+ #' |
||
222 | -77x - | +|||
115 | +
- simplify = FALSE+ #' `r lifecycle::badge("stable")` |
|||
223 | +116 |
- )+ #' |
||
224 | +117 |
-
+ #' This function is a wrapper for `shiny::validate`. |
||
225 | -77x - | +|||
118 | +
- modules_output+ #' |
|||
226 | +119 |
- })+ #' @param x Vector of values to test. |
||
227 | +120 |
- }+ #' @param choices Vector to test against. |
||
228 | +121 |
-
+ #' @param msg (`character(1)`) Error message to display if some elements of `x` are not elements of `choices`. |
||
229 | +122 |
- #' @rdname module_teal_module+ #' |
||
230 | +123 |
#' @export |
||
231 | +124 |
- srv_teal_module.teal_module <- function(id,+ #' |
||
232 | +125 |
- data_rv,+ #' @examples |
||
233 | +126 |
- modules,+ #' ui <- fluidPage( |
||
234 | +127 |
- datasets = NULL,+ #' selectInput( |
||
235 | +128 |
- slices_global,+ #' "species", |
||
236 | +129 |
- reporter = teal.reporter::Reporter$new(),+ #' "Select species", |
||
237 | +130 |
- data_load_status = reactive("ok"),+ #' choices = c("setosa", "versicolor", "virginica", "unknown species"), |
||
238 | +131 |
- is_active = reactive(TRUE)) {+ #' selected = "setosa", |
||
239 | -102x - | +|||
132 | +
- logger::log_debug("srv_teal_module.teal_module initializing the module: { deparse1(modules$label) }.")+ #' multiple = FALSE |
|||
240 | -102x - | +|||
133 | +
- moduleServer(id = id, module = function(input, output, session) {+ #' ), |
|||
241 | -102x - | +|||
134 | +
- module_out <- reactiveVal()+ #' verbatimTextOutput("summary") |
|||
242 | +135 |
-
+ #' ) |
||
243 | -102x - | +|||
136 | +
- active_datanames <- reactive({+ #' |
|||
244 | -79x - | +|||
137 | +
- .resolve_module_datanames(data = data_rv(), modules = modules)+ #' server <- function(input, output) { |
|||
245 | +138 |
- })+ #' output$summary <- renderPrint({ |
||
246 | -102x - | +|||
139 | +
- if (is.null(datasets)) {+ #' validate_in(input$species, iris$Species, "Species does not exist.") |
|||
247 | -20x - | +|||
140 | +
- datasets <- eventReactive(data_rv(), {+ #' nrow(iris[iris$Species == input$species, ]) |
|||
248 | -16x - | +|||
141 | +
- req(inherits(data_rv(), "teal_data"))+ #' }) |
|||
249 | -16x - | +|||
142 | +
- logger::log_debug("srv_teal_module@1 initializing module-specific FilteredData")+ #' } |
|||
250 | -16x - | +|||
143 | +
- teal_data_to_filtered_data(data_rv(), datanames = active_datanames())+ #' if (interactive()) { |
|||
251 | +144 |
- })+ #' shinyApp(ui, server) |
||
252 | +145 |
- }+ #' } |
||
253 | +146 |
-
+ #' |
||
254 | +147 |
- # manage module filters on the module level+ validate_in <- function(x, choices, msg) {+ |
+ ||
148 | +! | +
+ validate(need(length(x) > 0 && length(choices) > 0 && all(x %in% choices), msg)) |
||
255 | +149 |
- # important:+ } |
||
256 | +150 |
- # filter_manager_module_srv needs to be called before filter_panel_srv+ |
||
257 | +151 |
- # Because available_teal_slices is used in FilteredData$srv_available_slices (via srv_filter_panel)+ #' Validates that vector has length greater than 0 |
||
258 | +152 |
- # and if it is not set, then it won't be available in the srv_filter_panel+ #' |
||
259 | -102x - | +|||
153 | +
- srv_module_filter_manager(modules$label, module_fd = datasets, slices_global = slices_global)+ #' `r lifecycle::badge("stable")` |
|||
260 | +154 |
-
+ #' |
||
261 | -102x - | +|||
155 | +
- call_once_when(is_active(), {+ #' This function is a wrapper for `shiny::validate`. |
|||
262 | -76x - | +|||
156 | +
- filtered_teal_data <- srv_filter_data(+ #' |
|||
263 | -76x - | -
- "filter_panel",- |
- ||
264 | -76x - | +|||
157 | +
- datasets = datasets,+ #' @param x vector |
|||
265 | -76x - | +|||
158 | +
- active_datanames = active_datanames,+ #' @param msg message to display |
|||
266 | -76x - | +|||
159 | +
- data_rv = data_rv,+ #' |
|||
267 | -76x - | +|||
160 | +
- is_active = is_active+ #' @export |
|||
268 | +161 |
- )+ #' |
||
269 | -76x - | +|||
162 | +
- is_transformer_failed <- reactiveValues()+ #' @examples |
|||
270 | -76x - | +|||
163 | +
- transformed_teal_data <- srv_transform_data(+ #' data <- data.frame( |
|||
271 | -76x - | +|||
164 | +
- "data_transform",+ #' id = c(1:10, 11:20, 1:10), |
|||
272 | -76x - | +|||
165 | +
- data = filtered_teal_data,+ #' strata = rep(c("A", "B"), each = 15) |
|||
273 | -76x - | +|||
166 | +
- transformers = modules$transformers,+ #' ) |
|||
274 | -76x - | +|||
167 | +
- modules = modules,+ #' ui <- fluidPage( |
|||
275 | -76x - | +|||
168 | +
- is_transformer_failed = is_transformer_failed+ #' selectInput("ref1", "Select strata1 to compare", |
|||
276 | +169 |
- )+ #' choices = c("A", "B", "C"), selected = "A" |
||
277 | -76x - | +|||
170 | +
- any_transformer_failed <- reactive({+ #' ), |
|||
278 | -76x - | +|||
171 | +
- any(unlist(reactiveValuesToList(is_transformer_failed)))+ #' selectInput("ref2", "Select strata2 to compare", |
|||
279 | +172 |
- })+ #' choices = c("A", "B", "C"), selected = "B" |
||
280 | +173 |
-
+ #' ), |
||
281 | -76x - | +|||
174 | +
- observeEvent(any_transformer_failed(), {+ #' verbatimTextOutput("arm_summary") |
|||
282 | -76x - | +|||
175 | +
- if (isTRUE(any_transformer_failed())) {+ #' ) |
|||
283 | -6x - | +|||
176 | +
- shinyjs::hide("teal_module_ui")+ #' |
|||
284 | -6x - | +|||
177 | +
- shinyjs::hide("validate_datanames")+ #' server <- function(input, output) { |
|||
285 | -6x - | +|||
178 | +
- shinyjs::show("transformer_failure_info")+ #' output$arm_summary <- renderText({ |
|||
286 | +179 |
- } else {+ #' sample_1 <- data$id[data$strata == input$ref1] |
||
287 | -70x - | +|||
180 | +
- shinyjs::show("teal_module_ui")+ #' sample_2 <- data$id[data$strata == input$ref2] |
|||
288 | -70x - | +|||
181 | +
- shinyjs::show("validate_datanames")+ #' |
|||
289 | -70x - | +|||
182 | +
- shinyjs::hide("transformer_failure_info")+ #' validate_has_elements(sample_1, "No subjects in strata1.") |
|||
290 | +183 |
- }+ #' validate_has_elements(sample_2, "No subjects in strata2.") |
||
291 | +184 |
- })+ #' |
||
292 | +185 |
-
+ #' paste0( |
||
293 | -76x - | +|||
186 | +
- module_teal_data <- reactive({+ #' "Number of samples in: strata1=", length(sample_1), |
|||
294 | -84x - | +|||
187 | +
- req(inherits(transformed_teal_data(), "teal_data"))+ #' " comparions strata2=", length(sample_2) |
|||
295 | -78x - | +|||
188 | +
- all_teal_data <- transformed_teal_data()+ #' ) |
|||
296 | -78x - | +|||
189 | +
- module_datanames <- .resolve_module_datanames(data = all_teal_data, modules = modules)+ #' }) |
|||
297 | -78x - | +|||
190 | +
- .subset_teal_data(all_teal_data, module_datanames)+ #' } |
|||
298 | +191 |
- })+ #' if (interactive()) { |
||
299 | +192 |
-
+ #' shinyApp(ui, server) |
||
300 | -76x - | +|||
193 | +
- srv_validate_reactive_teal_data(+ #' } |
|||
301 | -76x - | +|||
194 | +
- "validate_datanames",+ validate_has_elements <- function(x, msg) { |
|||
302 | -76x - | +|||
195 | +! |
- data = module_teal_data,+ validate(need(length(x) > 0, msg)) |
||
303 | -76x - | +|||
196 | +
- modules = modules+ } |
|||
304 | +197 |
- )+ |
||
305 | +198 |
-
+ #' Validates no intersection between two vectors |
||
306 | -76x - | +|||
199 | +
- summary_table <- srv_data_summary("data_summary", module_teal_data)+ #' |
|||
307 | +200 |
-
+ #' `r lifecycle::badge("stable")` |
||
308 | +201 |
- # Call modules.+ #' |
||
309 | -76x - | +|||
202 | +
- if (!inherits(modules, "teal_module_previewer")) {+ #' This function is a wrapper for `shiny::validate`. |
|||
310 | -76x - | +|||
203 | +
- obs_module <- call_once_when(+ #' |
|||
311 | -76x - | +|||
204 | +
- !is.null(module_teal_data()),+ #' @param x vector |
|||
312 | -76x - | +|||
205 | +
- ignoreNULL = TRUE,+ #' @param y vector |
|||
313 | -76x - | +|||
206 | +
- handlerExpr = {+ #' @param msg (`character(1)`) message to display if `x` and `y` intersect |
|||
314 | -70x - | +|||
207 | +
- module_out(.call_teal_module(modules, datasets, module_teal_data, reporter))+ #' |
|||
315 | +208 |
- }+ #' @export |
||
316 | +209 |
- )+ #' |
||
317 | +210 |
- } else {+ #' @examples |
||
318 | +211 |
- # Report previewer must be initiated on app start for report cards to be included in bookmarks.+ #' data <- data.frame( |
||
319 | +212 |
- # When previewer is delayed, cards are bookmarked only if previewer has been initiated (visited).+ #' id = c(1:10, 11:20, 1:10), |
||
320 | -! | +|||
213 | +
- module_out(.call_teal_module(modules, datasets, module_teal_data, reporter))+ #' strata = rep(c("A", "B", "C"), each = 10) |
|||
321 | +214 |
- }+ #' ) |
||
322 | +215 |
-
+ #' |
||
323 | +216 |
- # todo: (feature request) add a ReporterCard to the reporter as an output from the teal_module+ #' ui <- fluidPage( |
||
324 | +217 |
- # how to determine if module returns a ReporterCard so that reportPreviewer is needed?+ #' selectInput("ref1", "Select strata1 to compare", |
||
325 | +218 |
- # Should we insertUI of the ReportPreviewer then?+ #' choices = c("A", "B", "C"), |
||
326 | +219 |
- # What about attr(module, "reportable") - similar to attr(module, "bookmarkable")+ #' selected = "A" |
||
327 | -76x - | +|||
220 | +
- if ("report" %in% names(module_out)) {+ #' ), |
|||
328 | +221 |
- # (reactively) add card to the reporter+ #' selectInput("ref2", "Select strata2 to compare", |
||
329 | +222 |
- }+ #' choices = c("A", "B", "C"), |
||
330 | +223 |
- })+ #' selected = "B" |
||
331 | +224 |
-
+ #' ), |
||
332 | -102x - | +|||
225 | +
- module_out+ #' verbatimTextOutput("summary") |
|||
333 | +226 |
- })+ #' ) |
||
334 | +227 |
- }+ #' |
||
335 | +228 |
-
+ #' server <- function(input, output) { |
||
336 | +229 |
- # This function calls a module server function.+ #' output$summary <- renderText({ |
||
337 | +230 |
- .call_teal_module <- function(modules, datasets, filtered_teal_data, reporter) {+ #' sample_1 <- data$id[data$strata == input$ref1] |
||
338 | +231 |
- # collect arguments to run teal_module+ #' sample_2 <- data$id[data$strata == input$ref2] |
||
339 | -70x - | +|||
232 | +
- args <- c(list(id = "module"), modules$server_args)+ #' |
|||
340 | -70x - | +|||
233 | +
- if (is_arg_used(modules$server, "reporter")) {+ #' validate_no_intersection( |
|||
341 | -1x - | +|||
234 | +
- args <- c(args, list(reporter = reporter))+ #' sample_1, sample_2, |
|||
342 | +235 |
- }+ #' "subjects within strata1 and strata2 cannot overlap" |
||
343 | +236 |
-
+ #' ) |
||
344 | -70x - | +|||
237 | +
- if (is_arg_used(modules$server, "datasets")) {+ #' paste0( |
|||
345 | -1x - | +|||
238 | +
- args <- c(args, datasets = datasets())+ #' "Number of subject in: reference treatment=", length(sample_1), |
|||
346 | -1x - | +|||
239 | +
- warning("datasets argument is not reactive and therefore it won't be updated when data is refreshed.")+ #' " comparions treatment=", length(sample_2) |
|||
347 | +240 |
- }+ #' ) |
||
348 | +241 |
-
+ #' }) |
||
349 | -70x - | +|||
242 | +
- if (is_arg_used(modules$server, "data")) {+ #' } |
|||
350 | -66x - | +|||
243 | +
- args <- c(args, data = list(filtered_teal_data))+ #' if (interactive()) { |
|||
351 | +244 |
- }+ #' shinyApp(ui, server) |
||
352 | +245 |
-
+ #' } |
||
353 | -70x - | +|||
246 | +
- if (is_arg_used(modules$server, "filter_panel_api")) {- |
- |||
354 | -1x - | -
- args <- c(args, filter_panel_api = teal.slice::FilterPanelAPI$new(datasets()))- |
- ||
355 | -- |
- }+ #' |
||
356 | +247 |
-
+ validate_no_intersection <- function(x, y, msg) { |
||
357 | -70x - | +|||
248 | +! |
- if (is_arg_used(modules$server, "id")) {+ validate(need(length(intersect(x, y)) == 0, msg)) |
||
358 | -70x - | +|||
249 | +
- do.call(modules$server, args)+ } |
|||
359 | +250 |
- } else {+ |
||
360 | -! | +|||
251 | +
- do.call(callModule, c(args, list(module = modules$server)))+ |
|||
361 | +252 |
- }+ #' Validates that dataset contains specific variable |
||
362 | +253 |
- }+ #' |
||
363 | +254 |
-
+ #' `r lifecycle::badge("stable")` |
||
364 | +255 |
- .resolve_module_datanames <- function(data, modules) {+ #' |
||
365 | -157x - | +|||
256 | +
- stopifnot("data_rv must be teal_data object." = inherits(data, "teal_data"))+ #' This function is a wrapper for `shiny::validate`. |
|||
366 | -157x - | +|||
257 | +
- if (is.null(modules$datanames) || identical(modules$datanames, "all")) {+ #' |
|||
367 | -131x - | +|||
258 | +
- .topologically_sort_datanames(ls(teal.code::get_env(data)), teal.data::join_keys(data))+ #' @param data (`data.frame`) |
|||
368 | +259 |
- } else {+ #' @param varname (`character(1)`) name of variable to check for in `data` |
||
369 | -26x - | +|||
260 | +
- intersect(+ #' @param msg (`character(1)`) message to display if `data` does not include `varname` |
|||
370 | -26x - | +|||
261 | +
- .include_parent_datanames(modules$datanames, teal.data::join_keys(data)),+ #' |
|||
371 | -26x - | +|||
262 | +
- ls(teal.code::get_env(data))+ #' @export |
|||
372 | +263 |
- )+ #' |
||
373 | +264 |
- }+ #' @examples |
||
374 | +265 |
- }+ #' data <- data.frame( |
||
375 | +266 |
-
+ #' one = rep("a", length.out = 20), |
||
376 | +267 |
- #' Calls expression when condition is met+ #' two = rep(c("a", "b"), length.out = 20) |
||
377 | +268 |
- #'+ #' ) |
||
378 | +269 |
- #' Function postpones `handlerExpr` to the moment when `eventExpr` (condition) returns `TRUE`,+ #' ui <- fluidPage( |
||
379 | +270 |
- #' otherwise nothing happens.+ #' selectInput( |
||
380 | +271 |
- #' @param eventExpr A (quoted or unquoted) logical expression that represents the event;+ #' "var", |
||
381 | +272 |
- #' this can be a simple reactive value like input$click, a call to a reactive expression+ #' "Select variable", |
||
382 | +273 |
- #' like dataset(), or even a complex expression inside curly braces.+ #' choices = c("one", "two", "three", "four"), |
||
383 | +274 |
- #' @param ... additional arguments passed to `observeEvent` with the exception of `eventExpr` that is not allowed.+ #' selected = "one" |
||
384 | +275 |
- #' @inheritParams shiny::observeEvent+ #' ), |
||
385 | +276 |
- #'+ #' verbatimTextOutput("summary") |
||
386 | +277 |
- #' @return An observer.+ #' ) |
||
387 | +278 |
#' |
||
388 | +279 |
- #' @keywords internal+ #' server <- function(input, output) { |
||
389 | +280 |
- call_once_when <- function(eventExpr, # nolint: object_name.+ #' output$summary <- renderText({ |
||
390 | +281 |
- handlerExpr, # nolint: object_name.+ #' validate_has_variable(data, input$var) |
||
391 | +282 |
- event.env = parent.frame(), # nolint: object_name.+ #' paste0("Selected treatment variables: ", paste(input$var, collapse = ", ")) |
||
392 | +283 |
- handler.env = parent.frame(), # nolint: object_name.+ #' }) |
||
393 | +284 |
- ...) {- |
- ||
394 | -178x - | -
- event_quo <- rlang::new_quosure(substitute(eventExpr), env = event.env)+ #' } |
||
395 | -178x - | +|||
285 | +
- handler_quo <- rlang::new_quosure(substitute(handlerExpr), env = handler.env)+ #' if (interactive()) { |
|||
396 | +286 |
-
+ #' shinyApp(ui, server) |
||
397 | +287 |
- # When `condExpr` is TRUE, then `handlerExpr` is evaluated once.+ #' } |
||
398 | -178x - | +|||
288 | +
- activator <- reactive({+ validate_has_variable <- function(data, varname, msg) { |
|||
399 | -178x - | +|||
289 | +! |
- if (isTRUE(rlang::eval_tidy(event_quo))) {+ if (length(varname) != 0) { |
||
400 | -146x - | +|||
290 | +! |
- TRUE+ has_vars <- varname %in% names(data) |
||
401 | +291 |
- }+ |
||
402 | -+ | |||
292 | +! |
- })+ if (!all(has_vars)) { |
||
403 | -+ | |||
293 | +! |
-
+ if (missing(msg)) { |
||
404 | -178x - | +|||
294 | +! |
- observeEvent(+ msg <- sprintf( |
||
405 | -178x - | +|||
295 | +! |
- eventExpr = activator(),+ "%s does not have the required variables: %s.", |
||
406 | -178x - | +|||
296 | +! |
- once = TRUE,+ deparse(substitute(data)), |
||
407 | -178x - | +|||
297 | +! |
- handlerExpr = rlang::eval_tidy(handler_quo),+ toString(varname[!has_vars]) |
||
408 | +298 |
- ...+ ) |
||
409 | +299 |
- )+ } |
||
410 | -+ | |||
300 | +! |
- }+ validate(need(FALSE, msg)) |
1 | +301 |
- #' Get client timezone+ } |
|
2 | +302 |
- #'+ } |
|
3 | +303 |
- #' User timezone in the browser may be different to the one on the server.+ } |
|
4 | +304 |
- #' This script can be run to register a `shiny` input which contains information about the timezone in the browser.+ |
|
5 | +305 |
- #'+ #' Validate that variables has expected number of levels |
|
6 | +306 |
- #' @param ns (`function`) namespace function passed from the `session` object in the `shiny` server.+ #' |
|
7 | +307 |
- #' For `shiny` modules this will allow for proper name spacing of the registered input.+ #' `r lifecycle::badge("stable")` |
|
8 | +308 |
#' |
|
9 | +309 |
- #' @return `NULL`, invisibly.+ #' If the number of levels of `x` is less than `min_levels` |
|
10 | +310 |
- #'+ #' or greater than `max_levels` the validation will fail. |
|
11 | +311 |
- #' @keywords internal+ #' This function is a wrapper for `shiny::validate`. |
|
12 | +312 |
#' |
|
13 | +313 |
- get_client_timezone <- function(ns) {+ #' @param x variable name. If `x` is not a factor, the unique values |
|
14 | -78x - | +||
314 | +
- script <- sprintf(+ #' are treated as levels. |
||
15 | -78x - | +||
315 | +
- "Shiny.setInputValue(`%s`, Intl.DateTimeFormat().resolvedOptions().timeZone)",+ #' @param min_levels cutoff for minimum number of levels of `x` |
||
16 | -78x - | +||
316 | +
- ns("timezone")+ #' @param max_levels cutoff for maximum number of levels of `x` |
||
17 | +317 |
- )+ #' @param var_name name of variable being validated for use in |
|
18 | -78x - | +||
318 | +
- shinyjs::runjs(script) # function does not return anything+ #' validation message |
||
19 | -78x - | +||
319 | +
- invisible(NULL)+ #' |
||
20 | +320 |
- }+ #' @export |
|
21 | +321 |
-
+ #' @examples |
|
22 | +322 |
- #' Resolve the expected bootstrap theme+ #' data <- data.frame( |
|
23 | +323 |
- #' @noRd+ #' one = rep("a", length.out = 20), |
|
24 | +324 |
- #' @keywords internal+ #' two = rep(c("a", "b"), length.out = 20), |
|
25 | +325 |
- get_teal_bs_theme <- function() {+ #' three = rep(c("a", "b", "c"), length.out = 20), |
|
26 | -4x - | +||
326 | +
- bs_theme <- getOption("teal.bs_theme")+ #' four = rep(c("a", "b", "c", "d"), length.out = 20), |
||
27 | +327 |
-
+ #' stringsAsFactors = TRUE |
|
28 | -4x - | +||
328 | +
- if (is.null(bs_theme)) {+ #' ) |
||
29 | -1x - | +||
329 | +
- return(NULL)+ #' ui <- fluidPage( |
||
30 | +330 |
- }+ #' selectInput( |
|
31 | +331 |
-
+ #' "var", |
|
32 | -3x - | +||
332 | +
- if (!checkmate::test_class(bs_theme, "bs_theme")) {+ #' "Select variable", |
||
33 | -2x - | +||
333 | +
- warning(+ #' choices = c("one", "two", "three", "four"), |
||
34 | -2x - | +||
334 | +
- "Assertion on 'teal.bs_theme' option value failed: ",+ #' selected = "one" |
||
35 | -2x - | +||
335 | +
- checkmate::check_class(bs_theme, "bs_theme"),+ #' ), |
||
36 | -2x - | +||
336 | +
- ". The default Shiny Bootstrap theme will be used."+ #' verbatimTextOutput("summary") |
||
37 | +337 |
- )+ #' ) |
|
38 | -2x - | +||
338 | +
- return(NULL)+ #' |
||
39 | +339 |
- }+ #' server <- function(input, output) { |
|
40 | +340 |
-
+ #' output$summary <- renderText({ |
|
41 | -1x - | +||
341 | +
- bs_theme+ #' validate_n_levels(data[[input$var]], min_levels = 2, max_levels = 15, var_name = input$var) |
||
42 | +342 |
- }+ #' paste0( |
|
43 | +343 |
-
+ #' "Levels of selected treatment variable: ", |
|
44 | +344 |
- #' Return parentnames along with datanames.+ #' paste(levels(data[[input$var]]), |
|
45 | +345 |
- #' @noRd+ #' collapse = ", " |
|
46 | +346 |
- #' @keywords internal+ #' ) |
|
47 | +347 |
- .include_parent_datanames <- function(datanames, join_keys) {- |
- |
48 | -157x - | -
- ordered_datanames <- datanames- |
- |
49 | -157x - | -
- for (i in datanames) {- |
- |
50 | -275x - | -
- parents <- character(0)- |
- |
51 | -275x - | -
- while (length(i) > 0) {- |
- |
52 | -288x - | -
- parent_i <- teal.data::parent(join_keys, i)- |
- |
53 | -288x - | -
- parents <- c(parent_i, parents)- |
- |
54 | -288x - | -
- i <- parent_i+ #' ) |
|
55 | +348 |
- }- |
- |
56 | -275x - | -
- ordered_datanames <- c(parents, ordered_datanames)+ #' }) |
|
57 | +349 |
- }- |
- |
58 | -157x - | -
- unique(ordered_datanames)+ #' } |
|
59 | +350 |
- }+ #' if (interactive()) { |
|
60 | +351 |
-
+ #' shinyApp(ui, server) |
|
61 | +352 |
- #' Return topologicaly sorted datanames+ #' } |
|
62 | +353 |
- #' @noRd+ validate_n_levels <- function(x, min_levels = 1, max_levels = 12, var_name) { |
|
63 | -+ | ||
354 | +! |
- #' @keywords internal+ x_levels <- if (is.factor(x)) { |
|
64 | -+ | ||
355 | +! |
- .topologically_sort_datanames <- function(datanames, join_keys) {+ levels(x) |
|
65 | -131x - | +||
356 | +
- datanames_with_parents <- .include_parent_datanames(datanames, join_keys)+ } else { |
||
66 | -131x - | +||
357 | +! |
- intersect(datanames, datanames_with_parents)+ unique(x) |
|
67 | +358 |
- }+ } |
|
68 | +359 | ||
69 | -+ | ||
360 | +! |
- #' Create a `FilteredData`+ if (!is.null(min_levels) && !(is.null(max_levels))) { |
|
70 | -+ | ||
361 | +! |
- #'+ validate(need( |
|
71 | -+ | ||
362 | +! |
- #' Create a `FilteredData` object from a `teal_data` object.+ length(x_levels) >= min_levels && length(x_levels) <= max_levels, |
|
72 | -+ | ||
363 | +! |
- #'+ sprintf( |
|
73 | -+ | ||
364 | +! |
- #' @param x (`teal_data`) object+ "%s variable needs minimum %s level(s) and maximum %s level(s).", |
|
74 | -+ | ||
365 | +! |
- #' @param datanames (`character`) vector of data set names to include; must be subset of `datanames(x)`+ var_name, min_levels, max_levels |
|
75 | +366 |
- #' @return A `FilteredData` object.+ ) |
|
76 | +367 |
- #' @keywords internal+ )) |
|
77 | -+ | ||
368 | +! |
- teal_data_to_filtered_data <- function(x, datanames = ls(teal.code::get_env(x))) {+ } else if (!is.null(min_levels)) { |
|
78 | -73x - | +||
369 | +! |
- checkmate::assert_class(x, "teal_data")+ validate(need( |
|
79 | -73x - | +||
370 | +! |
- checkmate::assert_character(datanames, min.chars = 1L, any.missing = FALSE)+ length(x_levels) >= min_levels,+ |
+ |
371 | +! | +
+ sprintf("%s variable needs minimum %s levels(s)", var_name, min_levels) |
|
80 | +372 |
- # Otherwise, FilteredData will be created in the modules' scope later+ )) |
|
81 | -73x - | +||
373 | +! |
- teal.slice::init_filtered_data(+ } else if (!is.null(max_levels)) { |
|
82 | -73x - | +||
374 | +! |
- x = Filter(+ validate(need( |
|
83 | -73x - | +||
375 | +! |
- length,+ length(x_levels) <= max_levels, |
|
84 | -73x - | +||
376 | +! |
- sapply(datanames, function(dn) x[[dn]], simplify = FALSE)+ sprintf("%s variable needs maximum %s level(s)", var_name, max_levels) |
|
85 | +377 |
- ),- |
- |
86 | -73x - | -
- join_keys = teal.data::join_keys(x)+ )) |
|
87 | +378 |
- )+ } |
|
88 | +379 |
} |
89 | +1 |
-
+ #' Module to transform `reactive` `teal_data` |
||
90 | +2 |
-
+ #' |
||
91 | +3 |
- #' Template function for `TealReportCard` creation and customization+ #' Module calls multiple [`module_teal_data`] in sequence so that `reactive teal_data` output |
||
92 | +4 |
- #'+ #' from one module is handed over to the following module's input. |
||
93 | +5 |
- #' This function generates a report card with a title,+ #' |
||
94 | +6 |
- #' an optional description, and the option to append the filter state list.+ #' @inheritParams module_teal_data |
||
95 | +7 |
- #'+ #' @inheritParams teal_modules |
||
96 | +8 |
- #' @param title (`character(1)`) title of the card (unless overwritten by label)+ #' @return `reactive` `teal_data` |
||
97 | +9 |
- #' @param label (`character(1)`) label provided by the user when adding the card+ #' |
||
98 | +10 |
- #' @param description (`character(1)`) optional, additional description+ #' |
||
99 | +11 |
- #' @param with_filter (`logical(1)`) flag indicating to add filter state+ #' @name module_transform_data |
||
100 | +12 |
- #' @param filter_panel_api (`FilterPanelAPI`) object with API that allows the generation+ #' @keywords internal |
||
101 | +13 |
- #' of the filter state in the report+ NULL |
||
102 | +14 |
- #'+ |
||
103 | +15 |
- #' @return (`TealReportCard`) populated with a title, description and filter state.+ #' @rdname module_transform_data |
||
104 | +16 |
- #'+ ui_transform_data <- function(id, transformers = list(), class = "well") { |
||
105 | -+ | |||
17 | +! |
- #' @export+ checkmate::assert_string(id) |
||
106 | -+ | |||
18 | +! |
- report_card_template <- function(title, label, description = NULL, with_filter, filter_panel_api) {+ checkmate::assert_list(transformers, "teal_transform_module") |
||
107 | -2x - | +|||
19 | +
- checkmate::assert_string(title)+ |
|||
108 | -2x - | +|||
20 | +! |
- checkmate::assert_string(label)+ ns <- NS(id) |
||
109 | -2x - | +|||
21 | +! |
- checkmate::assert_string(description, null.ok = TRUE)+ labels <- lapply(transformers, function(x) attr(x, "label")) |
||
110 | -2x - | +|||
22 | +! |
- checkmate::assert_flag(with_filter)+ ids <- get_unique_labels(labels) |
||
111 | -2x - | +|||
23 | +! |
- checkmate::assert_class(filter_panel_api, classes = "FilterPanelAPI")+ names(transformers) <- ids |
||
112 | +24 | |||
113 | -2x - | -
- card <- teal::TealReportCard$new()- |
- ||
114 | -2x - | +|||
25 | +! |
- title <- if (label == "") title else label+ lapply( |
||
115 | -2x - | +|||
26 | +! |
- card$set_name(title)+ names(transformers), |
||
116 | -2x - | +|||
27 | +! |
- card$append_text(title, "header2")+ function(name) { |
||
117 | -1x - | +|||
28 | +! |
- if (!is.null(description)) card$append_text(description, "header3")+ data_mod <- transformers[[name]] |
||
118 | -1x - | +|||
29 | +! |
- if (with_filter) card$append_fs(filter_panel_api$get_filter_state())+ wrapper_id <- ns(sprintf("wrapper_%s", name)) |
||
119 | -2x - | +|||
30 | +! |
- card+ div( # todo: accordion? |
||
120 | +31 |
- }+ # class .teal_validated changes the color of the boarder on error in ui_validate_reactive_teal_data |
||
121 | +32 |
-
+ # For details see tealValidate.js file. |
||
122 | -+ | |||
33 | +! |
-
+ class = c(class, "teal_validated"), |
||
123 | -+ | |||
34 | +! |
- #' Check `datanames` in modules+ title = attr(data_mod, "label"), |
||
124 | -+ | |||
35 | +! |
- #'+ tags$span( |
||
125 | -+ | |||
36 | +! |
- #' This function ensures specified `datanames` in modules match those in the data object,+ class = "text-primary mb-4", |
||
126 | -+ | |||
37 | +! |
- #' returning error messages or `TRUE` for successful validation.+ icon("fas fa-square-pen"), |
||
127 | -+ | |||
38 | +! |
- #'+ attr(data_mod, "label") |
||
128 | +39 |
- #' @param modules (`teal_modules`) object+ ), |
||
129 | -+ | |||
40 | +! |
- #' @param datanames (`character`) names of datasets available in the `data` object+ tags$i( |
||
130 | -+ | |||
41 | +! |
- #'+ class = "remove pull-right fa fa-angle-down", |
||
131 | -- |
- #' @return A `character(1)` containing error message or `TRUE` if validation passes.- |
- ||
132 | -- |
- #' @keywords internal- |
- ||
133 | -- |
- check_modules_datanames <- function(modules, datanames) {- |
- ||
134 | -166x - | -
- checkmate::assert_multi_class(modules, c("teal_modules", "teal_module"))- |
- ||
135 | -166x - | +|||
42 | +! |
- checkmate::assert_character(datanames)+ style = "cursor: pointer;", |
||
136 | -+ | |||
43 | +! |
-
+ title = "fold/expand transform panel", |
||
137 | -166x - | +|||
44 | +! |
- recursive_check_datanames <- function(modules, datanames) {+ onclick = sprintf("togglePanelItems(this, '%s', 'fa-angle-right', 'fa-angle-down');", wrapper_id) |
||
138 | +45 |
- # check teal_modules against datanames- |
- ||
139 | -259x - | -
- if (inherits(modules, "teal_modules")) {- |
- ||
140 | -73x - | -
- result <- lapply(modules$children, function(module) recursive_check_datanames(module, datanames = datanames))+ ), |
||
141 | -73x - | +|||
46 | +! |
- result <- result[vapply(result, Negate(is.null), logical(1L))]+ div( |
||
142 | -73x - | +|||
47 | +! |
- if (length(result) == 0) {+ id = wrapper_id, |
||
143 | -65x - | +|||
48 | +! |
- return(NULL)+ ui_teal_data(id = ns(name), data_module = transformers[[name]]$ui) |
||
144 | +49 |
- }- |
- ||
145 | -8x - | -
- list(- |
- ||
146 | -8x - | -
- string = do.call(c, as.list(unname(sapply(result, function(x) x$string)))),- |
- ||
147 | -8x - | -
- html = function(with_module_name = TRUE) {- |
- ||
148 | -6x - | -
- tagList(- |
- ||
149 | -6x - | -
- lapply(+ ) |
||
150 | -6x - | +|||
50 | +
- result,+ ) |
|||
151 | -6x - | +|||
51 | +
- function(x) x$html(with_module_name = with_module_name)+ } |
|||
152 | +52 |
- )+ ) |
||
153 | +53 |
- )+ } |
||
154 | +54 |
- }+ |
||
155 | +55 |
- )+ #' @rdname module_transform_data |
||
156 | +56 |
- } else {+ srv_transform_data <- function(id, data, transformers = list(), modules, is_transformer_failed = reactiveValues()) { |
||
157 | -186x - | +57 | +79x |
- extra_datanames <- setdiff(modules$datanames, c("all", datanames))+ checkmate::assert_string(id) |
158 | -186x - | +58 | +79x |
- if (length(extra_datanames)) {+ assert_reactive(data) |
159 | -11x - | +59 | +79x |
- list(+ checkmate::assert_list(transformers, "teal_transform_module") |
160 | -11x - | +60 | +79x |
- string = build_datanames_error_message(+ checkmate::assert_class(modules, "teal_module") |
161 | -11x - | +61 | +79x |
- modules$label,+ labels <- lapply(transformers, function(x) attr(x, "label")) |
162 | -11x - | +62 | +79x |
- datanames,+ ids <- get_unique_labels(labels) |
163 | -11x - | +63 | +79x |
- extra_datanames,+ names(transformers) <- ids |
164 | -11x - | +64 | +79x |
- tags = list(+ moduleServer(id, function(input, output, session) { |
165 | -11x - | +65 | +79x |
- span = function(..., .noWS = NULL) { # nolint: object_name+ logger::log_debug("srv_teal_data_modules initializing.") |
166 | -78x - | -
- trimws(paste(..., sep = ifelse(is.null(.noWS), " ", ""), collapse = " "))- |
- ||
167 | -+ | 66 | +79x |
- },+ Reduce( |
168 | -11x - | -
- code = function(x) toString(dQuote(x, q = FALSE))- |
- ||
169 | -+ | 67 | +79x |
- ),+ function(previous_result, name) { |
170 | -11x - | +68 | +19x |
- tagList = function(...) trimws(paste(...))+ srv_teal_data( |
171 | -+ | |||
69 | +19x |
- ),+ id = name, |
||
172 | -+ | |||
70 | +19x |
- # Build HTML representation of the error message with <pre> formatting+ data_module = function(id) transformers[[name]]$server(id, previous_result), |
||
173 | -11x - | +71 | +19x |
- html = function(with_module_name = TRUE) {+ modules = modules, |
174 | -9x - | +72 | +19x |
- tagList(+ is_transformer_failed = is_transformer_failed |
175 | -9x - | +|||
73 | +
- build_datanames_error_message(+ ) |
|||
176 | -9x - | +|||
74 | +
- if (with_module_name) modules$label,+ }, |
|||
177 | -9x - | +75 | +79x |
- datanames,+ x = names(transformers), |
178 | -9x - | +76 | +79x |
- extra_datanames+ init = data |
179 | +77 |
- ),- |
- ||
180 | -9x - | -
- tags$br(.noWS = "before")+ ) |
||
181 | +78 |
- )+ }) |
||
182 | +79 |
- }+ } |
183 | +1 |
- )+ #' Manage multiple `FilteredData` objects |
||
184 | +2 |
- }+ #' |
||
185 | +3 |
- }+ #' @description |
||
186 | +4 |
- }+ #' Oversee filter states across the entire application. |
||
187 | -166x - | +|||
5 | +
- check_datanames <- recursive_check_datanames(modules, datanames)+ #' |
|||
188 | -166x - | +|||
6 | +
- if (length(check_datanames)) {+ #' @section Slices global: |
|||
189 | -11x - | +|||
7 | +
- check_datanames+ #' The key role in maintaining the module-specific filter states is played by the `.slicesGlobal` |
|||
190 | +8 |
- } else {+ #' object. It is a reference class that holds the following fields: |
||
191 | -155x - | +|||
9 | +
- TRUE+ #' - `all_slices` (`reactiveVal`) - reactive value containing all filters registered in an app. |
|||
192 | +10 |
- }+ #' - `module_slices_api` (`reactiveValues`) - reactive field containing references to each modules' |
||
193 | +11 |
- }+ #' `FilteredData` object methods. At this moment it is used only in `srv_filter_manager` to display |
||
194 | +12 |
-
+ #' the filter states in a table combining informations from `all_slices` and from |
||
195 | +13 |
- #' Check `datanames` in filters+ #' `FilteredData$get_available_teal_slices()`. |
||
196 | +14 |
#' |
||
197 | +15 |
- #' This function checks whether `datanames` in filters correspond to those in `data`,+ #' During a session only new filters are added to `all_slices` unless [`module_snapshot_manager`] is |
||
198 | +16 |
- #' returning character vector with error messages or `TRUE` if all checks pass.+ #' used to restore previous state. Filters from `all_slices` can be activated or deactivated in a |
||
199 | +17 |
- #'+ #' module which is linked (both ways) by `attr(, "mapping")` so that: |
||
200 | +18 |
- #' @param filters (`teal_slices`) object+ #' - If module's filter is added or removed in its `FilteredData` object, this information is passed |
||
201 | +19 |
- #' @param datanames (`character`) names of datasets available in the `data` object+ #' to `SlicesGlobal` which updates `attr(, "mapping")` accordingly. |
||
202 | +20 |
- #'+ #' - When mapping changes in a `SlicesGlobal`, filters are set or removed from module's |
||
203 | +21 |
- #' @return A `character(1)` containing error message or TRUE if validation passes.+ #' `FilteredData`. |
||
204 | +22 |
- #' @keywords internal+ #' |
||
205 | +23 |
- check_filter_datanames <- function(filters, datanames) {+ #' @section Filter manager: |
||
206 | -73x - | +|||
24 | +
- checkmate::assert_class(filters, "teal_slices")+ #' Filter-manager is split into two parts: |
|||
207 | -73x - | +|||
25 | +
- checkmate::assert_character(datanames)+ #' 1. `ui/srv_filter_manager_panel` - Called once for the whole app. This module observes changes in |
|||
208 | +26 |
-
+ #' the filters in `slices_global` and displays them in a table utilizing information from `mapping`: |
||
209 | +27 |
- # check teal_slices against datanames+ #' - (`TRUE`) - filter is active in the module |
||
210 | -73x - | +|||
28 | +
- out <- unlist(sapply(+ #' - (`FALSE`) - filter is inactive in the module |
|||
211 | -73x - | +|||
29 | +
- filters, function(filter) {+ #' - (`NA`) - filter is not available in the module |
|||
212 | -24x - | +|||
30 | +
- dataname <- shiny::isolate(filter$dataname)+ #' 2. `ui/srv_module_filter_manager` - Called once for each `teal_module`. Handling filter states |
|||
213 | -24x - | +|||
31 | +
- if (!dataname %in% datanames) {+ #' for of single module and keeping module `FilteredData` consistent with `slices_global`, so that |
|||
214 | -3x - | +|||
32 | +
- sprintf(+ #' local filters are always reflected in the `slices_global` and its mapping and vice versa. |
|||
215 | -3x - | +|||
33 | +
- "- Filter '%s' refers to dataname not available in 'data':\n %s not in (%s)",+ #' |
|||
216 | -3x - | +|||
34 | +
- shiny::isolate(filter$id),+ #' |
|||
217 | -3x - | +|||
35 | +
- dQuote(dataname, q = FALSE),+ #' @param id (`character(1)`) |
|||
218 | -3x - | +|||
36 | +
- toString(dQuote(datanames, q = FALSE))+ #' `shiny` module instance id. |
|||
219 | +37 |
- )+ #' |
||
220 | +38 |
- }+ #' @param slices_global (`reactiveVal`) |
||
221 | +39 |
- }+ #' containing `teal_slices`. |
||
222 | +40 |
- ))+ #' |
||
223 | +41 |
-
+ #' @param module_fd (`FilteredData`) |
||
224 | +42 |
-
+ #' Object containing the data to be filtered in a single `teal` module. |
||
225 | -73x - | +|||
43 | +
- if (length(out)) {+ #' |
|||
226 | -3x - | +|||
44 | +
- paste(out, collapse = "\n")+ #' @return |
|||
227 | +45 |
- } else {+ #' Module returns a `slices_global` (`reactiveVal`) containing a `teal_slices` object with mapping. |
||
228 | -70x - | +|||
46 | +
- TRUE+ #' |
|||
229 | +47 |
- }+ #' @encoding UTF-8 |
||
230 | +48 |
- }+ #' |
||
231 | +49 |
-
+ #' @name module_filter_manager |
||
232 | +50 |
- #' Function for validating the title parameter of `teal::init`+ #' @rdname module_filter_manager |
||
233 | +51 |
#' |
||
234 | +52 |
- #' Checks if the input of the title from `teal::init` will create a valid title and favicon tag.+ NULL |
||
235 | +53 |
- #' @param shiny_tag (`shiny.tag`) Object to validate for a valid title.+ |
||
236 | +54 |
- #' @keywords internal+ #' @rdname module_filter_manager |
||
237 | +55 |
- validate_app_title_tag <- function(shiny_tag) {- |
- ||
238 | -7x - | -
- checkmate::assert_class(shiny_tag, "shiny.tag")- |
- ||
239 | -7x - | -
- checkmate::assert_true(shiny_tag$name == "head")- |
- ||
240 | -6x - | -
- child_names <- vapply(shiny_tag$children, `[[`, character(1L), "name")- |
- ||
241 | -6x - | -
- checkmate::assert_subset(c("title", "link"), child_names, .var.name = "child tags")+ ui_filter_manager_panel <- function(id) { |
||
242 | -4x - | +|||
56 | +! |
- rel_attr <- shiny_tag$children[[which(child_names == "link")]]$attribs$rel+ ns <- NS(id) |
||
243 | -4x - | +|||
57 | +! |
- checkmate::assert_subset(+ tags$button( |
||
244 | -4x - | +|||
58 | +! |
- rel_attr,+ id = ns("show_filter_manager"), |
||
245 | -4x - | +|||
59 | +! |
- c("icon", "shortcut icon"),+ class = "btn action-button wunder_bar_button", |
||
246 | -4x - | +|||
60 | +! |
- .var.name = "Link tag's rel attribute",+ title = "View filter mapping", |
||
247 | -4x - | +|||
61 | +! |
- empty.ok = FALSE+ suppressMessages(icon("fas fa-grip")) |
||
248 | +62 |
) |
||
249 | +63 |
} |
||
250 | +64 | |||
251 | -- |
- #' Build app title with favicon- |
- ||
252 | -- |
- #'- |
- ||
253 | -- |
- #' A helper function to create the browser title along with a logo.- |
- ||
254 | -- |
- #'- |
- ||
255 | -- |
- #' @param title (`character`) The browser title for the `teal` app.- |
- ||
256 | +65 |
- #' @param favicon (`character`) The path for the icon for the title.+ #' @rdname module_filter_manager |
||
257 | +66 |
- #' The image/icon path can be remote or the static path accessible by `shiny`, like the `www/`+ #' @keywords internal |
||
258 | +67 |
- #'+ srv_filter_manager_panel <- function(id, slices_global) { |
||
259 | -+ | |||
68 | +80x |
- #' @return A `shiny.tag` containing the element that adds the title and logo to the `shiny` app.+ checkmate::assert_string(id) |
||
260 | -+ | |||
69 | +80x |
- #' @export+ checkmate::assert_class(slices_global, ".slicesGlobal") |
||
261 | -+ | |||
70 | +80x |
- build_app_title <- function(+ moduleServer(id, function(input, output, session) { |
||
262 | -+ | |||
71 | +80x |
- title = "teal app",+ setBookmarkExclude(c("show_filter_manager")) |
||
263 | -+ | |||
72 | +80x |
- favicon = "https://raw.githubusercontent.com/insightsengineering/hex-stickers/main/PNG/nest.png") {+ observeEvent(input$show_filter_manager, { |
||
264 | -12x - | +|||
73 | +! |
- checkmate::assert_string(title, null.ok = TRUE)+ logger::log_debug("srv_filter_manager_panel@1 show_filter_manager button has been clicked.") |
||
265 | -12x - | +|||
74 | +! |
- checkmate::assert_string(favicon, null.ok = TRUE)+ showModal( |
||
266 | -12x - | +|||
75 | +! |
- tags$head(+ modalDialog( |
||
267 | -12x - | +|||
76 | +! |
- tags$title(title),+ ui_filter_manager(session$ns("filter_manager")), |
||
268 | -12x - | +|||
77 | +! |
- tags$link(+ class = "filter_manager_modal", |
||
269 | -12x - | +|||
78 | +! |
- rel = "icon",+ size = "l", |
||
270 | -12x - | +|||
79 | +! |
- href = favicon,+ footer = NULL, |
||
271 | -12x - | +|||
80 | +! |
- sizes = "any"+ easyClose = TRUE |
||
272 | +81 |
- )+ ) |
||
273 | +82 |
- )+ ) |
||
274 | +83 |
- }+ }) |
||
275 | -+ | |||
84 | +80x |
-
+ srv_filter_manager("filter_manager", slices_global = slices_global) |
||
276 | +85 |
- #' Application ID+ }) |
||
277 | +86 |
- #'+ } |
||
278 | +87 |
- #' Creates App ID used to match filter snapshots to application.+ |
||
279 | +88 |
- #'+ #' @rdname module_filter_manager |
||
280 | +89 |
- #' Calculate app ID that will be used to stamp filter state snapshots.+ ui_filter_manager <- function(id) { |
||
281 | -+ | |||
90 | +! |
- #' App ID is a hash of the app's data and modules.+ ns <- NS(id) |
||
282 | -+ | |||
91 | +! |
- #' See "transferring snapshots" section in ?snapshot.+ actionButton(ns("filter_manager"), NULL, icon = icon("fas fa-filter")) |
||
283 | -+ | |||
92 | +! |
- #'+ tags$div( |
||
284 | -+ | |||
93 | +! |
- #' @param data (`teal_data` or `teal_data_module`) as accepted by `init`+ class = "filter_manager_content", |
||
285 | -+ | |||
94 | +! |
- #' @param modules (`teal_modules`) object as accepted by `init`+ tableOutput(ns("slices_table")) |
||
286 | +95 |
- #'+ ) |
||
287 | +96 |
- #' @return A single character string.+ } |
||
288 | +97 |
- #'+ |
||
289 | +98 |
- #' @keywords internal+ #' @rdname module_filter_manager |
||
290 | +99 |
- create_app_id <- function(data, modules) {+ srv_filter_manager <- function(id, slices_global) { |
||
291 | -20x - | +100 | +80x |
- checkmate::assert_multi_class(data, c("teal_data", "teal_data_module"))+ checkmate::assert_string(id) |
292 | -19x - | +101 | +80x |
- checkmate::assert_class(modules, "teal_modules")+ checkmate::assert_class(slices_global, ".slicesGlobal") |
293 | +102 | |||
294 | -18x - | -
- data <- if (inherits(data, "teal_data")) {- |
- ||
295 | -16x - | +103 | +80x |
- as.list(teal.code::get_env(data))+ moduleServer(id, function(input, output, session) { |
296 | -18x - | +104 | +80x |
- } else if (inherits(data, "teal_data_module")) {+ logger::log_debug("filter_manager_srv initializing.") |
297 | -2x - | +|||
105 | +
- deparse1(body(data$server))+ |
|||
298 | +106 |
- }+ # Bookmark slices global with mapping. |
||
299 | -18x - | +107 | +80x |
- modules <- lapply(modules, defunction)+ session$onBookmark(function(state) { |
300 | -+ | |||
108 | +! |
-
+ logger::log_debug("filter_manager_srv@onBookmark: storing filter state") |
||
301 | -18x - | +|||
109 | +! |
- rlang::hash(list(data = data, modules = modules))+ state$values$filter_state_on_bookmark <- as.list( |
||
302 | -+ | |||
110 | +! |
- }+ slices_global$all_slices(), |
||
303 | -+ | |||
111 | +! |
-
+ recursive = TRUE |
||
304 | +112 |
- #' Go through list and extract bodies of encountered functions as string, recursively.+ ) |
||
305 | +113 |
- #' @keywords internal+ }) |
||
306 | +114 |
- #' @noRd+ |
||
307 | -+ | |||
115 | +80x |
- defunction <- function(x) {+ bookmarked_slices <- restoreValue(session$ns("filter_state_on_bookmark"), NULL) |
||
308 | -219x - | +116 | +80x |
- if (is.list(x)) {+ if (!is.null(bookmarked_slices)) { |
309 | -64x - | +|||
117 | +! |
- lapply(x, defunction)+ logger::log_debug("filter_manager_srv: restoring filter state from bookmark.") |
||
310 | -155x - | +|||
118 | +! |
- } else if (is.function(x)) {+ slices_global$slices_set(bookmarked_slices) |
||
311 | -48x - | +|||
119 | +
- deparse1(body(x))+ } |
|||
312 | +120 |
- } else {+ |
||
313 | -107x - | +121 | +80x |
- x+ mapping_table <- reactive({ |
314 | +122 |
- }+ # We want this to be reactive on slices_global$all_slices() only as get_available_teal_slices() |
||
315 | +123 |
- }+ # is dependent on slices_global$all_slices(). |
||
316 | -+ | |||
124 | +89x |
-
+ module_labels <- setdiff( |
||
317 | -+ | |||
125 | +89x |
- #' Get unique labels+ names(attr(slices_global$all_slices(), "mapping")), |
||
318 | -+ | |||
126 | +89x |
- #'+ "Report previewer" |
||
319 | +127 |
- #' Get unique labels for the modules to avoid namespace conflicts.+ ) |
||
320 | -+ | |||
128 | +89x |
- #'+ isolate({ |
||
321 | -+ | |||
129 | +89x |
- #' @param labels (`character`) vector of labels+ mm <- as.data.frame( |
||
322 | -+ | |||
130 | +89x |
- #'+ sapply( |
||
323 | -+ | |||
131 | +89x |
- #' @return (`character`) vector of unique labels+ module_labels, |
||
324 | -+ | |||
132 | +89x |
- #'+ simplify = FALSE, |
||
325 | -+ | |||
133 | +89x |
- #' @keywords internal+ function(module_label) { |
||
326 | -+ | |||
134 | +102x |
- get_unique_labels <- function(labels) {+ available_slices <- slices_global$module_slices_api[[module_label]]$get_available_teal_slices() |
||
327 | -204x - | +135 | +94x |
- make.unique(gsub("[^[:alnum:]]", "_", tolower(labels)), sep = "_")+ global_ids <- sapply(slices_global$all_slices(), `[[`, "id", simplify = FALSE) |
328 | -+ | |||
136 | +94x |
- }+ module_ids <- sapply(slices_global$slices_get(module_label), `[[`, "id", simplify = FALSE) |
||
329 | -+ | |||
137 | +94x |
-
+ allowed_ids <- vapply(available_slices, `[[`, character(1L), "id") |
||
330 | -+ | |||
138 | +94x |
- #' Remove ANSI escape sequences from a string+ active_ids <- global_ids %in% module_ids |
||
331 | -+ | |||
139 | +94x |
- #' @noRd+ setNames(nm = global_ids, ifelse(global_ids %in% allowed_ids, active_ids, NA)) |
||
332 | +140 |
- strip_style <- function(string) {- |
- ||
333 | -2x - | -
- checkmate::assert_string(string)+ } |
||
334 | +141 | - - | -||
335 | -2x - | -
- gsub(+ ), |
||
336 | -2x - | +142 | +89x |
- "(?:(?:\\x{001b}\\[)|\\x{009b})(?:(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m])|\\x{001b}[A-M]",+ check.names = FALSE |
337 | +143 |
- "",+ ) |
||
338 | -2x - | +144 | +81x |
- string,+ colnames(mm)[colnames(mm) == "global_filters"] <- "Global filters" |
339 | -2x - | +|||
145 | +
- perl = TRUE,+ |
|||
340 | -2x - | +146 | +81x |
- useBytes = TRUE+ mm |
341 | +147 |
- )+ }) |
||
342 | +148 |
- }+ }) |
||
343 | +149 | |||
344 | -+ | |||
150 | +80x |
- #' Convert character list to human readable html with commas and "and"+ output$slices_table <- renderTable( |
||
345 | -+ | |||
151 | +80x |
- #' @noRd+ expr = { |
||
346 | -+ | |||
152 | +89x |
- paste_datanames_character <- function(x,+ logger::log_debug("filter_manager_srv@1 rendering slices_table.") |
||
347 | -+ | |||
153 | +89x |
- tags = list(span = shiny::tags$span, code = shiny::tags$code),+ mm <- mapping_table() |
||
348 | +154 |
- tagList = shiny::tagList) { # nolint: object_name.+ |
||
349 | -38x - | +|||
155 | +
- checkmate::assert_character(x)+ # Display logical values as UTF characters. |
|||
350 | -38x - | +156 | +81x |
- do.call(+ mm[] <- lapply(mm, ifelse, yes = intToUtf8(9989), no = intToUtf8(10060)) |
351 | -38x - | +157 | +81x |
- tagList,+ mm[] <- lapply(mm, function(x) ifelse(is.na(x), intToUtf8(128306), x)) |
352 | -38x - | +|||
158 | +
- lapply(seq_along(x), function(.ix) {+ |
|||
353 | -46x - | +|||
159 | +
- tagList(+ # Display placeholder if no filters defined. |
|||
354 | -46x - | +160 | +81x |
- tags$code(x[.ix]),+ if (nrow(mm) == 0L) { |
355 | -46x - | +161 | +57x |
- if (.ix != length(x)) {+ mm <- data.frame(`Filter manager` = "No filters specified.", check.names = FALSE) |
356 | -8x - | +162 | +57x |
- tags$span(ifelse(.ix == length(x) - 1, " and ", ", "))+ rownames(mm) <- "" |
357 | +163 |
} |
||
358 | -+ | |||
164 | +81x |
- )+ mm |
||
359 | +165 |
- })+ }, |
||
360 | -+ | |||
166 | +80x |
- )+ rownames = TRUE |
||
361 | +167 |
- }+ ) |
||
362 | +168 | |||
363 | -+ | |||
169 | +80x |
- #' Build datanames error string for error message+ mapping_table # for testing purpose |
||
364 | +170 |
- #'+ }) |
||
365 | +171 |
- #' tags and tagList are overwritten in arguments allowing to create strings for+ } |
||
366 | +172 |
- #' logging purposes+ |
||
367 | +173 |
- #' @noRd+ #' @rdname module_filter_manager |
||
368 | +174 |
- build_datanames_error_message <- function(label = NULL,+ srv_module_filter_manager <- function(id, module_fd, slices_global) { |
||
369 | -+ | |||
175 | +105x |
- datanames,+ checkmate::assert_string(id) |
||
370 | -+ | |||
176 | +105x |
- extra_datanames,+ assert_reactive(module_fd) |
||
371 | -+ | |||
177 | +105x |
- tags = list(span = shiny::tags$span, code = shiny::tags$code),+ checkmate::assert_class(slices_global, ".slicesGlobal") |
||
372 | +178 |
- tagList = shiny::tagList) { # nolint: object_name.+ |
||
373 | -20x - | +179 | +105x |
- tags$span(+ moduleServer(id, function(input, output, session) { |
374 | -20x - | +180 | +105x |
- tags$span(ifelse(length(extra_datanames) > 1, "Datasets", "Dataset")),+ logger::log_debug("srv_module_filter_manager initializing for module: { id }.") |
375 | -20x - | +|||
181 | +
- paste_datanames_character(extra_datanames, tags, tagList),+ # Track filter global and local states. |
|||
376 | -20x - | +182 | +105x |
- tags$span(+ slices_global_module <- reactive({ |
377 | -20x - | +183 | +189x | +
+ slices_global$slices_get(module_label = id)+ |
+
184 | +
- paste0(+ }) |
|||
378 | -20x - | +185 | +105x | +
+ slices_module <- reactive(req(module_fd())$get_filter_state())+ |
+
186 | +
- ifelse(length(extra_datanames) > 1, "are missing", "is missing"),+ |
|||
379 | -20x - | +187 | +105x |
- ifelse(is.null(label), ".", sprintf(" for tab '%s'.", label))+ module_fd_previous <- reactiveVal(NULL) |
380 | +188 |
- )+ |
||
381 | +189 |
- ),+ # Set (reactively) available filters for the module. |
||
382 | -20x - | +190 | +105x |
- if (length(datanames) >= 1) {+ obs1 <- observeEvent(module_fd(), priority = 1, { |
383 | -18x - | +191 | +86x |
- tagList(+ logger::log_debug("srv_module_filter_manager@1 setting initial slices for module: { id }.") |
384 | -18x - | +|||
192 | +
- tags$span(ifelse(length(datanames) > 1, "Datasets", "Dataset")),+ # Filters relevant for the module in module-specific app. |
|||
385 | -18x - | +193 | +86x |
- tags$span("available in data:"),+ slices <- slices_global_module() |
386 | -18x - | +|||
194 | +
- tagList(+ |
|||
387 | -18x - | +|||
195 | +
- tags$span(+ # Clean up previous filter states and refresh cache of previous module_fd with current |
|||
388 | -18x - | +196 | +3x |
- paste_datanames_character(datanames, tags, tagList),+ if (!is.null(module_fd_previous())) module_fd_previous()$finalize() |
389 | -18x - | +197 | +86x |
- tags$span(".", .noWS = "outside"),+ module_fd_previous(module_fd()) |
390 | -18x - | +|||
198 | +
- .noWS = c("outside")+ |
|||
391 | +199 |
- )+ # Setting filter states from slices_global: |
||
392 | +200 |
- )+ # 1. when app initializes slices_global set to initial filters (specified by app developer) |
||
393 | +201 |
- )+ # 2. when data reinitializes slices_global reflects latest filter states |
||
394 | +202 |
- } else {+ |
||
395 | -2x - | +203 | +86x |
- tags$span("No datasets are available in data.")+ module_fd()$set_filter_state(slices) |
396 | +204 |
- }+ |
||
397 | +205 |
- )+ # irrelevant filters are discarded in FilteredData$set_available_teal_slices |
||
398 | +206 |
- }+ # it means we don't need to subset slices_global$all_slices() from filters refering to irrelevant datasets |
1 | -+ | |||
207 | +86x |
- #' `teal` main module+ module_fd()$set_available_teal_slices(slices_global$all_slices) |
||
2 | +208 |
- #'+ |
||
3 | +209 |
- #' @description+ # this needed in filter_manager_srv |
||
4 | -+ | |||
210 | +86x |
- #' `r lifecycle::badge("stable")`+ slices_global$module_slices_api_set( |
||
5 | -+ | |||
211 | +86x |
- #' Module to create a `teal` app. This module can be called directly instead of [init()] and+ id, |
||
6 | -+ | |||
212 | +86x |
- #' included in your custom application. Please note that [init()] adds `reporter_previewer_module`+ list( |
||
7 | -+ | |||
213 | +86x |
- #' automatically, which is not a case when calling `ui/srv_teal` directly.+ get_available_teal_slices = module_fd()$get_available_teal_slices(), |
||
8 | -+ | |||
214 | +86x |
- #'+ set_filter_state = module_fd()$set_filter_state, # for testing purpose |
||
9 | -+ | |||
215 | +86x |
- #' @details+ get_filter_state = module_fd()$get_filter_state # for testing purpose |
||
10 | +216 |
- #'+ ) |
||
11 | +217 |
- #' Module is responsible for creating the main `shiny` app layout and initializing all the necessary+ ) |
||
12 | +218 |
- #' components. This module establishes reactive connection between the input `data` and every other+ }) |
||
13 | +219 |
- #' component in the app. Reactive change of the `data` passed as an argument, reloads the app and+ |
||
14 | +220 |
- #' possibly keeps all input settings the same so the user can continue where one left off.+ # Update global state and mapping matrix when module filters change. |
||
15 | -+ | |||
221 | +105x |
- #'+ obs2 <- observeEvent(slices_module(), priority = 0, { |
||
16 | -+ | |||
222 | +108x |
- #' ## data flow in `teal` application+ this_slices <- slices_module() |
||
17 | -+ | |||
223 | +108x |
- #'+ slices_global$slices_append(this_slices) # append new slices to the all_slices list |
||
18 | -+ | |||
224 | +108x |
- #' This module supports multiple data inputs but eventually, they are all converted to `reactive`+ mapping_elem <- setNames(nm = id, list(vapply(this_slices, `[[`, character(1L), "id"))) |
||
19 | -+ | |||
225 | +108x |
- #' returning `teal_data` in this module. On this `reactive teal_data` object several actions are+ slices_global$slices_active(mapping_elem) |
||
20 | +226 |
- #' performed:+ }) |
||
21 | +227 |
- #' - data loading in [`module_init_data`]+ |
||
22 | -+ | |||
228 | +105x |
- #' - data filtering in [`module_filter_data`]+ obs3 <- observeEvent(slices_global_module(), { |
||
23 | -+ | |||
229 | +128x |
- #' - data transformation in [`module_transform_data`]+ global_vs_module <- setdiff_teal_slices(slices_global_module(), slices_module()) |
||
24 | -+ | |||
230 | +128x |
- #'+ module_vs_global <- setdiff_teal_slices(slices_module(), slices_global_module()) |
||
25 | -+ | |||
231 | +119x |
- #' ## Fallback on failure+ if (length(global_vs_module) || length(module_vs_global)) { |
||
26 | +232 |
- #'+ # Comment: (Nota Bene) Normally new filters for a module are added through module-filter-panel, and slices |
||
27 | +233 |
- #' `teal` is designed in such way that app will never crash if the error is introduced in any+ # global are updated automatically so slices_module -> slices_global_module are equal. |
||
28 | +234 |
- #' custom `shiny` module provided by app developer (e.g. [teal_data_module()], [teal_transform_module()]).+ # this if is valid only when a change is made on the global level so the change needs to be propagated down |
||
29 | +235 |
- #' If any module returns a failing object, the app will halt the evaluation and display a warning message.+ # to the module (for example through snapshot manager). If it happens both slices are different |
||
30 | -+ | |||
236 | +13x |
- #' App user should always have a chance to fix the improper input and continue without restarting the session.+ logger::log_debug("srv_module_filter_manager@3 (N.B.) global state has changed for a module:{ id }.") |
||
31 | -+ | |||
237 | +13x |
- #'+ module_fd()$clear_filter_states() |
||
32 | -+ | |||
238 | +13x |
- #' @rdname module_teal+ module_fd()$set_filter_state(slices_global_module()) |
||
33 | +239 |
- #' @name module_teal+ } |
||
34 | +240 |
- #'+ }) |
||
35 | +241 |
- #' @inheritParams module_init_data+ |
||
36 | -+ | |||
242 | +105x |
- #' @inheritParams init+ slices_module # returned for testing purpose |
||
37 | +243 |
- #'+ }) |
||
38 | +244 |
- #' @return `NULL` invisibly+ } |
||
39 | +245 |
- NULL+ |
||
40 | +246 |
-
+ #' @importFrom shiny reactiveVal reactiveValues |
||
41 | +247 |
- #' @rdname module_teal+ methods::setOldClass("reactiveVal") |
||
42 | +248 |
- #' @export+ methods::setOldClass("reactivevalues") |
||
43 | +249 |
- ui_teal <- function(id,+ |
||
44 | +250 |
- modules,+ #' @importFrom methods new |
||
45 | +251 |
- title = build_app_title(),+ #' @rdname module_filter_manager |
||
46 | +252 |
- header = tags$p(),+ .slicesGlobal <- methods::setRefClass(".slicesGlobal", # nolint: object_name. |
||
47 | +253 |
- footer = tags$p()) {- |
- ||
48 | -! | -
- checkmate::assert_character(id, max.len = 1, any.missing = FALSE)- |
- ||
49 | -! | -
- checkmate::assert(- |
- ||
50 | -! | -
- .var.name = "title",+ fields = list( |
||
51 | -! | +|||
254 | +
- checkmate::check_string(title),+ all_slices = "reactiveVal", |
|||
52 | -! | +|||
255 | +
- checkmate::check_multi_class(title, c("shiny.tag", "shiny.tag.list", "html"))+ module_slices_api = "reactivevalues" |
|||
53 | +256 |
- )+ ), |
||
54 | -! | +|||
257 | +
- checkmate::assert(+ methods = list( |
|||
55 | -! | +|||
258 | +
- .var.name = "header",+ initialize = function(slices = teal_slices(), module_labels) { |
|||
56 | -! | +|||
259 | +80x |
- checkmate::check_string(header),+ shiny::isolate({ |
||
57 | -! | +|||
260 | +80x |
- checkmate::check_multi_class(header, c("shiny.tag", "shiny.tag.list", "html"))+ checkmate::assert_class(slices, "teal_slices") |
||
58 | +261 |
- )- |
- ||
59 | -! | -
- checkmate::assert(+ # needed on init to not mix "global_filters" with module-specific-slots |
||
60 | -! | +|||
262 | +80x |
- .var.name = "footer",+ if (isTRUE(attr(slices, "module_specific"))) { |
||
61 | -! | +|||
263 | +11x |
- checkmate::check_string(footer),+ old_mapping <- attr(slices, "mapping") |
||
62 | -! | +|||
264 | +11x |
- checkmate::check_multi_class(footer, c("shiny.tag", "shiny.tag.list", "html"))+ new_mapping <- sapply(module_labels, simplify = FALSE, function(module_label) { |
||
63 | -+ | |||
265 | +20x |
- )+ unique(unlist(old_mapping[c(module_label, "global_filters")])) |
||
64 | +266 | - - | -||
65 | -! | -
- if (is.character(title)) {+ }) |
||
66 | -! | +|||
267 | +11x |
- title <- build_app_title(title)+ attr(slices, "mapping") <- new_mapping |
||
67 | +268 |
- } else {- |
- ||
68 | -! | -
- validate_app_title_tag(title)+ } |
||
69 | -+ | |||
269 | +80x |
- }+ .self$all_slices <<- shiny::reactiveVal(slices) |
||
70 | -+ | |||
270 | +80x |
-
+ .self$module_slices_api <<- shiny::reactiveValues() |
||
71 | -! | +|||
271 | +80x |
- if (checkmate::test_string(header)) {+ .self$slices_append(slices) |
||
72 | -! | +|||
272 | +80x |
- header <- tags$p(header)+ .self$slices_active(attr(slices, "mapping")) |
||
73 | -+ | |||
273 | +80x |
- }+ invisible(.self) |
||
74 | +274 | - - | -||
75 | -! | -
- if (checkmate::test_string(footer)) {- |
- ||
76 | -! | -
- footer <- tags$p(footer)+ }) |
||
77 | +275 |
- }+ }, |
||
78 | +276 |
-
+ is_module_specific = function() { |
||
79 | -! | +|||
277 | +277x |
- ns <- NS(id)+ isTRUE(attr(.self$all_slices(), "module_specific")) |
||
80 | +278 |
-
+ }, |
||
81 | +279 |
- # show busy icon when `shiny` session is busy computing stuff+ module_slices_api_set = function(module_label, functions_list) { |
||
82 | -+ | |||
280 | +86x |
- # based on https://stackoverflow.com/questions/17325521/r-shiny-display-loading-message-while-function-is-running/22475216#22475216 # nolint: line_length.+ shiny::isolate({ |
||
83 | -! | +|||
281 | +86x |
- shiny_busy_message_panel <- conditionalPanel(+ if (!.self$is_module_specific()) { |
||
84 | -! | +|||
282 | +70x |
- condition = "(($('html').hasClass('shiny-busy')) && (document.getElementById('shiny-notification-panel') == null))", # nolint: line_length.+ module_label <- "global_filters" |
||
85 | -! | +|||
283 | +
- tags$div(+ } |
|||
86 | -! | +|||
284 | +86x |
- icon("arrows-rotate", class = "fa-spin", prefer_type = "solid"),+ if (!identical(.self$module_slices_api[[module_label]], functions_list)) { |
||
87 | -! | +|||
285 | +86x |
- "Computing ...",+ .self$module_slices_api[[module_label]] <- functions_list |
||
88 | +286 |
- # CSS defined in `custom.css`+ } |
||
89 | -! | +|||
287 | +86x |
- class = "shinybusymessage"+ invisible(.self) |
||
90 | +288 |
- )+ }) |
||
91 | +289 |
- )+ }, |
||
92 | +290 |
-
+ slices_deactivate_all = function(module_label) { |
||
93 | +291 | ! |
- fluidPage(+ shiny::isolate({ |
|
94 | +292 | ! |
- id = id,+ new_slices <- .self$all_slices() |
|
95 | +293 | ! |
- title = title,+ old_mapping <- attr(new_slices, "mapping") |
|
96 | -! | +|||
294 | +
- theme = get_teal_bs_theme(),+ |
|||
97 | +295 | ! |
- include_teal_css_js(),+ new_mapping <- if (.self$is_module_specific()) { |
|
98 | +296 | ! |
- tags$header(header),+ new_module_mapping <- setNames(nm = module_label, list(character(0))) |
|
99 | +297 | ! |
- tags$hr(class = "my-2"),+ modifyList(old_mapping, new_module_mapping) |
|
100 | +298 | ! |
- shiny_busy_message_panel,+ } else if (missing(module_label)) { |
|
101 | +299 | ! |
- tags$div(+ lapply( |
|
102 | +300 | ! |
- id = ns("tabpanel_wrapper"),+ attr(.self$all_slices(), "mapping"), |
|
103 | +301 | ! |
- class = "teal-body",+ function(x) character(0) |
|
104 | -! | +|||
302 | +
- ui_teal_module(id = ns("teal_modules"), modules = modules)+ ) |
|||
105 | +303 |
- ),+ } else { |
||
106 | +304 | ! |
- tags$div(+ old_mapping[[module_label]] <- character(0) |
|
107 | +305 | ! |
- id = ns("options_buttons"),+ old_mapping |
|
108 | -! | +|||
306 | +
- style = "position: absolute; right: 10px;",+ } |
|||
109 | -! | +|||
307 | +
- ui_bookmark_panel(ns("bookmark_manager"), modules),+ |
|||
110 | +308 | ! |
- tags$button(+ if (!identical(new_mapping, old_mapping)) { |
|
111 | +309 | ! |
- class = "btn action-button filter_hamburger", # see sidebar.css for style filter_hamburger+ logger::log_debug(".slicesGlobal@slices_deactivate_all: deactivating all slices.") |
|
112 | +310 | ! |
- href = "javascript:void(0)",+ attr(new_slices, "mapping") <- new_mapping |
|
113 | +311 | ! |
- onclick = sprintf("toggleFilterPanel('%s');", ns("tabpanel_wrapper")),+ .self$all_slices(new_slices) |
|
114 | -! | +|||
312 | +
- title = "Toggle filter panel",+ } |
|||
115 | +313 | ! |
- icon("fas fa-bars")+ invisible(.self) |
|
116 | +314 |
- ),- |
- ||
117 | -! | -
- ui_snapshot_manager_panel(ns("snapshot_manager_panel")),+ }) |
||
118 | -! | +|||
315 | +
- ui_filter_manager_panel(ns("filter_manager_panel"))+ }, |
|||
119 | +316 |
- ),+ slices_active = function(mapping_elem) { |
||
120 | -! | +|||
317 | +191x |
- tags$script(+ shiny::isolate({ |
||
121 | -! | -
- HTML(- |
- ||
122 | -! | -
- sprintf(- |
- ||
123 | -- |
- "- |
- ||
124 | -! | -
- $(document).ready(function() {- |
- ||
125 | -! | +|||
318 | +191x |
- $('#%s').appendTo('#%s');+ if (.self$is_module_specific()) { |
||
126 | -+ | |||
319 | +36x |
- });+ new_mapping <- modifyList(attr(.self$all_slices(), "mapping"), mapping_elem) |
||
127 | +320 |
- ",- |
- ||
128 | -! | -
- ns("options_buttons"),- |
- ||
129 | -! | -
- ns("teal_modules-active_tab")+ } else { |
||
130 | -+ | |||
321 | +155x |
- )+ new_mapping <- setNames(nm = "global_filters", list(unique(unlist(mapping_elem)))) |
||
131 | +322 |
- )+ } |
||
132 | +323 |
- ),- |
- ||
133 | -! | -
- tags$hr(),- |
- ||
134 | -! | -
- tags$footer(+ |
||
135 | -! | +|||
324 | +191x |
- tags$div(+ if (!identical(new_mapping, attr(.self$all_slices(), "mapping"))) { |
||
136 | -! | +|||
325 | +134x |
- footer,+ mapping_modules <- toString(names(new_mapping)) |
||
137 | -! | +|||
326 | +134x |
- teal.widgets::verbatim_popup_ui(ns("sessionInfo"), "Session Info", type = "link"),+ logger::log_debug(".slicesGlobal@slices_active: changing mapping for module(s): { mapping_modules }.") |
||
138 | -! | +|||
327 | +134x |
- br(),+ new_slices <- .self$all_slices() |
||
139 | -! | +|||
328 | +134x |
- ui_teal_lockfile(ns("lockfile")),+ attr(new_slices, "mapping") <- new_mapping |
||
140 | -! | +|||
329 | +134x |
- textOutput(ns("identifier"))+ .self$all_slices(new_slices) |
||
141 | +330 |
- )+ } |
||
142 | +331 |
- )+ |
||
143 | -+ | |||
332 | +191x |
- )+ invisible(.self) |
||
144 | +333 |
- }+ }) |
||
145 | +334 |
-
+ }, |
||
146 | +335 |
- #' @rdname module_teal+ # - only new filters are appended to the $all_slices |
||
147 | +336 |
- #' @export+ # - mapping is not updated here |
||
148 | +337 |
- srv_teal <- function(id, data, modules, filter = teal_slices()) {+ slices_append = function(slices, activate = FALSE) { |
||
149 | -79x - | +338 | +191x |
- checkmate::assert_character(id, max.len = 1, any.missing = FALSE)+ shiny::isolate({ |
150 | -79x - | +339 | +191x |
- checkmate::assert_multi_class(data, c("teal_data", "teal_data_module", "reactive"))+ if (!is.teal_slices(slices)) { |
151 | -78x - | +|||
340 | +! |
- checkmate::assert_class(modules, "teal_modules")+ slices <- as.teal_slices(slices) |
||
152 | -78x - | +|||
341 | +
- checkmate::assert_class(filter, "teal_slices")+ } |
|||
153 | +342 | |||
154 | -78x - | +|||
343 | +
- moduleServer(id, function(input, output, session) {+ # to make sure that we don't unnecessary trigger $all_slices <reactiveVal> |
|||
155 | -78x - | +344 | +191x |
- logger::log_debug("srv_teal initializing.")+ new_slices <- setdiff_teal_slices(slices, .self$all_slices()) |
156 | -+ | |||
345 | +191x |
-
+ old_mapping <- attr(.self$all_slices(), "mapping") |
||
157 | -78x - | +346 | +191x |
- if (getOption("teal.show_js_log", default = FALSE)) {+ if (length(new_slices)) { |
158 | -! | +|||
347 | +6x |
- shinyjs::showLog()+ new_ids <- vapply(new_slices, `[[`, character(1L), "id") |
||
159 | -+ | |||
348 | +6x |
- }+ logger::log_debug(".slicesGlobal@slices_append: appending new slice(s): { new_ids }.") |
||
160 | -+ | |||
349 | +6x |
-
+ slices_ids <- vapply(.self$all_slices(), `[[`, character(1L), "id") |
||
161 | -78x - | +350 | +6x |
- srv_teal_lockfile("lockfile")+ lapply(new_slices, function(slice) { |
162 | +351 |
-
+ # In case the new state has the same id as an existing one, add a suffix |
||
163 | -78x - | +352 | +6x |
- output$identifier <- renderText(+ if (slice$id %in% slices_ids) { |
164 | -78x - | +353 | +1x |
- paste0("Pid:", Sys.getpid(), " Token:", substr(session$token, 25, 32))+ slice$id <- utils::tail(make.unique(c(slices_ids, slice$id), sep = "_"), 1) |
165 | +354 |
- )+ } |
||
166 | +355 |
-
+ }) |
||
167 | -78x - | +|||
356 | +
- teal.widgets::verbatim_popup_srv(+ |
|||
168 | -78x - | +357 | +6x |
- "sessionInfo",+ new_slices_all <- c(.self$all_slices(), new_slices) |
169 | -78x - | +358 | +6x |
- verbatim_content = utils::capture.output(utils::sessionInfo()),+ attr(new_slices_all, "mapping") <- old_mapping |
170 | -78x - | +359 | +6x |
- title = "SessionInfo"+ .self$all_slices(new_slices_all) |
171 | +360 |
- )+ } |
||
172 | +361 | |||
173 | -- |
- # `JavaScript` code- |
- ||
174 | -78x - | +362 | +191x |
- run_js_files(files = "init.js")+ invisible(.self) |
175 | +363 |
-
+ }) |
||
176 | +364 |
- # set timezone in shiny app+ }, |
||
177 | +365 |
- # timezone is set in the early beginning so it will be available also+ slices_get = function(module_label) { |
||
178 | -+ | |||
366 | +283x |
- # for `DDL` and all shiny modules+ if (missing(module_label)) { |
||
179 | -78x - | +|||
367 | +! |
- get_client_timezone(session$ns)+ .self$all_slices() |
||
180 | -78x - | +|||
368 | +
- observeEvent(+ } else { |
|||
181 | -78x - | +369 | +283x |
- eventExpr = input$timezone,+ module_ids <- unlist(attr(.self$all_slices(), "mapping")[c(module_label, "global_filters")]) |
182 | -78x - | +370 | +283x |
- once = TRUE,+ Filter( |
183 | -78x - | +371 | +283x |
- handlerExpr = {+ function(slice) slice$id %in% module_ids, |
184 | -! | +|||
372 | +283x |
- session$userData$timezone <- input$timezone+ .self$all_slices() |
||
185 | -! | +|||
373 | +
- logger::log_debug("srv_teal@1 Timezone set to client's timezone: { input$timezone }.")+ ) |
|||
186 | +374 |
} |
||
187 | +375 |
- )+ }, |
||
188 | +376 |
-
+ slices_set = function(slices) { |
||
189 | -78x - | +377 | +7x |
- data_pulled <- srv_init_data("data", data = data)+ shiny::isolate({ |
190 | -77x - | +378 | +7x |
- data_validated <- srv_validate_reactive_teal_data(+ if (!is.teal_slices(slices)) { |
191 | -77x - | +|||
379 | +! |
- "validate",+ slices <- as.teal_slices(slices) |
||
192 | -77x - | +|||
380 | +
- data = data_pulled,+ } |
|||
193 | -77x - | +381 | +7x |
- modules = modules,+ .self$all_slices(slices) |
194 | -77x - | +382 | +7x |
- validate_shiny_silent_error = FALSE+ invisible(.self) |
195 | +383 |
- )+ }) |
||
196 | -77x - | +|||
384 | +
- data_rv <- reactive({+ }, |
|||
197 | -132x - | +|||
385 | +
- req(inherits(data_validated(), "teal_data"))+ show = function() { |
|||
198 | -65x - | +|||
386 | +! |
- is_filter_ok <- check_filter_datanames(filter, ls(teal.code::get_env(data_validated())))+ shiny::isolate(print(.self$all_slices())) |
||
199 | -65x - | +|||
387 | +! |
- if (!isTRUE(is_filter_ok)) {+ invisible(.self) |
||
200 | -2x - | +|||
388 | +
- showNotification(+ } |
|||
201 | -2x - | +|||
389 | +
- "Some filters were not applied because of incompatibility with data. Contact app developer.",+ ) |
|||
202 | -2x - | +|||
390 | +
- type = "warning",+ ) |
|||
203 | -2x - | +|||
391 | +
- duration = 10+ # todo: prevent any teal_slices attribute except mapping |
204 | +1 |
- )+ #' Calls all `modules` |
|
205 | -2x - | +||
2 | +
- warning(is_filter_ok)+ #' |
||
206 | +3 |
- }+ #' On the UI side each `teal_modules` is translated to a `tabsetPanel` and each `teal_module` is a |
|
207 | -65x - | +||
4 | +
- .add_signature_to_data(data_validated())+ #' `tabPanel`. Both, UI and server are called recursively so that each tab is a separate module and |
||
208 | +5 |
- })+ #' reflect nested structure of `modules` argument. |
|
209 | +6 |
-
+ #' |
|
210 | -77x - | +||
7 | +
- data_load_status <- reactive({+ #' @name module_teal_module |
||
211 | -70x - | +||
8 | +
- if (inherits(data_pulled(), "teal_data")) {+ #' |
||
212 | -65x - | +||
9 | +
- "ok"+ #' @inheritParams module_teal |
||
213 | -5x - | -
- } else if (inherits(data, "teal_data_module")) {- |
- |
214 | -5x - | +||
10 | +
- "teal_data_module failed"+ #' |
||
215 | +11 |
- } else {+ #' @param data_rv (`reactive` returning `teal_data`) |
|
216 | -! | +||
12 | +
- "external failed"+ #' |
||
217 | +13 |
- }+ #' @param slices_global (`reactiveVal` returning `modules_teal_slices`) |
|
218 | +14 |
- })+ #' see [`module_filter_manager`] |
|
219 | +15 |
-
+ #' |
|
220 | -77x - | +||
16 | +
- datasets_rv <- if (!isTRUE(attr(filter, "module_specific"))) {+ #' @param depth (`integer(1)`) |
||
221 | -66x - | +||
17 | +
- eventReactive(data_rv(), {+ #' number which helps to determine depth of the modules nesting. |
||
222 | -56x - | +||
18 | +
- req(inherits(data_rv(), "teal_data"))+ #' |
||
223 | -56x - | +||
19 | +
- logger::log_debug("srv_teal@1 initializing FilteredData")+ #' @param datasets (`reactive` returning `FilteredData` or `NULL`) |
||
224 | -56x - | +||
20 | +
- teal_data_to_filtered_data(data_rv())+ #' When `datasets` is passed from the parent module (`srv_teal`) then `dataset` is a singleton |
||
225 | +21 |
- })+ #' which implies in filter-panel to be "global". When `NULL` then filter-panel is "module-specific". |
|
226 | +22 |
- }+ #' |
|
227 | +23 |
-
+ #' @param data_load_status (`reactive` returning `character`) |
|
228 | -77x - | +||
24 | +
- if (inherits(data, "teal_data_module")) {+ #' Determines action dependent on a data loading status: |
||
229 | -9x - | +||
25 | +
- setBookmarkExclude(c("teal_modules-active_tab"))+ #' - `"ok"` when `teal_data` is returned from the data loading. |
||
230 | -9x - | +||
26 | +
- shiny::insertTab(+ #' - `"teal_data_module failed"` when [teal_data_module()] didn't return `teal_data`. Disables tabs buttons. |
||
231 | -9x - | +||
27 | +
- inputId = "teal_modules-active_tab",+ #' - `"external failed"` when a `reactive` passed to `srv_teal(data)` didn't return `teal_data`. Hides the whole tab |
||
232 | -9x - | +||
28 | +
- position = "before",+ #' panel. |
||
233 | -9x - | +||
29 | +
- select = TRUE,+ #' |
||
234 | -9x - | +||
30 | +
- tabPanel(+ #' @return |
||
235 | -9x - | +||
31 | +
- title = icon("fas fa-database"),+ #' output of currently active module. |
||
236 | -9x - | +||
32 | +
- value = "teal_data_module",+ #' - `srv_teal_module.teal_module` returns `reactiveVal` containing output of the called module. |
||
237 | -9x - | +||
33 | +
- tags$div(+ #' - `srv_teal_module.teal_modules` returns output of module selected by `input$active_tab`. |
||
238 | -9x - | +||
34 | +
- ui_init_data(session$ns("data")),+ #' |
||
239 | -9x - | +||
35 | +
- ui_validate_reactive_teal_data(session$ns("validate"))+ #' @keywords internal |
||
240 | +36 |
- )+ NULL |
|
241 | +37 |
- )+ |
|
242 | +38 |
- )+ #' @rdname module_teal_module |
|
243 | +39 |
-
+ ui_teal_module <- function(id, modules, depth = 0L) { |
|
244 | -9x - | +||
40 | +! |
- if (attr(data, "once")) {+ checkmate::assert_multi_class(modules, c("teal_modules", "teal_module", "shiny.tag")) |
|
245 | -9x - | +||
41 | +! |
- observeEvent(data_rv(), once = TRUE, {+ checkmate::assert_count(depth) |
|
246 | -4x - | +||
42 | +! |
- logger::log_debug("srv_teal@2 removing data tab.")+ UseMethod("ui_teal_module", modules) |
|
247 | +43 |
- # when once = TRUE we pull data once and then remove data tab- |
- |
248 | -4x - | -
- removeTab("teal_modules-active_tab", target = "teal_data_module")+ } |
|
249 | +44 |
- })+ |
|
250 | +45 |
- }+ #' @rdname module_teal_module |
|
251 | +46 |
- } else {+ #' @export |
|
252 | +47 |
- # when no teal_data_module then we want to display messages above tabsetPanel (because there is no data-tab)+ ui_teal_module.default <- function(id, modules, depth = 0L) { |
|
253 | -68x - | +||
48 | +! |
- insertUI(+ stop("Modules class not supported: ", paste(class(modules), collapse = " ")) |
|
254 | -68x - | +||
49 | +
- selector = sprintf("#%s", session$ns("tabpanel_wrapper")),+ } |
||
255 | -68x - | +||
50 | +
- where = "beforeBegin",+ |
||
256 | -68x - | +||
51 | +
- ui = tags$div(ui_validate_reactive_teal_data(session$ns("validate")), tags$br())+ #' @rdname module_teal_module |
||
257 | +52 |
- )+ #' @export |
|
258 | +53 |
- }+ ui_teal_module.teal_modules <- function(id, modules, depth = 0L) { |
|
259 | -+ | ||
54 | +! |
-
+ ns <- NS(id) |
|
260 | -77x - | +||
55 | +! |
- module_labels <- unlist(module_labels(modules), use.names = FALSE)+ tags$div( |
|
261 | -77x - | +||
56 | +! |
- slices_global <- methods::new(".slicesGlobal", filter, module_labels)+ id = ns("wrapper"), |
|
262 | -77x - | +||
57 | +! |
- modules_output <- srv_teal_module(+ do.call( |
|
263 | -77x - | +||
58 | +! |
- id = "teal_modules",+ tabsetPanel, |
|
264 | -77x - | +||
59 | +! |
- data_rv = data_rv,+ c( |
|
265 | -77x - | +||
60 | +
- datasets = datasets_rv,+ # by giving an id, we can reactively respond to tab changes |
||
266 | -77x - | +||
61 | +! |
- modules = modules,+ list( |
|
267 | -77x - | +||
62 | +! |
- slices_global = slices_global,+ id = ns("active_tab"), |
|
268 | -77x - | +||
63 | +! |
- data_load_status = data_load_status+ type = if (modules$label == "root") "pills" else "tabs" |
|
269 | +64 |
- )+ ), |
|
270 | -77x - | +||
65 | +! |
- mapping_table <- srv_filter_manager_panel("filter_manager_panel", slices_global = slices_global)+ lapply( |
|
271 | -77x - | +||
66 | +! |
- snapshots <- srv_snapshot_manager_panel("snapshot_manager_panel", slices_global = slices_global)+ names(modules$children), |
|
272 | -77x - | +||
67 | +! |
- srv_bookmark_panel("bookmark_manager", modules)+ function(module_id) { |
|
273 | -+ | ||
68 | +! |
- })+ module_label <- modules$children[[module_id]]$label |
|
274 | -+ | ||
69 | +! |
-
+ if (is.null(module_label)) { |
|
275 | -77x - | +||
70 | +! |
- invisible(NULL)+ module_label <- icon("fas fa-database") |
|
276 | +71 |
- }+ } |
1 | -+ | |||
72 | +! |
- #' Filter state snapshot management+ tabPanel( |
||
2 | -+ | |||
73 | +! |
- #'+ title = module_label, |
||
3 | -+ | |||
74 | +! |
- #' Capture and restore snapshots of the global (app) filter state.+ value = module_id, # when clicked this tab value changes input$<tabset panel id> |
||
4 | -+ | |||
75 | +! |
- #'+ ui_teal_module( |
||
5 | -+ | |||
76 | +! |
- #' This module introduces snapshots: stored descriptions of the filter state of the entire application.+ id = ns(module_id), |
||
6 | -+ | |||
77 | +! |
- #' Snapshots allow the user to save the current filter state of the application for later use in the session,+ modules = modules$children[[module_id]], |
||
7 | -+ | |||
78 | +! |
- #' as well as to save it to file in order to share it with an app developer or other users,+ depth = depth + 1L |
||
8 | +79 |
- #' who in turn can upload it to their own session.+ ) |
||
9 | +80 |
- #'+ ) |
||
10 | +81 |
- #' The snapshot manager is accessed with the camera icon in the tabset bar.+ } |
||
11 | +82 |
- #' At the beginning of a session it presents three icons: a camera, an upload, and an circular arrow.+ ) |
||
12 | +83 |
- #' Clicking the camera captures a snapshot, clicking the upload adds a snapshot from a file+ ) |
||
13 | +84 |
- #' and applies the filter states therein, and clicking the arrow resets initial application state.+ ) |
||
14 | +85 |
- #' As snapshots are added, they will show up as rows in a table and each will have a select button and a save button.+ ) |
||
15 | +86 |
- #'+ } |
||
16 | +87 |
- #' @section Server logic:+ |
||
17 | +88 |
- #' Snapshots are basically `teal_slices` objects, however, since each module is served by a separate instance+ #' @rdname module_teal_module |
||
18 | +89 |
- #' of `FilteredData` and these objects require shared state, `teal_slice` is a `reactiveVal` so `teal_slices`+ #' @export |
||
19 | +90 |
- #' cannot be stored as is. Therefore, `teal_slices` are reversibly converted to a list of lists representation+ ui_teal_module.teal_module <- function(id, modules, depth = 0L) { |
||
20 | -+ | |||
91 | +! |
- #' (attributes are maintained).+ ns <- NS(id) |
||
21 | -+ | |||
92 | +! |
- #'+ args <- c(list(id = ns("module")), modules$ui_args) |
||
22 | +93 |
- #' Snapshots are stored in a `reactiveVal` as a named list.+ |
||
23 | -+ | |||
94 | +! |
- #' The first snapshot is the initial state of the application and the user can add a snapshot whenever they see fit.+ ui_teal <- tagList( |
||
24 | -+ | |||
95 | +! |
- #'+ div( |
||
25 | -+ | |||
96 | +! |
- #' For every snapshot except the initial one, a piece of UI is generated that contains+ id = ns("validate_datanames"), |
||
26 | -+ | |||
97 | +! |
- #' the snapshot name, a select button to restore that snapshot, and a save button to save it to a file.+ ui_validate_reactive_teal_data(ns("validate_datanames")) |
||
27 | +98 |
- #' The initial snapshot is restored by a separate "reset" button.+ ), |
||
28 | -+ | |||
99 | +! |
- #' It cannot be saved directly but a user is welcome to capture the initial state as a snapshot and save that.+ shinyjs::hidden( |
||
29 | -+ | |||
100 | +! |
- #'+ tags$div( |
||
30 | -+ | |||
101 | +! |
- #' @section Snapshot mechanics:+ id = ns("transformer_failure_info"), |
||
31 | -+ | |||
102 | +! |
- #' When a snapshot is captured, the user is prompted to name it.+ class = "teal_validated", |
||
32 | -+ | |||
103 | +! |
- #' Names are displayed as is but since they are used to create button ids,+ div( |
||
33 | -+ | |||
104 | +! |
- #' under the hood they are converted to syntactically valid strings.+ class = "teal-output-warning", |
||
34 | -+ | |||
105 | +! |
- #' New snapshot names are validated so that their valid versions are unique.+ "One of transformers failed. Please fix and continue." |
||
35 | +106 |
- #' Leading and trailing white space is trimmed.+ ) |
||
36 | +107 |
- #'+ ) |
||
37 | +108 |
- #' The module can read the global state of the application from `slices_global` and `mapping_matrix`.+ ), |
||
38 | -+ | |||
109 | +! |
- #' The former provides a list of all existing `teal_slice`s and the latter says which slice is active in which module.+ tags$div( |
||
39 | -+ | |||
110 | +! |
- #' Once a name has been accepted, `slices_global` is converted to a list of lists - a snapshot.+ id = ns("teal_module_ui"), |
||
40 | -+ | |||
111 | +! |
- #' The snapshot contains the `mapping` attribute of the initial application state+ do.call(modules$ui, args) |
||
41 | +112 |
- #' (or one that has been restored), which may not reflect the current one,+ ) |
||
42 | +113 |
- #' so `mapping_matrix` is transformed to obtain the current mapping, i.e. a list that,+ ) |
||
43 | +114 |
- #' when passed to the `mapping` argument of [teal_slices()], would result in the current mapping.+ |
||
44 | -+ | |||
115 | +! |
- #' This is substituted as the snapshot's `mapping` attribute and the snapshot is added to the snapshot list.+ div( |
||
45 | -+ | |||
116 | +! |
- #'+ id = id, |
||
46 | -+ | |||
117 | +! |
- #' To restore app state, a snapshot is retrieved from storage and rebuilt into a `teal_slices` object.+ class = "teal_module", |
||
47 | -+ | |||
118 | +! |
- #' Then state of all `FilteredData` objects (provided in `datasets`) is cleared+ uiOutput(ns("data_reactive"), inline = TRUE), |
||
48 | -+ | |||
119 | +! |
- #' and set anew according to the `mapping` attribute of the snapshot.+ tagList( |
||
49 | -+ | |||
120 | +! |
- #' The snapshot is then set as the current content of `slices_global`.+ if (depth >= 2L) tags$div(style = "mt-6"), |
||
50 | -+ | |||
121 | +! |
- #'+ if (!is.null(modules$datanames)) { |
||
51 | -+ | |||
122 | +! |
- #' To save a snapshot, the snapshot is retrieved and reassembled just like for restoring,+ fluidRow( |
||
52 | -+ | |||
123 | +! |
- #' and then saved to file with [slices_store()].+ column(width = 9, ui_teal, class = "teal_primary_col"), |
||
53 | -+ | |||
124 | +! |
- #'+ column( |
||
54 | -+ | |||
125 | +! |
- #' When a snapshot is uploaded, it will first be added to storage just like a newly created one,+ width = 3, |
||
55 | -+ | |||
126 | +! |
- #' and then used to restore app state much like a snapshot taken from storage.+ ui_data_summary(ns("data_summary")), |
||
56 | -+ | |||
127 | +! |
- #' Upon clicking the upload icon the user will be prompted for a file to upload+ ui_filter_data(ns("filter_panel")), |
||
57 | -+ | |||
128 | +! |
- #' and may choose to name the new snapshot. The name defaults to the name of the file (the extension is dropped)+ ui_transform_data(ns("data_transform"), transformers = modules$transformers, class = "well"), |
||
58 | -+ | |||
129 | +! |
- #' and normal naming rules apply. Loading the file yields a `teal_slices` object,+ class = "teal_secondary_col" |
||
59 | +130 |
- #' which is disassembled for storage and used directly for restoring app state.+ ) |
||
60 | +131 |
- #'+ ) |
||
61 | +132 |
- #' @section Transferring snapshots:+ } else { |
||
62 | -+ | |||
133 | +! |
- #' Snapshots uploaded from disk should only be used in the same application they come from,+ div( |
||
63 | -+ | |||
134 | +! |
- #' _i.e._ an application that uses the same data and the same modules.+ div( |
||
64 | -+ | |||
135 | +! |
- #' To ensure this is the case, `init` stamps `teal_slices` with an app id that is stored in the `app_id` attribute of+ class = "teal_validated", |
||
65 | -+ | |||
136 | +! |
- #' a `teal_slices` object. When a snapshot is restored from file, its `app_id` is compared to that+ uiOutput(ns("data_input_error")) |
||
66 | +137 |
- #' of the current app state and only if the match is the snapshot admitted to the session.+ ), |
||
67 | -+ | |||
138 | +! |
- #'+ ui_teal |
||
68 | +139 |
- #' @section Bookmarks:+ ) |
||
69 | +140 |
- #' An `onBookmark` callback creates a snapshot of the current filter state.+ } |
||
70 | +141 |
- #' This is done on the app session, not the module session.+ ) |
||
71 | +142 |
- #' (The snapshot will be retrieved by `module_teal` in order to set initial app state in a restored app.)+ ) |
||
72 | +143 |
- #' Then that snapshot, and the previous snapshot history are dumped into the `values.rds` file in `<bookmark_dir>`.+ } |
||
73 | +144 |
- #'+ |
||
74 | +145 |
- #' @param id (`character(1)`) `shiny` module instance id.+ #' @rdname module_teal_module |
||
75 | +146 |
- #' @param slices_global (`reactiveVal`) that contains a `teal_slices` object+ srv_teal_module <- function(id, |
||
76 | +147 |
- #' containing all `teal_slice`s existing in the app, both active and inactive.+ data_rv, |
||
77 | +148 |
- #'+ modules, |
||
78 | +149 |
- #' @return `list` containing the snapshot history, where each element is an unlisted `teal_slices` object.+ datasets = NULL, |
||
79 | +150 |
- #'+ slices_global, |
||
80 | +151 |
- #' @name module_snapshot_manager+ reporter = teal.reporter::Reporter$new(), |
||
81 | +152 |
- #' @rdname module_snapshot_manager+ data_load_status = reactive("ok"), |
||
82 | +153 |
- #'+ is_active = reactive(TRUE)) { |
||
83 | -+ | |||
154 | +185x |
- #' @author Aleksander Chlebowski+ checkmate::assert_string(id) |
||
84 | -+ | |||
155 | +185x |
- #' @keywords internal+ assert_reactive(data_rv) |
||
85 | -+ | |||
156 | +185x |
- NULL+ checkmate::assert_multi_class(modules, c("teal_modules", "teal_module")) |
||
86 | -+ | |||
157 | +185x |
-
+ assert_reactive(datasets, null.ok = TRUE) |
||
87 | -+ | |||
158 | +185x |
- #' @rdname module_snapshot_manager+ checkmate::assert_class(slices_global, ".slicesGlobal") |
||
88 | -+ | |||
159 | +185x |
- ui_snapshot_manager_panel <- function(id) {+ checkmate::assert_class(reporter, "Reporter") |
||
89 | -! | +|||
160 | +185x |
- ns <- NS(id)+ assert_reactive(data_load_status) |
||
90 | -! | +|||
161 | +185x |
- tags$button(+ UseMethod("srv_teal_module", modules) |
||
91 | -! | +|||
162 | +
- id = ns("show_snapshot_manager"),+ } |
|||
92 | -! | +|||
163 | +
- class = "btn action-button wunder_bar_button",+ |
|||
93 | -! | +|||
164 | +
- title = "View filter mapping",+ #' @rdname module_teal_module |
|||
94 | -! | +|||
165 | +
- suppressMessages(icon("fas fa-camera"))+ #' @export |
|||
95 | +166 |
- )+ srv_teal_module.default <- function(id, |
||
96 | +167 |
- }+ data_rv, |
||
97 | +168 |
-
+ modules, |
||
98 | +169 |
- #' @rdname module_snapshot_manager+ datasets = NULL, |
||
99 | +170 |
- srv_snapshot_manager_panel <- function(id, slices_global) {+ slices_global, |
||
100 | -77x - | +|||
171 | +
- moduleServer(id, function(input, output, session) {+ reporter = teal.reporter::Reporter$new(), |
|||
101 | -77x - | +|||
172 | +
- logger::log_debug("srv_snapshot_manager_panel initializing")+ data_load_status = reactive("ok"), |
|||
102 | -77x - | +|||
173 | +
- setBookmarkExclude(c("show_snapshot_manager"))- |
- |||
103 | -77x - | -
- observeEvent(input$show_snapshot_manager, {+ is_active = reactive(TRUE)) { |
||
104 | -! | -
- logger::log_debug("srv_snapshot_manager_panel@1 show_snapshot_manager button has been clicked.")- |
- ||
105 | +174 | ! |
- showModal(+ stop("Modules class not supported: ", paste(class(modules), collapse = " ")) |
|
106 | -! | +|||
175 | +
- modalDialog(+ } |
|||
107 | -! | +|||
176 | +
- ui_snapshot_manager(session$ns("module")),+ |
|||
108 | -! | +|||
177 | +
- class = "snapshot_manager_modal",+ #' @rdname module_teal_module |
|||
109 | -! | +|||
178 | +
- size = "m",+ #' @export |
|||
110 | -! | +|||
179 | +
- footer = NULL,+ srv_teal_module.teal_modules <- function(id, |
|||
111 | -! | +|||
180 | +
- easyClose = TRUE+ data_rv, |
|||
112 | +181 |
- )+ modules, |
||
113 | +182 |
- )+ datasets = NULL, |
||
114 | +183 |
- })+ slices_global, |
||
115 | -77x - | +|||
184 | +
- srv_snapshot_manager("module", slices_global = slices_global)+ reporter = teal.reporter::Reporter$new(), |
|||
116 | +185 |
- })+ data_load_status = reactive("ok"), |
||
117 | +186 |
- }+ is_active = reactive(TRUE)) { |
||
118 | -+ | |||
187 | +80x |
-
+ moduleServer(id = id, module = function(input, output, session) { |
||
119 | -+ | |||
188 | +80x |
- #' @rdname module_snapshot_manager+ logger::log_debug("srv_teal_module.teal_modules initializing the module { deparse1(modules$label) }.") |
||
120 | +189 |
- ui_snapshot_manager <- function(id) {+ |
||
121 | -! | +|||
190 | +80x |
- ns <- NS(id)+ observeEvent(data_load_status(), { |
||
122 | -! | +|||
191 | +73x |
- tags$div(+ tabs_selector <- sprintf("#%s li a", session$ns("active_tab")) |
||
123 | -! | +|||
192 | +73x |
- class = "manager_content",+ if (identical(data_load_status(), "ok")) { |
||
124 | -! | +|||
193 | +68x |
- tags$div(+ logger::log_debug("srv_teal_module@1 enabling modules tabs.") |
||
125 | -! | +|||
194 | +68x |
- class = "manager_table_row",+ shinyjs::show("wrapper") |
||
126 | -! | +|||
195 | +68x |
- tags$span(tags$b("Snapshot manager")),+ shinyjs::enable(selector = tabs_selector) |
||
127 | -! | +|||
196 | +5x |
- actionLink(ns("snapshot_add"), label = NULL, icon = icon("fas fa-camera"), title = "add snapshot"),+ } else if (identical(data_load_status(), "teal_data_module failed")) { |
||
128 | -! | +|||
197 | +5x |
- actionLink(ns("snapshot_load"), label = NULL, icon = icon("fas fa-upload"), title = "upload snapshot"),+ logger::log_debug("srv_teal_module@1 disabling modules tabs.") |
||
129 | -! | +|||
198 | +5x |
- actionLink(ns("snapshot_reset"), label = NULL, icon = icon("fas fa-undo"), title = "reset initial state"),+ shinyjs::disable(selector = tabs_selector) |
||
130 | +199 | ! |
- NULL+ } else if (identical(data_load_status(), "external failed")) { |
|
131 | -+ | |||
200 | +! |
- ),+ logger::log_debug("srv_teal_module@1 hiding modules tabs.") |
||
132 | +201 | ! |
- uiOutput(ns("snapshot_list"))+ shinyjs::hide("wrapper") |
|
133 | +202 |
- )+ } |
||
134 | +203 |
- }+ }) |
||
135 | +204 | |||
136 | -+ | |||
205 | +80x |
- #' @rdname module_snapshot_manager+ modules_output <- sapply( |
||
137 | -+ | |||
206 | +80x |
- srv_snapshot_manager <- function(id, slices_global) {+ names(modules$children), |
||
138 | -77x - | +207 | +80x |
- checkmate::assert_character(id)+ function(module_id) { |
139 | -+ | |||
208 | +105x |
-
+ srv_teal_module( |
||
140 | -77x - | +209 | +105x |
- moduleServer(id, function(input, output, session) {+ id = module_id, |
141 | -77x - | +210 | +105x |
- logger::log_debug("srv_snapshot_manager initializing")+ data_rv = data_rv, |
142 | -+ | |||
211 | +105x |
-
+ modules = modules$children[[module_id]], |
||
143 | -+ | |||
212 | +105x |
- # Set up bookmarking callbacks ----+ datasets = datasets, |
||
144 | -+ | |||
213 | +105x |
- # Register bookmark exclusions (all buttons and text fields).+ slices_global = slices_global, |
||
145 | -77x - | +214 | +105x |
- setBookmarkExclude(c(+ reporter = reporter, |
146 | -77x - | +215 | +105x |
- "snapshot_add", "snapshot_load", "snapshot_reset",+ is_active = reactive( |
147 | -77x - | +216 | +105x |
- "snapshot_name_accept", "snaphot_file_accept",+ is_active() && |
148 | -77x - | +217 | +105x |
- "snapshot_name", "snapshot_file"+ input$active_tab == module_id && |
149 | -+ | |||
218 | +105x |
- ))+ identical(data_load_status(), "ok") |
||
150 | +219 |
- # Add snapshot history to bookmark.- |
- ||
151 | -77x - | -
- session$onBookmark(function(state) {- |
- ||
152 | -! | -
- logger::log_debug("srv_snapshot_manager@onBookmark: storing snapshot and bookmark history")- |
- ||
153 | -! | -
- state$values$snapshot_history <- snapshot_history() # isolate this?+ ) |
||
154 | +220 |
- })+ ) |
||
155 | +221 |
-
+ }, |
||
156 | -77x - | +222 | +80x |
- ns <- session$ns+ simplify = FALSE |
157 | +223 |
-
+ ) |
||
158 | +224 |
- # Track global filter states ----+ |
||
159 | -77x - | +225 | +80x |
- snapshot_history <- reactiveVal({+ modules_output |
160 | +226 |
- # Restore directly from bookmarked state, if applicable.- |
- ||
161 | -77x - | -
- restoreValue(- |
- ||
162 | -77x - | -
- ns("snapshot_history"),- |
- ||
163 | -77x - | -
- list("Initial application state" = shiny::isolate(as.list(slices_global$all_slices(), recursive = TRUE)))+ }) |
||
164 | +227 |
- )+ } |
||
165 | +228 |
- })+ |
||
166 | +229 |
-
+ #' @rdname module_teal_module |
||
167 | +230 |
- # Snapshot current application state ----+ #' @export |
||
168 | +231 |
- # Name snaphsot.- |
- ||
169 | -77x - | -
- observeEvent(input$snapshot_add, {- |
- ||
170 | -! | -
- logger::log_debug("srv_snapshot_manager: snapshot_add button clicked")- |
- ||
171 | -! | -
- showModal(- |
- ||
172 | -! | -
- modalDialog(- |
- ||
173 | -! | -
- textInput(ns("snapshot_name"), "Name the snapshot", width = "100%", placeholder = "Meaningful, unique name"),- |
- ||
174 | -! | -
- footer = tagList(- |
- ||
175 | -! | -
- actionButton(ns("snapshot_name_accept"), "Accept", icon = icon("far fa-thumbs-up")),+ srv_teal_module.teal_module <- function(id, |
||
176 | -! | +|||
232 | +
- modalButton(label = "Cancel", icon = icon("far fa-thumbs-down"))+ data_rv, |
|||
177 | +233 |
- ),+ modules, |
||
178 | -! | +|||
234 | +
- size = "s"+ datasets = NULL, |
|||
179 | +235 |
- )+ slices_global, |
||
180 | +236 |
- )+ reporter = teal.reporter::Reporter$new(), |
||
181 | +237 |
- })+ data_load_status = reactive("ok"), |
||
182 | +238 |
- # Store snaphsot.+ is_active = reactive(TRUE)) { |
||
183 | -77x - | -
- observeEvent(input$snapshot_name_accept, {- |
- ||
184 | -! | -
- logger::log_debug("srv_snapshot_manager: snapshot_name_accept button clicked")- |
- ||
185 | -! | +239 | +105x |
- snapshot_name <- trimws(input$snapshot_name)+ logger::log_debug("srv_teal_module.teal_module initializing the module: { deparse1(modules$label) }.") |
186 | -! | +|||
240 | +105x |
- if (identical(snapshot_name, "")) {+ moduleServer(id = id, module = function(input, output, session) { |
||
187 | -! | +|||
241 | +105x |
- logger::log_debug("srv_snapshot_manager: snapshot name rejected")+ module_out <- reactiveVal() |
||
188 | -! | +|||
242 | +
- showNotification(+ |
|||
189 | -! | +|||
243 | +105x |
- "Please name the snapshot.",+ active_datanames <- reactive({ |
||
190 | -! | +|||
244 | +82x |
- type = "message"+ .resolve_module_datanames(data = data_rv(), modules = modules) |
||
191 | +245 |
- )- |
- ||
192 | -! | -
- updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name")+ }) |
||
193 | -! | +|||
246 | +105x |
- } else if (is.element(make.names(snapshot_name), make.names(names(snapshot_history())))) {+ if (is.null(datasets)) { |
||
194 | -! | +|||
247 | +20x |
- logger::log_debug("srv_snapshot_manager: snapshot name rejected")+ datasets <- eventReactive(data_rv(), { |
||
195 | -! | +|||
248 | +16x |
- showNotification(+ req(inherits(data_rv(), "teal_data")) |
||
196 | -! | +|||
249 | +16x |
- "This name is in conflict with other snapshot names. Please choose a different one.",+ logger::log_debug("srv_teal_module@1 initializing module-specific FilteredData") |
||
197 | -! | +|||
250 | +16x |
- type = "message"+ teal_data_to_filtered_data(data_rv(), datanames = active_datanames()) |
||
198 | +251 |
- )- |
- ||
199 | -! | -
- updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name")+ }) |
||
200 | +252 |
- } else {- |
- ||
201 | -! | -
- logger::log_debug("srv_snapshot_manager: snapshot name accepted, adding snapshot")- |
- ||
202 | -! | -
- snapshot <- as.list(slices_global$all_slices(), recursive = TRUE)- |
- ||
203 | -! | -
- snapshot_update <- c(snapshot_history(), list(snapshot))- |
- ||
204 | -! | -
- names(snapshot_update)[length(snapshot_update)] <- snapshot_name+ } |
||
205 | -! | +|||
253 | +
- snapshot_history(snapshot_update)+ |
|||
206 | -! | +|||
254 | +
- removeModal()+ # manage module filters on the module level |
|||
207 | +255 |
- # Reopen filter manager modal by clicking button in the main application.+ # important: |
||
208 | -! | +|||
256 | +
- shinyjs::click(id = "teal-wunder_bar-show_snapshot_manager", asis = TRUE)+ # filter_manager_module_srv needs to be called before filter_panel_srv |
|||
209 | +257 |
- }+ # Because available_teal_slices is used in FilteredData$srv_available_slices (via srv_filter_panel) |
||
210 | +258 |
- })+ # and if it is not set, then it won't be available in the srv_filter_panel |
||
211 | -+ | |||
259 | +105x |
-
+ srv_module_filter_manager(modules$label, module_fd = datasets, slices_global = slices_global) |
||
212 | +260 |
- # Upload a snapshot file ----+ |
||
213 | -+ | |||
261 | +105x |
- # Select file.+ call_once_when(is_active(), { |
||
214 | -77x - | +262 | +79x |
- observeEvent(input$snapshot_load, {+ filtered_teal_data <- srv_filter_data( |
215 | -! | +|||
263 | +79x |
- logger::log_debug("srv_snapshot_manager: snapshot_load button clicked")+ "filter_panel", |
||
216 | -! | +|||
264 | +79x |
- showModal(+ datasets = datasets, |
||
217 | -! | +|||
265 | +79x |
- modalDialog(+ active_datanames = active_datanames, |
||
218 | -! | +|||
266 | +79x |
- fileInput(ns("snapshot_file"), "Choose snapshot file", accept = ".json", width = "100%"),+ data_rv = data_rv, |
||
219 | -! | +|||
267 | +79x |
- textInput(+ is_active = is_active |
||
220 | -! | +|||
268 | +
- ns("snapshot_name"),+ ) |
|||
221 | -! | +|||
269 | +79x |
- "Name the snapshot (optional)",+ is_transformer_failed <- reactiveValues() |
||
222 | -! | +|||
270 | +79x |
- width = "100%",+ transformed_teal_data <- srv_transform_data( |
||
223 | -! | +|||
271 | +79x |
- placeholder = "Meaningful, unique name"+ "data_transform", |
||
224 | -+ | |||
272 | +79x |
- ),+ data = filtered_teal_data, |
||
225 | -! | +|||
273 | +79x |
- footer = tagList(+ transformers = modules$transformers, |
||
226 | -! | +|||
274 | +79x |
- actionButton(ns("snaphot_file_accept"), "Accept", icon = icon("far fa-thumbs-up")),+ modules = modules, |
||
227 | -! | +|||
275 | +79x |
- modalButton(label = "Cancel", icon = icon("far fa-thumbs-down"))+ is_transformer_failed = is_transformer_failed |
||
228 | +276 |
- )+ ) |
||
229 | -+ | |||
277 | +79x |
- )+ any_transformer_failed <- reactive({ |
||
230 | -+ | |||
278 | +79x |
- )+ any(unlist(reactiveValuesToList(is_transformer_failed))) |
||
231 | +279 |
- })+ }) |
||
232 | +280 |
- # Store new snapshot to list and restore filter states.+ |
||
233 | -77x - | -
- observeEvent(input$snaphot_file_accept, {- |
- ||
234 | -! | +281 | +79x |
- logger::log_debug("srv_snapshot_manager: snapshot_file_accept button clicked")+ observeEvent(any_transformer_failed(), { |
235 | -! | +|||
282 | +79x |
- snapshot_name <- trimws(input$snapshot_name)+ if (isTRUE(any_transformer_failed())) { |
||
236 | -! | +|||
283 | +6x |
- if (identical(snapshot_name, "")) {+ shinyjs::hide("teal_module_ui") |
||
237 | -! | +|||
284 | +6x |
- logger::log_debug("srv_snapshot_manager: no snapshot name provided, naming after file")+ shinyjs::hide("validate_datanames") |
||
238 | -! | +|||
285 | +6x |
- snapshot_name <- tools::file_path_sans_ext(input$snapshot_file$name)+ shinyjs::show("transformer_failure_info") |
||
239 | +286 |
- }- |
- ||
240 | -! | -
- if (is.element(make.names(snapshot_name), make.names(names(snapshot_history())))) {- |
- ||
241 | -! | -
- logger::log_debug("srv_snapshot_manager: snapshot name rejected")+ } else { |
||
242 | -! | +|||
287 | +73x |
- showNotification(+ shinyjs::show("teal_module_ui") |
||
243 | -! | +|||
288 | +73x |
- "This name is in conflict with other snapshot names. Please choose a different one.",+ shinyjs::show("validate_datanames") |
||
244 | -! | +|||
289 | +73x |
- type = "message"+ shinyjs::hide("transformer_failure_info") |
||
245 | +290 |
- )+ } |
||
246 | -! | +|||
291 | +
- updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name")+ }) |
|||
247 | +292 |
- } else {+ |
||
248 | -+ | |||
293 | +79x |
- # Restore snapshot and verify app compatibility.+ module_teal_data <- reactive({ |
||
249 | -! | +|||
294 | +87x |
- logger::log_debug("srv_snapshot_manager: snapshot name accepted, loading snapshot")+ req(inherits(transformed_teal_data(), "teal_data")) |
||
250 | -! | +|||
295 | +81x |
- snapshot_state <- try(slices_restore(input$snapshot_file$datapath))+ all_teal_data <- transformed_teal_data() |
||
251 | -! | +|||
296 | +81x |
- if (!inherits(snapshot_state, "modules_teal_slices")) {+ module_datanames <- .resolve_module_datanames(data = all_teal_data, modules = modules) |
||
252 | -! | +|||
297 | +81x |
- logger::log_debug("srv_snapshot_manager: snapshot file corrupt")+ .subset_teal_data(all_teal_data, module_datanames) |
||
253 | -! | +|||
298 | +
- showNotification(+ }) |
|||
254 | -! | +|||
299 | +
- "File appears to be corrupt.",+ |
|||
255 | -! | +|||
300 | +79x |
- type = "error"+ srv_validate_reactive_teal_data( |
||
256 | -+ | |||
301 | +79x |
- )+ "validate_datanames", |
||
257 | -! | +|||
302 | +79x |
- } else if (!identical(attr(snapshot_state, "app_id"), attr(slices_global$all_slices(), "app_id"))) {+ data = module_teal_data, |
||
258 | -! | +|||
303 | +79x |
- logger::log_debug("srv_snapshot_manager: snapshot not compatible with app")+ modules = modules |
||
259 | -! | +|||
304 | +
- showNotification(+ ) |
|||
260 | -! | +|||
305 | +
- "This snapshot file is not compatible with the app and cannot be loaded.",+ |
|||
261 | -! | +|||
306 | +79x |
- type = "warning"+ summary_table <- srv_data_summary("data_summary", module_teal_data) |
||
262 | +307 |
- )+ |
||
263 | +308 |
- } else {+ # Call modules. |
||
264 | -+ | |||
309 | +79x |
- # Add to snapshot history.+ if (!inherits(modules, "teal_module_previewer")) { |
||
265 | -! | +|||
310 | +79x |
- logger::log_debug("srv_snapshot_manager: snapshot loaded, adding to history")+ obs_module <- call_once_when( |
||
266 | -! | +|||
311 | +79x |
- snapshot <- as.list(slices_global$all_slices(), recursive = TRUE)+ !is.null(module_teal_data()), |
||
267 | -! | +|||
312 | +79x |
- snapshot_update <- c(snapshot_history(), list(snapshot))+ ignoreNULL = TRUE, |
||
268 | -! | +|||
313 | +79x |
- names(snapshot_update)[length(snapshot_update)] <- snapshot_name+ handlerExpr = { |
||
269 | -! | +|||
314 | +73x |
- snapshot_history(snapshot_update)+ module_out(.call_teal_module(modules, datasets, module_teal_data, reporter)) |
||
270 | +315 |
- ### Begin simplified restore procedure. ###+ } |
||
271 | -! | +|||
316 | +
- logger::log_debug("srv_snapshot_manager: restoring snapshot")+ ) |
|||
272 | -! | +|||
317 | +
- slices_global$slices_set(snapshot_state)+ } else { |
|||
273 | -! | +|||
318 | +
- removeModal()+ # Report previewer must be initiated on app start for report cards to be included in bookmarks. |
|||
274 | +319 |
- ### End simplified restore procedure. ###+ # When previewer is delayed, cards are bookmarked only if previewer has been initiated (visited). |
||
275 | -+ | |||
320 | +! |
- }+ module_out(.call_teal_module(modules, datasets, module_teal_data, reporter)) |
||
276 | +321 |
} |
||
277 | +322 |
- })+ |
||
278 | +323 |
- # Apply newly added snapshot.+ # todo: (feature request) add a ReporterCard to the reporter as an output from the teal_module |
||
279 | +324 |
-
+ # how to determine if module returns a ReporterCard so that reportPreviewer is needed? |
||
280 | +325 |
- # Restore initial state ----- |
- ||
281 | -77x - | -
- observeEvent(input$snapshot_reset, {+ # Should we insertUI of the ReportPreviewer then? |
||
282 | -2x - | +|||
326 | +
- logger::log_debug("srv_snapshot_manager: snapshot_reset button clicked, restoring snapshot")+ # What about attr(module, "reportable") - similar to attr(module, "bookmarkable") |
|||
283 | -2x - | +327 | +79x |
- s <- "Initial application state"+ if ("report" %in% names(module_out)) { |
284 | +328 |
- ### Begin restore procedure. ###- |
- ||
285 | -2x - | -
- snapshot <- snapshot_history()[[s]]+ # (reactively) add card to the reporter |
||
286 | +329 |
- # todo: as.teal_slices looses module-mapping if is not global+ } |
||
287 | -2x - | +|||
330 | +
- snapshot_state <- as.teal_slices(snapshot)+ }) |
|||
288 | -2x - | +|||
331 | +
- slices_global$slices_set(snapshot_state)+ |
|||
289 | -2x - | +332 | +105x |
- removeModal()+ module_out |
290 | +333 |
- ### End restore procedure. ###+ }) |
||
291 | +334 |
- })+ } |
||
292 | +335 | |||
293 | +336 |
- # Build snapshot table ----+ # This function calls a module server function. |
||
294 | +337 |
- # Create UI elements and server logic for the snapshot table.+ .call_teal_module <- function(modules, datasets, filtered_teal_data, reporter) { |
||
295 | +338 |
- # Observers must be tracked to avoid duplication and excess reactivity.+ # collect arguments to run teal_module |
||
296 | -+ | |||
339 | +73x |
- # Remaining elements are tracked likewise for consistency and a slight speed margin.+ args <- c(list(id = "module"), modules$server_args) |
||
297 | -77x - | +340 | +73x |
- observers <- reactiveValues()+ if (is_arg_used(modules$server, "reporter")) { |
298 | -77x - | +341 | +1x |
- handlers <- reactiveValues()+ args <- c(args, list(reporter = reporter)) |
299 | -77x - | +|||
342 | +
- divs <- reactiveValues()+ } |
|||
300 | +343 | |||
301 | -77x - | +344 | +73x |
- observeEvent(snapshot_history(), {+ if (is_arg_used(modules$server, "datasets")) { |
302 | -67x - | +345 | +1x |
- logger::log_debug("srv_snapshot_manager: snapshot history modified, updating snapshot list")+ args <- c(args, datasets = datasets()) |
303 | -67x - | +346 | +1x |
- lapply(names(snapshot_history())[-1L], function(s) {+ warning("datasets argument is not reactive and therefore it won't be updated when data is refreshed.") |
304 | -! | +|||
347 | +
- id_pickme <- sprintf("pickme_%s", make.names(s))+ } |
|||
305 | -! | +|||
348 | +
- id_saveme <- sprintf("saveme_%s", make.names(s))+ |
|||
306 | -! | +|||
349 | +73x |
- id_rowme <- sprintf("rowme_%s", make.names(s))+ if (is_arg_used(modules$server, "data")) {+ |
+ ||
350 | +69x | +
+ args <- c(args, data = list(filtered_teal_data)) |
||
307 | +351 |
-
+ } |
||
308 | +352 |
- # Observer for restoring snapshot.+ |
||
309 | -! | +|||
353 | +73x |
- if (!is.element(id_pickme, names(observers))) {+ if (is_arg_used(modules$server, "filter_panel_api")) { |
||
310 | -! | +|||
354 | +1x |
- observers[[id_pickme]] <- observeEvent(input[[id_pickme]], {+ args <- c(args, filter_panel_api = teal.slice::FilterPanelAPI$new(datasets())) |
||
311 | +355 |
- ### Begin restore procedure. ###+ } |
||
312 | -! | +|||
356 | +
- snapshot <- snapshot_history()[[s]]+ |
|||
313 | -! | +|||
357 | +73x |
- snapshot_state <- as.teal_slices(snapshot)+ if (is_arg_used(modules$server, "id")) { |
||
314 | -+ | |||
358 | +73x |
-
+ do.call(modules$server, args) |
||
315 | -! | +|||
359 | +
- slices_global$slices_set(snapshot_state)+ } else { |
|||
316 | +360 | ! |
- removeModal()+ do.call(callModule, c(args, list(module = modules$server))) |
|
317 | +361 |
- ### End restore procedure. ###+ } |
||
318 | +362 |
- })+ } |
||
319 | +363 |
- }+ |
||
320 | +364 |
- # Create handler for downloading snapshot.+ .resolve_module_datanames <- function(data, modules) { |
||
321 | -! | +|||
365 | +163x |
- if (!is.element(id_saveme, names(handlers))) {+ stopifnot("data_rv must be teal_data object." = inherits(data, "teal_data")) |
||
322 | -! | +|||
366 | +163x |
- output[[id_saveme]] <- downloadHandler(+ if (is.null(modules$datanames) || identical(modules$datanames, "all")) { |
||
323 | -! | +|||
367 | +131x |
- filename = function() {+ .topologically_sort_datanames(ls(teal.code::get_env(data)), teal.data::join_keys(data)) |
||
324 | -! | +|||
368 | +
- sprintf("teal_snapshot_%s_%s.json", s, Sys.Date())+ } else {+ |
+ |||
369 | +32x | +
+ intersect(+ |
+ ||
370 | +32x | +
+ .include_parent_datanames(modules$datanames, teal.data::join_keys(data)),+ |
+ ||
371 | +32x | +
+ ls(teal.code::get_env(data)) |
||
325 | +372 |
- },+ ) |
||
326 | -! | +|||
373 | +
- content = function(file) {+ } |
|||
327 | -! | +|||
374 | +
- snapshot <- snapshot_history()[[s]]+ } |
|||
328 | -! | +|||
375 | +
- snapshot_state <- as.teal_slices(snapshot)+ |
|||
329 | -! | +|||
376 | +
- slices_store(tss = snapshot_state, file = file)+ #' Calls expression when condition is met |
|||
330 | +377 |
- }+ #' |
||
331 | +378 |
- )+ #' Function postpones `handlerExpr` to the moment when `eventExpr` (condition) returns `TRUE`, |
||
332 | -! | +|||
379 | +
- handlers[[id_saveme]] <- id_saveme+ #' otherwise nothing happens. |
|||
333 | +380 |
- }+ #' @param eventExpr A (quoted or unquoted) logical expression that represents the event; |
||
334 | +381 |
- # Create a row for the snapshot table.+ #' this can be a simple reactive value like input$click, a call to a reactive expression |
||
335 | -! | +|||
382 | +
- if (!is.element(id_rowme, names(divs))) {+ #' like dataset(), or even a complex expression inside curly braces. |
|||
336 | -! | +|||
383 | +
- divs[[id_rowme]] <- tags$div(+ #' @param ... additional arguments passed to `observeEvent` with the exception of `eventExpr` that is not allowed. |
|||
337 | -! | +|||
384 | +
- class = "manager_table_row",+ #' @inheritParams shiny::observeEvent |
|||
338 | -! | +|||
385 | +
- tags$span(tags$h5(s)),+ #' |
|||
339 | -! | +|||
386 | +
- actionLink(inputId = ns(id_pickme), label = icon("far fa-circle-check"), title = "select"),+ #' @return An observer. |
|||
340 | -! | +|||
387 | +
- downloadLink(outputId = ns(id_saveme), label = icon("far fa-save"), title = "save to file")+ #' |
|||
341 | +388 |
- )+ #' @keywords internal |
||
342 | +389 |
- }+ call_once_when <- function(eventExpr, # nolint: object_name. |
||
343 | +390 |
- })+ handlerExpr, # nolint: object_name. |
||
344 | +391 |
- })+ event.env = parent.frame(), # nolint: object_name. |
||
345 | +392 |
-
+ handler.env = parent.frame(), # nolint: object_name. |
||
346 | +393 |
- # Create table to display list of snapshots and their actions.+ ...) { |
||
347 | -77x - | +394 | +184x |
- output$snapshot_list <- renderUI({+ event_quo <- rlang::new_quosure(substitute(eventExpr), env = event.env) |
348 | -67x - | +395 | +184x |
- rows <- rev(reactiveValuesToList(divs))+ handler_quo <- rlang::new_quosure(substitute(handlerExpr), env = handler.env) |
349 | -67x - | +|||
396 | +
- if (length(rows) == 0L) {+ + |
+ |||
397 | ++ |
+ # When `condExpr` is TRUE, then `handlerExpr` is evaluated once. |
||
350 | -67x - | +398 | +184x |
- tags$div(+ activator <- reactive({ |
351 | -67x - | +399 | +184x |
- class = "manager_placeholder",+ if (isTRUE(rlang::eval_tidy(event_quo))) { |
352 | -67x - | +400 | +152x |
- "Snapshots will appear here."+ TRUE |
353 | +401 |
- )+ } |
||
354 | +402 |
- } else {+ }) |
||
355 | -! | +|||
403 | +
- rows+ |
|||
356 | -+ | |||
404 | +184x |
- }+ observeEvent( |
||
357 | -+ | |||
405 | +184x |
- })+ eventExpr = activator(), |
||
358 | -+ | |||
406 | +184x |
-
+ once = TRUE, |
||
359 | -77x - | +407 | +184x |
- snapshot_history+ handlerExpr = rlang::eval_tidy(handler_quo), |
360 | +408 |
- })+ ... |
||
361 | +409 | ++ |
+ )+ |
+ |
410 |
}@@ -23058,14 +21348,14 @@ teal coverage - 59.73% |
1 |
- #' App state management.+ #' Send input validation messages to output |
|||
3 |
- #' @description+ #' Captures messages from `InputValidator` objects and collates them |
|||
4 |
- #' `r lifecycle::badge("experimental")`+ #' into one message passed to `validate`. |
|||
6 |
- #' Capture and restore the global (app) input state.+ #' `shiny::validate` is used to withhold rendering of an output element until |
|||
7 |
- #'+ #' certain conditions are met and to print a validation message in place |
|||
8 |
- #' @details+ #' of the output element. |
|||
9 |
- #' This module introduces bookmarks into `teal` apps: the `shiny` bookmarking mechanism becomes enabled+ #' `shinyvalidate::InputValidator` allows to validate input elements |
|||
10 |
- #' and server-side bookmarks can be created.+ #' and to display specific messages in their respective input widgets. |
|||
11 |
- #'+ #' `validate_inputs` provides a hybrid solution. |
|||
12 |
- #' The bookmark manager presents a button with the bookmark icon and is placed in the tab-bar.+ #' Given an `InputValidator` object, messages corresponding to inputs that fail validation |
|||
13 |
- #' When clicked, the button creates a bookmark and opens a modal which displays the bookmark URL.+ #' are extracted and placed in one validation message that is passed to a `validate`/`need` call. |
|||
14 |
- #'+ #' This way the input `validator` messages are repeated in the output. |
|||
15 |
- #' `teal` does not guarantee that all modules (`teal_module` objects) are bookmarkable.+ #' |
|||
16 |
- #' Those that are, have a `teal_bookmarkable` attribute set to `TRUE`. If any modules are not bookmarkable,+ #' The `...` argument accepts any number of `InputValidator` objects |
|||
17 |
- #' the bookmark manager modal displays a warning and the bookmark button displays a flag.+ #' or a nested list of such objects. |
|||
18 |
- #' In order to communicate that a external module is bookmarkable, the module developer+ #' If `validators` are passed directly, all their messages are printed together |
|||
19 |
- #' should set the `teal_bookmarkable` attribute to `TRUE`.+ #' under one (optional) header message specified by `header`. If a list is passed, |
|||
20 |
- #'+ #' messages are grouped by `validator`. The list's names are used as headers |
|||
21 |
- #' @section Server logic:+ #' for their respective message groups. |
|||
22 |
- #' A bookmark is a URL that contains the app address with a `/?_state_id_=<bookmark_dir>` suffix.+ #' If neither of the nested list elements is named, a header message is taken from `header`. |
|||
23 |
- #' `<bookmark_dir>` is a directory created on the server, where the state of the application is saved.+ #' |
|||
24 |
- #' Accessing the bookmark URL opens a new session of the app that starts in the previously saved state.+ #' @param ... either any number of `InputValidator` objects |
|||
25 |
- #'+ #' or an optionally named, possibly nested `list` of `InputValidator` |
|||
26 |
- #' @section Note:+ #' objects, see `Details` |
|||
27 |
- #' To enable bookmarking use either:+ #' @param header (`character(1)`) generic validation message; set to NULL to omit |
|||
28 |
- #' - `shiny` app by using `shinyApp(..., enableBookmarking = "server")` (not supported in `shinytest2`)+ #' |
|||
29 |
- #' - set `options(shiny.bookmarkStore = "server")` before running the app+ #' @return |
|||
30 |
- #'+ #' Returns NULL if the final validation call passes and a `shiny.silent.error` if it fails. |
|||
32 |
- #' @inheritParams init+ #' @seealso [`shinyvalidate::InputValidator`], [`shiny::validate`] |
|||
34 |
- #' @return Invisible `NULL`.+ #' @examplesIf require("shinyvalidate") |
|||
35 |
- #'+ #' library(shiny) |
|||
36 |
- #' @aliases bookmark bookmark_manager bookmark_manager_module+ #' library(shinyvalidate) |
|||
38 |
- #' @name module_bookmark_manager+ #' ui <- fluidPage( |
|||
39 |
- #' @rdname module_bookmark_manager+ #' selectInput("method", "validation method", c("sequential", "combined", "grouped")), |
|||
40 |
- #'+ #' sidebarLayout( |
|||
41 |
- #' @keywords internal+ #' sidebarPanel( |
|||
42 |
- #'+ #' selectInput("letter", "select a letter:", c(letters[1:3], LETTERS[4:6])), |
|||
43 |
- NULL+ #' selectInput("number", "select a number:", 1:6), |
|||
44 |
-
+ #' tags$br(), |
|||
45 |
- #' @rdname module_bookmark_manager+ #' selectInput("color", "select a color:", |
|||
46 |
- ui_bookmark_panel <- function(id, modules) {+ #' c("black", "indianred2", "springgreen2", "cornflowerblue"), |
|||
47 | -! | +
- ns <- NS(id)+ #' multiple = TRUE |
||
48 |
-
+ #' ), |
|||
49 | -! | +
- bookmark_option <- get_bookmarking_option()+ #' sliderInput("size", "select point size:", |
||
50 | -! | +
- is_unbookmarkable <- need_bookmarking(modules)+ #' min = 0.1, max = 4, value = 0.25 |
||
51 | -! | +
- shinyOptions(bookmarkStore = bookmark_option)+ #' ) |
||
52 |
-
+ #' ), |
|||
53 |
- # Render bookmark warnings count+ #' mainPanel(plotOutput("plot")) |
|||
54 | -! | +
- if (!all(is_unbookmarkable) && identical(bookmark_option, "server")) {+ #' ) |
||
55 | -! | +
- tags$button(+ #' ) |
||
56 | -! | +
- id = ns("do_bookmark"),+ #' |
||
57 | -! | +
- class = "btn action-button wunder_bar_button bookmark_manager_button",+ #' server <- function(input, output) { |
||
58 | -! | +
- title = "Add bookmark",+ #' # set up input validation |
||
59 | -! | +
- tags$span(+ #' iv <- InputValidator$new() |
||
60 | -! | +
- suppressMessages(icon("fas fa-bookmark")),+ #' iv$add_rule("letter", sv_in_set(LETTERS, "choose a capital letter")) |
||
61 | -! | +
- if (any(is_unbookmarkable)) {+ #' iv$add_rule("number", function(x) { |
||
62 | -! | +
- tags$span(+ #' if (as.integer(x) %% 2L == 1L) "choose an even number" |
||
63 | -! | +
- sum(is_unbookmarkable),+ #' })+ |
+ ||
64 | ++ |
+ #' iv$enable()+ |
+ ||
65 | ++ |
+ #' # more input validation+ |
+ ||
66 | ++ |
+ #' iv_par <- InputValidator$new()+ |
+ ||
67 | ++ |
+ #' iv_par$add_rule("color", sv_required(message = "choose a color"))+ |
+ ||
68 | ++ |
+ #' iv_par$add_rule("color", function(x) {+ |
+ ||
69 | ++ |
+ #' if (length(x) > 1L) "choose only one color"+ |
+ ||
70 | ++ |
+ #' })+ |
+ ||
71 | ++ |
+ #' iv_par$add_rule(+ |
+ ||
72 | ++ |
+ #' "size",+ |
+ ||
73 | ++ |
+ #' sv_between(+ |
+ ||
74 | ++ |
+ #' left = 0.5, right = 3,+ |
+ ||
75 | ++ |
+ #' message_fmt = "choose a value between {left} and {right}"+ |
+ ||
76 | ++ |
+ #' )+ |
+ ||
77 | ++ |
+ #' )+ |
+ ||
78 | ++ |
+ #' iv_par$enable()+ |
+ ||
79 | ++ |
+ #'+ |
+ ||
80 | ++ |
+ #' output$plot <- renderPlot({+ |
+ ||
81 | ++ |
+ #' # validate output+ |
+ ||
82 | ++ |
+ #' switch(input[["method"]],+ |
+ ||
83 | ++ |
+ #' "sequential" = {+ |
+ ||
84 | ++ |
+ #' validate_inputs(iv)+ |
+ ||
85 | ++ |
+ #' validate_inputs(iv_par, header = "Set proper graphical parameters")+ |
+ ||
86 | ++ |
+ #' },+ |
+ ||
87 | ++ |
+ #' "combined" = validate_inputs(iv, iv_par),+ |
+ ||
88 | ++ |
+ #' "grouped" = validate_inputs(list(+ |
+ ||
89 | ++ |
+ #' "Some inputs require attention" = iv,+ |
+ ||
90 | ++ |
+ #' "Set proper graphical parameters" = iv_par+ |
+ ||
91 | ++ |
+ #' ))+ |
+ ||
92 | ++ |
+ #' )+ |
+ ||
93 | ++ |
+ #'+ |
+ ||
94 | ++ |
+ #' plot(faithful$eruptions ~ faithful$waiting,+ |
+ ||
95 | ++ |
+ #' las = 1, pch = 16,+ |
+ ||
96 | ++ |
+ #' col = input[["color"]], cex = input[["size"]]+ |
+ ||
97 | ++ |
+ #' ) |
||
64 | -! | +|||
98 | +
- class = "badge-warning badge-count text-white bg-danger"+ #' }) |
|||
65 | +99 |
- )+ #' } |
||
66 | +100 |
- }+ #' |
||
67 | +101 |
- )+ #' if (interactive()) { |
||
68 | +102 |
- )+ #' shinyApp(ui, server) |
||
69 | +103 |
- }+ #' } |
||
70 | +104 |
- }+ #' |
||
71 | +105 |
-
+ #' @export |
||
72 | +106 |
- #' @rdname module_bookmark_manager+ #' |
||
73 | +107 |
- srv_bookmark_panel <- function(id, modules) {+ validate_inputs <- function(..., header = "Some inputs require attention") { |
||
74 | -77x - | +108 | +36x |
- checkmate::assert_character(id)+ dots <- list(...) |
75 | -77x - | +109 | +2x |
- checkmate::assert_class(modules, "teal_modules")+ if (!is_validators(dots)) stop("validate_inputs accepts validators or a list thereof") |
76 | -77x - | +|||
110 | +
- moduleServer(id, function(input, output, session) {+ |
|||
77 | -77x - | +111 | +34x |
- logger::log_debug("bookmark_manager_srv initializing")+ messages <- extract_validator(dots, header) |
78 | -77x - | +112 | +34x |
- ns <- session$ns+ failings <- if (!any_names(dots)) { |
79 | -77x - | +113 | +29x |
- bookmark_option <- get_bookmarking_option()+ add_header(messages, header)+ |
+
114 | ++ |
+ } else { |
||
80 | -77x - | +115 | +5x |
- is_unbookmarkable <- need_bookmarking(modules)+ unlist(messages) |
81 | +116 | ++ |
+ }+ |
+ |
117 | ||||
118 | +34x | +
+ shiny::validate(shiny::need(is.null(failings), failings))+ |
+ ||
82 | +119 |
- # Set up bookmarking callbacks ----+ } |
||
83 | +120 |
- # Register bookmark exclusions: do_bookmark button to avoid re-bookmarking+ |
||
84 | -77x - | +|||
121 | +
- setBookmarkExclude(c("do_bookmark"))+ ### internal functions |
|||
85 | +122 |
- # This bookmark can only be used on the app session.+ |
||
86 | -77x - | +|||
123 | +
- app_session <- .subset2(session, "parent")+ #' @noRd |
|||
87 | -77x - | +|||
124 | +
- app_session$onBookmarked(function(url) {+ #' @keywords internal |
|||
88 | -! | +|||
125 | +
- logger::log_debug("bookmark_manager_srv@onBookmarked: bookmark button clicked, registering bookmark")+ # recursive object type test |
|||
89 | -! | +|||
126 | +
- modal_content <- if (bookmark_option != "server") {+ # returns logical of length 1 |
|||
90 | -! | +|||
127 | +
- msg <- sprintf(+ is_validators <- function(x) { |
|||
91 | -! | +|||
128 | +118x |
- "Bookmarking has been set to \"%s\".\n%s\n%s",+ all(if (is.list(x)) unlist(lapply(x, is_validators)) else inherits(x, "InputValidator")) |
||
92 | -! | +|||
129 | +
- bookmark_option,+ } |
|||
93 | -! | +|||
130 | +
- "Only server-side bookmarking is supported.",+ |
|||
94 | -! | +|||
131 | +
- "Please contact your app developer."+ #' @noRd |
|||
95 | +132 |
- )+ #' @keywords internal |
||
96 | -! | +|||
133 | +
- tags$div(+ # test if an InputValidator object is enabled |
|||
97 | -! | +|||
134 | +
- tags$p(msg, class = "text-warning")+ # returns logical of length 1 |
|||
98 | +135 |
- )+ # official method requested at https://github.com/rstudio/shinyvalidate/issues/64 |
||
99 | +136 |
- } else {+ validator_enabled <- function(x) { |
||
100 | -! | +|||
137 | +49x |
- tags$div(+ x$.__enclos_env__$private$enabled |
||
101 | -! | +|||
138 | +
- tags$span(+ } |
|||
102 | -! | +|||
139 | +
- tags$pre(url)+ |
|||
103 | +140 |
- ),+ #' Recursively extract messages from validator list |
||
104 | -! | +|||
141 | +
- if (any(is_unbookmarkable)) {+ #' @return A character vector or a list of character vectors, possibly nested and named. |
|||
105 | -! | +|||
142 | +
- bkmb_summary <- rapply2(+ #' @noRd |
|||
106 | -! | +|||
143 | +
- modules_bookmarkable(modules),+ #' @keywords internal |
|||
107 | -! | +|||
144 | +
- function(x) {+ extract_validator <- function(iv, header) { |
|||
108 | -! | +|||
145 | +113x |
- if (isTRUE(x)) {+ if (inherits(iv, "InputValidator")) { |
||
109 | -! | +|||
146 | +49x |
- "\u2705" # check mark+ add_header(gather_messages(iv), header) |
||
110 | -! | +|||
147 | +
- } else if (isFALSE(x)) {+ } else { |
|||
111 | -! | +|||
148 | +58x |
- "\u274C" # cross mark+ if (is.null(names(iv))) names(iv) <- rep("", length(iv))+ |
+ ||
149 | +64x | +
+ mapply(extract_validator, iv = iv, header = names(iv), SIMPLIFY = FALSE) |
||
112 | +150 |
- } else {+ } |
||
113 | -! | +|||
151 | +
- "\u2753" # question mark+ } |
|||
114 | +152 |
- }+ |
||
115 | +153 |
- }+ #' Collate failing messages from validator. |
||
116 | +154 |
- )+ #' @return `list` |
||
117 | -! | +|||
155 | +
- tags$div(+ #' @noRd |
|||
118 | -! | +|||
156 | +
- tags$p(+ #' @keywords internal |
|||
119 | -! | +|||
157 | +
- icon("fas fa-exclamation-triangle"),+ gather_messages <- function(iv) { |
|||
120 | -! | +|||
158 | +49x |
- "Some modules will not be restored when using this bookmark.",+ if (validator_enabled(iv)) { |
||
121 | -! | +|||
159 | +46x |
- tags$br(),+ status <- iv$validate() |
||
122 | -! | +|||
160 | +46x |
- "Check the list below to see which modules are not bookmarkable.",+ failing_inputs <- Filter(Negate(is.null), status) |
||
123 | -! | +|||
161 | +46x |
- class = "text-warning"+ unique(lapply(failing_inputs, function(x) x[["message"]])) |
||
124 | +162 |
- ),+ } else { |
||
125 | -! | +|||
163 | +3x |
- tags$pre(yaml::as.yaml(bkmb_summary))+ warning("Validator is disabled and will be omitted.")+ |
+ ||
164 | +3x | +
+ list() |
||
126 | +165 |
- )+ } |
||
127 | +166 |
- }+ } |
||
128 | +167 |
- )+ |
||
129 | +168 |
- }+ #' Add optional header to failing messages |
||
130 | +169 |
-
+ #' @noRd |
||
131 | -! | +|||
170 | +
- showModal(+ #' @keywords internal |
|||
132 | -! | +|||
171 | +
- modalDialog(+ add_header <- function(messages, header = "") { |
|||
133 | -! | +|||
172 | +78x |
- id = ns("bookmark_modal"),+ ans <- unlist(messages) |
||
134 | -! | +|||
173 | +78x |
- title = "Bookmarked teal app url",+ if (length(ans) != 0L && isTRUE(nchar(header) > 0L)) { |
||
135 | -! | +|||
174 | +31x |
- modal_content,+ ans <- c(paste0(header, "\n"), ans, "\n") |
||
136 | -! | +|||
175 | +
- easyClose = TRUE+ }+ |
+ |||
176 | +78x | +
+ ans |
||
137 | +177 |
- )+ } |
||
138 | +178 |
- )+ |
||
139 | +179 |
- })+ #' Recursively check if the object contains a named list |
||
140 | +180 |
-
+ #' @noRd |
||
141 | +181 |
- # manually trigger bookmarking because of the problems reported on windows with bookmarkButton in teal+ #' @keywords internal |
||
142 | -77x - | +|||
182 | +
- observeEvent(input$do_bookmark, {+ any_names <- function(x) { |
|||
143 | -! | +|||
183 | +103x |
- logger::log_debug("bookmark_manager_srv@1 do_bookmark module clicked.")+ any( |
||
144 | -! | +|||
184 | +103x |
- session$doBookmark()+ if (is.list(x)) { |
||
145 | -+ | |||
185 | +58x |
- })+ if (!is.null(names(x)) && any(names(x) != "")) TRUE else unlist(lapply(x, any_names)) |
||
146 | +186 |
-
+ } else { |
||
147 | -77x - | +187 | +40x |
- invisible(NULL)+ FALSE |
148 | +188 |
- })+ } |
||
149 | +189 |
- }+ ) |
||
150 | +190 |
-
+ } |
151 | +1 |
-
+ #' `teal` main module |
||
152 | +2 |
- #' @rdname module_bookmark_manager+ #' |
||
153 | +3 |
- get_bookmarking_option <- function() {- |
- ||
154 | -77x - | -
- bookmark_option <- getShinyOption("bookmarkStore")+ #' @description |
||
155 | -77x - | +|||
4 | +
- if (is.null(bookmark_option) && identical(getOption("shiny.bookmarkStore"), "server")) {+ #' `r lifecycle::badge("stable")` |
|||
156 | -! | +|||
5 | +
- bookmark_option <- getOption("shiny.bookmarkStore")+ #' Module to create a `teal` app. This module can be called directly instead of [init()] and |
|||
157 | +6 |
- }+ #' included in your custom application. Please note that [init()] adds `reporter_previewer_module` |
||
158 | -77x - | +|||
7 | +
- bookmark_option+ #' automatically, which is not a case when calling `ui/srv_teal` directly. |
|||
159 | +8 |
- }+ #' |
||
160 | +9 |
-
+ #' @details |
||
161 | +10 |
- #' @rdname module_bookmark_manager+ #' |
||
162 | +11 |
- need_bookmarking <- function(modules) {+ #' Module is responsible for creating the main `shiny` app layout and initializing all the necessary |
||
163 | -77x - | +|||
12 | +
- unlist(rapply2(+ #' components. This module establishes reactive connection between the input `data` and every other |
|||
164 | -77x - | +|||
13 | +
- modules_bookmarkable(modules),+ #' component in the app. Reactive change of the `data` passed as an argument, reloads the app and |
|||
165 | -77x - | +|||
14 | +
- Negate(isTRUE)+ #' possibly keeps all input settings the same so the user can continue where one left off. |
|||
166 | +15 |
- ))+ #' |
||
167 | +16 |
- }+ #' ## data flow in `teal` application |
||
168 | +17 |
-
+ #' |
||
169 | +18 |
-
+ #' This module supports multiple data inputs but eventually, they are all converted to `reactive` |
||
170 | +19 |
- # utilities ----+ #' returning `teal_data` in this module. On this `reactive teal_data` object several actions are |
||
171 | +20 |
-
+ #' performed: |
||
172 | +21 |
- #' Restore value from bookmark.+ #' - data loading in [`module_init_data`] |
||
173 | +22 |
- #'+ #' - data filtering in [`module_filter_data`] |
||
174 | +23 |
- #' Get value from bookmark or return default.+ #' - data transformation in [`module_transform_data`] |
||
175 | +24 |
#' |
||
176 | +25 |
- #' Bookmarks can store not only inputs but also arbitrary values.+ #' ## Fallback on failure |
||
177 | +26 |
- #' These values are stored by `onBookmark` callbacks and restored by `onBookmarked` callbacks,+ #' |
||
178 | +27 |
- #' and they are placed in the `values` environment in the `session$restoreContext` field.+ #' `teal` is designed in such way that app will never crash if the error is introduced in any |
||
179 | +28 |
- #' Using `teal_data_module` makes it impossible to run the callbacks+ #' custom `shiny` module provided by app developer (e.g. [teal_data_module()], [teal_transform_module()]). |
||
180 | +29 |
- #' because the app becomes ready before modules execute and callbacks are registered.+ #' If any module returns a failing object, the app will halt the evaluation and display a warning message. |
||
181 | +30 |
- #' In those cases the stored values can still be recovered from the `session` object directly.+ #' App user should always have a chance to fix the improper input and continue without restarting the session. |
||
182 | +31 |
#' |
||
183 | +32 |
- #' Note that variable names in the `values` environment are prefixed with module name space names,+ #' @rdname module_teal |
||
184 | +33 |
- #' therefore, when using this function in modules, `value` must be run through the name space function.+ #' @name module_teal |
||
185 | +34 |
#' |
||
186 | +35 |
- #' @param value (`character(1)`) name of value to restore+ #' @inheritParams module_init_data |
||
187 | +36 |
- #' @param default fallback value+ #' @inheritParams init |
||
188 | +37 |
#' |
||
189 | +38 |
- #' @return+ #' @return `NULL` invisibly |
||
190 | +39 |
- #' In an application restored from a server-side bookmark,+ NULL |
||
191 | +40 |
- #' the variable specified by `value` from the `values` environment.+ |
||
192 | +41 |
- #' Otherwise `default`.+ #' @rdname module_teal |
||
193 | +42 |
- #'+ #' @export |
||
194 | +43 |
- #' @keywords internal+ ui_teal <- function(id, |
||
195 | +44 |
- #'+ modules, |
||
196 | +45 |
- restoreValue <- function(value, default) { # nolint: object_name.- |
- ||
197 | -154x - | -
- checkmate::assert_character("value")- |
- ||
198 | -154x - | -
- session_default <- shiny::getDefaultReactiveDomain()+ title = build_app_title(), |
||
199 | -154x - | +|||
46 | +
- session_parent <- .subset2(session_default, "parent")+ header = tags$p(), |
|||
200 | -154x - | +|||
47 | +
- session <- if (is.null(session_parent)) session_default else session_parent+ footer = tags$p()) { |
|||
201 | -+ | |||
48 | +! |
-
+ checkmate::assert_character(id, max.len = 1, any.missing = FALSE) |
||
202 | -154x - | +|||
49 | +! |
- if (isTRUE(session$restoreContext$active) && exists(value, session$restoreContext$values, inherits = FALSE)) {+ checkmate::assert( |
||
203 | +50 | ! |
- session$restoreContext$values[[value]]+ .var.name = "title", |
|
204 | -+ | |||
51 | +! |
- } else {+ checkmate::check_string(title), |
||
205 | -154x - | +|||
52 | +! |
- default+ checkmate::check_multi_class(title, c("shiny.tag", "shiny.tag.list", "html")) |
||
206 | +53 |
- }+ ) |
||
207 | -+ | |||
54 | +! |
- }+ checkmate::assert( |
||
208 | -+ | |||
55 | +! |
-
+ .var.name = "header", |
||
209 | -+ | |||
56 | +! |
- #' Compare bookmarks.+ checkmate::check_string(header), |
||
210 | -+ | |||
57 | +! |
- #'+ checkmate::check_multi_class(header, c("shiny.tag", "shiny.tag.list", "html")) |
||
211 | +58 |
- #' Test if two bookmarks store identical state.+ ) |
||
212 | -+ | |||
59 | +! |
- #'+ checkmate::assert( |
||
213 | -+ | |||
60 | +! |
- #' `input` environments are compared one variable at a time and if not identical,+ .var.name = "footer", |
||
214 | -+ | |||
61 | +! |
- #' values in both bookmarks are reported. States of `datatable`s are stripped+ checkmate::check_string(footer), |
||
215 | -+ | |||
62 | +! |
- #' of the `time` element before comparing because the time stamp is always different.+ checkmate::check_multi_class(footer, c("shiny.tag", "shiny.tag.list", "html")) |
||
216 | +63 |
- #' The contents themselves are not printed as they are large and the contents are not informative.+ ) |
||
217 | +64 |
- #' Elements present in one bookmark and absent in the other are also reported.+ |
||
218 | -+ | |||
65 | +! |
- #' Differences are printed as messages.+ if (is.character(title)) { |
||
219 | -+ | |||
66 | +! |
- #'+ title <- build_app_title(title) |
||
220 | +67 |
- #' `values` environments are compared with `all.equal`.+ } else { |
||
221 | -+ | |||
68 | +! |
- #'+ validate_app_title_tag(title) |
||
222 | +69 |
- #' @section How to use:+ } |
||
223 | +70 |
- #' Open an application, change relevant inputs (typically, all of them), and create a bookmark.+ |
||
224 | -+ | |||
71 | +! |
- #' Then open that bookmark and immediately create a bookmark of that.+ if (checkmate::test_string(header)) { |
||
225 | -+ | |||
72 | +! |
- #' If restoring bookmarks occurred properly, the two bookmarks should store the same state.+ header <- tags$p(header) |
||
226 | +73 |
- #'+ } |
||
227 | +74 |
- #'+ |
||
228 | -+ | |||
75 | +! |
- #' @param book1,book2 bookmark directories stored in `shiny_bookmarks/`;+ if (checkmate::test_string(footer)) { |
||
229 | -+ | |||
76 | +! |
- #' default to the two most recently modified directories+ footer <- tags$p(footer) |
||
230 | +77 |
- #'+ } |
||
231 | +78 |
- #' @return+ |
||
232 | -+ | |||
79 | +! |
- #' Invisible `NULL` if bookmarks are identical or if there are no bookmarks to test.+ ns <- NS(id) |
||
233 | +80 |
- #' `FALSE` if inconsistencies are detected.+ |
||
234 | +81 |
- #'+ # show busy icon when `shiny` session is busy computing stuff |
||
235 | +82 |
- #' @keywords internal+ # based on https://stackoverflow.com/questions/17325521/r-shiny-display-loading-message-while-function-is-running/22475216#22475216 # nolint: line_length. |
||
236 | -+ | |||
83 | +! |
- #'+ shiny_busy_message_panel <- conditionalPanel( |
||
237 | -+ | |||
84 | +! |
- bookmarks_identical <- function(book1, book2) {+ condition = "(($('html').hasClass('shiny-busy')) && (document.getElementById('shiny-notification-panel') == null))", # nolint: line_length. |
||
238 | +85 | ! |
- if (!dir.exists("shiny_bookmarks")) {+ tags$div( |
|
239 | +86 | ! |
- message("no bookmark directory")+ icon("arrows-rotate", class = "fa-spin", prefer_type = "solid"), |
|
240 | +87 | ! |
- return(invisible(NULL))+ "Computing ...", |
|
241 | +88 |
- }+ # CSS defined in `custom.css`+ |
+ ||
89 | +! | +
+ class = "shinybusymessage" |
||
242 | +90 |
-
+ ) |
||
243 | -! | +|||
91 | +
- ans <- TRUE+ ) |
|||
244 | +92 | |||
245 | +93 | ! |
- if (missing(book1) && missing(book2)) {+ fluidPage( |
|
246 | +94 | ! |
- dirs <- list.dirs("shiny_bookmarks", recursive = FALSE)+ id = id, |
|
247 | +95 | ! |
- bookmarks_sorted <- basename(rev(dirs[order(file.mtime(dirs))]))+ title = title, |
|
248 | +96 | ! |
- if (length(bookmarks_sorted) < 2L) {+ theme = get_teal_bs_theme(), |
|
249 | +97 | ! |
- message("no bookmarks to compare")+ include_teal_css_js(), |
|
250 | +98 | ! |
- return(invisible(NULL))+ tags$header(header), |
|
251 | -+ | |||
99 | +! |
- }+ tags$hr(class = "my-2"), |
||
252 | +100 | ! |
- book1 <- bookmarks_sorted[2L]+ shiny_busy_message_panel, |
|
253 | +101 | ! |
- book2 <- bookmarks_sorted[1L]+ tags$div( |
|
254 | -+ | |||
102 | +! |
- } else {+ id = ns("tabpanel_wrapper"), |
||
255 | +103 | ! |
- if (!dir.exists(file.path("shiny_bookmarks", book1))) stop(book1, " not found")+ class = "teal-body", |
|
256 | +104 | ! |
- if (!dir.exists(file.path("shiny_bookmarks", book2))) stop(book2, " not found")+ ui_teal_module(id = ns("teal_modules"), modules = modules) |
|
257 | +105 |
- }+ ), |
||
258 | -+ | |||
106 | +! |
-
+ tags$div( |
||
259 | +107 | ! |
- book1_input <- readRDS(file.path("shiny_bookmarks", book1, "input.rds"))+ id = ns("options_buttons"), |
|
260 | +108 | ! |
- book2_input <- readRDS(file.path("shiny_bookmarks", book2, "input.rds"))+ style = "position: absolute; right: 10px;", |
|
261 | -+ | |||
109 | +! |
-
+ ui_bookmark_panel(ns("bookmark_manager"), modules), |
||
262 | +110 | ! |
- elements_common <- intersect(names(book1_input), names(book2_input))+ tags$button( |
|
263 | +111 | ! |
- dt_states <- grepl("_state$", elements_common)+ class = "btn action-button filter_hamburger", # see sidebar.css for style filter_hamburger |
|
264 | +112 | ! |
- if (any(dt_states)) {+ href = "javascript:void(0)", |
|
265 | +113 | ! |
- for (el in elements_common[dt_states]) {+ onclick = sprintf("toggleFilterPanel('%s');", ns("tabpanel_wrapper")), |
|
266 | +114 | ! |
- book1_input[[el]][["time"]] <- NULL+ title = "Toggle filter panel", |
|
267 | +115 | ! |
- book2_input[[el]][["time"]] <- NULL+ icon("fas fa-bars") |
|
268 | +116 |
- }+ ), |
||
269 | -+ | |||
117 | +! |
- }+ ui_snapshot_manager_panel(ns("snapshot_manager_panel")),+ |
+ ||
118 | +! | +
+ ui_filter_manager_panel(ns("filter_manager_panel")) |
||
270 | +119 |
-
+ ), |
||
271 | +120 | ! |
- identicals <- mapply(identical, book1_input[elements_common], book2_input[elements_common])+ tags$script( |
|
272 | +121 | ! |
- non_identicals <- names(identicals[!identicals])+ HTML( |
|
273 | +122 | ! |
- compares <- sprintf("$ %s:\t%s --- %s", non_identicals, book1_input[non_identicals], book2_input[non_identicals])+ sprintf( |
|
274 | -! | +|||
123 | +
- if (length(compares) != 0L) {+ " |
|||
275 | +124 | ! |
- message("common elements not identical: \n", paste(compares, collapse = "\n"))+ $(document).ready(function() { |
|
276 | +125 | ! |
- ans <- FALSE+ $('#%s').appendTo('#%s'); |
|
277 | +126 |
- }+ }); |
||
278 | +127 |
-
+ ", |
||
279 | +128 | ! |
- elements_boook1 <- setdiff(names(book1_input), names(book2_input))+ ns("options_buttons"), |
|
280 | +129 | ! |
- if (length(elements_boook1) != 0L) {+ ns("teal_modules-active_tab") |
|
281 | -! | +|||
130 | +
- dt_states <- grepl("_state$", elements_boook1)+ )+ |
+ |||
131 | ++ |
+ )+ |
+ ||
132 | ++ |
+ ), |
||
282 | +133 | ! |
- if (any(dt_states)) {+ tags$hr(), |
|
283 | +134 | ! |
- for (el in elements_boook1[dt_states]) {+ tags$footer( |
|
284 | +135 | ! |
- if (is.list(book1_input[[el]])) book1_input[[el]] <- "--- data table state ---"+ tags$div( |
|
285 | -+ | |||
136 | +! |
- }+ footer, |
||
286 | -+ | |||
137 | +! |
- }+ teal.widgets::verbatim_popup_ui(ns("sessionInfo"), "Session Info", type = "link"), |
||
287 | +138 | ! |
- excess1 <- sprintf("$ %s:\t%s", elements_boook1, book1_input[elements_boook1])+ br(), |
|
288 | +139 | ! |
- message("elements only in book1: \n", paste(excess1, collapse = "\n"))+ ui_teal_lockfile(ns("lockfile")), |
|
289 | +140 | ! |
- ans <- FALSE+ textOutput(ns("identifier")) |
|
290 | +141 |
- }+ ) |
||
291 | +142 | ++ |
+ )+ |
+ |
143 | ++ |
+ )+ |
+ ||
144 | ++ |
+ }+ |
+ ||
145 | ||||
292 | -! | +|||
146 | +
- elements_boook2 <- setdiff(names(book2_input), names(book1_input))+ #' @rdname module_teal |
|||
293 | -! | +|||
147 | +
- if (length(elements_boook2) != 0L) {+ #' @export |
|||
294 | -! | +|||
148 | +
- dt_states <- grepl("_state$", elements_boook1)+ srv_teal <- function(id, data, modules, filter = teal_slices()) { |
|||
295 | -! | +|||
149 | +82x |
- if (any(dt_states)) {+ checkmate::assert_character(id, max.len = 1, any.missing = FALSE) |
||
296 | -! | +|||
150 | +82x |
- for (el in elements_boook1[dt_states]) {+ checkmate::assert_multi_class(data, c("teal_data", "teal_data_module", "reactive")) |
||
297 | -! | +|||
151 | +81x |
- if (is.list(book2_input[[el]])) book2_input[[el]] <- "--- data table state ---"+ checkmate::assert_class(modules, "teal_modules")+ |
+ ||
152 | +81x | +
+ checkmate::assert_class(filter, "teal_slices") |
||
298 | +153 |
- }+ + |
+ ||
154 | +81x | +
+ moduleServer(id, function(input, output, session) {+ |
+ ||
155 | +81x | +
+ logger::log_debug("srv_teal initializing.") |
||
299 | +156 |
- }+ |
||
300 | -! | +|||
157 | +81x |
- excess2 <- sprintf("$ %s:\t%s", elements_boook2, book2_input[elements_boook2])+ if (getOption("teal.show_js_log", default = FALSE)) { |
||
301 | +158 | ! |
- message("elements only in book2: \n", paste(excess2, collapse = "\n"))+ shinyjs::showLog() |
|
302 | -! | +|||
159 | +
- ans <- FALSE+ } |
|||
303 | +160 |
- }+ + |
+ ||
161 | +81x | +
+ srv_teal_lockfile("lockfile") |
||
304 | +162 | |||
305 | -! | +|||
163 | +81x |
- book1_values <- readRDS(file.path("shiny_bookmarks", book1, "values.rds"))+ output$identifier <- renderText( |
||
306 | -! | +|||
164 | +81x |
- book2_values <- readRDS(file.path("shiny_bookmarks", book2, "values.rds"))+ paste0("Pid:", Sys.getpid(), " Token:", substr(session$token, 25, 32)) |
||
307 | +165 | ++ |
+ )+ |
+ |
166 | ||||
308 | -! | +|||
167 | +81x |
- if (!isTRUE(all.equal(book1_values, book2_values))) {+ teal.widgets::verbatim_popup_srv( |
||
309 | -! | +|||
168 | +81x |
- message("different values detected")+ "sessionInfo", |
||
310 | -! | +|||
169 | +81x |
- message("choices for numeric filters MAY be different, see RangeFilterState$set_choices")+ verbatim_content = utils::capture.output(utils::sessionInfo()), |
||
311 | -! | +|||
170 | +81x |
- ans <- FALSE+ title = "SessionInfo" |
||
312 | +171 |
- }+ ) |
||
313 | +172 | |||
314 | -! | +|||
173 | +
- if (ans) message("perfect!")+ # `JavaScript` code |
|||
315 | -! | +|||
174 | +81x |
- invisible(NULL)+ run_js_files(files = "init.js") |
||
316 | +175 |
- }+ |
||
317 | +176 |
-
+ # set timezone in shiny app |
||
318 | +177 |
-
+ # timezone is set in the early beginning so it will be available also |
||
319 | +178 |
- # Replacement for [base::rapply] which doesn't handle NULL values - skips the evaluation+ # for `DDL` and all shiny modules+ |
+ ||
179 | +81x | +
+ get_client_timezone(session$ns)+ |
+ ||
180 | +81x | +
+ observeEvent(+ |
+ ||
181 | +81x | +
+ eventExpr = input$timezone,+ |
+ ||
182 | +81x | +
+ once = TRUE,+ |
+ ||
183 | +81x | +
+ handlerExpr = {+ |
+ ||
184 | +! | +
+ session$userData$timezone <- input$timezone+ |
+ ||
185 | +! | +
+ logger::log_debug("srv_teal@1 Timezone set to client's timezone: { input$timezone }.") |
||
320 | +186 |
- # of the function and returns NULL for given element.+ } |
||
321 | +187 |
- rapply2 <- function(x, f) {- |
- ||
322 | -179x - | -
- if (inherits(x, "list")) {- |
- ||
323 | -77x - | -
- lapply(x, rapply2, f = f)+ ) |
||
324 | +188 |
- } else {+ |
||
325 | -102x - | +189 | +81x |
- f(x)+ data_pulled <- srv_init_data("data", data = data) |
326 | -+ | |||
190 | +80x |
- }+ data_validated <- srv_validate_reactive_teal_data( |
||
327 | -+ | |||
191 | +80x |
- }+ "validate", |
1 | -+ | |||
192 | +80x |
- #' Generate lockfile for application's environment reproducibility+ data = data_pulled, |
||
2 | -+ | |||
193 | +80x |
- #'+ modules = modules, |
||
3 | -+ | |||
194 | +80x |
- #' @param lockfile_path (`character`) path to the lockfile.+ validate_shiny_silent_error = FALSE |
||
4 | +195 |
- #'+ ) |
||
5 | -+ | |||
196 | +80x |
- #' @section Different ways of creating lockfile:+ data_rv <- reactive({ |
||
6 | -+ | |||
197 | +138x |
- #' `teal` leverages [renv::snapshot()], which offers multiple methods for lockfile creation.+ req(inherits(data_validated(), "teal_data")) |
||
7 | -+ | |||
198 | +68x |
- #'+ is_filter_ok <- check_filter_datanames(filter, ls(teal.code::get_env(data_validated()))) |
||
8 | -+ | |||
199 | +68x |
- #' - **Working directory lockfile**: `teal`, by default, will create an `implicit` type lockfile that uses+ if (!isTRUE(is_filter_ok)) { |
||
9 | -+ | |||
200 | +2x |
- #' `renv::dependencies()` to detect all R packages in the current project's working directory.+ showNotification( |
||
10 | -+ | |||
201 | +2x |
- #' - **`DESCRIPTION`-based lockfile**: To generate a lockfile based on a `DESCRIPTION` file in your working+ "Some filters were not applied because of incompatibility with data. Contact app developer.", |
||
11 | -+ | |||
202 | +2x |
- #' directory, set `renv::settings$snapshot.type("explicit")`. The naming convention for `type` follows+ type = "warning", |
||
12 | -+ | |||
203 | +2x |
- #' `renv::snapshot()`. For the `"explicit"` type, refer to `renv::settings$package.dependency.fields()` for the+ duration = 10 |
||
13 | +204 |
- #' `DESCRIPTION` fields included in the lockfile.+ ) |
||
14 | -+ | |||
205 | +2x |
- #' - **Custom files-based lockfile**: To specify custom files as the basis for the lockfile, set+ warning(is_filter_ok) |
||
15 | +206 |
- #' `renv::settings$snapshot.type("custom")` and configure the `renv.snapshot.filter` option.+ } |
||
16 | -+ | |||
207 | +68x |
- #'+ .add_signature_to_data(data_validated()) |
||
17 | +208 |
- #' @section lockfile usage:+ }) |
||
18 | +209 |
- #' After creating the lockfile, you can restore the application's environment using `renv::restore()`.+ |
||
19 | -+ | |||
210 | +80x |
- #'+ data_load_status <- reactive({ |
||
20 | -+ | |||
211 | +73x |
- #' @seealso [renv::snapshot()], [renv::restore()].+ if (inherits(data_pulled(), "teal_data")) { |
||
21 | -+ | |||
212 | +68x |
- #'+ "ok" |
||
22 | +213 |
- #' @return `NULL`+ # todo: should we hide warnings on top for a data? |
||
23 | -+ | |||
214 | +5x |
- #'+ } else if (inherits(data, "teal_data_module")) { |
||
24 | -+ | |||
215 | +5x |
- #' @name module_teal_lockfile+ "teal_data_module failed" |
||
25 | +216 |
- #' @rdname module_teal_lockfile+ } else { |
||
26 | -+ | |||
217 | +! |
- #'+ "external failed" |
||
27 | +218 |
- #' @keywords internal+ } |
||
28 | +219 |
- NULL+ }) |
||
29 | +220 | |||
30 | -- |
- #' @rdname module_teal_lockfile- |
- ||
31 | -+ | |||
221 | +80x |
- ui_teal_lockfile <- function(id) {+ datasets_rv <- if (!isTRUE(attr(filter, "module_specific"))) { |
||
32 | -! | +|||
222 | +69x |
- ns <- NS(id)+ eventReactive(data_rv(), { |
||
33 | -! | +|||
223 | +59x |
- shiny::tagList(+ req(inherits(data_rv(), "teal_data")) |
||
34 | -! | +|||
224 | +59x |
- tags$span("", id = ns("lockFileStatus")),+ logger::log_debug("srv_teal@1 initializing FilteredData") |
||
35 | -! | +|||
225 | +59x |
- shinyjs::disabled(downloadLink(ns("lockFileLink"), "Download lockfile"))+ teal_data_to_filtered_data(data_rv()) |
||
36 | +226 |
- )+ }) |
||
37 | +227 |
- }+ } |
||
38 | +228 | |||
39 | -+ | |||
229 | +80x |
- #' @rdname module_teal_lockfile+ if (inherits(data, "teal_data_module")) { |
||
40 | -+ | |||
230 | +9x |
- srv_teal_lockfile <- function(id) {+ setBookmarkExclude(c("teal_modules-active_tab")) |
||
41 | -78x - | +231 | +9x |
- moduleServer(id, function(input, output, session) {+ shiny::insertTab( |
42 | -78x - | +232 | +9x |
- logger::log_debug("Initialize srv_teal_lockfile.")+ inputId = "teal_modules-active_tab", |
43 | -78x - | +233 | +9x |
- enable_lockfile_download <- function() {+ position = "before", |
44 | -! | +|||
234 | +9x |
- shinyjs::html("lockFileStatus", "Application lockfile ready.")+ select = TRUE, |
||
45 | -! | +|||
235 | +9x |
- shinyjs::hide("lockFileStatus", anim = TRUE)+ tabPanel( |
||
46 | -! | +|||
236 | +9x |
- shinyjs::enable("lockFileLink")+ title = icon("fas fa-database"), |
||
47 | -! | +|||
237 | +9x |
- output$lockFileLink <- shiny::downloadHandler(+ value = "teal_data_module", |
||
48 | -! | +|||
238 | +9x |
- filename = function() {+ tags$div( |
||
49 | -! | +|||
239 | +9x |
- "renv.lock"+ ui_init_data(session$ns("data")), |
||
50 | -+ | |||
240 | +9x |
- },+ ui_validate_reactive_teal_data(session$ns("validate")) |
||
51 | -! | +|||
241 | +
- content = function(file) {+ ) |
|||
52 | -! | +|||
242 | +
- file.copy(lockfile_path, file)+ ) |
|||
53 | -! | +|||
243 | +
- file+ ) |
|||
54 | +244 |
- },+ |
||
55 | -! | +|||
245 | +9x |
- contentType = "application/json"+ if (attr(data, "once")) { |
||
56 | -+ | |||
246 | +9x |
- )+ observeEvent(data_rv(), once = TRUE, { |
||
57 | -+ | |||
247 | +4x |
- }+ logger::log_debug("srv_teal@2 removing data tab.") |
||
58 | -78x - | +|||
248 | +
- disable_lockfile_download <- function() {+ # when once = TRUE we pull data once and then remove data tab |
|||
59 | -! | +|||
249 | +4x |
- warning("Lockfile creation failed.", call. = FALSE)+ removeTab("teal_modules-active_tab", target = "teal_data_module") |
||
60 | -! | +|||
250 | +
- shinyjs::html("lockFileStatus", "Lockfile creation failed.")+ }) |
|||
61 | -! | +|||
251 | +
- shinyjs::hide("lockFileLink")+ } |
|||
62 | +252 |
- }+ } else { |
||
63 | +253 |
-
+ # when no teal_data_module then we want to display messages above tabsetPanel (because there is no data-tab) |
||
64 | -78x - | +254 | +71x |
- shiny::onStop(function() {+ insertUI( |
65 | -78x - | +255 | +71x |
- if (file.exists(lockfile_path) && !shiny::isRunning()) {+ selector = sprintf("#%s", session$ns("tabpanel_wrapper")), |
66 | -1x - | +256 | +71x |
- logger::log_debug("Removing lockfile after shutting down the app")+ where = "beforeBegin", |
67 | -1x - | +257 | +71x |
- file.remove(lockfile_path)+ ui = tags$div(ui_validate_reactive_teal_data(session$ns("validate")), tags$br()) |
68 | +258 |
- }+ ) |
||
69 | +259 |
- })+ } |
||
70 | +260 | |||
71 | -78x - | +261 | +80x |
- lockfile_path <- "teal_app.lock"+ module_labels <- unlist(module_labels(modules), use.names = FALSE) |
72 | -78x - | +262 | +80x |
- mode <- getOption("teal.lockfile.mode", default = "")+ slices_global <- methods::new(".slicesGlobal", filter, module_labels) |
73 | -+ | |||
263 | +80x |
-
+ modules_output <- srv_teal_module( |
||
74 | -78x - | +264 | +80x |
- if (!(mode %in% c("auto", "enabled", "disabled"))) {+ id = "teal_modules", |
75 | -! | +|||
265 | +80x |
- stop("'teal.lockfile.mode' option can only be one of \"auto\", \"disabled\" or \"disabled\". ")+ data_rv = data_rv, |
||
76 | -+ | |||
266 | +80x |
- }+ datasets = datasets_rv, |
||
77 | -+ | |||
267 | +80x |
-
+ modules = modules, |
||
78 | -78x - | +268 | +80x | +
+ slices_global = slices_global,+ |
+
269 | +80x | +
+ data_load_status = data_load_status+ |
+ ||
270 | +
- if (mode == "disabled") {+ ) |
|||
79 | -1x - | +271 | +80x |
- logger::log_debug("'teal.lockfile.mode' option is set to 'disabled'. Hiding lockfile download button.")+ mapping_table <- srv_filter_manager_panel("filter_manager_panel", slices_global = slices_global) |
80 | -1x - | +272 | +80x |
- shinyjs::hide("lockFileLink")+ snapshots <- srv_snapshot_manager_panel("snapshot_manager_panel", slices_global = slices_global) |
81 | -1x - | +273 | +80x |
- return(NULL)+ srv_bookmark_panel("bookmark_manager", modules) |
82 | +274 |
- }+ }) |
||
83 | +275 | |||
84 | -77x - | -
- if (file.exists(lockfile_path)) {- |
- ||
85 | -! | -
- logger::log_debug("Lockfile has already been created for this app - skipping automatic creation.")- |
- ||
86 | -! | +276 | +80x |
- enable_lockfile_download()+ invisible(NULL) |
87 | -! | +|||
277 | +
- return(NULL)+ } |
88 | +1 |
- }+ #' Filter state snapshot management |
||
89 | +2 |
-
+ #' |
||
90 | -77x - | +|||
3 | +
- if (mode == "auto" && .is_disabled_lockfile_scenario()) {+ #' Capture and restore snapshots of the global (app) filter state. |
|||
91 | -76x - | +|||
4 | +
- logger::log_debug(+ #' |
|||
92 | -76x - | +|||
5 | +
- "Automatic lockfile creation disabled. Execution scenario satisfies teal:::.is_disabled_lockfile_scenario()."+ #' This module introduces snapshots: stored descriptions of the filter state of the entire application. |
|||
93 | +6 |
- )+ #' Snapshots allow the user to save the current filter state of the application for later use in the session, |
||
94 | -76x - | +|||
7 | +
- shinyjs::hide("lockFileLink")+ #' as well as to save it to file in order to share it with an app developer or other users, |
|||
95 | -76x - | +|||
8 | +
- return(NULL)+ #' who in turn can upload it to their own session. |
|||
96 | +9 |
- }+ #' |
||
97 | +10 |
-
+ #' The snapshot manager is accessed with the camera icon in the tabset bar. |
||
98 | -1x - | +|||
11 | +
- if (!.is_lockfile_deps_installed()) {+ #' At the beginning of a session it presents three icons: a camera, an upload, and an circular arrow. |
|||
99 | -! | +|||
12 | +
- warning("Automatic lockfile creation disabled. `mirai` and `renv` packages must be installed.")+ #' Clicking the camera captures a snapshot, clicking the upload adds a snapshot from a file |
|||
100 | -! | +|||
13 | +
- shinyjs::hide("lockFileLink")+ #' and applies the filter states therein, and clicking the arrow resets initial application state. |
|||
101 | -! | +|||
14 | +
- return(NULL)+ #' As snapshots are added, they will show up as rows in a table and each will have a select button and a save button. |
|||
102 | +15 |
- }+ #' |
||
103 | +16 |
-
+ #' @section Server logic: |
||
104 | +17 |
- # - Will be run only if the lockfile doesn't exist (see the if-s above)+ #' Snapshots are basically `teal_slices` objects, however, since each module is served by a separate instance |
||
105 | +18 |
- # - We render to the tempfile because the process might last after session is closed and we don't+ #' of `FilteredData` and these objects require shared state, `teal_slice` is a `reactiveVal` so `teal_slices` |
||
106 | +19 |
- # want to make a "teal_app.renv" then. This is why we copy only during active session.+ #' cannot be stored as is. Therefore, `teal_slices` are reversibly converted to a list of lists representation |
||
107 | -1x - | +|||
20 | +
- process <- .teal_lockfile_process_invoke(lockfile_path)+ #' (attributes are maintained). |
|||
108 | -1x - | +|||
21 | +
- observeEvent(process$status(), {+ #' |
|||
109 | -! | +|||
22 | +
- if (process$status() %in% c("initial", "running")) {+ #' Snapshots are stored in a `reactiveVal` as a named list. |
|||
110 | -! | +|||
23 | +
- shinyjs::html("lockFileStatus", "Creating lockfile...")+ #' The first snapshot is the initial state of the application and the user can add a snapshot whenever they see fit. |
|||
111 | -! | +|||
24 | +
- } else if (process$status() == "success") {+ #' |
|||
112 | -! | +|||
25 | +
- result <- process$result()+ #' For every snapshot except the initial one, a piece of UI is generated that contains |
|||
113 | -! | +|||
26 | +
- if (any(grepl("Lockfile written to", result$out))) {+ #' the snapshot name, a select button to restore that snapshot, and a save button to save it to a file. |
|||
114 | -! | +|||
27 | +
- logger::log_debug("Lockfile containing { length(result$res$Packages) } packages created.")+ #' The initial snapshot is restored by a separate "reset" button. |
|||
115 | -! | +|||
28 | +
- if (any(grepl("(WARNING|ERROR):", result$out))) {+ #' It cannot be saved directly but a user is welcome to capture the initial state as a snapshot and save that. |
|||
116 | -! | +|||
29 | +
- warning("Lockfile created with warning(s) or error(s):", call. = FALSE)+ #' |
|||
117 | -! | +|||
30 | +
- for (i in result$out) {+ #' @section Snapshot mechanics: |
|||
118 | -! | +|||
31 | +
- warning(i, call. = FALSE)+ #' When a snapshot is captured, the user is prompted to name it. |
|||
119 | +32 |
- }+ #' Names are displayed as is but since they are used to create button ids, |
||
120 | +33 |
- }+ #' under the hood they are converted to syntactically valid strings. |
||
121 | -! | +|||
34 | +
- enable_lockfile_download()+ #' New snapshot names are validated so that their valid versions are unique. |
|||
122 | +35 |
- } else {+ #' Leading and trailing white space is trimmed. |
||
123 | -! | +|||
36 | +
- disable_lockfile_download()+ #' |
|||
124 | +37 |
- }+ #' The module can read the global state of the application from `slices_global` and `mapping_matrix`. |
||
125 | -! | +|||
38 | +
- } else if (process$status() == "error") {+ #' The former provides a list of all existing `teal_slice`s and the latter says which slice is active in which module. |
|||
126 | -! | +|||
39 | +
- disable_lockfile_download()+ #' Once a name has been accepted, `slices_global` is converted to a list of lists - a snapshot. |
|||
127 | +40 |
- }+ #' The snapshot contains the `mapping` attribute of the initial application state |
||
128 | +41 |
- })+ #' (or one that has been restored), which may not reflect the current one, |
||
129 | +42 |
-
+ #' so `mapping_matrix` is transformed to obtain the current mapping, i.e. a list that, |
||
130 | -1x - | +|||
43 | +
- NULL+ #' when passed to the `mapping` argument of [teal_slices()], would result in the current mapping. |
|||
131 | +44 |
- })+ #' This is substituted as the snapshot's `mapping` attribute and the snapshot is added to the snapshot list. |
||
132 | +45 |
- }+ #' |
||
133 | +46 |
-
+ #' To restore app state, a snapshot is retrieved from storage and rebuilt into a `teal_slices` object. |
||
134 | +47 |
- utils::globalVariables(c("opts", "sysenv", "libpaths", "wd", "lockfilepath", "run")) # needed for mirai call+ #' Then state of all `FilteredData` objects (provided in `datasets`) is cleared |
||
135 | +48 |
- #' @rdname module_teal_lockfile+ #' and set anew according to the `mapping` attribute of the snapshot. |
||
136 | +49 |
- .teal_lockfile_process_invoke <- function(lockfile_path) {+ #' The snapshot is then set as the current content of `slices_global`. |
||
137 | -1x - | +|||
50 | +
- mirai_obj <- NULL+ #' |
|||
138 | -1x - | +|||
51 | +
- process <- shiny::ExtendedTask$new(function() {+ #' To save a snapshot, the snapshot is retrieved and reassembled just like for restoring, |
|||
139 | -1x - | +|||
52 | +
- m <- mirai::mirai(+ #' and then saved to file with [slices_store()]. |
|||
140 | +53 |
- {+ #' |
||
141 | -1x - | +|||
54 | +
- options(opts)+ #' When a snapshot is uploaded, it will first be added to storage just like a newly created one, |
|||
142 | -1x - | +|||
55 | +
- do.call(Sys.setenv, sysenv)+ #' and then used to restore app state much like a snapshot taken from storage. |
|||
143 | -1x - | +|||
56 | +
- .libPaths(libpaths)+ #' Upon clicking the upload icon the user will be prompted for a file to upload |
|||
144 | -1x - | +|||
57 | +
- setwd(wd)+ #' and may choose to name the new snapshot. The name defaults to the name of the file (the extension is dropped) |
|||
145 | -1x - | +|||
58 | +
- run(lockfile_path = lockfile_path)+ #' and normal naming rules apply. Loading the file yields a `teal_slices` object, |
|||
146 | +59 |
- },+ #' which is disassembled for storage and used directly for restoring app state. |
||
147 | -1x - | +|||
60 | +
- run = .renv_snapshot,+ #' |
|||
148 | -1x - | +|||
61 | +
- lockfile_path = lockfile_path,+ #' @section Transferring snapshots: |
|||
149 | -1x - | +|||
62 | +
- opts = options(),+ #' Snapshots uploaded from disk should only be used in the same application they come from, |
|||
150 | -1x - | +|||
63 | +
- libpaths = .libPaths(),+ #' _i.e._ an application that uses the same data and the same modules. |
|||
151 | -1x - | +|||
64 | +
- sysenv = as.list(Sys.getenv()),+ #' To ensure this is the case, `init` stamps `teal_slices` with an app id that is stored in the `app_id` attribute of |
|||
152 | -1x - | +|||
65 | +
- wd = getwd()+ #' a `teal_slices` object. When a snapshot is restored from file, its `app_id` is compared to that |
|||
153 | +66 |
- )+ #' of the current app state and only if the match is the snapshot admitted to the session. |
||
154 | -1x - | +|||
67 | +
- mirai_obj <<- m+ #' |
|||
155 | -1x - | +|||
68 | +
- m+ #' @section Bookmarks: |
|||
156 | +69 |
- })+ #' An `onBookmark` callback creates a snapshot of the current filter state. |
||
157 | +70 |
-
+ #' This is done on the app session, not the module session. |
||
158 | -1x - | +|||
71 | +
- shiny::onStop(function() {+ #' (The snapshot will be retrieved by `module_teal` in order to set initial app state in a restored app.) |
|||
159 | -1x - | +|||
72 | +
- if (mirai::unresolved(mirai_obj)) {+ #' Then that snapshot, and the previous snapshot history are dumped into the `values.rds` file in `<bookmark_dir>`. |
|||
160 | -! | +|||
73 | +
- logger::log_debug("Terminating a running lockfile process...")+ #' |
|||
161 | -! | +|||
74 | +
- mirai::stop_mirai(mirai_obj) # this doesn't stop running - renv will be created even if session is closed+ #' @param id (`character(1)`) `shiny` module instance id. |
|||
162 | +75 |
- }+ #' @param slices_global (`reactiveVal`) that contains a `teal_slices` object |
||
163 | +76 |
- })+ #' containing all `teal_slice`s existing in the app, both active and inactive. |
||
164 | +77 |
-
+ #' |
||
165 | -1x - | +|||
78 | +
- suppressWarnings({ # 'package:stats' may not be available when loading+ #' @return `list` containing the snapshot history, where each element is an unlisted `teal_slices` object. |
|||
166 | -1x - | +|||
79 | +
- process$invoke()+ #' |
|||
167 | +80 |
- })+ #' @name module_snapshot_manager |
||
168 | +81 |
-
+ #' @rdname module_snapshot_manager |
||
169 | -1x - | +|||
82 | +
- logger::log_debug("Lockfile creation started based on { getwd() }.")+ #' |
|||
170 | +83 |
-
+ #' @author Aleksander Chlebowski |
||
171 | -1x - | +|||
84 | +
- process+ #' @keywords internal |
|||
172 | +85 |
- }+ NULL |
||
173 | +86 | |||
174 | +87 |
- #' @rdname module_teal_lockfile+ #' @rdname module_snapshot_manager |
||
175 | +88 |
- .renv_snapshot <- function(lockfile_path) {- |
- ||
176 | -1x - | -
- out <- utils::capture.output(+ ui_snapshot_manager_panel <- function(id) { |
||
177 | -1x - | +|||
89 | +! |
- res <- renv::snapshot(+ ns <- NS(id) |
||
178 | -1x - | +|||
90 | +! |
- lockfile = lockfile_path,+ tags$button( |
||
179 | -1x - | +|||
91 | +! |
- prompt = FALSE,+ id = ns("show_snapshot_manager"), |
||
180 | -1x - | +|||
92 | +! |
- force = TRUE,+ class = "btn action-button wunder_bar_button", |
||
181 | -1x - | +|||
93 | +! |
- type = renv::settings$snapshot.type() # see the section "Different ways of creating lockfile" above here+ title = "View filter mapping", |
||
182 | -+ | |||
94 | +! |
- )+ suppressMessages(icon("fas fa-camera")) |
||
183 | +95 |
) |
||
184 | -- | - - | -||
185 | -1x - | -
- list(out = out, res = res)- |
- ||
186 | +96 |
} |
||
187 | +97 | |||
188 | +98 |
- #' @rdname module_teal_lockfile+ #' @rdname module_snapshot_manager |
||
189 | +99 |
- .is_lockfile_deps_installed <- function() {+ srv_snapshot_manager_panel <- function(id, slices_global) { |
||
190 | -1x - | +100 | +80x |
- requireNamespace("mirai", quietly = TRUE) && requireNamespace("renv", quietly = TRUE)+ moduleServer(id, function(input, output, session) { |
191 | -+ | |||
101 | +80x |
- }+ logger::log_debug("srv_snapshot_manager_panel initializing") |
||
192 | -+ | |||
102 | +80x |
-
+ setBookmarkExclude(c("show_snapshot_manager")) |
||
193 | -+ | |||
103 | +80x |
- #' @rdname module_teal_lockfile+ observeEvent(input$show_snapshot_manager, { |
||
194 | -+ | |||
104 | +! |
- .is_disabled_lockfile_scenario <- function() {+ logger::log_debug("srv_snapshot_manager_panel@1 show_snapshot_manager button has been clicked.") |
||
195 | -76x - | +|||
105 | +! |
- identical(Sys.getenv("CALLR_IS_RUNNING"), "true") || # inside callr process+ showModal( |
||
196 | -76x - | +|||
106 | +! |
- identical(Sys.getenv("TESTTHAT"), "true") || # inside devtools::test+ modalDialog( |
||
197 | -76x - | +|||
107 | +! |
- !identical(Sys.getenv("QUARTO_PROJECT_ROOT"), "") || # inside Quarto process+ ui_snapshot_manager(session$ns("module")), |
||
198 | -+ | |||
108 | +! |
- (+ class = "snapshot_manager_modal", |
||
199 | -76x - | +|||
109 | +! |
- ("CheckExEnv" %in% search()) || any(c("_R_CHECK_TIMINGS_", "_R_CHECK_LICENSE_") %in% names(Sys.getenv()))+ size = "m", |
||
200 | -76x - | +|||
110 | +! |
- ) # inside R CMD CHECK+ footer = NULL, |
||
201 | -+ | |||
111 | +! |
- }+ easyClose = TRUE |
1 | +112 |
- #' Execute and validate `teal_data_module`+ ) |
||
2 | +113 |
- #'+ ) |
||
3 | +114 |
- #' This is a low level module to handle `teal_data_module` execution and validation.+ }) |
||
4 | -+ | |||
115 | +80x |
- #' [teal_transform_module()] inherits from [teal_data_module()] so it is handled by this module too.+ srv_snapshot_manager("module", slices_global = slices_global) |
||
5 | +116 |
- #' [srv_teal()] accepts various `data` objects and eventually they are all transformed to `reactive`+ }) |
||
6 | +117 |
- #' [teal_data()] which is a standard data class in whole `teal` framework.+ } |
||
7 | +118 |
- #'+ |
||
8 | +119 |
- #' @section data validation:+ #' @rdname module_snapshot_manager |
||
9 | +120 |
- #'+ ui_snapshot_manager <- function(id) { |
||
10 | -+ | |||
121 | +! |
- #' Executed [teal_data_module()] is validated and output is validated for consistency.+ ns <- NS(id) |
||
11 | -+ | |||
122 | +! |
- #' Output `data` is invalid if:+ tags$div( |
||
12 | -+ | |||
123 | +! |
- #' 1. [teal_data_module()] is invalid if server doesn't return `reactive`. **Immediately crashes an app!**+ class = "manager_content", |
||
13 | -+ | |||
124 | +! |
- #' 2. `reactive` throws a `shiny.error` - happens when module creating [teal_data()] fails.+ tags$div( |
||
14 | -+ | |||
125 | +! |
- #' 3. `reactive` returns `qenv.error` - happens when [teal_data()] evaluates a failing code.+ class = "manager_table_row", |
||
15 | -+ | |||
126 | +! |
- #' 4. `reactive` object doesn't return [teal_data()].+ tags$span(tags$b("Snapshot manager")), |
||
16 | -+ | |||
127 | +! |
- #' 5. [teal_data()] object lacks any `datanames` specified in the `modules` argument.+ actionLink(ns("snapshot_add"), label = NULL, icon = icon("fas fa-camera"), title = "add snapshot"), |
||
17 | -+ | |||
128 | +! |
- #'+ actionLink(ns("snapshot_load"), label = NULL, icon = icon("fas fa-upload"), title = "upload snapshot"), |
||
18 | -+ | |||
129 | +! |
- #' `teal` (observers in `srv_teal`) always waits to render an app until `reactive` `teal_data` is+ actionLink(ns("snapshot_reset"), label = NULL, icon = icon("fas fa-undo"), title = "reset initial state"), |
||
19 | -+ | |||
130 | +! |
- #' returned. If error 2-4 occurs, relevant error message is displayed to the app user. Once the issue is+ NULL |
||
20 | +131 |
- #' resolved, the app will continue to run. `teal` guarantees that errors in data don't crash the app+ ), |
||
21 | -+ | |||
132 | +! |
- #' (except error 1).+ uiOutput(ns("snapshot_list")) |
||
22 | +133 |
- #'+ ) |
||
23 | +134 |
- #' @param id (`character(1)`) Module id+ } |
||
24 | +135 |
- #' @param data (`reactive teal_data`)+ |
||
25 | +136 |
- #' @param data_module (`teal_data_module`)+ #' @rdname module_snapshot_manager |
||
26 | +137 |
- #' @param modules (`teal_modules` or `teal_module`) For `datanames` validation purpose+ srv_snapshot_manager <- function(id, slices_global) { |
||
27 | -+ | |||
138 | +80x |
- #' @param validate_shiny_silent_error (`logical`) If `TRUE`, then `shiny.silent.error` is validated and+ checkmate::assert_character(id) |
||
28 | +139 |
- #' @param is_transformer_failed (`reactiveValues`) contains `logical` flags named after each transformer.+ |
||
29 | -+ | |||
140 | +80x |
- #' Help to determine if any previous transformer failed, so that following transformers can be disabled+ moduleServer(id, function(input, output, session) { |
||
30 | -+ | |||
141 | +80x |
- #' and display a generic failure message.+ logger::log_debug("srv_snapshot_manager initializing") |
||
31 | +142 |
- #'+ |
||
32 | +143 |
- #' @return `reactive` `teal_data`+ # Set up bookmarking callbacks ---- |
||
33 | +144 |
- #'+ # Register bookmark exclusions (all buttons and text fields). |
||
34 | -+ | |||
145 | +80x |
- #' @rdname module_teal_data+ setBookmarkExclude(c( |
||
35 | -+ | |||
146 | +80x |
- #' @name module_teal_data+ "snapshot_add", "snapshot_load", "snapshot_reset", |
||
36 | -+ | |||
147 | +80x |
- #' @keywords internal+ "snapshot_name_accept", "snaphot_file_accept", |
||
37 | -+ | |||
148 | +80x |
- NULL+ "snapshot_name", "snapshot_file" |
||
38 | +149 |
-
+ )) |
||
39 | +150 |
- #' @rdname module_teal_data+ # Add snapshot history to bookmark. |
||
40 | -+ | |||
151 | +80x |
- ui_teal_data <- function(id, data_module = function(id) NULL) {+ session$onBookmark(function(state) { |
||
41 | +152 | ! |
- checkmate::assert_string(id)+ logger::log_debug("srv_snapshot_manager@onBookmark: storing snapshot and bookmark history") |
|
42 | +153 | ! |
- checkmate::assert_function(data_module, args = "id")+ state$values$snapshot_history <- snapshot_history() # isolate this? |
|
43 | -! | +|||
154 | +
- ns <- NS(id)+ }) |
|||
44 | +155 | |||
45 | -! | +|||
156 | +80x |
- shiny::tagList(+ ns <- session$ns |
||
46 | -! | +|||
157 | +
- tags$div(id = ns("wrapper"), data_module(id = ns("data"))),+ |
|||
47 | -! | +|||
158 | +
- ui_validate_reactive_teal_data(ns("validate"))+ # Track global filter states ---- |
|||
48 | -+ | |||
159 | +80x |
- )+ snapshot_history <- reactiveVal({ |
||
49 | +160 |
- }+ # Restore directly from bookmarked state, if applicable.+ |
+ ||
161 | +80x | +
+ restoreValue( |
||
50 | -+ | |||
162 | +80x |
-
+ ns("snapshot_history"), |
||
51 | -+ | |||
163 | +80x |
- #' @rdname module_teal_data+ list("Initial application state" = shiny::isolate(as.list(slices_global$all_slices(), recursive = TRUE))) |
||
52 | +164 |
- srv_teal_data <- function(id,+ ) |
||
53 | +165 |
- data_module = function(id) NULL,+ }) |
||
54 | +166 |
- modules = NULL,+ |
||
55 | +167 |
- validate_shiny_silent_error = TRUE,+ # Snapshot current application state ---- |
||
56 | +168 |
- is_transformer_failed = reactiveValues()) {+ # Name snaphsot. |
||
57 | -19x - | +169 | +80x |
- checkmate::assert_string(id)+ observeEvent(input$snapshot_add, { |
58 | -19x - | +|||
170 | +! |
- checkmate::assert_function(data_module, args = "id")+ logger::log_debug("srv_snapshot_manager: snapshot_add button clicked") |
||
59 | -19x - | +|||
171 | +! |
- checkmate::assert_multi_class(modules, c("teal_modules", "teal_module"), null.ok = TRUE)+ showModal( |
||
60 | -19x - | +|||
172 | +! |
- checkmate::assert_class(is_transformer_failed, "reactivevalues")+ modalDialog( |
||
61 | -+ | |||
173 | +! |
-
+ textInput(ns("snapshot_name"), "Name the snapshot", width = "100%", placeholder = "Meaningful, unique name"), |
||
62 | -19x - | +|||
174 | +! |
- moduleServer(id, function(input, output, session) {+ footer = tagList( |
||
63 | -19x - | +|||
175 | +! |
- logger::log_debug("srv_teal_data initializing.")+ actionButton(ns("snapshot_name_accept"), "Accept", icon = icon("far fa-thumbs-up")), |
||
64 | -19x - | +|||
176 | +! |
- is_transformer_failed[[id]] <- FALSE+ modalButton(label = "Cancel", icon = icon("far fa-thumbs-down")) |
||
65 | -19x - | +|||
177 | +
- data_out <- data_module(id = "data")+ ), |
|||
66 | -19x - | +|||
178 | +! |
- data_handled <- reactive(tryCatch(data_out(), error = function(e) e))+ size = "s" |
||
67 | -19x - | +|||
179 | +
- observeEvent(data_handled(), {+ ) |
|||
68 | -21x - | +|||
180 | +
- if (!inherits(data_handled(), "teal_data")) {+ ) |
|||
69 | -6x - | +|||
181 | +
- is_transformer_failed[[id]] <- TRUE+ }) |
|||
70 | +182 |
- } else {+ # Store snaphsot. |
||
71 | -15x - | -
- is_transformer_failed[[id]] <- FALSE- |
- ||
72 | -+ | 183 | +80x |
- }+ observeEvent(input$snapshot_name_accept, { |
73 | -+ | |||
184 | +! |
- })+ logger::log_debug("srv_snapshot_manager: snapshot_name_accept button clicked") |
||
74 | -+ | |||
185 | +! |
-
+ snapshot_name <- trimws(input$snapshot_name) |
||
75 | -19x - | +|||
186 | +! |
- is_previous_failed <- reactive({+ if (identical(snapshot_name, "")) { |
||
76 | -19x - | +|||
187 | +! |
- idx_this <- which(names(is_transformer_failed) == id)+ logger::log_debug("srv_snapshot_manager: snapshot name rejected") |
||
77 | -19x - | +|||
188 | +! |
- is_transformer_failed_list <- reactiveValuesToList(is_transformer_failed)+ showNotification( |
||
78 | -19x - | +|||
189 | +! |
- idx_failures <- which(unlist(is_transformer_failed_list))+ "Please name the snapshot.", |
||
79 | -19x - | +|||
190 | +! |
- any(idx_failures < idx_this)+ type = "message" |
||
80 | +191 |
- })+ ) |
||
81 | -+ | |||
192 | +! |
-
+ updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name") |
||
82 | -19x - | +|||
193 | +! |
- observeEvent(is_previous_failed(), {+ } else if (is.element(make.names(snapshot_name), make.names(names(snapshot_history())))) { |
||
83 | -19x - | +|||
194 | +! |
- if (is_previous_failed()) {+ logger::log_debug("srv_snapshot_manager: snapshot name rejected") |
||
84 | +195 | ! |
- shinyjs::disable("wrapper")+ showNotification( |
|
85 | -+ | |||
196 | +! |
- } else {+ "This name is in conflict with other snapshot names. Please choose a different one.", |
||
86 | -19x - | +|||
197 | +! |
- shinyjs::enable("wrapper")+ type = "message" |
||
87 | +198 |
- }+ ) |
||
88 | -+ | |||
199 | +! |
- })+ updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name") |
||
89 | +200 |
-
+ } else { |
||
90 | -19x - | +|||
201 | +! |
- srv_validate_reactive_teal_data(+ logger::log_debug("srv_snapshot_manager: snapshot name accepted, adding snapshot") |
||
91 | -19x - | +|||
202 | +! |
- "validate",+ snapshot <- as.list(slices_global$all_slices(), recursive = TRUE) |
||
92 | -19x - | +|||
203 | +! |
- data = data_handled,+ snapshot_update <- c(snapshot_history(), list(snapshot)) |
||
93 | -19x - | +|||
204 | +! |
- modules = modules,+ names(snapshot_update)[length(snapshot_update)] <- snapshot_name |
||
94 | -19x - | +|||
205 | +! |
- validate_shiny_silent_error = validate_shiny_silent_error,+ snapshot_history(snapshot_update) |
||
95 | -19x - | +|||
206 | +! |
- hide_validation_error = is_previous_failed+ removeModal() |
||
96 | +207 |
- )+ # Reopen filter manager modal by clicking button in the main application. |
||
97 | -+ | |||
208 | +! |
- })+ shinyjs::click(id = "teal-wunder_bar-show_snapshot_manager", asis = TRUE) |
||
98 | +209 |
- }+ } |
||
99 | +210 |
-
+ }) |
||
100 | +211 |
- #' @rdname module_teal_data+ |
||
101 | +212 |
- ui_validate_reactive_teal_data <- function(id) {- |
- ||
102 | -77x - | -
- ns <- NS(id)+ # Upload a snapshot file ---- |
||
103 | -77x - | +|||
213 | +
- tagList(+ # Select file. |
|||
104 | -77x - | +214 | +80x |
- div(+ observeEvent(input$snapshot_load, { |
105 | -77x - | +|||
215 | +! |
- id = ns("validate_messages"),+ logger::log_debug("srv_snapshot_manager: snapshot_load button clicked") |
||
106 | -77x - | +|||
216 | +! |
- class = "teal_validated",+ showModal( |
||
107 | -77x - | +|||
217 | +! |
- ui_validate_error(ns("silent_error")),+ modalDialog( |
||
108 | -77x - | +|||
218 | +! |
- ui_check_class_teal_data(ns("class_teal_data")),+ fileInput(ns("snapshot_file"), "Choose snapshot file", accept = ".json", width = "100%"), |
||
109 | -77x - | +|||
219 | +! |
- ui_check_shiny_warnings(ns("shiny_warnings"))+ textInput( |
||
110 | -+ | |||
220 | +! |
- ),+ ns("snapshot_name"), |
||
111 | -77x - | +|||
221 | +! |
- div(+ "Name the snapshot (optional)", |
||
112 | -77x - | +|||
222 | +! |
- class = "teal_validated",+ width = "100%", |
||
113 | -77x - | +|||
223 | +! |
- uiOutput(ns("previous_failed"))+ placeholder = "Meaningful, unique name" |
||
114 | +224 |
- )+ ), |
||
115 | -+ | |||
225 | +! |
- )+ footer = tagList( |
||
116 | -+ | |||
226 | +! |
- }+ actionButton(ns("snaphot_file_accept"), "Accept", icon = icon("far fa-thumbs-up")), |
||
117 | -+ | |||
227 | +! |
-
+ modalButton(label = "Cancel", icon = icon("far fa-thumbs-down")) |
||
118 | +228 |
- #' @rdname module_teal_data+ ) |
||
119 | +229 |
- srv_validate_reactive_teal_data <- function(id, # nolint: object_length+ ) |
||
120 | +230 |
- data,+ ) |
||
121 | +231 |
- modules = NULL,+ }) |
||
122 | +232 |
- validate_shiny_silent_error = FALSE,+ # Store new snapshot to list and restore filter states. |
||
123 | -+ | |||
233 | +80x |
- hide_validation_error = reactive(FALSE)) {+ observeEvent(input$snaphot_file_accept, { |
||
124 | -172x - | +|||
234 | +! |
- checkmate::assert_string(id)+ logger::log_debug("srv_snapshot_manager: snapshot_file_accept button clicked") |
||
125 | -172x - | +|||
235 | +! |
- checkmate::assert_multi_class(modules, c("teal_modules", "teal_module"), null.ok = TRUE)+ snapshot_name <- trimws(input$snapshot_name) |
||
126 | -172x - | +|||
236 | +! |
- checkmate::assert_flag(validate_shiny_silent_error)+ if (identical(snapshot_name, "")) { |
||
127 | -+ | |||
237 | +! |
-
+ logger::log_debug("srv_snapshot_manager: no snapshot name provided, naming after file") |
||
128 | -172x - | +|||
238 | +! |
- moduleServer(id, function(input, output, session) {+ snapshot_name <- tools::file_path_sans_ext(input$snapshot_file$name) |
||
129 | +239 |
- # there is an empty reactive cycle on `init` and `data_rv` has `shiny.silent.error` class- |
- ||
130 | -172x - | -
- srv_validate_error("silent_error", data, validate_shiny_silent_error)+ } |
||
131 | -172x - | +|||
240 | +! |
- srv_check_class_teal_data("class_teal_data", data)+ if (is.element(make.names(snapshot_name), make.names(names(snapshot_history())))) { |
||
132 | -172x - | +|||
241 | +! |
- srv_check_shiny_warnings("shiny_warnings", data, modules)+ logger::log_debug("srv_snapshot_manager: snapshot name rejected") |
||
133 | -172x - | +|||
242 | +! |
- output$previous_failed <- renderUI({+ showNotification( |
||
134 | -162x - | +|||
243 | +! |
- if (hide_validation_error()) {+ "This name is in conflict with other snapshot names. Please choose a different one.", |
||
135 | +244 | ! |
- shinyjs::hide("validate_messages")+ type = "message"+ |
+ |
245 | ++ |
+ ) |
||
136 | +246 | ! |
- tags$div("One of previous transformers failed. Please fix and continue.", class = "teal-output-warning")+ updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name") |
|
137 | +247 |
} else { |
||
138 | -162x - | +|||
248 | +
- shinyjs::show("validate_messages")+ # Restore snapshot and verify app compatibility. |
|||
139 | -162x - | +|||
249 | +! |
- NULL+ logger::log_debug("srv_snapshot_manager: snapshot name accepted, loading snapshot") |
||
140 | -+ | |||
250 | +! |
- }+ snapshot_state <- try(slices_restore(input$snapshot_file$datapath)) |
||
141 | -+ | |||
251 | +! |
- })+ if (!inherits(snapshot_state, "modules_teal_slices")) { |
||
142 | -+ | |||
252 | +! |
-
+ logger::log_debug("srv_snapshot_manager: snapshot file corrupt") |
||
143 | -172x - | +|||
253 | +! |
- .trigger_on_success(data)+ showNotification( |
||
144 | -+ | |||
254 | +! |
- })+ "File appears to be corrupt.", |
||
145 | -+ | |||
255 | +! |
- }+ type = "error" |
||
146 | +256 |
-
+ ) |
||
147 | -+ | |||
257 | +! |
- #' @keywords internal+ } else if (!identical(attr(snapshot_state, "app_id"), attr(slices_global$all_slices(), "app_id"))) { |
||
148 | -+ | |||
258 | +! |
- ui_validate_error <- function(id) {+ logger::log_debug("srv_snapshot_manager: snapshot not compatible with app") |
||
149 | -77x - | +|||
259 | +! |
- ns <- NS(id)+ showNotification( |
||
150 | -77x - | +|||
260 | +! |
- uiOutput(ns("message"))+ "This snapshot file is not compatible with the app and cannot be loaded.", |
||
151 | -+ | |||
261 | +! |
- }+ type = "warning" |
||
152 | +262 |
-
+ ) |
||
153 | +263 |
- #' @keywords internal+ } else { |
||
154 | +264 |
- srv_validate_error <- function(id, data, validate_shiny_silent_error) {- |
- ||
155 | -172x - | -
- checkmate::assert_string(id)+ # Add to snapshot history. |
||
156 | -172x - | +|||
265 | +! |
- checkmate::assert_flag(validate_shiny_silent_error)+ logger::log_debug("srv_snapshot_manager: snapshot loaded, adding to history") |
||
157 | -172x - | +|||
266 | +! |
- moduleServer(id, function(input, output, session) {+ snapshot <- as.list(slices_global$all_slices(), recursive = TRUE) |
||
158 | -172x - | +|||
267 | +! |
- output$message <- renderUI({+ snapshot_update <- c(snapshot_history(), list(snapshot)) |
||
159 | -175x - | +|||
268 | +! |
- is_shiny_silent_error <- inherits(data(), "shiny.silent.error") && identical(data()$message, "")+ names(snapshot_update)[length(snapshot_update)] <- snapshot_name |
||
160 | -169x - | +|||
269 | +! |
- if (inherits(data(), "qenv.error")) {+ snapshot_history(snapshot_update) |
||
161 | -2x - | +|||
270 | +
- validate(+ ### Begin simplified restore procedure. ### |
|||
162 | -2x - | +|||
271 | +! |
- need(+ logger::log_debug("srv_snapshot_manager: restoring snapshot") |
||
163 | -2x - | +|||
272 | +! |
- FALSE,+ slices_global$slices_set(snapshot_state) |
||
164 | -2x - | +|||
273 | +! |
- paste(+ removeModal() |
||
165 | -2x - | +|||
274 | +
- "Error when executing the `data` module:",+ ### End simplified restore procedure. ### |
|||
166 | -2x - | +|||
275 | +
- strip_style(paste(data()$message, collapse = "\n")),+ } |
|||
167 | -2x - | +|||
276 | +
- "\nCheck your inputs or contact app developer if error persists.",+ } |
|||
168 | -2x - | +|||
277 | +
- collapse = "\n"+ }) |
|||
169 | +278 |
- )+ # Apply newly added snapshot. |
||
170 | +279 |
- )+ |
||
171 | +280 |
- )+ # Restore initial state ---- |
||
172 | -167x - | +281 | +80x |
- } else if (inherits(data(), "error")) {+ observeEvent(input$snapshot_reset, { |
173 | -7x - | +282 | +2x |
- if (is_shiny_silent_error && !validate_shiny_silent_error) {+ logger::log_debug("srv_snapshot_manager: snapshot_reset button clicked, restoring snapshot") |
174 | -1x - | +283 | +2x |
- return(NULL)+ s <- "Initial application state" |
175 | +284 |
- }- |
- ||
176 | -6x - | -
- validate(+ ### Begin restore procedure. ### |
||
177 | -6x - | +285 | +2x |
- need(+ snapshot <- snapshot_history()[[s]] |
178 | -6x - | +|||
286 | +
- FALSE,+ # todo: as.teal_slices looses module-mapping if is not global |
|||
179 | -6x - | +287 | +2x |
- sprintf(+ snapshot_state <- as.teal_slices(snapshot) |
180 | -6x - | +288 | +2x |
- "Shiny error when executing the `data` module.\n%s\n%s",+ slices_global$slices_set(snapshot_state) |
181 | -6x - | +289 | +2x |
- data()$message,+ removeModal() |
182 | -6x - | +|||
290 | +
- "Check your inputs or contact app developer if error persists."+ ### End restore procedure. ### |
|||
183 | +291 |
- )+ }) |
||
184 | +292 |
- )+ |
||
185 | +293 |
- )+ # Build snapshot table ---- |
||
186 | +294 |
- }+ # Create UI elements and server logic for the snapshot table. |
||
187 | +295 |
- })+ # Observers must be tracked to avoid duplication and excess reactivity. |
||
188 | +296 |
- })+ # Remaining elements are tracked likewise for consistency and a slight speed margin. |
||
189 | -+ | |||
297 | +80x |
- }+ observers <- reactiveValues() |
||
190 | -+ | |||
298 | +80x |
-
+ handlers <- reactiveValues() |
||
191 | -+ | |||
299 | +80x |
-
+ divs <- reactiveValues() |
||
192 | +300 |
- #' @keywords internal+ |
||
193 | -+ | |||
301 | +80x |
- ui_check_class_teal_data <- function(id) {+ observeEvent(snapshot_history(), { |
||
194 | -77x - | +302 | +70x |
- ns <- NS(id)+ logger::log_debug("srv_snapshot_manager: snapshot history modified, updating snapshot list") |
195 | -77x - | +303 | +70x |
- uiOutput(ns("message"))+ lapply(names(snapshot_history())[-1L], function(s) { |
196 | -+ | |||
304 | +! |
- }+ id_pickme <- sprintf("pickme_%s", make.names(s)) |
||
197 | -+ | |||
305 | +! |
-
+ id_saveme <- sprintf("saveme_%s", make.names(s)) |
||
198 | -+ | |||
306 | +! |
- #' @keywords internal+ id_rowme <- sprintf("rowme_%s", make.names(s)) |
||
199 | +307 |
- srv_check_class_teal_data <- function(id, data) {+ |
||
200 | -172x - | +|||
308 | +
- checkmate::assert_string(id)+ # Observer for restoring snapshot. |
|||
201 | -172x - | +|||
309 | +! |
- moduleServer(id, function(input, output, session) {+ if (!is.element(id_pickme, names(observers))) { |
||
202 | -172x - | +|||
310 | +! |
- output$message <- renderUI({+ observers[[id_pickme]] <- observeEvent(input[[id_pickme]], { |
||
203 | -175x - | +|||
311 | +
- validate(+ ### Begin restore procedure. ### |
|||
204 | -175x - | +|||
312 | +! |
- need(+ snapshot <- snapshot_history()[[s]] |
||
205 | -175x - | +|||
313 | +! |
- inherits(data(), c("teal_data", "error")),+ snapshot_state <- as.teal_slices(snapshot) |
||
206 | -175x - | +|||
314 | +
- "Did not receive `teal_data` object. Cannot proceed further."+ |
|||
207 | -+ | |||
315 | +! |
- )+ slices_global$slices_set(snapshot_state) |
||
208 | -+ | |||
316 | +! |
- )+ removeModal() |
||
209 | +317 |
- })+ ### End restore procedure. ### |
||
210 | +318 |
- })+ }) |
||
211 | +319 |
- }+ } |
||
212 | +320 |
-
+ # Create handler for downloading snapshot. |
||
213 | -+ | |||
321 | +! |
- #' @keywords internal+ if (!is.element(id_saveme, names(handlers))) { |
||
214 | -+ | |||
322 | +! |
- ui_check_shiny_warnings <- function(id) {+ output[[id_saveme]] <- downloadHandler( |
||
215 | -77x - | +|||
323 | +! |
- ns <- NS(id)+ filename = function() { |
||
216 | -77x - | +|||
324 | +! |
- uiOutput(NS(id, "message"))+ sprintf("teal_snapshot_%s_%s.json", s, Sys.Date()) |
||
217 | +325 |
- }+ }, |
||
218 | -+ | |||
326 | +! |
-
+ content = function(file) { |
||
219 | -+ | |||
327 | +! |
- #' @keywords internal+ snapshot <- snapshot_history()[[s]] |
||
220 | -+ | |||
328 | +! |
- srv_check_shiny_warnings <- function(id, data, modules) {+ snapshot_state <- as.teal_slices(snapshot) |
||
221 | -172x - | +|||
329 | +! |
- checkmate::assert_string(id)+ slices_store(tss = snapshot_state, file = file) |
||
222 | -172x - | +|||
330 | +
- moduleServer(id, function(input, output, session) {+ } |
|||
223 | -172x - | +|||
331 | +
- output$message <- renderUI({+ ) |
|||
224 | -175x - | +|||
332 | +! |
- if (inherits(data(), "teal_data")) {+ handlers[[id_saveme]] <- id_saveme |
||
225 | -158x - | +|||
333 | +
- is_modules_ok <- check_modules_datanames(modules = modules, datanames = ls(teal.code::get_env(data())))+ } |
|||
226 | -158x - | +|||
334 | +
- if (!isTRUE(is_modules_ok)) {+ # Create a row for the snapshot table. |
|||
227 | -9x - | +|||
335 | +! |
- tags$div(+ if (!is.element(id_rowme, names(divs))) { |
||
228 | -9x - | +|||
336 | +! |
- class = "teal-output-warning",+ divs[[id_rowme]] <- tags$div( |
||
229 | -9x - | +|||
337 | +! |
- is_modules_ok$html(+ class = "manager_table_row", |
||
230 | -+ | |||
338 | +! |
- # Show modules prefix on message only in teal_data_module tab+ tags$span(tags$h5(s)), |
||
231 | -9x - | +|||
339 | +! |
- grepl(sprintf("data-teal_data_module-%s", id), session$ns(NULL), fixed = TRUE)+ actionLink(inputId = ns(id_pickme), label = icon("far fa-circle-check"), title = "select"), |
||
232 | -+ | |||
340 | +! |
- )+ downloadLink(outputId = ns(id_saveme), label = icon("far fa-save"), title = "save to file") |
||
233 | +341 |
) |
||
234 | +342 |
} |
||
235 | +343 |
- }+ }) |
||
236 | +344 |
}) |
||
237 | +345 |
- })+ |
||
238 | +346 |
- }+ # Create table to display list of snapshots and their actions. |
||
239 | -+ | |||
347 | +80x |
-
+ output$snapshot_list <- renderUI({ |
||
240 | -+ | |||
348 | +70x |
- .trigger_on_success <- function(data) {+ rows <- rev(reactiveValuesToList(divs)) |
||
241 | -172x - | +349 | +70x |
- out <- reactiveVal(NULL)+ if (length(rows) == 0L) { |
242 | -172x - | +350 | +70x |
- observeEvent(data(), {+ tags$div( |
243 | -169x - | +351 | +70x |
- if (inherits(data(), "teal_data")) {+ class = "manager_placeholder", |
244 | -158x - | +352 | +70x |
- if (!identical(data(), out())) {+ "Snapshots will appear here." |
245 | -158x - | +|||
353 | +
- out(data())+ ) |
|||
246 | +354 |
- }+ } else {+ |
+ ||
355 | +! | +
+ rows |
||
247 | +356 |
- }+ } |
||
248 | +357 |
- })+ }) |
||
249 | +358 | |||
250 | -172x - | +359 | +80x |
- out+ snapshot_history |
251 | +360 | ++ |
+ })+ |
+ |
361 |
}@@ -28719,14 +27162,14 @@ teal coverage - 59.73% |
1 |
- #' Create a `tdata` object+ #' App state management. |
||
3 |
- #' @description `r lifecycle::badge("superseded")`+ #' @description |
||
4 |
- #'+ #' `r lifecycle::badge("experimental")` |
||
5 |
- #' Recent changes in `teal` cause modules to fail because modules expect a `tdata` object+ #' |
||
6 |
- #' to be passed to the `data` argument but instead they receive a `teal_data` object,+ #' Capture and restore the global (app) input state. |
||
7 |
- #' which is additionally wrapped in a reactive expression in the server functions.+ #' |
||
8 |
- #' In order to easily adapt such modules without a proper refactor,+ #' @details |
||
9 |
- #' use this function to downgrade the `data` argument.+ #' This module introduces bookmarks into `teal` apps: the `shiny` bookmarking mechanism becomes enabled |
||
10 |
- #'+ #' and server-side bookmarks can be created. |
||
11 |
- #' @name tdata+ #' |
||
12 |
- #' @param ... ignored+ #' The bookmark manager presents a button with the bookmark icon and is placed in the tab-bar. |
||
13 |
- #' @return nothing+ #' When clicked, the button creates a bookmark and opens a modal which displays the bookmark URL. |
||
14 |
- NULL+ #' |
||
15 |
-
+ #' `teal` does not guarantee that all modules (`teal_module` objects) are bookmarkable. |
||
16 |
- #' @rdname tdata+ #' Those that are, have a `teal_bookmarkable` attribute set to `TRUE`. If any modules are not bookmarkable, |
||
17 |
- #' @export+ #' the bookmark manager modal displays a warning and the bookmark button displays a flag. |
||
18 |
- new_tdata <- function(...) {+ #' In order to communicate that a external module is bookmarkable, the module developer |
||
19 | -! | +
- .deprecate_tdata_msg()+ #' should set the `teal_bookmarkable` attribute to `TRUE`. |
|
20 |
- }+ #' |
||
21 |
-
+ #' @section Server logic: |
||
22 |
- #' @rdname tdata+ #' A bookmark is a URL that contains the app address with a `/?_state_id_=<bookmark_dir>` suffix. |
||
23 |
- #' @export+ #' `<bookmark_dir>` is a directory created on the server, where the state of the application is saved. |
||
24 |
- tdata2env <- function(...) {+ #' Accessing the bookmark URL opens a new session of the app that starts in the previously saved state. |
||
25 | -! | +
- .deprecate_tdata_msg()+ #' |
|
26 |
- }+ #' @section Note: |
||
27 |
-
+ #' To enable bookmarking use either: |
||
28 |
- #' @rdname tdata+ #' - `shiny` app by using `shinyApp(..., enableBookmarking = "server")` (not supported in `shinytest2`) |
||
29 |
- #' @export+ #' - set `options(shiny.bookmarkStore = "server")` before running the app |
||
30 |
- get_code_tdata <- function(...) {+ #' |
||
31 | -! | +
- .deprecate_tdata_msg()+ #' |
|
32 |
- }+ #' @inheritParams init |
||
33 |
-
+ #' |
||
34 |
- #' @rdname tdata+ #' @return Invisible `NULL`. |
||
35 |
- #' @export+ #' |
||
36 |
- join_keys.tdata <- function(...) {+ #' @aliases bookmark bookmark_manager bookmark_manager_module |
||
37 | -! | +
- .deprecate_tdata_msg()+ #' |
|
38 |
- }+ #' @name module_bookmark_manager |
||
39 |
-
+ #' @rdname module_bookmark_manager |
||
40 |
- #' @rdname tdata+ #' |
||
41 |
- #' @export+ #' @keywords internal |
||
42 |
- get_metadata <- function(...) {+ #' |
||
43 | -! | +
- .deprecate_tdata_msg()+ NULL |
|
44 |
- }+ |
||
45 |
-
+ #' @rdname module_bookmark_manager |
||
46 |
- #' @rdname tdata+ ui_bookmark_panel <- function(id, modules) { |
||
47 | -+ | ! |
- #' @export+ ns <- NS(id) |
48 |
- as_tdata <- function(...) {+ |
||
49 | ! |
- .deprecate_tdata_msg()+ bookmark_option <- get_bookmarking_option() |
|
50 | -+ | ! |
- }+ is_unbookmarkable <- need_bookmarking(modules) |
51 | -+ | ! |
-
+ shinyOptions(bookmarkStore = bookmark_option) |
53 |
- .deprecate_tdata_msg <- function() {+ # Render bookmark warnings count |
||
54 | ! |
- lifecycle::deprecate_stop(+ if (!all(is_unbookmarkable) && identical(bookmark_option, "server")) { |
|
55 | ! |
- when = "0.16",+ tags$button( |
|
56 | ! |
- what = "tdata()",+ id = ns("do_bookmark"), |
|
57 | ! |
- details = paste(+ class = "btn action-button wunder_bar_button bookmark_manager_button", |
|
58 | ! |
- "tdata has been removed in favour of `teal_data`.\n",+ title = "Add bookmark", |
|
59 | ! |
- "Please follow migration instructions https://github.com/insightsengineering/teal/discussions/987."+ tags$span( |
|
60 | -+ | ! |
- )+ suppressMessages(icon("fas fa-bookmark")), |
61 | -+ | ! |
- )+ if (any(is_unbookmarkable)) { |
62 | -- |
- }- |
-
1 | -- |
- #' Filter panel module in teal- |
- ||
2 | -- |
- #'- |
- ||
3 | -- |
- #' Creates filter panel module from `teal_data` object and returns `teal_data`. It is build in a way- |
- ||
4 | -- |
- #' that filter panel changes and anything what happens before (e.g. [`module_init_data`]) is triggering- |
- ||
5 | -- |
- #' further reactive events only if something has changed and if the module is visible. Thanks to- |
- ||
6 | -- |
- #' this special implementation all modules' data are recalculated only for those modules which are- |
- ||
7 | -- |
- #' currently displayed.- |
- ||
8 | -- |
- #'- |
- ||
9 | -- |
- #' @return A `eventReactive` containing `teal_data` containing filtered objects and filter code.- |
- ||
10 | -- |
- #' `eventReactive` triggers only if all conditions are met:- |
- ||
11 | -- |
- #' - tab is selected (`is_active`)- |
- ||
12 | -- |
- #' - when filters are changed (`get_filter_expr` is different than previous)- |
- ||
13 | -- |
- #'- |
- ||
14 | -- |
- #' @inheritParams module_teal_module- |
- ||
15 | -- |
- #' @param active_datanames (`reactive` returning `character`) this module's data names- |
- ||
16 | -+ | ! |
- #' @name module_filter_data+ tags$span( |
|
17 | -+ | |||
63 | +! |
- #' @keywords internal+ sum(is_unbookmarkable), |
||
18 | -+ | |||
64 | +! |
- NULL+ class = "badge-warning badge-count text-white bg-danger" |
||
19 | +65 |
-
+ ) |
||
20 | +66 |
- #' @rdname module_filter_data+ } |
||
21 | +67 |
- ui_filter_data <- function(id) {+ ) |
||
22 | -! | +|||
68 | +
- ns <- shiny::NS(id)+ ) |
|||
23 | -! | +|||
69 | +
- uiOutput(ns("panel"))+ } |
|||
24 | +70 |
} |
||
25 | +71 | |||
26 | +72 |
- #' @rdname module_filter_data+ #' @rdname module_bookmark_manager |
||
27 | +73 |
- srv_filter_data <- function(id, datasets, active_datanames, data_rv, is_active) {+ srv_bookmark_panel <- function(id, modules) { |
||
28 | -76x - | +74 | +80x |
- assert_reactive(datasets)+ checkmate::assert_character(id) |
29 | -76x - | +75 | +80x |
- moduleServer(id, function(input, output, session) {+ checkmate::assert_class(modules, "teal_modules") |
30 | -76x - | +76 | +80x |
- active_corrected <- reactive(intersect(active_datanames(), datasets()$datanames()))+ moduleServer(id, function(input, output, session) { |
31 | -+ | |||
77 | +80x |
-
+ logger::log_debug("bookmark_manager_srv initializing") |
||
32 | -76x - | +78 | +80x |
- output$panel <- renderUI({+ ns <- session$ns |
33 | -78x - | +79 | +80x |
- req(inherits(datasets(), "FilteredData"))+ bookmark_option <- get_bookmarking_option() |
34 | -78x - | +80 | +80x |
- isolate({+ is_unbookmarkable <- need_bookmarking(modules) |
35 | +81 |
- # render will be triggered only when FilteredData object changes (not when filters change)+ |
||
36 | +82 |
- # technically it means that teal_data_module needs to be refreshed+ # Set up bookmarking callbacks ---- |
||
37 | -78x - | +|||
83 | +
- logger::log_debug("srv_filter_panel rendering filter panel.")+ # Register bookmark exclusions: do_bookmark button to avoid re-bookmarking |
|||
38 | -78x - | +84 | +80x |
- if (length(active_corrected())) {+ setBookmarkExclude(c("do_bookmark")) |
39 | -77x - | +|||
85 | +
- datasets()$srv_active("filters", active_datanames = active_corrected)+ # This bookmark can only be used on the app session. |
|||
40 | -77x - | -
- datasets()$ui_active(session$ns("filters"), active_datanames = active_corrected)- |
- ||
41 | -+ | 86 | +80x |
- }+ app_session <- .subset2(session, "parent") |
42 | -+ | |||
87 | +80x |
- })+ app_session$onBookmarked(function(url) { |
||
43 | -+ | |||
88 | +! |
- })+ logger::log_debug("bookmark_manager_srv@onBookmarked: bookmark button clicked, registering bookmark") |
||
44 | -+ | |||
89 | +! |
-
+ modal_content <- if (bookmark_option != "server") { |
||
45 | -76x - | +|||
90 | +! |
- trigger_data <- .observe_active_filter_changed(datasets, is_active, active_corrected, data_rv)+ msg <- sprintf( |
||
46 | -+ | |||
91 | +! |
-
+ "Bookmarking has been set to \"%s\".\n%s\n%s", |
||
47 | -76x - | +|||
92 | +! |
- eventReactive(trigger_data(), {+ bookmark_option, |
||
48 | -79x - | +|||
93 | +! |
- .make_filtered_teal_data(modules, data = data_rv(), datasets = datasets(), datanames = active_corrected())+ "Only server-side bookmarking is supported.", |
||
49 | -+ | |||
94 | +! |
- })+ "Please contact your app developer." |
||
50 | +95 |
- })+ ) |
||
51 | -+ | |||
96 | +! |
- }+ tags$div( |
||
52 | -+ | |||
97 | +! |
-
+ tags$p(msg, class = "text-warning") |
||
53 | +98 |
- #' @rdname module_filter_data+ ) |
||
54 | +99 |
- .make_filtered_teal_data <- function(modules, data, datasets = NULL, datanames) {+ } else { |
||
55 | -79x - | +|||
100 | +! |
- data <- eval_code(+ tags$div( |
||
56 | -79x - | +|||
101 | +! |
- data,+ tags$span( |
||
57 | -79x - | +|||
102 | +! |
- paste0(+ tags$pre(url) |
||
58 | -79x - | +|||
103 | +
- ".raw_data <- list2env(list(",+ ), |
|||
59 | -79x - | +|||
104 | +! |
- toString(sprintf("%1$s = %1$s", sapply(datanames, as.name))),+ if (any(is_unbookmarkable)) { |
||
60 | -79x - | +|||
105 | +! |
- "))\n",+ bkmb_summary <- rapply2( |
||
61 | -79x - | +|||
106 | +! |
- "lockEnvironment(.raw_data) # @linksto .raw_data" # this is environment and it is shared by qenvs. CAN'T MODIFY!+ modules_bookmarkable(modules), |
||
62 | -+ | |||
107 | +! |
- )+ function(x) { |
||
63 | -+ | |||
108 | +! |
- )+ if (isTRUE(x)) { |
||
64 | -79x - | +|||
109 | +! |
- filtered_code <- .get_filter_expr(datasets = datasets, datanames = datanames)+ "\u2705" # check mark |
||
65 | -79x - | +|||
110 | +! |
- filtered_teal_data <- .append_evaluated_code(data, filtered_code)+ } else if (isFALSE(x)) { |
||
66 | -79x - | +|||
111 | +! |
- filtered_datasets <- sapply(datanames, function(x) datasets$get_data(x, filtered = TRUE), simplify = FALSE)+ "\u274C" # cross mark |
||
67 | -79x - | +|||
112 | +
- filtered_teal_data <- .append_modified_data(filtered_teal_data, filtered_datasets)+ } else { |
|||
68 | -79x - | +|||
113 | +! |
- filtered_teal_data+ "\u2753" # question mark |
||
69 | +114 |
- }+ } |
||
70 | +115 |
-
+ } |
||
71 | +116 |
- #' @rdname module_filter_data+ ) |
||
72 | -+ | |||
117 | +! |
- .observe_active_filter_changed <- function(datasets, is_active, active_datanames, data_rv) {+ tags$div( |
||
73 | -76x - | +|||
118 | +! |
- previous_signature <- reactiveVal(NULL)+ tags$p( |
||
74 | -76x - | +|||
119 | +! |
- filter_changed <- reactive({+ icon("fas fa-exclamation-triangle"), |
||
75 | -175x - | +|||
120 | +! |
- req(inherits(datasets(), "FilteredData"))+ "Some modules will not be restored when using this bookmark.", |
||
76 | -175x - | +|||
121 | +! |
- new_signature <- c(+ tags$br(), |
||
77 | -175x - | +|||
122 | +! |
- teal.code::get_code(data_rv()),+ "Check the list below to see which modules are not bookmarkable.", |
||
78 | -175x - | +|||
123 | +! |
- .get_filter_expr(datasets = datasets(), datanames = active_datanames())+ class = "text-warning" |
||
79 | +124 |
- )- |
- ||
80 | -175x - | -
- if (!identical(previous_signature(), new_signature)) {- |
- ||
81 | -84x - | -
- previous_signature(new_signature)+ ), |
||
82 | -84x - | +|||
125 | +! |
- TRUE+ tags$pre(yaml::as.yaml(bkmb_summary)) |
||
83 | +126 |
- } else {+ ) |
||
84 | -91x - | +|||
127 | +
- FALSE+ } |
|||
85 | +128 |
- }+ ) |
||
86 | +129 |
- })+ } |
||
87 | +130 | |||
88 | -76x - | -
- trigger_data <- reactiveVal(NULL)- |
- ||
89 | -76x - | -
- observe({- |
- ||
90 | -188x - | +|||
131 | +! |
- if (isTRUE(is_active() && filter_changed())) {+ showModal( |
||
91 | -84x - | +|||
132 | +! |
- isolate({+ modalDialog( |
||
92 | -84x - | +|||
133 | +! |
- if (is.null(trigger_data())) {+ id = ns("bookmark_modal"), |
||
93 | -76x - | +|||
134 | +! |
- trigger_data(0)+ title = "Bookmarked teal app url", |
||
94 | -+ | |||
135 | +! |
- } else {+ modal_content, |
||
95 | -8x - | +|||
136 | +! |
- trigger_data(trigger_data() + 1)+ easyClose = TRUE |
||
96 | +137 |
- }+ ) |
||
97 | +138 |
- })+ ) |
||
98 | +139 |
- }+ }) |
||
99 | +140 |
- })+ |
||
100 | +141 |
-
+ # manually trigger bookmarking because of the problems reported on windows with bookmarkButton in teal |
||
101 | -76x - | -
- trigger_data- |
- ||
102 | -+ | 142 | +80x |
- }+ observeEvent(input$do_bookmark, { |
103 | -+ | |||
143 | +! |
-
+ logger::log_debug("bookmark_manager_srv@1 do_bookmark module clicked.") |
||
104 | -+ | |||
144 | +! |
- #' @rdname module_filter_data+ session$doBookmark() |
||
105 | +145 |
- .get_filter_expr <- function(datasets, datanames) {- |
- ||
106 | -254x - | -
- if (length(datanames)) {- |
- ||
107 | -251x - | -
- teal.slice::get_filter_expr(datasets = datasets, datanames = datanames)+ }) |
||
108 | +146 |
- } else {+ |
||
109 | -3x - | +147 | +80x |
- NULL+ invisible(NULL) |
110 | +148 |
- }+ }) |
||
111 | +149 |
} |
1 | +150 |
- #' Filter settings for `teal` applications+ |
||
2 | +151 |
- #'+ |
||
3 | +152 |
- #' Specify initial filter states and filtering settings for a `teal` app.+ #' @rdname module_bookmark_manager |
||
4 | +153 |
- #'+ get_bookmarking_option <- function() { |
||
5 | -+ | |||
154 | +80x |
- #' Produces a `teal_slices` object.+ bookmark_option <- getShinyOption("bookmarkStore") |
||
6 | -+ | |||
155 | +80x |
- #' The `teal_slice` components will specify filter states that will be active when the app starts.+ if (is.null(bookmark_option) && identical(getOption("shiny.bookmarkStore"), "server")) { |
||
7 | -+ | |||
156 | +! |
- #' Attributes (created with the named arguments) will configure the way the app applies filters.+ bookmark_option <- getOption("shiny.bookmarkStore") |
||
8 | +157 |
- #' See argument descriptions for details.+ } |
||
9 | -+ | |||
158 | +80x |
- #'+ bookmark_option |
||
10 | +159 |
- #' @inheritParams teal.slice::teal_slices+ } |
||
11 | +160 |
- #'+ |
||
12 | +161 |
- #' @param module_specific (`logical(1)`) optional,+ #' @rdname module_bookmark_manager |
||
13 | +162 |
- #' - `FALSE` (default) when one filter panel applied to all modules.+ need_bookmarking <- function(modules) { |
||
14 | -+ | |||
163 | +80x |
- #' All filters will be shared by all modules.+ unlist(rapply2( |
||
15 | -+ | |||
164 | +80x |
- #' - `TRUE` when filter panel module-specific.+ modules_bookmarkable(modules), |
||
16 | -+ | |||
165 | +80x |
- #' Modules can have different set of filters specified - see `mapping` argument.+ Negate(isTRUE) |
||
17 | +166 |
- #' @param mapping `r lifecycle::badge("experimental")`+ )) |
||
18 | +167 |
- #' _This is a new feature. Do kindly share your opinions on+ } |
||
19 | +168 |
- #' [`teal`'s GitHub repository](https://github.com/insightsengineering/teal/)._+ |
||
20 | +169 |
- #'+ |
||
21 | +170 |
- #' (named `list`) specifies which filters will be active in which modules on app start.+ # utilities ---- |
||
22 | +171 |
- #' Elements should contain character vector of `teal_slice` `id`s (see [`teal.slice::teal_slice`]).+ |
||
23 | +172 |
- #' Names of the list should correspond to `teal_module` `label` set in [module()] function.+ #' Restore value from bookmark. |
||
24 | +173 |
- #' - `id`s listed under `"global_filters` will be active in all modules.+ #' |
||
25 | +174 |
- #' - If missing, all filters will be applied to all modules.+ #' Get value from bookmark or return default. |
||
26 | +175 |
- #' - If empty list, all filters will be available to all modules but will start inactive.+ #' |
||
27 | +176 |
- #' - If `module_specific` is `FALSE`, only `global_filters` will be active on start.+ #' Bookmarks can store not only inputs but also arbitrary values. |
||
28 | +177 |
- #' @param app_id (`character(1)`)+ #' These values are stored by `onBookmark` callbacks and restored by `onBookmarked` callbacks, |
||
29 | +178 |
- #' For internal use only, do not set manually.+ #' and they are placed in the `values` environment in the `session$restoreContext` field. |
||
30 | +179 |
- #' Added by `init` so that a `teal_slices` can be matched to the app in which it was used.+ #' Using `teal_data_module` makes it impossible to run the callbacks |
||
31 | +180 |
- #' Used for verifying snapshots uploaded from file. See `snapshot`.+ #' because the app becomes ready before modules execute and callbacks are registered. |
||
32 | +181 |
- #'+ #' In those cases the stored values can still be recovered from the `session` object directly. |
||
33 | +182 |
- #' @param x (`list`) of lists to convert to `teal_slices`+ #' |
||
34 | +183 |
- #'+ #' Note that variable names in the `values` environment are prefixed with module name space names, |
||
35 | +184 |
- #' @return+ #' therefore, when using this function in modules, `value` must be run through the name space function. |
||
36 | +185 |
- #' A `teal_slices` object.+ #' |
||
37 | +186 |
- #'+ #' @param value (`character(1)`) name of value to restore |
||
38 | +187 |
- #' @seealso [`teal.slice::teal_slices`], [`teal.slice::teal_slice`], [slices_store()]+ #' @param default fallback value |
||
39 | +188 |
#' |
||
40 | +189 |
- #' @examples+ #' @return |
||
41 | +190 |
- #' filter <- teal_slices(+ #' In an application restored from a server-side bookmark, |
||
42 | +191 |
- #' teal_slice(dataname = "iris", varname = "Species", id = "species"),+ #' the variable specified by `value` from the `values` environment. |
||
43 | +192 |
- #' teal_slice(dataname = "iris", varname = "Sepal.Length", id = "sepal_length"),+ #' Otherwise `default`. |
||
44 | +193 |
- #' teal_slice(+ #' |
||
45 | +194 |
- #' dataname = "iris", id = "long_petals", title = "Long petals", expr = "Petal.Length > 5"+ #' @keywords internal |
||
46 | +195 |
- #' ),+ #' |
||
47 | +196 |
- #' teal_slice(dataname = "mtcars", varname = "mpg", id = "mtcars_mpg"),+ restoreValue <- function(value, default) { # nolint: object_name. |
||
48 | -+ | |||
197 | +160x |
- #' mapping = list(+ checkmate::assert_character("value") |
||
49 | -+ | |||
198 | +160x |
- #' module1 = c("species", "sepal_length"),+ session_default <- shiny::getDefaultReactiveDomain() |
||
50 | -+ | |||
199 | +160x |
- #' module2 = c("mtcars_mpg"),+ session_parent <- .subset2(session_default, "parent") |
||
51 | -+ | |||
200 | +160x |
- #' global_filters = "long_petals"+ session <- if (is.null(session_parent)) session_default else session_parent |
||
52 | +201 |
- #' )+ |
||
53 | -+ | |||
202 | +160x |
- #' )+ if (isTRUE(session$restoreContext$active) && exists(value, session$restoreContext$values, inherits = FALSE)) { |
||
54 | -+ | |||
203 | +! |
- #'+ session$restoreContext$values[[value]] |
||
55 | +204 |
- #' app <- init(+ } else { |
||
56 | -+ | |||
205 | +160x |
- #' data = teal_data(iris = iris, mtcars = mtcars),+ default |
||
57 | +206 |
- #' modules = list(+ } |
||
58 | +207 |
- #' module("module1"),+ } |
||
59 | +208 |
- #' module("module2")+ |
||
60 | +209 |
- #' ),+ #' Compare bookmarks. |
||
61 | +210 |
- #' filter = filter+ #' |
||
62 | +211 |
- #' )+ #' Test if two bookmarks store identical state. |
||
63 | +212 |
#' |
||
64 | +213 |
- #' if (interactive()) {+ #' `input` environments are compared one variable at a time and if not identical, |
||
65 | +214 |
- #' shinyApp(app$ui, app$server)+ #' values in both bookmarks are reported. States of `datatable`s are stripped |
||
66 | +215 |
- #' }+ #' of the `time` element before comparing because the time stamp is always different. |
||
67 | +216 |
- #'+ #' The contents themselves are not printed as they are large and the contents are not informative. |
||
68 | +217 |
- #' @export+ #' Elements present in one bookmark and absent in the other are also reported. |
||
69 | +218 |
- teal_slices <- function(...,+ #' Differences are printed as messages. |
||
70 | +219 |
- exclude_varnames = NULL,+ #' |
||
71 | +220 |
- include_varnames = NULL,+ #' `values` environments are compared with `all.equal`. |
||
72 | +221 |
- count_type = NULL,+ #' |
||
73 | +222 |
- allow_add = TRUE,+ #' @section How to use: |
||
74 | +223 |
- module_specific = FALSE,+ #' Open an application, change relevant inputs (typically, all of them), and create a bookmark. |
||
75 | +224 |
- mapping,+ #' Then open that bookmark and immediately create a bookmark of that. |
||
76 | +225 |
- app_id = NULL) {+ #' If restoring bookmarks occurred properly, the two bookmarks should store the same state. |
||
77 | -154x - | +|||
226 | +
- shiny::isolate({+ #' |
|||
78 | -154x - | +|||
227 | +
- checkmate::assert_flag(allow_add)+ #' |
|||
79 | -154x - | +|||
228 | +
- checkmate::assert_flag(module_specific)+ #' @param book1,book2 bookmark directories stored in `shiny_bookmarks/`; |
|||
80 | -50x - | +|||
229 | +
- if (!missing(mapping)) checkmate::assert_list(mapping, types = c("character", "NULL"), names = "named")+ #' default to the two most recently modified directories |
|||
81 | -151x - | +|||
230 | +
- checkmate::assert_string(app_id, null.ok = TRUE)+ #' |
|||
82 | +231 |
-
+ #' @return |
||
83 | -151x - | +|||
232 | +
- slices <- list(...)+ #' Invisible `NULL` if bookmarks are identical or if there are no bookmarks to test. |
|||
84 | -151x - | +|||
233 | +
- all_slice_id <- vapply(slices, `[[`, character(1L), "id")+ #' `FALSE` if inconsistencies are detected. |
|||
85 | +234 |
-
+ #' |
||
86 | -151x - | +|||
235 | +
- if (missing(mapping)) {+ #' @keywords internal |
|||
87 | -104x - | +|||
236 | +
- mapping <- if (length(all_slice_id)) {+ #' |
|||
88 | -26x - | +|||
237 | +
- list(global_filters = all_slice_id)+ bookmarks_identical <- function(book1, book2) { |
|||
89 | -+ | |||
238 | +! |
- } else {+ if (!dir.exists("shiny_bookmarks")) { |
||
90 | -78x - | +|||
239 | +! |
- list()+ message("no bookmark directory") |
||
91 | -+ | |||
240 | +! |
- }+ return(invisible(NULL)) |
||
92 | +241 |
- }+ } |
||
93 | +242 | |||
94 | -151x - | -
- if (!module_specific) {- |
- ||
95 | -132x - | -
- mapping[setdiff(names(mapping), "global_filters")] <- NULL- |
- ||
96 | -+ | |||
243 | +! |
- }+ ans <- TRUE |
||
97 | +244 | |||
98 | -151x - | +|||
245 | +! |
- failed_slice_id <- setdiff(unlist(mapping), all_slice_id)+ if (missing(book1) && missing(book2)) { |
||
99 | -151x - | +|||
246 | +! |
- if (length(failed_slice_id)) {+ dirs <- list.dirs("shiny_bookmarks", recursive = FALSE) |
||
100 | -1x - | +|||
247 | +! |
- stop(sprintf(+ bookmarks_sorted <- basename(rev(dirs[order(file.mtime(dirs))])) |
||
101 | -1x - | +|||
248 | +! |
- "Filters in mapping don't match any available filter.\n %s not in %s",+ if (length(bookmarks_sorted) < 2L) { |
||
102 | -1x - | +|||
249 | +! |
- toString(failed_slice_id),+ message("no bookmarks to compare") |
||
103 | -1x - | +|||
250 | +! |
- toString(all_slice_id)+ return(invisible(NULL)) |
||
104 | +251 |
- ))+ } |
||
105 | -+ | |||
252 | +! |
- }+ book1 <- bookmarks_sorted[2L]+ |
+ ||
253 | +! | +
+ book2 <- bookmarks_sorted[1L] |
||
106 | +254 |
-
+ } else { |
||
107 | -150x - | +|||
255 | +! |
- tss <- teal.slice::teal_slices(+ if (!dir.exists(file.path("shiny_bookmarks", book1))) stop(book1, " not found") |
||
108 | -+ | |||
256 | +! |
- ...,+ if (!dir.exists(file.path("shiny_bookmarks", book2))) stop(book2, " not found") |
||
109 | -150x - | +|||
257 | +
- exclude_varnames = exclude_varnames,+ } |
|||
110 | -150x - | +|||
258 | +
- include_varnames = include_varnames,+ |
|||
111 | -150x - | +|||
259 | +! |
- count_type = count_type,+ book1_input <- readRDS(file.path("shiny_bookmarks", book1, "input.rds")) |
||
112 | -150x - | +|||
260 | +! |
- allow_add = allow_add+ book2_input <- readRDS(file.path("shiny_bookmarks", book2, "input.rds")) |
||
113 | +261 |
- )+ |
||
114 | -150x - | +|||
262 | +! |
- attr(tss, "mapping") <- mapping+ elements_common <- intersect(names(book1_input), names(book2_input)) |
||
115 | -150x - | +|||
263 | +! |
- attr(tss, "module_specific") <- module_specific+ dt_states <- grepl("_state$", elements_common) |
||
116 | -150x - | +|||
264 | +! |
- attr(tss, "app_id") <- app_id+ if (any(dt_states)) { |
||
117 | -150x - | +|||
265 | +! |
- class(tss) <- c("modules_teal_slices", class(tss))+ for (el in elements_common[dt_states]) { |
||
118 | -150x - | +|||
266 | +! |
- tss+ book1_input[[el]][["time"]] <- NULL |
||
119 | -+ | |||
267 | +! |
- })+ book2_input[[el]][["time"]] <- NULL |
||
120 | +268 |
- }+ } |
||
121 | +269 |
-
+ } |
||
122 | +270 | |||
123 | -+ | |||
271 | +! |
- #' @rdname teal_slices+ identicals <- mapply(identical, book1_input[elements_common], book2_input[elements_common]) |
||
124 | -+ | |||
272 | +! |
- #' @export+ non_identicals <- names(identicals[!identicals]) |
||
125 | -+ | |||
273 | +! |
- #' @keywords internal+ compares <- sprintf("$ %s:\t%s --- %s", non_identicals, book1_input[non_identicals], book2_input[non_identicals]) |
||
126 | -+ | |||
274 | +! |
- #'+ if (length(compares) != 0L) { |
||
127 | -+ | |||
275 | +! |
- as.teal_slices <- function(x) { # nolint: object_name.+ message("common elements not identical: \n", paste(compares, collapse = "\n")) |
||
128 | -12x - | +|||
276 | +! |
- checkmate::assert_list(x)+ ans <- FALSE |
||
129 | -12x - | +|||
277 | +
- lapply(x, checkmate::assert_list, names = "named", .var.name = "list element")+ } |
|||
130 | +278 | |||
131 | -12x - | +|||
279 | +! |
- attrs <- attributes(unclass(x))+ elements_boook1 <- setdiff(names(book1_input), names(book2_input)) |
||
132 | -12x - | +|||
280 | +! |
- ans <- lapply(x, function(x) if (is.teal_slice(x)) x else as.teal_slice(x))+ if (length(elements_boook1) != 0L) { |
||
133 | -12x - | +|||
281 | +! |
- do.call(teal_slices, c(ans, attrs))+ dt_states <- grepl("_state$", elements_boook1) |
||
134 | -+ | |||
282 | +! |
- }+ if (any(dt_states)) { |
||
135 | -+ | |||
283 | +! |
-
+ for (el in elements_boook1[dt_states]) { |
||
136 | -+ | |||
284 | +! |
-
+ if (is.list(book1_input[[el]])) book1_input[[el]] <- "--- data table state ---" |
||
137 | +285 |
- #' @rdname teal_slices+ } |
||
138 | +286 |
- #' @export+ } |
||
139 | -+ | |||
287 | +! |
- #' @keywords internal+ excess1 <- sprintf("$ %s:\t%s", elements_boook1, book1_input[elements_boook1])+ |
+ ||
288 | +! | +
+ message("elements only in book1: \n", paste(excess1, collapse = "\n"))+ |
+ ||
289 | +! | +
+ ans <- FALSE |
||
140 | +290 |
- #'+ } |
||
141 | +291 |
- c.teal_slices <- function(...) {+ |
||
142 | -6x - | +|||
292 | +! |
- x <- list(...)+ elements_boook2 <- setdiff(names(book2_input), names(book1_input)) |
||
143 | -6x - | +|||
293 | +! |
- checkmate::assert_true(all(vapply(x, is.teal_slices, logical(1L))), .var.name = "all arguments are teal_slices")+ if (length(elements_boook2) != 0L) { |
||
144 | -+ | |||
294 | +! |
-
+ dt_states <- grepl("_state$", elements_boook1) |
||
145 | -6x - | +|||
295 | +! |
- all_attributes <- lapply(x, attributes)+ if (any(dt_states)) { |
||
146 | -6x - | +|||
296 | +! |
- all_attributes <- coalesce_r(all_attributes)+ for (el in elements_boook1[dt_states]) { |
||
147 | -6x - | +|||
297 | +! |
- all_attributes <- all_attributes[names(all_attributes) != "class"]+ if (is.list(book2_input[[el]])) book2_input[[el]] <- "--- data table state ---" |
||
148 | +298 |
-
+ } |
||
149 | -6x - | +|||
299 | +
- do.call(+ } |
|||
150 | -6x - | +|||
300 | +! |
- teal_slices,+ excess2 <- sprintf("$ %s:\t%s", elements_boook2, book2_input[elements_boook2]) |
||
151 | -6x - | +|||
301 | +! |
- c(+ message("elements only in book2: \n", paste(excess2, collapse = "\n")) |
||
152 | -6x - | +|||
302 | +! |
- unique(unlist(x, recursive = FALSE)),+ ans <- FALSE |
||
153 | -6x - | +|||
303 | +
- all_attributes+ } |
|||
154 | +304 |
- )+ |
||
155 | -+ | |||
305 | +! |
- )+ book1_values <- readRDS(file.path("shiny_bookmarks", book1, "values.rds")) |
||
156 | -+ | |||
306 | +! |
- }+ book2_values <- readRDS(file.path("shiny_bookmarks", book2, "values.rds")) |
||
157 | +307 | |||
158 | -+ | |||
308 | +! |
-
+ if (!isTRUE(all.equal(book1_values, book2_values))) { |
||
159 | -+ | |||
309 | +! |
- #' Deep copy `teal_slices`+ message("different values detected") |
||
160 | -+ | |||
310 | +! |
- #'+ message("choices for numeric filters MAY be different, see RangeFilterState$set_choices") |
||
161 | -+ | |||
311 | +! |
- #' it's important to create a new copy of `teal_slices` when+ ans <- FALSE |
||
162 | +312 |
- #' starting a new `shiny` session. Otherwise, object will be shared+ } |
||
163 | +313 |
- #' by multiple users as it is created in global environment before+ |
||
164 | -+ | |||
314 | +! |
- #' `shiny` session starts.+ if (ans) message("perfect!")+ |
+ ||
315 | +! | +
+ invisible(NULL) |
||
165 | +316 |
- #' @param filter (`teal_slices`)+ } |
||
166 | +317 |
- #' @return `teal_slices`+ |
||
167 | +318 |
- #' @keywords internal+ |
||
168 | +319 |
- deep_copy_filter <- function(filter) {+ # Replacement for [base::rapply] which doesn't handle NULL values - skips the evaluation |
||
169 | -1x - | +|||
320 | +
- checkmate::assert_class(filter, "teal_slices")+ # of the function and returns NULL for given element. |
|||
170 | -1x - | +|||
321 | +
- shiny::isolate({+ rapply2 <- function(x, f) { |
|||
171 | -1x - | +322 | +185x |
- filter_copy <- lapply(filter, function(slice) {+ if (inherits(x, "list")) { |
172 | -2x - | +323 | +80x |
- teal.slice::as.teal_slice(as.list(slice))+ lapply(x, rapply2, f = f) |
173 | +324 |
- })- |
- ||
174 | -1x - | -
- attributes(filter_copy) <- attributes(filter)+ } else { |
||
175 | -1x - | +325 | +105x |
- filter_copy+ f(x) |
176 | +326 |
- })+ } |
||
177 | +327 |
}@@ -31283,49 +29457,49 @@ teal coverage - 59.73% |
1 |
- # This is the main function from teal to be used by the end-users. Although it delegates+ #' Execute and validate `teal_data_module` |
|||
2 |
- # directly to `module_teal_with_splash.R`, we keep it in a separate file because its documentation is quite large+ #' |
|||
3 |
- # and it is very end-user oriented. It may also perform more argument checking with more informative+ #' This is a low level module to handle `teal_data_module` execution and validation. |
|||
4 |
- # error messages.+ #' [teal_transform_module()] inherits from [teal_data_module()] so it is handled by this module too. |
|||
5 |
-
+ #' [srv_teal()] accepts various `data` objects and eventually they are all transformed to `reactive` |
|||
6 |
- #' Create the server and UI function for the `shiny` app+ #' [teal_data()] which is a standard data class in whole `teal` framework. |
|||
8 |
- #' @description `r lifecycle::badge("stable")`+ #' @section data validation: |
|||
10 |
- #' End-users: This is the most important function for you to start a+ #' Executed [teal_data_module()] is validated and output is validated for consistency. |
|||
11 |
- #' `teal` app that is composed of `teal` modules.+ #' Output `data` is invalid if: |
|||
12 |
- #'+ #' 1. [teal_data_module()] is invalid if server doesn't return `reactive`. **Immediately crashes an app!** |
|||
13 |
- #' @param data (`teal_data` or `teal_data_module`)+ #' 2. `reactive` throws a `shiny.error` - happens when module creating [teal_data()] fails. |
|||
14 |
- #' For constructing the data object, refer to [teal_data()] and [teal_data_module()].+ #' 3. `reactive` returns `qenv.error` - happens when [teal_data()] evaluates a failing code. |
|||
15 |
- #' If `datanames` are not set for the `teal_data` object, defaults from the `teal_data` environment will be used.+ #' 4. `reactive` object doesn't return [teal_data()]. |
|||
16 |
- #' @param modules (`list` or `teal_modules` or `teal_module`)+ #' 5. [teal_data()] object lacks any `datanames` specified in the `modules` argument. |
|||
17 |
- #' Nested list of `teal_modules` or `teal_module` objects or a single+ #' |
|||
18 |
- #' `teal_modules` or `teal_module` object. These are the specific output modules which+ #' `teal` (observers in `srv_teal`) always waits to render an app until `reactive` `teal_data` is |
|||
19 |
- #' will be displayed in the `teal` application. See [modules()] and [module()] for+ #' returned. If error 2-4 occurs, relevant error message is displayed to the app user. Once the issue is |
|||
20 |
- #' more details.+ #' resolved, the app will continue to run. `teal` guarantees that errors in data don't crash the app |
|||
21 |
- #' @param filter (`teal_slices`) Optionally,+ #' (except error 1). |
|||
22 |
- #' specifies the initial filter using [teal_slices()].+ #' |
|||
23 |
- #' @param title (`shiny.tag` or `character(1)`) Optionally,+ #' @param id (`character(1)`) Module id |
|||
24 |
- #' the browser window title. Defaults to a title "teal app" with the icon of NEST.+ #' @param data (`reactive teal_data`) |
|||
25 |
- #' Can be created using the `build_app_title()` or+ #' @param data_module (`teal_data_module`) |
|||
26 |
- #' by passing a valid `shiny.tag` which is a head tag with title and link tag.+ #' @param modules (`teal_modules` or `teal_module`) For `datanames` validation purpose |
|||
27 |
- #' @param header (`shiny.tag` or `character(1)`) Optionally,+ #' @param validate_shiny_silent_error (`logical`) If `TRUE`, then `shiny.silent.error` is validated and |
|||
28 |
- #' the header of the app.+ #' @param is_transformer_failed (`reactiveValues`) contains `logical` flags named after each transformer. |
|||
29 |
- #' @param footer (`shiny.tag` or `character(1)`) Optionally,+ #' Help to determine if any previous transformer failed, so that following transformers can be disabled |
|||
30 |
- #' the footer of the app.+ #' and display a generic failure message. |
|||
31 |
- #' @param id (`character`) Optionally,+ #' |
|||
32 |
- #' a string specifying the `shiny` module id in cases it is used as a `shiny` module+ #' @return `reactive` `teal_data` |
|||
33 |
- #' rather than a standalone `shiny` app. This is a legacy feature.+ #' |
|||
34 |
- #' @param landing_popup (`teal_module_landing`) Optionally,+ #' @rdname module_teal_data |
|||
35 |
- #' a `landing_popup_module` to show up as soon as the teal app is initialized.+ #' @name module_teal_data |
|||
36 |
- #'+ #' @keywords internal |
|||
37 |
- #' @return Named list containing server and UI functions.+ NULL |
|||
38 |
- #'+ |
|||
39 |
- #' @export+ #' @rdname module_teal_data |
|||
40 |
- #'+ ui_teal_data <- function(id, data_module = function(id) NULL) { |
|||
41 | -+ | ! |
- #' @include modules.R+ checkmate::assert_string(id) |
|
42 | -+ | ! |
- #'+ checkmate::assert_function(data_module, args = "id") |
|
43 | -+ | ! |
- #' @examples+ ns <- NS(id) |
|
44 |
- #' app <- init(+ |
|||
45 | -+ | ! |
- #' data = within(+ shiny::tagList( |
|
46 | -+ | ! |
- #' teal_data(),+ tags$div(id = ns("wrapper"), data_module(id = ns("data"))), |
|
47 | -+ | ! |
- #' {+ ui_validate_reactive_teal_data(ns("validate")) |
|
48 |
- #' new_iris <- transform(iris, id = seq_len(nrow(iris)))+ ) |
|||
49 |
- #' new_mtcars <- transform(mtcars, id = seq_len(nrow(mtcars)))+ } |
|||
50 |
- #' }+ |
|||
51 |
- #' ),+ #' @rdname module_teal_data |
|||
52 |
- #' modules = modules(+ srv_teal_data <- function(id, |
|||
53 |
- #' module(+ data_module = function(id) NULL, |
|||
54 |
- #' label = "data source",+ modules = NULL, |
|||
55 |
- #' server = function(input, output, session, data) {},+ validate_shiny_silent_error = TRUE, |
|||
56 |
- #' ui = function(id, ...) tags$div(p("information about data source")),+ is_transformer_failed = reactiveValues()) { |
|||
57 | -+ | 19x |
- #' datanames = "all"+ checkmate::assert_string(id) |
|
58 | -+ | 19x |
- #' ),+ checkmate::assert_function(data_module, args = "id") |
|
59 | -+ | 19x |
- #' example_module(label = "example teal module"),+ checkmate::assert_multi_class(modules, c("teal_modules", "teal_module"), null.ok = TRUE) |
|
60 | -+ | 19x |
- #' module(+ checkmate::assert_class(is_transformer_failed, "reactivevalues") |
|
61 |
- #' "Iris Sepal.Length histogram",+ |
|||
62 | -+ | 19x |
- #' server = function(input, output, session, data) {+ moduleServer(id, function(input, output, session) { |
|
63 | -+ | 19x |
- #' output$hist <- renderPlot(+ logger::log_debug("srv_teal_data initializing.") |
|
64 | -+ | 19x |
- #' hist(data()[["new_iris"]]$Sepal.Length)+ is_transformer_failed[[id]] <- FALSE |
|
65 | -+ | 19x |
- #' )+ data_out <- data_module(id = "data") |
|
66 | -+ | 19x |
- #' },+ data_handled <- reactive(tryCatch(data_out(), error = function(e) e)) |
|
67 | -+ | 19x |
- #' ui = function(id, ...) {+ observeEvent(data_handled(), { |
|
68 | -+ | 21x |
- #' ns <- NS(id)+ if (!inherits(data_handled(), "teal_data")) { |
|
69 | -+ | 6x |
- #' plotOutput(ns("hist"))+ is_transformer_failed[[id]] <- TRUE |
|
70 |
- #' },+ } else { |
|||
71 | -+ | 15x |
- #' datanames = "new_iris"+ is_transformer_failed[[id]] <- FALSE |
|
72 |
- #' )+ } |
|||
73 |
- #' ),+ }) |
|||
74 |
- #' filter = teal_slices(+ |
|||
75 | -+ | 19x |
- #' teal_slice(dataname = "new_iris", varname = "Species"),+ is_previous_failed <- reactive({ |
|
76 | -+ | 19x |
- #' teal_slice(dataname = "new_iris", varname = "Sepal.Length"),+ idx_this <- which(names(is_transformer_failed) == id) |
|
77 | -+ | 19x |
- #' teal_slice(dataname = "new_mtcars", varname = "cyl"),+ is_transformer_failed_list <- reactiveValuesToList(is_transformer_failed) |
|
78 | -+ | 19x |
- #' exclude_varnames = list(new_iris = c("Sepal.Width", "Petal.Width")),+ idx_failures <- which(unlist(is_transformer_failed_list)) |
|
79 | -+ | 19x |
- #' module_specific = TRUE,+ any(idx_failures < idx_this) |
|
80 |
- #' mapping = list(+ }) |
|||
81 |
- #' `example teal module` = "new_iris Species",+ |
|||
82 | -+ | 19x |
- #' `Iris Sepal.Length histogram` = "new_iris Species",+ observeEvent(is_previous_failed(), { |
|
83 | -+ | 19x |
- #' global_filters = "new_mtcars cyl"+ if (is_previous_failed()) { |
|
84 | -+ | ! |
- #' )+ shinyjs::disable("wrapper") |
|
85 |
- #' ),+ } else { |
|||
86 | -+ | 19x |
- #' title = "App title",+ shinyjs::enable("wrapper") |
|
87 |
- #' header = tags$h1("Sample App"),+ } |
|||
88 |
- #' footer = tags$p("Sample footer")+ }) |
|||
89 |
- #' )+ |
|||
90 | -+ | 19x |
- #' if (interactive()) {+ srv_validate_reactive_teal_data( |
|
91 | -+ | 19x |
- #' shinyApp(app$ui, app$server)+ "validate", |
|
92 | -+ | 19x |
- #' }+ data = data_handled, |
|
93 | -+ | 19x |
- #'+ modules = modules, |
|
94 | -+ | 19x |
- init <- function(data,+ validate_shiny_silent_error = validate_shiny_silent_error, |
|
95 | -+ | 19x |
- modules,+ hide_validation_error = is_previous_failed |
|
96 |
- filter = teal_slices(),+ ) |
|||
97 |
- title = build_app_title(),+ }) |
|||
98 |
- header = tags$p(),+ } |
|||
99 |
- footer = tags$p(),+ |
|||
100 |
- id = character(0),+ #' @rdname module_teal_data |
|||
101 |
- landing_popup = NULL) {+ ui_validate_reactive_teal_data <- function(id) { |
|||
102 | -11x - | +80x |
- logger::log_debug("init initializing teal app with: data ('{ class(data) }').")+ ns <- NS(id) |
|
103 | -+ | 80x |
-
+ tagList( |
|
104 | -+ | 80x |
- # argument checking (independent)+ div( |
|
105 | -+ | 80x |
- ## `data`+ id = ns("validate_messages"), |
|
106 | -11x - | +80x |
- if (inherits(data, "TealData")) {+ class = "teal_validated", |
|
107 | -! | +80x |
- lifecycle::deprecate_stop(+ ui_validate_error(ns("silent_error")), |
|
108 | -! | +80x |
- when = "0.15.0",+ ui_check_class_teal_data(ns("class_teal_data")), |
|
109 | -! | +80x |
- what = "init(data)",+ ui_check_shiny_warnings(ns("shiny_warnings")) |
|
110 | -! | +
- paste(+ ), |
||
111 | -! | +80x |
- "TealData is no longer supported. Use teal_data() instead.",+ div( |
|
112 | -! | +80x |
- "Please follow migration instructions https://github.com/insightsengineering/teal/discussions/988."+ class = "teal_validated", |
|
113 | -+ | 80x |
- )+ uiOutput(ns("previous_failed")) |
|
115 |
- }+ ) |
|||
116 | -11x - | +
- checkmate::assert_multi_class(data, c("teal_data", "teal_data_module"))+ } |
||
117 | -11x - | +
- checkmate::assert_class(landing_popup, "teal_module_landing", null.ok = TRUE)+ |
||
118 |
-
+ #' @rdname module_teal_data |
|||
119 |
- ## `modules`+ srv_validate_reactive_teal_data <- function(id, # nolint: object_length |
|||
120 | -11x - | +
- checkmate::assert(+ data, |
||
121 | -11x - | +
- .var.name = "modules",+ modules = NULL, |
||
122 | -11x - | +
- checkmate::check_multi_class(modules, c("teal_modules", "teal_module")),+ validate_shiny_silent_error = FALSE, |
||
123 | -11x - | +
- checkmate::check_list(modules, min.len = 1, any.missing = FALSE, types = c("teal_module", "teal_modules"))+ hide_validation_error = reactive(FALSE)) { |
||
124 | -+ | 178x |
- )+ checkmate::assert_string(id) |
|
125 | -11x - | +178x |
- if (inherits(modules, "teal_module")) {+ checkmate::assert_multi_class(modules, c("teal_modules", "teal_module"), null.ok = TRUE) |
|
126 | -1x - | +178x |
- modules <- list(modules)+ checkmate::assert_flag(validate_shiny_silent_error) |
|
127 |
- }+ |
|||
128 | -11x - | +178x |
- if (checkmate::test_list(modules, min.len = 1, any.missing = FALSE, types = c("teal_module", "teal_modules"))) {+ moduleServer(id, function(input, output, session) { |
|
129 | -5x - | +
- modules <- do.call(teal::modules, modules)+ # there is an empty reactive cycle on `init` and `data_rv` has `shiny.silent.error` class |
||
130 | -+ | 178x |
- }+ srv_validate_error("silent_error", data, validate_shiny_silent_error) |
|
131 | -+ | 178x |
-
+ srv_check_class_teal_data("class_teal_data", data) |
|
132 | -+ | 178x |
- ## `filter`+ srv_check_shiny_warnings("shiny_warnings", data, modules) |
|
133 | -11x - | +178x |
- checkmate::assert_class(filter, "teal_slices")+ output$previous_failed <- renderUI({ |
|
134 | -+ | 168x |
-
+ if (hide_validation_error()) { |
|
135 | -+ | ! |
- ## all other arguments+ shinyjs::hide("validate_messages") |
|
136 | -10x - | +! |
- checkmate::assert(+ tags$div("One of previous transformers failed. Please fix and continue.", class = "teal-output-warning") |
|
137 | -10x - | +
- .var.name = "title",+ } else { |
||
138 | -10x - | +168x |
- checkmate::check_string(title),+ shinyjs::show("validate_messages") |
|
139 | -10x - | +168x |
- checkmate::check_multi_class(title, c("shiny.tag", "shiny.tag.list", "html"))+ NULL |
|
140 |
- )+ } |
|||
141 | -10x - | +
- checkmate::assert(+ }) |
||
142 | -10x - | +
- .var.name = "header",+ |
||
143 | -10x - | +178x |
- checkmate::check_string(header),+ .trigger_on_success(data) |
|
144 | -10x - | +
- checkmate::check_multi_class(header, c("shiny.tag", "shiny.tag.list", "html"))+ }) |
||
145 |
- )+ } |
|||
146 | -10x - | +
- checkmate::assert(+ |
||
147 | -10x - | +
- .var.name = "footer",+ #' @keywords internal |
||
148 | -10x - | +
- checkmate::check_string(footer),+ ui_validate_error <- function(id) { |
||
149 | -10x - | +80x |
- checkmate::check_multi_class(footer, c("shiny.tag", "shiny.tag.list", "html"))+ ns <- NS(id) |
|
150 | -+ | 80x |
- )+ uiOutput(ns("message")) |
|
151 | -10x - | +
- checkmate::assert_character(id, max.len = 1, any.missing = FALSE)+ } |
||
153 |
- # log+ #' @keywords internal |
|||
154 | -10x - | +
- teal.logger::log_system_info()+ srv_validate_error <- function(id, data, validate_shiny_silent_error) { |
||
155 | -+ | 178x |
-
+ checkmate::assert_string(id) |
|
156 | -+ | 178x |
- # argument transformations+ checkmate::assert_flag(validate_shiny_silent_error) |
|
157 | -+ | 178x |
- ## `modules` - landing module+ moduleServer(id, function(input, output, session) { |
|
158 | -10x - | +178x |
- landing <- extract_module(modules, "teal_module_landing")+ output$message <- renderUI({ |
|
159 | -10x - | +181x |
- if (length(landing) == 1L) {+ is_shiny_silent_error <- inherits(data(), "shiny.silent.error") && identical(data()$message, "") |
|
160 | -! | +175x |
- landing_popup <- landing[[1L]]+ if (inherits(data(), "qenv.error")) { |
|
161 | -! | +2x |
- modules <- drop_module(modules, "teal_module_landing")+ validate( |
|
162 | -! | +2x |
- lifecycle::deprecate_soft(+ need( |
|
163 | -! | +2x |
- when = "0.15.3",+ FALSE, |
|
164 | -! | +2x |
- what = "landing_popup_module()",+ paste( |
|
165 | -! | +2x |
- details = paste(+ "Error when executing the `data` module:", |
|
166 | -! | +2x |
- "Pass `landing_popup_module` to the `landing_popup` argument of the `init` ",+ strip_style(paste(data()$message, collapse = "\n")), |
|
167 | -! | +2x |
- "instead of wrapping it into `modules()` and passing to the `modules` argument"+ "\nCheck your inputs or contact app developer if error persists.", |
|
168 | -+ | 2x |
- )+ collapse = "\n" |
|
169 |
- )+ ) |
|||
170 | -10x - | +
- } else if (length(landing) > 1L) {+ ) |
||
171 | -! | +
- stop("Only one `landing_popup_module` can be used.")+ ) |
||
172 | -+ | 173x |
- }+ } else if (inherits(data(), "error")) { |
|
173 | -+ | 7x |
-
+ if (is_shiny_silent_error && !validate_shiny_silent_error) { |
|
174 | -+ | 1x |
- ## `filter` - set app_id attribute unless present (when restoring bookmark)+ return(NULL) |
|
175 | -10x - | +
- if (is.null(attr(filter, "app_id", exact = TRUE))) attr(filter, "app_id") <- create_app_id(data, modules)+ } |
||
176 | -+ | 6x |
-
+ validate( |
|
177 | -+ | 6x |
- ## `filter` - convert teal.slice::teal_slices to teal::teal_slices+ need( |
|
178 | -10x - | +6x |
- filter <- as.teal_slices(as.list(filter))+ FALSE, |
|
179 | -+ | 6x |
-
+ sprintf( |
|
180 | -+ | 6x |
- # argument checking (interdependent)+ "Shiny error when executing the `data` module.\n%s\n%s", |
|
181 | -+ | 6x |
- ## `filter` - `modules`+ data()$message, |
|
182 | -10x - | +6x |
- if (isTRUE(attr(filter, "module_specific"))) {+ "Check your inputs or contact app developer if error persists." |
|
183 | -! | +
- module_names <- unlist(c(module_labels(modules), "global_filters"))+ ) |
||
184 | -! | +
- failed_mod_names <- setdiff(names(attr(filter, "mapping")), module_names)+ ) |
||
185 | -! | +
- if (length(failed_mod_names)) {+ ) |
||
186 | -! | +
- stop(+ } |
||
187 | -! | +
- sprintf(+ }) |
||
188 | -! | +
- "Some module names in the mapping arguments don't match module labels.\n %s not in %s",+ }) |
||
189 | -! | +
- toString(failed_mod_names),+ } |
||
190 | -! | +
- toString(unique(module_names))+ |
||
191 |
- )+ |
|||
192 |
- )+ #' @keywords internal |
|||
193 |
- }+ ui_check_class_teal_data <- function(id) { |
|||
194 | -+ | 80x |
-
+ ns <- NS(id) |
|
195 | -! | +80x |
- if (anyDuplicated(module_names)) {+ uiOutput(ns("message")) |
|
196 |
- # In teal we are able to set nested modules with duplicated label.+ } |
|||
197 |
- # Because mapping argument bases on the relationship between module-label and filter-id,+ |
|||
198 |
- # it is possible that module-label in mapping might refer to multiple teal_module (identified by the same label)+ #' @keywords internal |
|||
199 | -! | +
- stop(+ srv_check_class_teal_data <- function(id, data) { |
||
200 | -! | +178x |
- sprintf(+ checkmate::assert_string(id) |
|
201 | -! | +178x |
- "Module labels should be unique when teal_slices(mapping = TRUE). Duplicated labels:\n%s ",+ moduleServer(id, function(input, output, session) { |
|
202 | -! | +178x |
- toString(module_names[duplicated(module_names)])+ output$message <- renderUI({ |
|
203 | -+ | 181x |
- )+ validate( |
|
204 | -+ | 181x |
- )+ need( |
|
205 | -+ | 181x |
- }+ inherits(data(), c("teal_data", "error")), |
|
206 | -+ | 181x |
- }+ "Did not receive `teal_data` object. Cannot proceed further." |
|
207 |
-
+ ) |
|||
208 |
- ## `data` - `modules`+ ) |
|||
209 | -10x - | +
- if (inherits(data, "teal_data")) {+ }) |
||
210 | -9x - | +
- if (length(ls(teal.code::get_env(data))) == 0) {+ }) |
||
211 | -1x - | +
- stop("The environment of `data` is empty.")+ } |
||
212 |
- }+ |
|||
213 |
-
+ #' @keywords internal |
|||
214 | -8x - | +
- is_modules_ok <- check_modules_datanames(modules, ls(teal.code::get_env(data)))+ ui_check_shiny_warnings <- function(id) { |
||
215 | -8x - | +80x |
- if (!isTRUE(is_modules_ok) && length(unlist(extract_transformers(modules))) == 0) {+ ns <- NS(id) |
|
216 | -1x - | +80x |
- lapply(is_modules_ok$string, warning, call. = FALSE)+ uiOutput(NS(id, "message")) |
|
217 |
- }+ } |
|||
219 | -8x - | +
- is_filter_ok <- check_filter_datanames(filter, ls(teal.code::get_env(data)))+ #' @keywords internal |
||
220 | -8x - | +
- if (!isTRUE(is_filter_ok)) {+ srv_check_shiny_warnings <- function(id, data, modules) { |
||
221 | -1x - | +178x |
- warning(is_filter_ok)+ checkmate::assert_string(id) |
|
222 | -+ | 178x |
- # we allow app to continue if applied filters are outside+ moduleServer(id, function(input, output, session) { |
|
223 | -+ | 178x |
- # of possible data range+ output$message <- renderUI({ |
|
224 | -+ | 181x |
- }+ if (inherits(data(), "teal_data")) { |
|
225 | -+ | 164x |
- }+ is_modules_ok <- check_modules_datanames_html( |
|
226 | -+ | 164x |
-
+ modules = modules, datanames = ls(teal.code::get_env(data())) |
|
227 | -9x - | +
- reporter <- teal.reporter::Reporter$new()$set_id(attr(filter, "app_id"))+ ) |
||
228 | -9x - | +164x |
- if (is_arg_used(modules, "reporter") && length(extract_module(modules, "teal_module_previewer")) == 0) {+ if (!isTRUE(is_modules_ok)) { |
|
229 | -! | +15x |
- modules <- append_module(+ tags$div(is_modules_ok, class = "teal-output-warning") |
|
230 | -! | +
- modules,+ } |
||
231 | -! | +
- reporter_previewer_module(server_args = list(previewer_buttons = c("download", "reset")))+ } |
||
232 |
- )+ }) |
|||
233 |
- }+ }) |
|||
234 |
-
+ } |
|||
235 | -9x - | +
- ns <- NS(id)+ |
||
236 |
- # Note: UI must be a function to support bookmarking.+ .trigger_on_success <- function(data) { |
|||
237 | -9x - | +178x |
- res <- list(+ out <- reactiveVal(NULL) |
|
238 | -9x - | +178x |
- ui = function(request) {+ observeEvent(data(), { |
|
239 | -! | +175x |
- ui_teal(+ if (inherits(data(), "teal_data")) { |
|
240 | -! | -
- id = ns("teal"),- |
- ||
241 | -! | -
- modules = modules,- |
- ||
242 | -! | -
- title = title,- |
- ||
243 | -! | -
- header = header,- |
- ||
244 | -! | -
- footer = footer- |
- ||
245 | -- |
- )- |
- ||
246 | -+ | 164x |
- },+ if (!identical(data(), out())) { |
|
247 | -9x - | -
- server = function(input, output, session) {- |
- ||
248 | -! | -
- if (!is.null(landing_popup)) {- |
- ||
249 | -! | +241 | +164x |
- do.call(landing_popup$server, c(list(id = "landing_module_shiny_id"), landing_popup$server_args))+ out(data()) |
250 | +242 |
} |
||
251 | -! | -
- srv_teal(id = ns("teal"), data = data, modules = modules, filter = deep_copy_filter(filter))- |
- ||
252 | +243 |
} |
||
253 | -- |
- )- |
- ||
254 | +244 | - - | -||
255 | -9x - | -
- logger::log_debug("init teal app has been initialized.")+ }) |
||
256 | +245 | |||
257 | -9x - | +246 | +178x |
- res+ out |
258 | +247 |
}@@ -33145,14 +31192,14 @@ teal coverage - 59.73% |
1 |
- #' An example `teal` module+ #' Generate lockfile for application's environment reproducibility |
||
3 |
- #' `r lifecycle::badge("experimental")`+ #' @param lockfile_path (`character`) path to the lockfile. |
||
5 |
- #' @inheritParams teal_modules+ #' @section Different ways of creating lockfile: |
||
6 |
- #' @return A `teal` module which can be included in the `modules` argument to [init()].+ #' `teal` leverages [renv::snapshot()], which offers multiple methods for lockfile creation. |
||
7 |
- #' @examples+ #' |
||
8 |
- #' app <- init(+ #' - **Working directory lockfile**: `teal`, by default, will create an `implicit` type lockfile that uses |
||
9 |
- #' data = teal_data(IRIS = iris, MTCARS = mtcars),+ #' `renv::dependencies()` to detect all R packages in the current project's working directory. |
||
10 |
- #' modules = example_module()+ #' - **`DESCRIPTION`-based lockfile**: To generate a lockfile based on a `DESCRIPTION` file in your working |
||
11 |
- #' )+ #' directory, set `renv::settings$snapshot.type("explicit")`. The naming convention for `type` follows |
||
12 |
- #' if (interactive()) {+ #' `renv::snapshot()`. For the `"explicit"` type, refer to `renv::settings$package.dependency.fields()` for the |
||
13 |
- #' shinyApp(app$ui, app$server)+ #' `DESCRIPTION` fields included in the lockfile. |
||
14 |
- #' }+ #' - **Custom files-based lockfile**: To specify custom files as the basis for the lockfile, set |
||
15 |
- #' @export+ #' `renv::settings$snapshot.type("custom")` and configure the `renv.snapshot.filter` option. |
||
16 |
- example_module <- function(label = "example teal module", datanames = "all", transformers = list()) {+ #' |
||
17 | -37x - | +
- checkmate::assert_string(label)+ #' @section lockfile usage: |
|
18 | -37x - | +
- ans <- module(+ #' After creating the lockfile, you can restore the application's environment using `renv::restore()`. |
|
19 | -37x - | +
- label,+ #' |
|
20 | -37x - | +
- server = function(id, data) {+ #' @seealso [renv::snapshot()], [renv::restore()]. |
|
21 | -2x - | +
- checkmate::assert_class(isolate(data()), "teal_data")+ #' |
|
22 | -2x - | +
- moduleServer(id, function(input, output, session) {+ #' @return `NULL` |
|
23 | -2x - | +
- datanames_rv <- reactive(ls(teal.code::get_env((req(data())))))+ #' |
|
24 | -2x - | +
- observeEvent(datanames_rv(), {+ #' @name module_teal_lockfile |
|
25 | -2x - | +
- selected <- input$dataname+ #' @rdname module_teal_lockfile |
|
26 | -2x - | +
- if (identical(selected, "")) {+ #' |
|
27 | -! | +
- selected <- restoreInput(session$ns("dataname"), NULL)+ #' @keywords internal |
|
28 | -2x - | +
- } else if (isFALSE(selected %in% datanames_rv())) {+ NULL |
|
29 | -! | +
- selected <- datanames_rv()[1]+ |
|
30 |
- }+ #' @rdname module_teal_lockfile |
||
31 | -2x - | +
- updateSelectInput(+ ui_teal_lockfile <- function(id) { |
|
32 | -2x - | +! |
- session = session,+ ns <- NS(id) |
33 | -2x - | +! |
- inputId = "dataname",+ shiny::tagList( |
34 | -2x - | +! |
- choices = datanames_rv(),+ tags$span("", id = ns("lockFileStatus")), |
35 | -2x - | +! |
- selected = selected+ shinyjs::disabled(downloadLink(ns("lockFileLink"), "Download lockfile")) |
36 |
- )+ ) |
||
37 |
- })+ } |
||
39 | -2x - | +
- output$text <- renderPrint({+ #' @rdname module_teal_lockfile |
|
40 | -2x - | +
- req(input$dataname)+ srv_teal_lockfile <- function(id) { |
|
41 | -! | +81x |
- data()[[input$dataname]]+ moduleServer(id, function(input, output, session) { |
42 | -+ | 81x |
- })+ logger::log_debug("Initialize srv_teal_lockfile.") |
43 | -+ | 81x |
-
+ enable_lockfile_download <- function() { |
44 | -2x - | +! |
- teal.widgets::verbatim_popup_srv(+ shinyjs::html("lockFileStatus", "Application lockfile ready.") |
45 | -2x - | +! |
- id = "rcode",+ shinyjs::hide("lockFileStatus", anim = TRUE) |
46 | -2x - | +! |
- verbatim_content = reactive(teal.code::get_code(data())),+ shinyjs::enable("lockFileLink") |
47 | -2x - | +! |
- title = "Example Code"+ output$lockFileLink <- shiny::downloadHandler( |
48 | -+ | ! |
- )+ filename = function() { |
49 | -+ | ! |
- })+ "renv.lock" |
50 |
- },+ }, |
||
51 | -37x - | +! |
- ui = function(id) {+ content = function(file) { |
52 | ! |
- ns <- NS(id)+ file.copy(lockfile_path, file) |
|
53 | ! |
- teal.widgets::standard_layout(+ file |
|
54 | -! | +
- output = verbatimTextOutput(ns("text")),+ }, |
|
55 | ! |
- encoding = tags$div(+ contentType = "application/json" |
|
56 | -! | +
- selectInput(ns("dataname"), "Choose a dataset", choices = NULL),+ ) |
|
57 | -! | +
- teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")+ } |
|
58 | -+ | 81x |
- )+ disable_lockfile_download <- function() { |
59 | -+ | ! |
- )+ warning("Lockfile creation failed.", call. = FALSE) |
60 | -+ | ! |
- },+ shinyjs::html("lockFileStatus", "Lockfile creation failed.") |
61 | -37x - | +! |
- datanames = datanames,+ shinyjs::hide("lockFileLink") |
62 | -37x - | +
- transformers = transformers+ } |
|
63 |
- )+ |
||
64 | -37x - | +81x |
- attr(ans, "teal_bookmarkable") <- TRUE+ shiny::onStop(function() { |
65 | -37x - | +81x |
- ans+ if (file.exists(lockfile_path) && !shiny::isRunning()) { |
66 | -- |
- }- |
-
1 | -+ | 1x |
- setOldClass("teal_data_module")+ logger::log_debug("Removing lockfile after shutting down the app") |
|
2 | -+ | |||
67 | +1x |
-
+ file.remove(lockfile_path) |
||
3 | +68 |
- #' Evaluate code on `teal_data_module`+ } |
||
4 | +69 |
- #'+ }) |
||
5 | +70 |
- #' @details+ |
||
6 | -+ | |||
71 | +81x |
- #' `eval_code` evaluates given code in the environment of the `teal_data` object created by the `teal_data_module`.+ lockfile_path <- "teal_app.lock" |
||
7 | -+ | |||
72 | +81x |
- #' The code is added to the `@code` slot of the `teal_data`.+ mode <- getOption("teal.lockfile.mode", default = "") |
||
8 | +73 |
- #'+ |
||
9 | -+ | |||
74 | +81x |
- #' @param object (`teal_data_module`)+ if (!(mode %in% c("auto", "enabled", "disabled"))) { |
||
10 | -+ | |||
75 | +! |
- #' @inheritParams teal.code::eval_code+ stop("'teal.lockfile.mode' option can only be one of \"auto\", \"disabled\" or \"disabled\". ") |
||
11 | +76 |
- #'+ } |
||
12 | +77 |
- #' @return+ |
||
13 | -+ | |||
78 | +81x |
- #' `eval_code` returns a `teal_data_module` object with a delayed evaluation of `code` when the module is run.+ if (mode == "disabled") { |
||
14 | -+ | |||
79 | +1x |
- #'+ logger::log_debug("'teal.lockfile.mode' option is set to 'disabled'. Hiding lockfile download button.") |
||
15 | -+ | |||
80 | +1x |
- #' @examples+ shinyjs::hide("lockFileLink") |
||
16 | -+ | |||
81 | +1x |
- #' eval_code(tdm, "dataset1 <- subset(dataset1, Species == 'virginica')")+ return(NULL) |
||
17 | +82 |
- #'+ } |
||
18 | +83 |
- #' @include teal_data_module.R+ |
||
19 | -+ | |||
84 | +80x |
- #' @name eval_code+ if (file.exists(lockfile_path)) { |
||
20 | -+ | |||
85 | +! |
- #' @rdname teal_data_module+ logger::log_debug("Lockfile has already been created for this app - skipping automatic creation.") |
||
21 | -+ | |||
86 | +! |
- #' @aliases eval_code,teal_data_module,character-method+ enable_lockfile_download() |
||
22 | -+ | |||
87 | +! |
- #' @aliases eval_code,teal_data_module,language-method+ return(NULL) |
||
23 | +88 |
- #' @aliases eval_code,teal_data_module,expression-method+ } |
||
24 | +89 |
- #'+ |
||
25 | -+ | |||
90 | +80x |
- #' @importFrom methods setMethod+ if (mode == "auto" && .is_disabled_lockfile_scenario()) { |
||
26 | -+ | |||
91 | +79x |
- #' @importMethodsFrom teal.code eval_code+ logger::log_debug( |
||
27 | -+ | |||
92 | +79x |
- #'+ "Automatic lockfile creation disabled. Execution scenario satisfies teal:::.is_disabled_lockfile_scenario()." |
||
28 | +93 |
- setMethod("eval_code", signature = c("teal_data_module", "character"), function(object, code) {- |
- ||
29 | -9x - | -
- teal_data_module(+ ) |
||
30 | -9x - | +94 | +79x |
- ui = function(id) {+ shinyjs::hide("lockFileLink") |
31 | -1x - | +95 | +79x |
- ns <- NS(id)+ return(NULL) |
32 | -1x - | +|||
96 | +
- object$ui(ns("mutate_inner"))+ } |
|||
33 | +97 |
- },+ |
||
34 | -9x - | +98 | +1x |
- server = function(id) {+ if (!.is_lockfile_deps_installed()) { |
35 | -7x - | +|||
99 | +! |
- moduleServer(id, function(input, output, session) {+ warning("Automatic lockfile creation disabled. `mirai` and `renv` packages must be installed.") |
||
36 | -7x - | +|||
100 | +! |
- teal_data_rv <- object$server("mutate_inner")+ shinyjs::hide("lockFileLink") |
||
37 | -6x - | +|||
101 | +! |
- td <- eventReactive(teal_data_rv(),+ return(NULL) |
||
38 | +102 |
- {- |
- ||
39 | -6x - | -
- if (inherits(teal_data_rv(), c("teal_data", "qenv.error"))) {+ } |
||
40 | -4x - | +|||
103 | +
- eval_code(teal_data_rv(), code)+ |
|||
41 | +104 |
- } else {+ # - Will be run only if the lockfile doesn't exist (see the if-s above) |
||
42 | -2x - | +|||
105 | +
- teal_data_rv()+ # - We render to the tempfile because the process might last after session is closed and we don't |
|||
43 | +106 |
- }+ # want to make a "teal_app.renv" then. This is why we copy only during active session. |
||
44 | -+ | |||
107 | +1x |
- },+ process <- .teal_lockfile_process_invoke(lockfile_path) |
||
45 | -6x - | +108 | +1x |
- ignoreNULL = FALSE+ observeEvent(process$status(), { |
46 | -+ | |||
109 | +! |
- )+ if (process$status() %in% c("initial", "running")) { |
||
47 | -6x - | +|||
110 | +! |
- td+ shinyjs::html("lockFileStatus", "Creating lockfile...") |
||
48 | -+ | |||
111 | +! |
- })+ } else if (process$status() == "success") { |
||
49 | -+ | |||
112 | +! |
- }+ result <- process$result() |
||
50 | -+ | |||
113 | +! |
- )+ if (any(grepl("Lockfile written to", result$out))) { |
||
51 | -+ | |||
114 | +! |
- })+ logger::log_debug("Lockfile containing { length(result$res$Packages) } packages created.") |
||
52 | -+ | |||
115 | +! |
-
+ if (any(grepl("(WARNING|ERROR):", result$out))) { |
||
53 | -+ | |||
116 | +! |
- setMethod("eval_code", signature = c("teal_data_module", "language"), function(object, code) {+ warning("Lockfile created with warning(s) or error(s):", call. = FALSE) |
||
54 | -1x - | +|||
117 | +! |
- eval_code(object, code = paste(lang2calls(code), collapse = "\n"))+ for (i in result$out) { |
||
55 | -+ | |||
118 | +! |
- })+ warning(i, call. = FALSE) |
||
56 | +119 |
-
+ } |
||
57 | +120 |
- setMethod("eval_code", signature = c("teal_data_module", "expression"), function(object, code) {- |
- ||
58 | -2x - | -
- eval_code(object, code = paste(lang2calls(code), collapse = "\n"))+ } |
||
59 | -+ | |||
121 | +! |
- })+ enable_lockfile_download() |
1 | +122 |
- #' Module to transform `reactive` `teal_data`+ } else { |
||
2 | -+ | |||
123 | +! |
- #'+ disable_lockfile_download() |
||
3 | +124 |
- #' Module calls multiple [`module_teal_data`] in sequence so that `reactive teal_data` output+ } |
||
4 | -+ | |||
125 | +! |
- #' from one module is handed over to the following module's input.+ } else if (process$status() == "error") { |
||
5 | -+ | |||
126 | +! |
- #'+ disable_lockfile_download() |
||
6 | +127 |
- #' @inheritParams module_teal_data+ } |
||
7 | +128 |
- #' @inheritParams teal_modules+ }) |
||
8 | +129 |
- #' @return `reactive` `teal_data`+ |
||
9 | -+ | |||
130 | +1x |
- #'+ NULL |
||
10 | +131 |
- #'+ }) |
||
11 | +132 |
- #' @name module_transform_data+ } |
||
12 | +133 |
- #' @keywords internal+ |
||
13 | +134 |
- NULL+ utils::globalVariables(c("opts", "sysenv", "libpaths", "wd", "lockfilepath", "run")) # needed for mirai call |
||
14 | +135 |
-
+ #' @rdname module_teal_lockfile |
||
15 | +136 |
- #' @rdname module_transform_data+ .teal_lockfile_process_invoke <- function(lockfile_path) { |
||
16 | -+ | |||
137 | +1x |
- ui_transform_data <- function(id, transformers = list(), class = "well") {+ mirai_obj <- NULL |
||
17 | -! | +|||
138 | +1x |
- checkmate::assert_string(id)+ process <- shiny::ExtendedTask$new(function() { |
||
18 | -! | +|||
139 | +1x |
- checkmate::assert_list(transformers, "teal_transform_module")+ m <- mirai::mirai( |
||
19 | +140 |
-
+ { |
||
20 | -! | +|||
141 | +1x |
- ns <- NS(id)+ options(opts) |
||
21 | -! | +|||
142 | +1x |
- labels <- lapply(transformers, function(x) attr(x, "label"))+ do.call(Sys.setenv, sysenv) |
||
22 | -! | +|||
143 | +1x |
- ids <- get_unique_labels(labels)+ .libPaths(libpaths) |
||
23 | -! | +|||
144 | +1x |
- names(transformers) <- ids+ setwd(wd) |
||
24 | -+ | |||
145 | +1x |
-
+ run(lockfile_path = lockfile_path) |
||
25 | -! | +|||
146 | +
- lapply(+ }, |
|||
26 | -! | +|||
147 | +1x |
- names(transformers),+ run = .renv_snapshot, |
||
27 | -! | +|||
148 | +1x |
- function(name) {+ lockfile_path = lockfile_path, |
||
28 | -! | +|||
149 | +1x |
- data_mod <- transformers[[name]]+ opts = options(), |
||
29 | -! | +|||
150 | +1x |
- wrapper_id <- ns(sprintf("wrapper_%s", name))+ libpaths = .libPaths(), |
||
30 | -! | +|||
151 | +1x |
- div( # todo: accordion?+ sysenv = as.list(Sys.getenv()), |
||
31 | -+ | |||
152 | +1x |
- # class .teal_validated changes the color of the boarder on error in ui_validate_reactive_teal_data+ wd = getwd() |
||
32 | +153 |
- # For details see tealValidate.js file.- |
- ||
33 | -! | -
- class = c(class, "teal_validated"),- |
- ||
34 | -! | -
- title = attr(data_mod, "label"),- |
- ||
35 | -! | -
- tags$span(+ ) |
||
36 | -! | +|||
154 | +1x |
- class = "text-primary mb-4",+ mirai_obj <<- m |
||
37 | -! | +|||
155 | +1x |
- icon("fas fa-square-pen"),+ m |
||
38 | -! | +|||
156 | +
- attr(data_mod, "label")+ }) |
|||
39 | +157 |
- ),+ |
||
40 | -! | +|||
158 | +1x |
- tags$i(+ shiny::onStop(function() { |
||
41 | -! | +|||
159 | +1x |
- class = "remove pull-right fa fa-angle-down",+ if (mirai::unresolved(mirai_obj)) { |
||
42 | +160 | ! |
- style = "cursor: pointer;",+ logger::log_debug("Terminating a running lockfile process...") |
|
43 | +161 | ! |
- title = "fold/expand transform panel",+ mirai::stop_mirai(mirai_obj) # this doesn't stop running - renv will be created even if session is closed |
|
44 | -! | +|||
162 | +
- onclick = sprintf("togglePanelItems(this, '%s', 'fa-angle-right', 'fa-angle-down');", wrapper_id)+ } |
|||
45 | +163 |
- ),+ }) |
||
46 | -! | +|||
164 | +
- div(+ |
|||
47 | -! | +|||
165 | +1x |
- id = wrapper_id,+ suppressWarnings({ # 'package:stats' may not be available when loading |
||
48 | -! | +|||
166 | +1x |
- ui_teal_data(id = ns(name), data_module = transformers[[name]]$ui)+ process$invoke() |
||
49 | +167 |
- )+ }) |
||
50 | +168 |
- )+ |
||
51 | -+ | |||
169 | +1x |
- }+ logger::log_debug("Lockfile creation started based on { getwd() }.") |
||
52 | +170 |
- )+ + |
+ ||
171 | +1x | +
+ process |
||
53 | +172 |
} |
||
54 | +173 | |||
55 | +174 |
- #' @rdname module_transform_data+ #' @rdname module_teal_lockfile |
||
56 | +175 |
- srv_transform_data <- function(id, data, transformers = list(), modules, is_transformer_failed = reactiveValues()) {+ .renv_snapshot <- function(lockfile_path) { |
||
57 | -76x - | +176 | +1x |
- checkmate::assert_string(id)+ out <- utils::capture.output( |
58 | -76x - | +177 | +1x |
- assert_reactive(data)+ res <- renv::snapshot( |
59 | -76x - | +178 | +1x |
- checkmate::assert_list(transformers, "teal_transform_module")+ lockfile = lockfile_path, |
60 | -76x - | +179 | +1x |
- checkmate::assert_class(modules, "teal_module")+ prompt = FALSE, |
61 | -76x - | +180 | +1x |
- labels <- lapply(transformers, function(x) attr(x, "label"))+ force = TRUE, |
62 | -76x - | +181 | +1x |
- ids <- get_unique_labels(labels)+ type = renv::settings$snapshot.type() # see the section "Different ways of creating lockfile" above here |
63 | -76x - | +|||
182 | +
- names(transformers) <- ids+ ) |
|||
64 | -76x - | +|||
183 | +
- moduleServer(id, function(input, output, session) {+ ) |
|||
65 | -76x - | +|||
184 | +
- logger::log_debug("srv_teal_data_modules initializing.")+ |
|||
66 | -76x - | +185 | +1x |
- Reduce(+ list(out = out, res = res) |
67 | -76x - | +|||
186 | +
- function(previous_result, name) {+ } |
|||
68 | -19x - | +|||
187 | +
- srv_teal_data(+ |
|||
69 | -19x - | +|||
188 | +
- id = name,+ #' @rdname module_teal_lockfile |
|||
70 | -19x - | +|||
189 | +
- data_module = function(id) transformers[[name]]$server(id, previous_result),+ .is_lockfile_deps_installed <- function() { |
|||
71 | -19x - | +190 | +1x |
- modules = modules,+ requireNamespace("mirai", quietly = TRUE) && requireNamespace("renv", quietly = TRUE) |
72 | -19x - | +|||
191 | +
- is_transformer_failed = is_transformer_failed+ } |
|||
73 | +192 |
- )+ |
||
74 | +193 |
- },+ #' @rdname module_teal_lockfile+ |
+ ||
194 | ++ |
+ .is_disabled_lockfile_scenario <- function() { |
||
75 | -76x - | +195 | +79x |
- x = names(transformers),+ identical(Sys.getenv("CALLR_IS_RUNNING"), "true") || # inside callr process |
76 | -76x - | +196 | +79x |
- init = data+ identical(Sys.getenv("TESTTHAT"), "true") || # inside devtools::test |
77 | -+ | |||
197 | +79x |
- )+ !identical(Sys.getenv("QUARTO_PROJECT_ROOT"), "") || # inside Quarto process |
||
78 | +198 |
- })+ (+ |
+ ||
199 | +79x | +
+ ("CheckExEnv" %in% search()) || any(c("_R_CHECK_TIMINGS_", "_R_CHECK_LICENSE_") %in% names(Sys.getenv()))+ |
+ ||
200 | +79x | +
+ ) # inside R CMD CHECK |
||
79 | +201 |
}@@ -34824,64 +32778,56 @@ teal coverage - 59.73% | ||
25 | -4x - | +4x |
checkmate::assert_character(src, min.len = 0, max.len = 1) |
|
26 | -4x - | +4x |
params <- list(...) |
|
27 | -4x - | +4x |
params$eval <- FALSE |
|
28 | -4x - | +4x |
rblock <- RcodeBlock$new(src) |
|
29 | -4x - | +4x |
rblock$set_params(params) |
|
30 | -4x - | +4x |
self$append_content(rblock) |
|
31 | -4x - | +4x |
self$append_metadata("SRC", src) |
|
32 | -4x - | +4x |
invisible(self) |
@@ -34951,32 +32897,28 @@ |
42 | -5x - | +5x |
checkmate::assert_class(fs, "teal_slices") |
|
43 | -4x - | +4x |
self$append_text("Filter State", "header3") |
|
44 | -4x - | +4x |
if (length(fs)) { |
|
45 | -3x - | +3x |
self$append_content(TealSlicesBlock$new(fs)) |
@@ -34990,8 +32932,7 @@ |
47 | -1x - | +1x |
self$append_text("No filters specified.") |
@@ -35005,8 +32946,7 @@ |
49 | -4x - | +4x |
invisible(self) |
@@ -35083,64 +33023,56 @@ |
60 | -4x - | +4x |
checkmate::assert_list(encodings) |
|
61 | -4x - | +4x |
self$append_text("Selected Options", "header3") |
|
62 | -4x - | +4x |
if (requireNamespace("yaml", quietly = TRUE)) { |
|
63 | -4x - | +4x |
self$append_text(yaml::as.yaml(encodings, handlers = list( |
|
64 | -4x - | +4x |
POSIXct = function(x) format(x, "%Y-%m-%d"), |
|
65 | -4x - | +4x |
POSIXlt = function(x) format(x, "%Y-%m-%d"), |
|
66 | -4x - | +4x |
Date = function(x) format(x, "%Y-%m-%d") |
|
67 | -4x - | +4x |
)), "verbatim") |
@@ -35168,16 +33100,14 @@ |
71 | -4x - | +4x |
self$append_metadata("Encodings", encodings) |
|
72 | -4x - | +4x |
invisible(self) |
@@ -35380,24 +33310,21 @@ |
101 | -9x - | +9x |
self$set_content(content) |
|
102 | -8x - | +8x |
self$set_style(style) |
|
103 | -8x - | +8x |
invisible(self) |
@@ -35488,64 +33415,56 @@ |
116 | -9x - | +9x |
checkmate::assert_class(content, "teal_slices") |
|
117 | -8x - | +8x |
if (length(content) != 0) { |
|
118 | -6x - | +6x |
states_list <- lapply(content, function(x) { |
|
119 | -6x - | +6x |
x_list <- shiny::isolate(as.list(x)) |
|
120 | -6x - | +6x |
if ( |
|
121 | -6x - | +6x |
inherits(x_list$choices, c("integer", "numeric", "Date", "POSIXct", "POSIXlt")) && |
|
122 | -6x - | +6x |
length(x_list$choices) == 2 && |
|
123 | -6x - | +6x |
length(x_list$selected) == 2 |
@@ -35580,8 +33499,7 @@ |
128 | -6x - | +6x |
if (!is.null(x_list$arg)) { |
@@ -35609,16 +33527,14 @@ |
132 | -6x - | +6x |
x_list <- x_list[ |
|
133 | -6x - | +6x |
c("dataname", "varname", "experiment", "arg", "expr", "selected", "range", "keep_na", "keep_inf") |
@@ -35632,24 +33548,21 @@ |
135 | -6x - | +6x |
names(x_list) <- c( |
|
136 | -6x - | +6x |
"Dataset name", "Variable name", "Experiment", "Filtering by", "Applied expression", |
|
137 | -6x - | +6x |
"Selected Values", "Selected range", "Include NA values", "Include Inf values" |
@@ -35670,8 +33583,7 @@ |
140 | -6x - | +6x |
Filter(Negate(is.null), x_list) |
@@ -35692,16 +33604,14 @@ |
143 | -6x - | +6x |
if (requireNamespace("yaml", quietly = TRUE)) { |
|
144 | -6x - | +6x |
super$set_content(yaml::as.yaml(states_list)) |
@@ -35736,16 +33646,14 @@ |
149 | -8x - | +8x |
private$teal_slices <- content |
|
150 | -8x - | +8x |
invisible(self) |
@@ -35843,4236 +33751,4650 @@ |
164 | -1x - | +1x |
checkmate::assert_list(x) |
|
165 | -1x - | +1x | +
+ checkmate::assert_names(names(x), must.include = c("text", "style"))+ |
+ |
166 | +1x | +
+ super$set_content(x$text)+ |
+ ||
167 | +1x | +
+ super$set_style(x$style)+ |
+ ||
168 | +1x | +
+ invisible(self)+ |
+ ||
169 | ++ |
+ },+ |
+ ||
170 | ++ |
+ #' @description Convert the `TealSlicesBlock` to a list.+ |
+ ||
171 | ++ |
+ #'+ |
+ ||
172 | ++ |
+ #' @return `named list` with a text and style.+ |
+ ||
173 | ++ |
+ #' @examples+ |
+ ||
174 | ++ |
+ #' TealSlicesBlock <- getFromNamespace("TealSlicesBlock", "teal")+ |
+ ||
175 | ++ |
+ #' block <- TealSlicesBlock$new()+ |
+ ||
176 | ++ |
+ #' block$to_list()+ |
+ ||
177 | ++ |
+ #'+ |
+ ||
178 | ++ |
+ to_list = function() {+ |
+ ||
179 | +2x | +
+ content <- self$get_content()+ |
+ ||
180 | +2x | +
+ list(+ |
+ ||
181 | +2x | +
+ text = if (length(content)) content else "",+ |
+ ||
182 | +2x | +
+ style = self$get_style()+ |
+ ||
183 | ++ |
+ )+ |
+ ||
184 | ++ |
+ }+ |
+ ||
185 | ++ |
+ ),+ |
+ ||
186 | ++ |
+ private = list(+ |
+ ||
187 | ++ |
+ style = "verbatim",+ |
+ ||
188 | ++ |
+ teal_slices = NULL # teal_slices+ |
+ ||
189 | ++ |
+ )+ |
+ ||
190 | ++ |
+ )+ |
+
1 | ++ |
+ #' Filter panel module in teal+ |
+ ||
2 | ++ |
+ #'+ |
+ ||
3 | ++ |
+ #' Creates filter panel module from `teal_data` object and returns `teal_data`. It is build in a way+ |
+ ||
4 | ++ |
+ #' that filter panel changes and anything what happens before (e.g. [`module_init_data`]) is triggering+ |
+ ||
5 | ++ |
+ #' further reactive events only if something has changed and if the module is visible. Thanks to+ |
+ ||
6 | ++ |
+ #' this special implementation all modules' data are recalculated only for those modules which are+ |
+ ||
7 | ++ |
+ #' currently displayed.+ |
+ ||
8 | ++ |
+ #'+ |
+ ||
9 | ++ |
+ #' @return A `eventReactive` containing `teal_data` containing filtered objects and filter code.+ |
+ ||
10 | ++ |
+ #' `eventReactive` triggers only if all conditions are met:+ |
+ ||
11 | ++ |
+ #' - tab is selected (`is_active`)+ |
+ ||
12 | ++ |
+ #' - when filters are changed (`get_filter_expr` is different than previous)+ |
+ ||
13 | ++ |
+ #'+ |
+ ||
14 | ++ |
+ #' @inheritParams module_teal_module+ |
+ ||
15 | ++ |
+ #' @param active_datanames (`reactive` returning `character`) this module's data names+ |
+ ||
16 | ++ |
+ #' @name module_filter_data+ |
+ ||
17 | ++ |
+ #' @keywords internal+ |
+ ||
18 | ++ |
+ NULL+ |
+ ||
19 | ++ | + + | +||
20 | ++ |
+ #' @rdname module_filter_data+ |
+ ||
21 | ++ |
+ ui_filter_data <- function(id) {+ |
+ ||
22 | +! | +
+ ns <- shiny::NS(id)+ |
+ ||
23 | +! | +
+ uiOutput(ns("panel"))+ |
+ ||
24 | ++ |
+ }+ |
+ ||
25 | ++ | + + | +||
26 | ++ |
+ #' @rdname module_filter_data+ |
+ ||
27 | ++ |
+ srv_filter_data <- function(id, datasets, active_datanames, data_rv, is_active) {+ |
+ ||
28 | +79x | +
+ assert_reactive(datasets)+ |
+ ||
29 | +79x | +
+ moduleServer(id, function(input, output, session) {+ |
+ ||
30 | +79x | +
+ active_corrected <- reactive(intersect(active_datanames(), datasets()$datanames()))+ |
+ ||
31 | +
- checkmate::assert_names(names(x), must.include = c("text", "style"))+ |
|||
166 | -1x - | +32 | +79x |
- super$set_content(x$text)+ output$panel <- renderUI({ |
167 | -1x - | +33 | +81x |
- super$set_style(x$style)+ req(inherits(datasets(), "FilteredData")) |
168 | -1x - | +34 | +81x |
- invisible(self)+ isolate({ |
169 | +35 |
- },+ # render will be triggered only when FilteredData object changes (not when filters change) |
||
170 | +36 |
- #' @description Convert the `TealSlicesBlock` to a list.+ # technically it means that teal_data_module needs to be refreshed |
||
171 | -+ | |||
37 | +81x |
- #'+ logger::log_debug("srv_filter_panel rendering filter panel.") |
||
172 | -+ | |||
38 | +81x |
- #' @return `named list` with a text and style.+ if (length(active_corrected())) { |
||
173 | -+ | |||
39 | +79x |
- #' @examples+ datasets()$srv_active("filters", active_datanames = active_corrected) |
||
174 | -+ | |||
40 | +79x |
- #' TealSlicesBlock <- getFromNamespace("TealSlicesBlock", "teal")+ datasets()$ui_active(session$ns("filters"), active_datanames = active_corrected) |
||
175 | +41 |
- #' block <- TealSlicesBlock$new()+ } |
||
176 | +42 |
- #' block$to_list()+ }) |
||
177 | +43 |
- #'+ }) |
||
178 | +44 |
- to_list = function() {+ |
||
179 | -2x - | +45 | +79x |
- content <- self$get_content()+ trigger_data <- .observe_active_filter_changed(datasets, is_active, active_corrected, data_rv) |
180 | -2x - | +|||
46 | +
- list(+ |
|||
181 | -2x - | +47 | +79x |
- text = if (length(content)) content else "",+ eventReactive(trigger_data(), { |
182 | -2x - | +48 | +82x |
- style = self$get_style()+ .make_filtered_teal_data(modules, data = data_rv(), datasets = datasets(), datanames = active_corrected()) |
183 | +49 |
- )+ }) |
||
184 | +50 |
- }+ }) |
||
185 | +51 |
- ),+ } |
||
186 | +52 |
- private = list(+ |
||
187 | +53 |
- style = "verbatim",+ #' @rdname module_filter_data |
||
188 | +54 |
- teal_slices = NULL # teal_slices+ .make_filtered_teal_data <- function(modules, data, datasets = NULL, datanames) { |
||
189 | -+ | |||
55 | +82x |
- )+ data <- eval_code( |
||
190 | -+ | |||
56 | +82x |
- )+ data, |
1 | -+ | ||
57 | +82x |
- #' Send input validation messages to output+ paste0( |
|
2 | -+ | ||
58 | +82x |
- #'+ ".raw_data <- list2env(list(", |
|
3 | -+ | ||
59 | +82x |
- #' Captures messages from `InputValidator` objects and collates them+ toString(sprintf("%1$s = %1$s", sapply(datanames, as.name))), |
|
4 | -+ | ||
60 | +82x |
- #' into one message passed to `validate`.+ "))\n", |
|
5 | -+ | ||
61 | +82x |
- #'+ "lockEnvironment(.raw_data) # @linksto .raw_data" # this is environment and it is shared by qenvs. CAN'T MODIFY! |
|
6 | +62 |
- #' `shiny::validate` is used to withhold rendering of an output element until+ ) |
|
7 | +63 |
- #' certain conditions are met and to print a validation message in place+ ) |
|
8 | -+ | ||
64 | +82x |
- #' of the output element.+ filtered_code <- .get_filter_expr(datasets = datasets, datanames = datanames) |
|
9 | -+ | ||
65 | +82x |
- #' `shinyvalidate::InputValidator` allows to validate input elements+ filtered_teal_data <- .append_evaluated_code(data, filtered_code) |
|
10 | -+ | ||
66 | +82x |
- #' and to display specific messages in their respective input widgets.+ filtered_datasets <- sapply(datanames, function(x) datasets$get_data(x, filtered = TRUE), simplify = FALSE) |
|
11 | -+ | ||
67 | +82x |
- #' `validate_inputs` provides a hybrid solution.+ filtered_teal_data <- .append_modified_data(filtered_teal_data, filtered_datasets) |
|
12 | -+ | ||
68 | +82x |
- #' Given an `InputValidator` object, messages corresponding to inputs that fail validation+ filtered_teal_data |
|
13 | +69 |
- #' are extracted and placed in one validation message that is passed to a `validate`/`need` call.+ } |
|
14 | +70 |
- #' This way the input `validator` messages are repeated in the output.+ |
|
15 | +71 |
- #'+ #' @rdname module_filter_data |
|
16 | +72 |
- #' The `...` argument accepts any number of `InputValidator` objects+ .observe_active_filter_changed <- function(datasets, is_active, active_datanames, data_rv) { |
|
17 | -+ | ||
73 | +79x |
- #' or a nested list of such objects.+ previous_signature <- reactiveVal(NULL) |
|
18 | -+ | ||
74 | +79x |
- #' If `validators` are passed directly, all their messages are printed together+ filter_changed <- reactive({ |
|
19 | -+ | ||
75 | +181x |
- #' under one (optional) header message specified by `header`. If a list is passed,+ req(inherits(datasets(), "FilteredData")) |
|
20 | -+ | ||
76 | +181x |
- #' messages are grouped by `validator`. The list's names are used as headers+ new_signature <- c( |
|
21 | -+ | ||
77 | +181x |
- #' for their respective message groups.+ teal.code::get_code(data_rv()), |
|
22 | -+ | ||
78 | +181x |
- #' If neither of the nested list elements is named, a header message is taken from `header`.+ .get_filter_expr(datasets = datasets(), datanames = active_datanames()) |
|
23 | +79 |
- #'+ ) |
|
24 | -+ | ||
80 | +181x |
- #' @param ... either any number of `InputValidator` objects+ if (!identical(previous_signature(), new_signature)) { |
|
25 | -+ | ||
81 | +87x |
- #' or an optionally named, possibly nested `list` of `InputValidator`+ previous_signature(new_signature) |
|
26 | -+ | ||
82 | +87x |
- #' objects, see `Details`+ TRUE |
|
27 | +83 |
- #' @param header (`character(1)`) generic validation message; set to NULL to omit+ } else { |
|
28 | -+ | ||
84 | +94x |
- #'+ FALSE |
|
29 | +85 |
- #' @return+ } |
|
30 | +86 |
- #' Returns NULL if the final validation call passes and a `shiny.silent.error` if it fails.+ }) |
|
31 | +87 |
- #'+ |
|
32 | -+ | ||
88 | +79x |
- #' @seealso [`shinyvalidate::InputValidator`], [`shiny::validate`]+ trigger_data <- reactiveVal(NULL) |
|
33 | -+ | ||
89 | +79x |
- #'+ observe({ |
|
34 | -+ | ||
90 | +194x |
- #' @examplesIf require("shinyvalidate")+ if (isTRUE(is_active() && filter_changed())) { |
|
35 | -+ | ||
91 | +87x |
- #' library(shiny)+ isolate({ |
|
36 | -+ | ||
92 | +87x |
- #' library(shinyvalidate)+ if (is.null(trigger_data())) { |
|
37 | -+ | ||
93 | +79x |
- #'+ trigger_data(0) |
|
38 | +94 |
- #' ui <- fluidPage(+ } else { |
|
39 | -+ | ||
95 | +8x |
- #' selectInput("method", "validation method", c("sequential", "combined", "grouped")),+ trigger_data(trigger_data() + 1) |
|
40 | +96 |
- #' sidebarLayout(+ } |
|
41 | +97 |
- #' sidebarPanel(+ }) |
|
42 | +98 |
- #' selectInput("letter", "select a letter:", c(letters[1:3], LETTERS[4:6])),+ } |
|
43 | +99 |
- #' selectInput("number", "select a number:", 1:6),+ }) |
|
44 | +100 |
- #' tags$br(),+ |
|
45 | -+ | ||
101 | +79x |
- #' selectInput("color", "select a color:",+ trigger_data |
|
46 | +102 |
- #' c("black", "indianred2", "springgreen2", "cornflowerblue"),+ } |
|
47 | +103 |
- #' multiple = TRUE+ |
|
48 | +104 |
- #' ),+ #' @rdname module_filter_data |
|
49 | +105 |
- #' sliderInput("size", "select point size:",+ .get_filter_expr <- function(datasets, datanames) { |
|
50 | -+ | ||
106 | +263x |
- #' min = 0.1, max = 4, value = 0.25+ if (length(datanames)) {+ |
+ |
107 | +257x | +
+ teal.slice::get_filter_expr(datasets = datasets, datanames = datanames) |
|
51 | +108 |
- #' )+ } else {+ |
+ |
109 | +6x | +
+ NULL |
|
52 | +110 |
- #' ),+ } |
|
53 | +111 |
- #' mainPanel(plotOutput("plot"))+ } |
54 | +1 |
- #' )+ # This is the main function from teal to be used by the end-users. Although it delegates |
||
55 | +2 |
- #' )+ # directly to `module_teal_with_splash.R`, we keep it in a separate file because its documentation is quite large |
||
56 | +3 |
- #'+ # and it is very end-user oriented. It may also perform more argument checking with more informative |
||
57 | +4 |
- #' server <- function(input, output) {+ # error messages. |
||
58 | +5 |
- #' # set up input validation+ |
||
59 | +6 |
- #' iv <- InputValidator$new()+ #' Create the server and UI function for the `shiny` app |
||
60 | +7 |
- #' iv$add_rule("letter", sv_in_set(LETTERS, "choose a capital letter"))+ #' |
||
61 | +8 |
- #' iv$add_rule("number", function(x) {+ #' @description `r lifecycle::badge("stable")` |
||
62 | +9 |
- #' if (as.integer(x) %% 2L == 1L) "choose an even number"+ #' |
||
63 | +10 |
- #' })+ #' End-users: This is the most important function for you to start a |
||
64 | +11 |
- #' iv$enable()+ #' `teal` app that is composed of `teal` modules. |
||
65 | +12 |
- #' # more input validation+ #' |
||
66 | +13 |
- #' iv_par <- InputValidator$new()+ #' @param data (`teal_data` or `teal_data_module`) |
||
67 | +14 |
- #' iv_par$add_rule("color", sv_required(message = "choose a color"))+ #' For constructing the data object, refer to [teal_data()] and [teal_data_module()]. |
||
68 | +15 |
- #' iv_par$add_rule("color", function(x) {+ #' If `datanames` are not set for the `teal_data` object, defaults from the `teal_data` environment will be used. |
||
69 | +16 |
- #' if (length(x) > 1L) "choose only one color"+ #' @param modules (`list` or `teal_modules` or `teal_module`) |
||
70 | +17 |
- #' })+ #' Nested list of `teal_modules` or `teal_module` objects or a single |
||
71 | +18 |
- #' iv_par$add_rule(+ #' `teal_modules` or `teal_module` object. These are the specific output modules which |
||
72 | +19 |
- #' "size",+ #' will be displayed in the `teal` application. See [modules()] and [module()] for |
||
73 | +20 |
- #' sv_between(+ #' more details. |
||
74 | +21 |
- #' left = 0.5, right = 3,+ #' @param filter (`teal_slices`) Optionally, |
||
75 | +22 |
- #' message_fmt = "choose a value between {left} and {right}"+ #' specifies the initial filter using [teal_slices()]. |
||
76 | +23 |
- #' )+ #' @param title (`shiny.tag` or `character(1)`) Optionally, |
||
77 | +24 |
- #' )+ #' the browser window title. Defaults to a title "teal app" with the icon of NEST. |
||
78 | +25 |
- #' iv_par$enable()+ #' Can be created using the `build_app_title()` or |
||
79 | +26 |
- #'+ #' by passing a valid `shiny.tag` which is a head tag with title and link tag. |
||
80 | +27 |
- #' output$plot <- renderPlot({+ #' @param header (`shiny.tag` or `character(1)`) Optionally, |
||
81 | +28 |
- #' # validate output+ #' the header of the app. |
||
82 | +29 |
- #' switch(input[["method"]],+ #' @param footer (`shiny.tag` or `character(1)`) Optionally, |
||
83 | +30 |
- #' "sequential" = {+ #' the footer of the app. |
||
84 | +31 |
- #' validate_inputs(iv)+ #' @param id (`character`) Optionally, |
||
85 | +32 |
- #' validate_inputs(iv_par, header = "Set proper graphical parameters")+ #' a string specifying the `shiny` module id in cases it is used as a `shiny` module |
||
86 | +33 |
- #' },+ #' rather than a standalone `shiny` app. This is a legacy feature. |
||
87 | +34 |
- #' "combined" = validate_inputs(iv, iv_par),+ #' @param landing_popup (`teal_module_landing`) Optionally, |
||
88 | +35 |
- #' "grouped" = validate_inputs(list(+ #' a `landing_popup_module` to show up as soon as the teal app is initialized. |
||
89 | +36 |
- #' "Some inputs require attention" = iv,+ #' |
||
90 | +37 |
- #' "Set proper graphical parameters" = iv_par+ #' @return Named list containing server and UI functions. |
||
91 | +38 |
- #' ))+ #' |
||
92 | +39 |
- #' )+ #' @export |
||
93 | +40 |
#' |
||
94 | +41 |
- #' plot(faithful$eruptions ~ faithful$waiting,+ #' @include modules.R |
||
95 | +42 |
- #' las = 1, pch = 16,+ #' |
||
96 | +43 |
- #' col = input[["color"]], cex = input[["size"]]+ #' @examples |
||
97 | +44 |
- #' )+ #' app <- init( |
||
98 | +45 |
- #' })+ #' data = within( |
||
99 | +46 |
- #' }+ #' teal_data(), |
||
100 | +47 |
- #'+ #' { |
||
101 | +48 |
- #' if (interactive()) {+ #' new_iris <- transform(iris, id = seq_len(nrow(iris))) |
||
102 | +49 |
- #' shinyApp(ui, server)+ #' new_mtcars <- transform(mtcars, id = seq_len(nrow(mtcars))) |
||
103 | +50 |
- #' }+ #' } |
||
104 | +51 |
- #'+ #' ), |
||
105 | +52 |
- #' @export+ #' modules = modules( |
||
106 | +53 |
- #'+ #' module( |
||
107 | +54 |
- validate_inputs <- function(..., header = "Some inputs require attention") {+ #' label = "data source", |
||
108 | -36x - | +|||
55 | +
- dots <- list(...)+ #' server = function(input, output, session, data) {}, |
|||
109 | -2x - | +|||
56 | +
- if (!is_validators(dots)) stop("validate_inputs accepts validators or a list thereof")+ #' ui = function(id, ...) tags$div(p("information about data source")), |
|||
110 | +57 |
-
+ #' datanames = "all" |
||
111 | -34x - | +|||
58 | +
- messages <- extract_validator(dots, header)+ #' ), |
|||
112 | -34x - | +|||
59 | +
- failings <- if (!any_names(dots)) {+ #' example_module(label = "example teal module"), |
|||
113 | -29x - | +|||
60 | +
- add_header(messages, header)+ #' module( |
|||
114 | +61 |
- } else {+ #' "Iris Sepal.Length histogram", |
||
115 | -5x - | +|||
62 | +
- unlist(messages)+ #' server = function(input, output, session, data) { |
|||
116 | +63 |
- }+ #' output$hist <- renderPlot( |
||
117 | +64 |
-
+ #' hist(data()[["new_iris"]]$Sepal.Length) |
||
118 | -34x - | +|||
65 | +
- shiny::validate(shiny::need(is.null(failings), failings))+ #' ) |
|||
119 | +66 |
- }+ #' }, |
||
120 | +67 |
-
+ #' ui = function(id, ...) { |
||
121 | +68 |
- ### internal functions+ #' ns <- NS(id) |
||
122 | +69 |
-
+ #' plotOutput(ns("hist")) |
||
123 | +70 |
- #' @noRd+ #' }, |
||
124 | +71 |
- #' @keywords internal+ #' datanames = "new_iris" |
||
125 | +72 |
- # recursive object type test+ #' ) |
||
126 | +73 |
- # returns logical of length 1+ #' ), |
||
127 | +74 |
- is_validators <- function(x) {+ #' filter = teal_slices( |
||
128 | -118x - | +|||
75 | +
- all(if (is.list(x)) unlist(lapply(x, is_validators)) else inherits(x, "InputValidator"))+ #' teal_slice(dataname = "new_iris", varname = "Species"), |
|||
129 | +76 |
- }+ #' teal_slice(dataname = "new_iris", varname = "Sepal.Length"), |
||
130 | +77 |
-
+ #' teal_slice(dataname = "new_mtcars", varname = "cyl"), |
||
131 | +78 |
- #' @noRd+ #' exclude_varnames = list(new_iris = c("Sepal.Width", "Petal.Width")), |
||
132 | +79 |
- #' @keywords internal+ #' module_specific = TRUE, |
||
133 | +80 |
- # test if an InputValidator object is enabled+ #' mapping = list( |
||
134 | +81 |
- # returns logical of length 1+ #' `example teal module` = "new_iris Species", |
||
135 | +82 |
- # official method requested at https://github.com/rstudio/shinyvalidate/issues/64+ #' `Iris Sepal.Length histogram` = "new_iris Species", |
||
136 | +83 |
- validator_enabled <- function(x) {+ #' global_filters = "new_mtcars cyl" |
||
137 | -49x - | +|||
84 | +
- x$.__enclos_env__$private$enabled+ #' ) |
|||
138 | +85 |
- }+ #' ), |
||
139 | +86 |
-
+ #' title = "App title", |
||
140 | +87 |
- #' Recursively extract messages from validator list+ #' header = tags$h1("Sample App"), |
||
141 | +88 |
- #' @return A character vector or a list of character vectors, possibly nested and named.+ #' footer = tags$p("Sample footer") |
||
142 | +89 |
- #' @noRd+ #' ) |
||
143 | +90 |
- #' @keywords internal+ #' if (interactive()) { |
||
144 | +91 |
- extract_validator <- function(iv, header) {+ #' shinyApp(app$ui, app$server) |
||
145 | -113x - | +|||
92 | +
- if (inherits(iv, "InputValidator")) {+ #' } |
|||
146 | -49x - | +|||
93 | +
- add_header(gather_messages(iv), header)+ #' |
|||
147 | +94 |
- } else {+ init <- function(data, |
||
148 | -58x - | +|||
95 | +
- if (is.null(names(iv))) names(iv) <- rep("", length(iv))+ modules, |
|||
149 | -64x - | +|||
96 | +
- mapply(extract_validator, iv = iv, header = names(iv), SIMPLIFY = FALSE)+ filter = teal_slices(), |
|||
150 | +97 |
- }+ title = build_app_title(), |
||
151 | +98 |
- }+ header = tags$p(), |
||
152 | +99 |
-
+ footer = tags$p(), |
||
153 | +100 |
- #' Collate failing messages from validator.+ id = character(0), |
||
154 | +101 |
- #' @return `list`+ landing_popup = NULL) {+ |
+ ||
102 | +12x | +
+ logger::log_debug("init initializing teal app with: data ('{ class(data) }').") |
||
155 | +103 |
- #' @noRd+ |
||
156 | +104 |
- #' @keywords internal+ # argument checking (independent) |
||
157 | +105 |
- gather_messages <- function(iv) {+ ## `data` |
||
158 | -49x - | +106 | +12x |
- if (validator_enabled(iv)) {+ if (inherits(data, "TealData")) { |
159 | -46x - | +|||
107 | +! |
- status <- iv$validate()+ lifecycle::deprecate_stop( |
||
160 | -46x - | +|||
108 | +! |
- failing_inputs <- Filter(Negate(is.null), status)+ when = "0.15.0", |
||
161 | -46x - | +|||
109 | +! |
- unique(lapply(failing_inputs, function(x) x[["message"]]))+ what = "init(data)", |
||
162 | -+ | |||
110 | +! |
- } else {+ paste( |
||
163 | -3x - | +|||
111 | +! |
- warning("Validator is disabled and will be omitted.")+ "TealData is no longer supported. Use teal_data() instead.", |
||
164 | -3x - | +|||
112 | +! |
- list()+ "Please follow migration instructions https://github.com/insightsengineering/teal/discussions/988." |
||
165 | +113 |
- }+ ) |
||
166 | +114 |
- }+ ) |
||
167 | +115 |
-
+ } |
||
168 | -+ | |||
116 | +12x |
- #' Add optional header to failing messages+ checkmate::assert_multi_class(data, c("teal_data", "teal_data_module")) |
||
169 | -+ | |||
117 | +12x |
- #' @noRd+ checkmate::assert_class(landing_popup, "teal_module_landing", null.ok = TRUE) |
||
170 | +118 |
- #' @keywords internal+ |
||
171 | +119 |
- add_header <- function(messages, header = "") {+ ## `modules` |
||
172 | -78x - | +120 | +12x |
- ans <- unlist(messages)+ checkmate::assert( |
173 | -78x - | +121 | +12x |
- if (length(ans) != 0L && isTRUE(nchar(header) > 0L)) {+ .var.name = "modules", |
174 | -31x - | +122 | +12x | +
+ checkmate::check_multi_class(modules, c("teal_modules", "teal_module")),+ |
+
123 | +12x | +
+ checkmate::check_list(modules, min.len = 1, any.missing = FALSE, types = c("teal_module", "teal_modules"))+ |
+ ||
124 | ++ |
+ )+ |
+ ||
125 | +12x | +
+ if (inherits(modules, "teal_module")) {+ |
+ ||
126 | +1x |
- ans <- c(paste0(header, "\n"), ans, "\n")+ modules <- list(modules) |
||
175 | +127 |
} |
||
176 | -78x - | +128 | +12x |
- ans+ if (checkmate::test_list(modules, min.len = 1, any.missing = FALSE, types = c("teal_module", "teal_modules"))) {+ |
+
129 | +6x | +
+ modules <- do.call(teal::modules, modules) |
||
177 | +130 |
- }+ } |
||
178 | +131 | |||
179 | +132 |
- #' Recursively check if the object contains a named list+ ## `filter` |
||
180 | -+ | |||
133 | +12x |
- #' @noRd+ checkmate::assert_class(filter, "teal_slices") |
||
181 | +134 |
- #' @keywords internal+ |
||
182 | +135 |
- any_names <- function(x) {+ ## all other arguments |
||
183 | -103x - | +136 | +11x |
- any(+ checkmate::assert( |
184 | -103x - | +137 | +11x |
- if (is.list(x)) {+ .var.name = "title", |
185 | -58x - | -
- if (!is.null(names(x)) && any(names(x) != "")) TRUE else unlist(lapply(x, any_names))- |
- ||
186 | -+ | 138 | +11x |
- } else {+ checkmate::check_string(title), |
187 | -40x - | +139 | +11x |
- FALSE+ checkmate::check_multi_class(title, c("shiny.tag", "shiny.tag.list", "html")) |
188 | +140 |
- }+ ) |
||
189 | -+ | |||
141 | +11x |
- )+ checkmate::assert( |
||
190 | -+ | |||
142 | +11x |
- }+ .var.name = "header", |
1 | -+ | ||
143 | +11x |
- #' Validate that dataset has a minimum number of observations+ checkmate::check_string(header), |
|
2 | -+ | ||
144 | +11x |
- #'+ checkmate::check_multi_class(header, c("shiny.tag", "shiny.tag.list", "html")) |
|
3 | +145 |
- #' `r lifecycle::badge("stable")`+ ) |
|
4 | -+ | ||
146 | +11x |
- #'+ checkmate::assert( |
|
5 | -+ | ||
147 | +11x |
- #' This function is a wrapper for `shiny::validate`.+ .var.name = "footer", |
|
6 | -+ | ||
148 | +11x |
- #'+ checkmate::check_string(footer), |
|
7 | -+ | ||
149 | +11x |
- #' @param x (`data.frame`)+ checkmate::check_multi_class(footer, c("shiny.tag", "shiny.tag.list", "html")) |
|
8 | +150 |
- #' @param min_nrow (`numeric(1)`) Minimum allowed number of rows in `x`.+ ) |
|
9 | -+ | ||
151 | +11x |
- #' @param complete (`logical(1)`) Flag specifying whether to check only complete cases. Defaults to `FALSE`.+ checkmate::assert_character(id, max.len = 1, any.missing = FALSE) |
|
10 | +152 |
- #' @param allow_inf (`logical(1)`) Flag specifying whether to allow infinite values. Defaults to `TRUE`.+ |
|
11 | +153 |
- #' @param msg (`character(1)`) Additional message to display alongside the default message.+ # log |
|
12 | -+ | ||
154 | +11x |
- #'+ teal.logger::log_system_info() |
|
13 | +155 |
- #' @export+ |
|
14 | +156 |
- #'+ # argument transformations |
|
15 | +157 |
- #' @examples+ ## `modules` - landing module |
|
16 | -+ | ||
158 | +11x |
- #' library(teal)+ landing <- extract_module(modules, "teal_module_landing") |
|
17 | -+ | ||
159 | +11x |
- #' ui <- fluidPage(+ if (length(landing) == 1L) { |
|
18 | -+ | ||
160 | +! |
- #' sliderInput("len", "Max Length of Sepal",+ landing_popup <- landing[[1L]] |
|
19 | -+ | ||
161 | +! |
- #' min = 4.3, max = 7.9, value = 5+ modules <- drop_module(modules, "teal_module_landing") |
|
20 | -+ | ||
162 | +! |
- #' ),+ lifecycle::deprecate_soft( |
|
21 | -+ | ||
163 | +! |
- #' plotOutput("plot")+ when = "0.15.3", |
|
22 | -+ | ||
164 | +! |
- #' )+ what = "landing_popup_module()", |
|
23 | -+ | ||
165 | +! |
- #'+ details = paste( |
|
24 | -+ | ||
166 | +! |
- #' server <- function(input, output) {+ "Pass `landing_popup_module` to the `landing_popup` argument of the `init` ", |
|
25 | -+ | ||
167 | +! |
- #' output$plot <- renderPlot({+ "instead of wrapping it into `modules()` and passing to the `modules` argument" |
|
26 | +168 |
- #' iris_df <- iris[iris$Sepal.Length <= input$len, ]+ ) |
|
27 | +169 |
- #' validate_has_data(+ ) |
|
28 | -+ | ||
170 | +11x |
- #' iris_df,+ } else if (length(landing) > 1L) { |
|
29 | -+ | ||
171 | +! |
- #' min_nrow = 10,+ stop("Only one `landing_popup_module` can be used.") |
|
30 | +172 |
- #' complete = FALSE,+ } |
|
31 | +173 |
- #' msg = "Please adjust Max Length of Sepal"+ |
|
32 | +174 |
- #' )+ ## `filter` - set app_id attribute unless present (when restoring bookmark) |
|
33 | -+ | ||
175 | +11x |
- #'+ if (is.null(attr(filter, "app_id", exact = TRUE))) attr(filter, "app_id") <- create_app_id(data, modules) |
|
34 | +176 |
- #' hist(iris_df$Sepal.Length, breaks = 5)+ |
|
35 | +177 |
- #' })+ ## `filter` - convert teal.slice::teal_slices to teal::teal_slices |
|
36 | -+ | ||
178 | +11x |
- #' }+ filter <- as.teal_slices(as.list(filter)) |
|
37 | +179 |
- #' if (interactive()) {+ |
|
38 | +180 |
- #' shinyApp(ui, server)+ # argument checking (interdependent) |
|
39 | +181 |
- #' }+ ## `filter` - `modules` |
|
40 | -+ | ||
182 | +11x |
- #'+ if (isTRUE(attr(filter, "module_specific"))) { |
|
41 | -+ | ||
183 | +! |
- validate_has_data <- function(x,+ module_names <- unlist(c(module_labels(modules), "global_filters")) |
|
42 | -+ | ||
184 | +! |
- min_nrow = NULL,+ failed_mod_names <- setdiff(names(attr(filter, "mapping")), module_names) |
|
43 | -+ | ||
185 | +! |
- complete = FALSE,+ if (length(failed_mod_names)) { |
|
44 | -+ | ||
186 | +! |
- allow_inf = TRUE,+ stop( |
|
45 | -+ | ||
187 | +! |
- msg = NULL) {+ sprintf( |
|
46 | -17x - | +||
188 | +! |
- checkmate::assert_string(msg, null.ok = TRUE)+ "Some module names in the mapping arguments don't match module labels.\n %s not in %s", |
|
47 | -15x - | +||
189 | +! |
- checkmate::assert_data_frame(x)+ toString(failed_mod_names), |
|
48 | -15x - | +||
190 | +! |
- if (!is.null(min_nrow)) {+ toString(unique(module_names)) |
|
49 | -15x - | +||
191 | +
- if (complete) {+ ) |
||
50 | -5x - | +||
192 | +
- complete_index <- stats::complete.cases(x)+ ) |
||
51 | -5x - | +||
193 | +
- validate(need(+ } |
||
52 | -5x - | +||
194 | +
- sum(complete_index) > 0 && nrow(x[complete_index, , drop = FALSE]) >= min_nrow,+ |
||
53 | -5x - | +||
195 | +! |
- paste(c(paste("Number of complete cases is less than:", min_nrow), msg), collapse = "\n")+ if (anyDuplicated(module_names)) { |
|
54 | +196 |
- ))+ # In teal we are able to set nested modules with duplicated label. |
|
55 | +197 |
- } else {+ # Because mapping argument bases on the relationship between module-label and filter-id, |
|
56 | -10x - | +||
198 | +
- validate(need(+ # it is possible that module-label in mapping might refer to multiple teal_module (identified by the same label) |
||
57 | -10x - | +||
199 | +! |
- nrow(x) >= min_nrow,+ stop( |
|
58 | -10x - | +||
200 | +! |
- paste(+ sprintf( |
|
59 | -10x - | +||
201 | +! |
- c(paste("Minimum number of records not met: >=", min_nrow, "records required."), msg),+ "Module labels should be unique when teal_slices(mapping = TRUE). Duplicated labels:\n%s ", |
|
60 | -10x - | +||
202 | +! |
- collapse = "\n"+ toString(module_names[duplicated(module_names)]) |
|
61 | +203 |
) |
|
62 | +204 |
- ))+ ) |
|
63 | +205 |
} |
|
64 | +206 | - - | -|
65 | -10x - | -
- if (!allow_inf) {- |
- |
66 | -6x - | -
- validate(need(- |
- |
67 | -6x - | -
- all(vapply(x, function(col) !is.numeric(col) || !any(is.infinite(col)), logical(1))),- |
- |
68 | -6x - | -
- "Dataframe contains Inf values which is not allowed."+ } |
|
69 | +207 |
- ))+ |
|
70 | +208 |
- }- |
- |
71 | -+ | ||
209 | +11x |
- }+ if (inherits(data, "teal_data")) { |
|
72 | -+ | ||
210 | +10x |
- }+ if (length(ls(teal.code::get_env(data))) == 0) { |
|
73 | -+ | ||
211 | +1x |
-
+ stop("The environment of `data` is empty.") |
|
74 | +212 |
- #' Validate that dataset has unique rows for key variables+ } |
|
75 | +213 |
- #'+ |
|
76 | -+ | ||
214 | +9x |
- #' `r lifecycle::badge("stable")`+ is_modules_ok <- check_modules_datanames(modules, ls(teal.code::get_env(data))) |
|
77 | -+ | ||
215 | +9x |
- #'+ if (!isTRUE(is_modules_ok) && length(unlist(extract_transformers(modules))) == 0) { |
|
78 | -+ | ||
216 | +2x |
- #' This function is a wrapper for `shiny::validate`.+ warning(is_modules_ok, call. = FALSE) |
|
79 | +217 |
- #'+ } |
|
80 | +218 |
- #' @param x (`data.frame`)+ |
|
81 | -+ | ||
219 | +9x |
- #' @param key (`character`) Vector of ID variables from `x` that identify unique records.+ is_filter_ok <- check_filter_datanames(filter, ls(teal.code::get_env(data))) |
|
82 | -+ | ||
220 | +9x |
- #'+ if (!isTRUE(is_filter_ok)) { |
|
83 | -+ | ||
221 | +1x |
- #' @export+ warning(is_filter_ok) |
|
84 | +222 |
- #'+ # we allow app to continue if applied filters are outside |
|
85 | +223 |
- #' @examples+ # of possible data range |
|
86 | +224 |
- #' iris$id <- rep(1:50, times = 3)+ } |
|
87 | +225 |
- #' ui <- fluidPage(+ } |
|
88 | +226 |
- #' selectInput(+ |
|
89 | -+ | ||
227 | +10x |
- #' inputId = "species",+ reporter <- teal.reporter::Reporter$new()$set_id(attr(filter, "app_id")) |
|
90 | -+ | ||
228 | +10x |
- #' label = "Select species",+ if (is_arg_used(modules, "reporter") && length(extract_module(modules, "teal_module_previewer")) == 0) { |
|
91 | -+ | ||
229 | +! |
- #' choices = c("setosa", "versicolor", "virginica"),+ modules <- append_module( |
|
92 | -+ | ||
230 | +! |
- #' selected = "setosa",+ modules, |
|
93 | -+ | ||
231 | +! |
- #' multiple = TRUE+ reporter_previewer_module(server_args = list(previewer_buttons = c("download", "reset"))) |
|
94 | +232 |
- #' ),+ ) |
|
95 | +233 |
- #' plotOutput("plot")+ } |
|
96 | +234 |
- #' )+ |
|
97 | -+ | ||
235 | +10x |
- #' server <- function(input, output) {+ ns <- NS(id) |
|
98 | +236 |
- #' output$plot <- renderPlot({+ # Note: UI must be a function to support bookmarking. |
|
99 | -+ | ||
237 | +10x |
- #' iris_f <- iris[iris$Species %in% input$species, ]+ res <- list( |
|
100 | -+ | ||
238 | +10x |
- #' validate_one_row_per_id(iris_f, key = c("id"))+ ui = function(request) { |
|
101 | -+ | ||
239 | +! |
- #'+ ui_teal( |
|
102 | -+ | ||
240 | +! |
- #' hist(iris_f$Sepal.Length, breaks = 5)+ id = ns("teal"), |
|
103 | -+ | ||
241 | +! |
- #' })+ modules = modules, |
|
104 | -+ | ||
242 | +! |
- #' }+ title = title, |
|
105 | -+ | ||
243 | +! |
- #' if (interactive()) {+ header = header, |
|
106 | -+ | ||
244 | +! |
- #' shinyApp(ui, server)+ footer = footer |
|
107 | +245 |
- #' }+ ) |
|
108 | +246 |
- #'+ }, |
|
109 | -+ | ||
247 | +10x |
- validate_one_row_per_id <- function(x, key = c("USUBJID", "STUDYID")) {+ server = function(input, output, session) { |
|
110 | +248 | ! |
- validate(need(!any(duplicated(x[key])), paste("Found more than one row per id.")))+ if (!is.null(landing_popup)) { |
111 | -+ | ||
249 | +! |
- }+ do.call(landing_popup$server, c(list(id = "landing_module_shiny_id"), landing_popup$server_args)) |
|
112 | +250 |
-
+ } |
|
113 | -+ | ||
251 | +! |
- #' Validates that vector includes all expected values+ srv_teal(id = ns("teal"), data = data, modules = modules, filter = deep_copy_filter(filter)) |
|
114 | +252 |
- #'+ } |
|
115 | +253 |
- #' `r lifecycle::badge("stable")`+ ) |
|
116 | +254 |
- #'+ |
|
117 | -+ | ||
255 | +10x |
- #' This function is a wrapper for `shiny::validate`.+ logger::log_debug("init teal app has been initialized.") |
|
118 | +256 |
- #'+ |
|
119 | -+ | ||
257 | +10x |
- #' @param x Vector of values to test.+ res |
|
120 | +258 |
- #' @param choices Vector to test against.+ } |
121 | +1 |
- #' @param msg (`character(1)`) Error message to display if some elements of `x` are not elements of `choices`.+ #' Data module for `teal` applications |
|
122 | +2 |
#' |
|
123 | +3 |
- #' @export+ #' @description |
|
124 | +4 |
- #'+ #' `r lifecycle::badge("experimental")` |
|
125 | +5 |
- #' @examples+ #' |
|
126 | +6 |
- #' ui <- fluidPage(+ #' Create a `teal_data_module` object and evaluate code on it with history tracking. |
|
127 | +7 |
- #' selectInput(+ #' |
|
128 | +8 |
- #' "species",+ #' @details |
|
129 | +9 |
- #' "Select species",+ #' `teal_data_module` creates a `shiny` module to interactively supply or modify data in a `teal` application. |
|
130 | +10 |
- #' choices = c("setosa", "versicolor", "virginica", "unknown species"),+ #' The module allows for running any code (creation _and_ some modification) after the app starts or reloads. |
|
131 | +11 |
- #' selected = "setosa",+ #' The body of the server function will be run in the app rather than in the global environment. |
|
132 | +12 |
- #' multiple = FALSE+ #' This means it will be run every time the app starts, so use sparingly. |
|
133 | +13 |
- #' ),+ #' |
|
134 | +14 |
- #' verbatimTextOutput("summary")+ #' Pass this module instead of a `teal_data` object in a call to [init()]. |
|
135 | +15 |
- #' )+ #' Note that the server function must always return a `teal_data` object wrapped in a reactive expression. |
|
136 | +16 |
#' |
|
137 | +17 |
- #' server <- function(input, output) {+ #' See vignette `vignette("data-as-shiny-module", package = "teal")` for more details. |
|
138 | +18 |
- #' output$summary <- renderPrint({+ #' |
|
139 | +19 |
- #' validate_in(input$species, iris$Species, "Species does not exist.")+ #' @param ui (`function(id)`) |
|
140 | +20 |
- #' nrow(iris[iris$Species == input$species, ])+ #' `shiny` module UI function; must only take `id` argument |
|
141 | +21 |
- #' })+ #' @param server (`function(id)`) |
|
142 | +22 |
- #' }+ #' `shiny` module server function; must only take `id` argument; |
|
143 | +23 |
- #' if (interactive()) {+ #' must return reactive expression containing `teal_data` object |
|
144 | +24 |
- #' shinyApp(ui, server)+ #' @param label (`character(1)`) Label of the module. |
|
145 | +25 |
- #' }+ #' @param once (`logical(1)`) |
|
146 | +26 |
- #'+ #' If `TRUE`, the data module will be shown only once and will disappear after successful data loading. |
|
147 | +27 |
- validate_in <- function(x, choices, msg) {+ #' App user will no longer be able to interact with this module anymore. |
|
148 | -! | +||
28 | +
- validate(need(length(x) > 0 && length(choices) > 0 && all(x %in% choices), msg))+ #' If `FALSE`, the data module can be reused multiple times. |
||
149 | +29 |
- }+ #' App user will be able to interact and change the data output from the module multiple times. |
|
150 | +30 |
-
+ #' |
|
151 | +31 |
- #' Validates that vector has length greater than 0+ #' @return |
|
152 | +32 |
- #'+ #' `teal_data_module` returns a list of class `teal_data_module` containing two elements, `ui` and |
|
153 | +33 |
- #' `r lifecycle::badge("stable")`+ #' `server` provided via arguments. |
|
154 | +34 |
#' |
|
155 | +35 |
- #' This function is a wrapper for `shiny::validate`.+ #' @examples |
|
156 | +36 |
- #'+ #' tdm <- teal_data_module( |
|
157 | +37 |
- #' @param x vector+ #' ui = function(id) { |
|
158 | +38 |
- #' @param msg message to display+ #' ns <- NS(id) |
|
159 | +39 |
- #'+ #' actionButton(ns("submit"), label = "Load data") |
|
160 | +40 |
- #' @export+ #' }, |
|
161 | +41 |
- #'+ #' server = function(id) { |
|
162 | +42 |
- #' @examples+ #' moduleServer(id, function(input, output, session) { |
|
163 | +43 |
- #' data <- data.frame(+ #' eventReactive(input$submit, { |
|
164 | +44 |
- #' id = c(1:10, 11:20, 1:10),+ #' data <- within( |
|
165 | +45 |
- #' strata = rep(c("A", "B"), each = 15)+ #' teal_data(), |
|
166 | +46 |
- #' )+ #' { |
|
167 | +47 |
- #' ui <- fluidPage(+ #' dataset1 <- iris |
|
168 | +48 |
- #' selectInput("ref1", "Select strata1 to compare",+ #' dataset2 <- mtcars |
|
169 | +49 |
- #' choices = c("A", "B", "C"), selected = "A"+ #' } |
|
170 | +50 |
- #' ),+ #' ) |
|
171 | +51 |
- #' selectInput("ref2", "Select strata2 to compare",+ #' datanames(data) <- c("dataset1", "dataset2") |
|
172 | +52 |
- #' choices = c("A", "B", "C"), selected = "B"+ #' |
|
173 | +53 |
- #' ),+ #' data |
|
174 | +54 |
- #' verbatimTextOutput("arm_summary")+ #' }) |
|
175 | +55 |
- #' )+ #' }) |
|
176 | +56 |
- #'+ #' } |
|
177 | +57 |
- #' server <- function(input, output) {+ #' ) |
|
178 | +58 |
- #' output$arm_summary <- renderText({+ #' |
|
179 | +59 |
- #' sample_1 <- data$id[data$strata == input$ref1]+ #' @name teal_data_module |
|
180 | +60 |
- #' sample_2 <- data$id[data$strata == input$ref2]+ #' @seealso [`teal.data::teal_data-class`], [teal.code::qenv()] |
|
181 | +61 |
#' |
|
182 | +62 |
- #' validate_has_elements(sample_1, "No subjects in strata1.")+ #' @export |
|
183 | +63 |
- #' validate_has_elements(sample_2, "No subjects in strata2.")+ teal_data_module <- function(ui, server, label = "data module", once = TRUE) { |
|
184 | -+ | ||
64 | +33x |
- #'+ checkmate::assert_function(ui, args = "id", nargs = 1) |
|
185 | -+ | ||
65 | +32x |
- #' paste0(+ checkmate::assert_function(server, args = "id", nargs = 1) |
|
186 | -+ | ||
66 | +30x |
- #' "Number of samples in: strata1=", length(sample_1),+ checkmate::assert_string(label) |
|
187 | -+ | ||
67 | +30x |
- #' " comparions strata2=", length(sample_2)+ checkmate::assert_flag(once) |
|
188 | -+ | ||
68 | +30x |
- #' )+ structure( |
|
189 | -+ | ||
69 | +30x |
- #' })+ list( |
|
190 | -+ | ||
70 | +30x |
- #' }+ ui = ui, |
|
191 | -+ | ||
71 | +30x |
- #' if (interactive()) {+ server = function(id) { |
|
192 | -+ | ||
72 | +23x |
- #' shinyApp(ui, server)+ data_out <- server(id) |
|
193 | -+ | ||
73 | +22x |
- #' }+ decorate_err_msg( |
|
194 | -+ | ||
74 | +22x |
- validate_has_elements <- function(x, msg) {+ assert_reactive(data_out), |
|
195 | -! | +||
75 | +22x |
- validate(need(length(x) > 0, msg))+ pre = sprintf("From: 'teal_data_module()':\nA 'teal_data_module' with \"%s\" label:", label), |
|
196 | -+ | ||
76 | +22x |
- }+ post = "Please make sure that this module returns a 'reactive` object containing 'teal_data' class of object." # nolint: line_length_linter. |
|
197 | +77 |
-
+ ) |
|
198 | +78 |
- #' Validates no intersection between two vectors+ } |
|
199 | +79 |
- #'+ ), |
|
200 | -+ | ||
80 | +30x |
- #' `r lifecycle::badge("stable")`+ label = label, |
|
201 | -+ | ||
81 | +30x |
- #'+ class = "teal_data_module", |
|
202 | -+ | ||
82 | +30x |
- #' This function is a wrapper for `shiny::validate`.+ once = once |
|
203 | +83 |
- #'+ ) |
|
204 | +84 |
- #' @param x vector+ } |
|
205 | +85 |
- #' @param y vector+ |
|
206 | +86 |
- #' @param msg (`character(1)`) message to display if `x` and `y` intersect+ #' Data module for `teal` transformers. |
|
207 | +87 |
#' |
|
208 | +88 |
- #' @export+ #' @description |
|
209 | +89 |
- #'+ #' `r lifecycle::badge("experimental")` |
|
210 | +90 |
- #' @examples+ #' |
|
211 | +91 |
- #' data <- data.frame(+ #' Create a `teal_data_module` object for custom transformation of data for pre-processing |
|
212 | +92 |
- #' id = c(1:10, 11:20, 1:10),+ #' before passing the data into the module. |
|
213 | +93 |
- #' strata = rep(c("A", "B", "C"), each = 10)+ #' |
|
214 | +94 |
- #' )+ #' @details |
|
215 | +95 |
- #'+ #' `teal_transform_module` creates a [`teal_data_module`] object to transform data in a `teal` |
|
216 | +96 |
- #' ui <- fluidPage(+ #' application. This transformation happens after the data has passed through the filtering activity |
|
217 | +97 |
- #' selectInput("ref1", "Select strata1 to compare",+ #' in teal. The transformed data is then sent to the server of the [teal_module()]. |
|
218 | +98 |
- #' choices = c("A", "B", "C"),+ #' |
|
219 | +99 |
- #' selected = "A"+ #' See vignette `vignette("data-transform-as-shiny-module", package = "teal")` for more details. |
|
220 | +100 |
- #' ),+ #' |
|
221 | +101 |
- #' selectInput("ref2", "Select strata2 to compare",+ #' |
|
222 | +102 |
- #' choices = c("A", "B", "C"),+ #' @inheritParams teal_data_module |
|
223 | +103 |
- #' selected = "B"+ #' @param server (`function(id, data)`) |
|
224 | +104 |
- #' ),+ #' `shiny` module server function; that takes `id` and `data` argument, |
|
225 | +105 |
- #' verbatimTextOutput("summary")+ #' where the `id` is the module id and `data` is the reactive `teal_data` input. |
|
226 | +106 |
- #' )+ #' The server function must return reactive expression containing `teal_data` object. |
|
227 | +107 |
#' |
|
228 | +108 |
- #' server <- function(input, output) {+ #' The server function definition should not use `eventReactive` as it may lead to |
|
229 | +109 |
- #' output$summary <- renderText({+ #' unexpected behavior. |
|
230 | +110 |
- #' sample_1 <- data$id[data$strata == input$ref1]+ #' See `vignettes("data-transform-as-shiny-module")` for more information. |
|
231 | +111 |
- #' sample_2 <- data$id[data$strata == input$ref2]+ #' @param datanames (`character`) |
|
232 | +112 |
- #'+ #' Names of the datasets that are relevant for this module to evaluate. If set to `character(0)` |
|
233 | +113 |
- #' validate_no_intersection(+ #' then module would receive [modules()] `datanames`. |
|
234 | +114 |
- #' sample_1, sample_2,+ #' @examples |
|
235 | +115 |
- #' "subjects within strata1 and strata2 cannot overlap"+ #' my_transformers <- list( |
|
236 | +116 |
- #' )+ #' teal_transform_module( |
|
237 | +117 |
- #' paste0(+ #' label = "Custom transform for iris", |
|
238 | +118 |
- #' "Number of subject in: reference treatment=", length(sample_1),+ #' datanames = "iris", |
|
239 | +119 |
- #' " comparions treatment=", length(sample_2)+ #' ui = function(id) { |
|
240 | +120 |
- #' )+ #' ns <- NS(id) |
|
241 | +121 |
- #' })+ #' tags$div( |
|
242 | +122 |
- #' }+ #' numericInput(ns("n_rows"), "Subset n rows", value = 6, min = 1, max = 150, step = 1) |
|
243 | +123 |
- #' if (interactive()) {+ #' ) |
|
244 | +124 |
- #' shinyApp(ui, server)+ #' }, |
|
245 | +125 |
- #' }+ #' server = function(id, data) { |
|
246 | +126 |
- #'+ #' moduleServer(id, function(input, output, session) { |
|
247 | +127 |
- validate_no_intersection <- function(x, y, msg) {+ #' reactive({ |
|
248 | -! | +||
128 | +
- validate(need(length(intersect(x, y)) == 0, msg))+ #' within(data(), |
||
249 | +129 |
- }+ #' { |
|
250 | +130 |
-
+ #' iris <- head(iris, num_rows) |
|
251 | +131 |
-
+ #' }, |
|
252 | +132 |
- #' Validates that dataset contains specific variable+ #' num_rows = input$n_rows |
|
253 | +133 |
- #'+ #' ) |
|
254 | +134 |
- #' `r lifecycle::badge("stable")`+ #' }) |
|
255 | +135 |
- #'+ #' }) |
|
256 | +136 |
- #' This function is a wrapper for `shiny::validate`.+ #' } |
|
257 | +137 |
- #'+ #' ) |
|
258 | +138 |
- #' @param data (`data.frame`)+ #' ) |
|
259 | +139 |
- #' @param varname (`character(1)`) name of variable to check for in `data`+ #' |
|
260 | +140 |
- #' @param msg (`character(1)`) message to display if `data` does not include `varname`+ #' @name teal_transform_module |
|
261 | +141 |
#' |
|
262 | +142 |
#' @export |
|
263 | +143 |
- #'+ teal_transform_module <- function(ui = function(id) NULL, |
|
264 | +144 |
- #' @examples+ server = function(id, data) data, |
|
265 | +145 |
- #' data <- data.frame(+ label = "transform module", |
|
266 | +146 |
- #' one = rep("a", length.out = 20),+ datanames = character(0)) { |
|
267 | -+ | ||
147 | +19x |
- #' two = rep(c("a", "b"), length.out = 20)+ checkmate::assert_function(ui, args = "id", nargs = 1) |
|
268 | -+ | ||
148 | +19x |
- #' )+ checkmate::assert_function(server, args = c("id", "data"), nargs = 2) |
|
269 | -+ | ||
149 | +19x |
- #' ui <- fluidPage(+ checkmate::assert_string(label) |
|
270 | -+ | ||
150 | +19x |
- #' selectInput(+ checkmate::assert_character(datanames) |
|
271 | -+ | ||
151 | +19x |
- #' "var",+ if (identical(datanames, "all")) { |
|
272 | -+ | ||
152 | +1x |
- #' "Select variable",+ stop( |
|
273 | -+ | ||
153 | +1x |
- #' choices = c("one", "two", "three", "four"),+ "teal_transform_module can't have datanames property equal to 'all'. Set `datanames = character(0)` instead.", |
|
274 | -+ | ||
154 | +1x |
- #' selected = "one"+ call. = FALSE |
|
275 | +155 |
- #' ),+ ) |
|
276 | +156 |
- #' verbatimTextOutput("summary")+ } |
|
277 | -+ | ||
157 | +18x |
- #' )+ structure( |
|
278 | -+ | ||
158 | +18x |
- #'+ list( |
|
279 | -+ | ||
159 | +18x |
- #' server <- function(input, output) {+ ui = ui, |
|
280 | -+ | ||
160 | +18x |
- #' output$summary <- renderText({+ server = function(id, data) { |
|
281 | -+ | ||
161 | +19x |
- #' validate_has_variable(data, input$var)+ data_out <- server(id, data) |
|
282 | +162 |
- #' paste0("Selected treatment variables: ", paste(input$var, collapse = ", "))+ |
|
283 | -+ | ||
163 | +19x |
- #' })+ if (inherits(data_out, "reactive.event")) { |
|
284 | +164 |
- #' }+ # This warning message partially detects when `eventReactive` is used in `data_module`. |
|
285 | -+ | ||
165 | +1x |
- #' if (interactive()) {+ warning( |
|
286 | -+ | ||
166 | +1x |
- #' shinyApp(ui, server)+ "teal_transform_module() ", |
|
287 | -+ | ||
167 | +1x |
- #' }+ "Using eventReactive in teal_transform module server code should be avoided as it ", |
|
288 | -+ | ||
168 | +1x |
- validate_has_variable <- function(data, varname, msg) {+ "may lead to unexpected behavior. See the vignettes for more information ", |
|
289 | -! | +||
169 | +1x |
- if (length(varname) != 0) {+ "(`vignette(\"data-transform-as-shiny-module\", package = \"teal\")`).", |
|
290 | -! | +||
170 | +1x |
- has_vars <- varname %in% names(data)+ call. = FALSE |
|
291 | +171 |
-
+ ) |
|
292 | -! | +||
172 | +
- if (!all(has_vars)) {+ } |
||
293 | -! | +||
173 | +
- if (missing(msg)) {+ |
||
294 | -! | +||
174 | +19x |
- msg <- sprintf(+ decorate_err_msg( |
|
295 | -! | +||
175 | +19x |
- "%s does not have the required variables: %s.",+ assert_reactive(data_out), |
|
296 | -! | +||
176 | +19x |
- deparse(substitute(data)),+ pre = sprintf("From: 'teal_transform_module()':\nA 'teal_transform_module' with \"%s\" label:", label), |
|
297 | -! | +||
177 | +19x |
- toString(varname[!has_vars])+ post = "Please make sure that this module returns a 'reactive` object containing 'teal_data' class of object." # nolint: line_length_linter. |
|
298 | +178 |
) |
|
299 | +179 |
} |
|
300 | -! | +||
180 | +
- validate(need(FALSE, msg))+ ), |
||
301 | -+ | ||
181 | +18x |
- }+ label = label,+ |
+ |
182 | +18x | +
+ datanames = datanames,+ |
+ |
183 | +18x | +
+ class = c("teal_transform_module", "teal_data_module") |
|
302 | +184 |
- }+ ) |
|
303 | +185 |
} |
|
304 | +186 | ||
305 | +187 |
- #' Validate that variables has expected number of levels+ |
|
306 | +188 |
- #'+ #' Extract all `transformers` from `modules`. |
|
307 | +189 |
- #' `r lifecycle::badge("stable")`+ #' |
|
308 | +190 |
- #'+ #' @param modules `teal_modules` or `teal_module` |
|
309 | +191 |
- #' If the number of levels of `x` is less than `min_levels`+ #' @return A list of `teal_transform_module` nested in the same way as input `modules`. |
|
310 | +192 |
- #' or greater than `max_levels` the validation will fail.+ #' @keywords internal |
|
311 | +193 |
- #' This function is a wrapper for `shiny::validate`.+ extract_transformers <- function(modules) { |
|
312 | -+ | ||
194 | +6x |
- #'+ if (inherits(modules, "teal_module")) { |
|
313 | -+ | ||
195 | +3x |
- #' @param x variable name. If `x` is not a factor, the unique values+ modules$transformers |
|
314 | -+ | ||
196 | +3x |
- #' are treated as levels.+ } else if (inherits(modules, "teal_modules")) { |
|
315 | -+ | ||
197 | +3x |
- #' @param min_levels cutoff for minimum number of levels of `x`+ lapply(modules$children, extract_transformers) |
|
316 | +198 |
- #' @param max_levels cutoff for maximum number of levels of `x`+ } |
|
317 | +199 |
- #' @param var_name name of variable being validated for use in+ } |
318 | +1 |
- #' validation message+ #' An example `teal` module |
|
319 | +2 |
#' |
|
320 | +3 |
- #' @export+ #' `r lifecycle::badge("experimental")` |
|
321 | +4 |
- #' @examples+ #' |
|
322 | +5 |
- #' data <- data.frame(+ #' @inheritParams teal_modules |
|
323 | +6 |
- #' one = rep("a", length.out = 20),+ #' @return A `teal` module which can be included in the `modules` argument to [init()]. |
|
324 | +7 |
- #' two = rep(c("a", "b"), length.out = 20),+ #' @examples |
|
325 | +8 |
- #' three = rep(c("a", "b", "c"), length.out = 20),+ #' app <- init( |
|
326 | +9 |
- #' four = rep(c("a", "b", "c", "d"), length.out = 20),+ #' data = teal_data(IRIS = iris, MTCARS = mtcars), |
|
327 | +10 |
- #' stringsAsFactors = TRUE+ #' modules = example_module() |
|
328 | +11 |
#' ) |
|
329 | +12 |
- #' ui <- fluidPage(+ #' if (interactive()) { |
|
330 | +13 |
- #' selectInput(+ #' shinyApp(app$ui, app$server) |
|
331 | +14 |
- #' "var",+ #' } |
|
332 | +15 |
- #' "Select variable",+ #' @export |
|
333 | +16 |
- #' choices = c("one", "two", "three", "four"),+ example_module <- function(label = "example teal module", datanames = "all", transformers = list()) { |
|
334 | -+ | ||
17 | +38x |
- #' selected = "one"+ checkmate::assert_string(label) |
|
335 | -+ | ||
18 | +38x |
- #' ),+ ans <- module( |
|
336 | -+ | ||
19 | +38x |
- #' verbatimTextOutput("summary")+ label, |
|
337 | -+ | ||
20 | +38x |
- #' )+ server = function(id, data) { |
|
338 | -+ | ||
21 | +2x |
- #'+ checkmate::assert_class(isolate(data()), "teal_data") |
|
339 | -+ | ||
22 | +2x |
- #' server <- function(input, output) {+ moduleServer(id, function(input, output, session) { |
|
340 | -+ | ||
23 | +2x |
- #' output$summary <- renderText({+ datanames_rv <- reactive(ls(teal.code::get_env((req(data()))))) |
|
341 | -+ | ||
24 | +2x |
- #' validate_n_levels(data[[input$var]], min_levels = 2, max_levels = 15, var_name = input$var)+ observeEvent(datanames_rv(), { |
|
342 | -+ | ||
25 | +2x |
- #' paste0(+ selected <- input$dataname |
|
343 | -+ | ||
26 | +2x |
- #' "Levels of selected treatment variable: ",+ if (identical(selected, "")) { |
|
344 | -+ | ||
27 | +! |
- #' paste(levels(data[[input$var]]),+ selected <- restoreInput(session$ns("dataname"), NULL) |
|
345 | -+ | ||
28 | +2x |
- #' collapse = ", "+ } else if (isFALSE(selected %in% datanames_rv())) { |
|
346 | -+ | ||
29 | +! |
- #' )+ selected <- datanames_rv()[1] |
|
347 | +30 |
- #' )+ } |
|
348 | -+ | ||
31 | +2x |
- #' })+ updateSelectInput( |
|
349 | -+ | ||
32 | +2x |
- #' }+ session = session, |
|
350 | -+ | ||
33 | +2x |
- #' if (interactive()) {+ inputId = "dataname", |
|
351 | -+ | ||
34 | +2x |
- #' shinyApp(ui, server)+ choices = datanames_rv(),+ |
+ |
35 | +2x | +
+ selected = selected |
|
352 | +36 |
- #' }+ ) |
|
353 | +37 |
- validate_n_levels <- function(x, min_levels = 1, max_levels = 12, var_name) {+ }) |
|
354 | -! | +||
38 | +
- x_levels <- if (is.factor(x)) {+ |
||
355 | -! | +||
39 | +2x |
- levels(x)+ output$text <- renderPrint({ |
|
356 | -+ | ||
40 | +2x |
- } else {+ req(input$dataname) |
|
357 | +41 | ! |
- unique(x)+ data()[[input$dataname]] |
358 | +42 |
- }+ }) |
|
359 | +43 | ||
360 | -! | +||
44 | +2x |
- if (!is.null(min_levels) && !(is.null(max_levels))) {+ teal.widgets::verbatim_popup_srv( |
|
361 | -! | +||
45 | +2x |
- validate(need(+ id = "rcode", |
|
362 | -! | +||
46 | +2x |
- length(x_levels) >= min_levels && length(x_levels) <= max_levels,+ verbatim_content = reactive(teal.code::get_code(data())), |
|
363 | -! | +||
47 | +2x |
- sprintf(+ title = "Example Code" |
|
364 | -! | +||
48 | +
- "%s variable needs minimum %s level(s) and maximum %s level(s).",+ ) |
||
365 | -! | +||
49 | +
- var_name, min_levels, max_levels+ }) |
||
366 | +50 |
- )+ }, |
|
367 | -+ | ||
51 | +38x |
- ))+ ui = function(id) {+ |
+ |
52 | +! | +
+ ns <- NS(id)+ |
+ |
53 | +! | +
+ teal.widgets::standard_layout( |
|
368 | +54 | ! |
- } else if (!is.null(min_levels)) {+ output = verbatimTextOutput(ns("text")), |
369 | +55 | ! |
- validate(need(+ encoding = tags$div( |
370 | +56 | ! |
- length(x_levels) >= min_levels,+ selectInput(ns("dataname"), "Choose a dataset", choices = NULL), |
371 | +57 | ! |
- sprintf("%s variable needs minimum %s levels(s)", var_name, min_levels)+ teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code") |
372 | +58 |
- ))+ ) |
|
373 | -! | +||
59 | +
- } else if (!is.null(max_levels)) {+ ) |
||
374 | -! | +||
60 | +
- validate(need(+ }, |
||
375 | -! | +||
61 | +38x |
- length(x_levels) <= max_levels,+ datanames = datanames, |
|
376 | -! | +||
62 | +38x |
- sprintf("%s variable needs maximum %s level(s)", var_name, max_levels)+ transformers = transformers |
|
377 | +63 |
- ))+ ) |
|
378 | -+ | ||
64 | +38x |
- }+ attr(ans, "teal_bookmarkable") <- TRUE+ |
+ |
65 | +38x | +
+ ans |
|
379 | +66 |
}@@ -40081,1444 +38403,1458 @@ teal coverage - 59.73% |
1 |
- #' Data module for `teal` applications+ setOldClass("teal_data_module") |
||
2 |
- #'+ |
||
3 |
- #' @description+ #' Evaluate code on `teal_data_module` |
||
4 |
- #' `r lifecycle::badge("experimental")`+ #' |
||
5 |
- #'+ #' @details |
||
6 |
- #' Create a `teal_data_module` object and evaluate code on it with history tracking.+ #' `eval_code` evaluates given code in the environment of the `teal_data` object created by the `teal_data_module`. |
||
7 |
- #'+ #' The code is added to the `@code` slot of the `teal_data`. |
||
8 |
- #' @details+ #' |
||
9 |
- #' `teal_data_module` creates a `shiny` module to interactively supply or modify data in a `teal` application.+ #' @param object (`teal_data_module`) |
||
10 |
- #' The module allows for running any code (creation _and_ some modification) after the app starts or reloads.+ #' @inheritParams teal.code::eval_code |
||
11 |
- #' The body of the server function will be run in the app rather than in the global environment.+ #' |
||
12 |
- #' This means it will be run every time the app starts, so use sparingly.+ #' @return |
||
13 |
- #'+ #' `eval_code` returns a `teal_data_module` object with a delayed evaluation of `code` when the module is run. |
||
14 |
- #' Pass this module instead of a `teal_data` object in a call to [init()].+ #' |
||
15 |
- #' Note that the server function must always return a `teal_data` object wrapped in a reactive expression.+ #' @examples |
||
16 |
- #'+ #' eval_code(tdm, "dataset1 <- subset(dataset1, Species == 'virginica')") |
||
17 |
- #' See vignette `vignette("data-as-shiny-module", package = "teal")` for more details.+ #' |
||
18 |
- #'+ #' @include teal_data_module.R |
||
19 |
- #' @param ui (`function(id)`)+ #' @name eval_code |
||
20 |
- #' `shiny` module UI function; must only take `id` argument+ #' @rdname teal_data_module |
||
21 |
- #' @param server (`function(id)`)+ #' @aliases eval_code,teal_data_module,character-method |
||
22 |
- #' `shiny` module server function; must only take `id` argument;+ #' @aliases eval_code,teal_data_module,language-method |
||
23 |
- #' must return reactive expression containing `teal_data` object+ #' @aliases eval_code,teal_data_module,expression-method |
||
24 |
- #' @param label (`character(1)`) Label of the module.+ #' |
||
25 |
- #' @param once (`logical(1)`)+ #' @importFrom methods setMethod |
||
26 |
- #' If `TRUE`, the data module will be shown only once and will disappear after successful data loading.+ #' @importMethodsFrom teal.code eval_code |
||
27 |
- #' App user will no longer be able to interact with this module anymore.+ #' |
||
28 |
- #' If `FALSE`, the data module can be reused multiple times.+ setMethod("eval_code", signature = c("teal_data_module", "character"), function(object, code) { |
||
29 | -+ | 9x |
- #' App user will be able to interact and change the data output from the module multiple times.+ teal_data_module( |
30 | -+ | 9x |
- #'+ ui = function(id) { |
31 | -+ | 1x |
- #' @return+ ns <- NS(id) |
32 | -+ | 1x |
- #' `teal_data_module` returns a list of class `teal_data_module` containing two elements, `ui` and+ object$ui(ns("mutate_inner")) |
33 |
- #' `server` provided via arguments.+ }, |
||
34 | -+ | 9x |
- #'+ server = function(id) { |
35 | -+ | 7x |
- #' @examples+ moduleServer(id, function(input, output, session) { |
36 | -+ | 7x |
- #' tdm <- teal_data_module(+ teal_data_rv <- object$server("mutate_inner") |
37 | -+ | 6x |
- #' ui = function(id) {+ td <- eventReactive(teal_data_rv(), |
38 |
- #' ns <- NS(id)+ { |
||
39 | -+ | 6x |
- #' actionButton(ns("submit"), label = "Load data")+ if (inherits(teal_data_rv(), c("teal_data", "qenv.error"))) { |
40 | -+ | 4x |
- #' },+ eval_code(teal_data_rv(), code) |
41 |
- #' server = function(id) {+ } else { |
||
42 | -+ | 2x |
- #' moduleServer(id, function(input, output, session) {+ teal_data_rv() |
43 |
- #' eventReactive(input$submit, {+ } |
||
44 |
- #' data <- within(+ }, |
||
45 | -+ | 6x |
- #' teal_data(),+ ignoreNULL = FALSE |
46 |
- #' {+ ) |
||
47 | -+ | 6x |
- #' dataset1 <- iris+ td |
48 |
- #' dataset2 <- mtcars+ }) |
||
49 |
- #' }+ } |
||
50 |
- #' )+ ) |
||
51 |
- #' datanames(data) <- c("dataset1", "dataset2")+ }) |
||
52 |
- #'+ |
||
53 |
- #' data+ setMethod("eval_code", signature = c("teal_data_module", "language"), function(object, code) { |
||
54 | -+ | 1x |
- #' })+ eval_code(object, code = paste(lang2calls(code), collapse = "\n")) |
55 |
- #' })+ }) |
||
56 |
- #' }+ |
||
57 |
- #' )+ setMethod("eval_code", signature = c("teal_data_module", "expression"), function(object, code) { |
||
58 | +2x | +
+ eval_code(object, code = paste(lang2calls(code), collapse = "\n"))+ |
+ |
59 | ++ |
+ })+ |
+
1 | ++ |
+ #' Include `CSS` files from `/inst/css/` package directory to application header+ |
+ |
2 |
#' |
||
59 | +3 |
- #' @name teal_data_module+ #' `system.file` should not be used to access files in other packages, it does |
|
60 | +4 |
- #' @seealso [`teal.data::teal_data-class`], [teal.code::qenv()]+ #' not work with `devtools`. Therefore, we redefine this method in each package |
|
61 | +5 | ++ |
+ #' as needed. Thus, we do not export this method.+ |
+
6 |
#' |
||
62 | +7 |
- #' @export+ #' @param pattern (`character`) pattern of files to be included |
|
63 | +8 |
- teal_data_module <- function(ui, server, label = "data module", once = TRUE) {+ #' |
|
64 | -33x - | +||
9 | +
- checkmate::assert_function(ui, args = "id", nargs = 1)+ #' @return HTML code that includes `CSS` files. |
||
65 | -32x - | +||
10 | +
- checkmate::assert_function(server, args = "id", nargs = 1)+ #' @keywords internal |
||
66 | -30x - | +||
11 | +
- checkmate::assert_string(label)+ include_css_files <- function(pattern = "*") { |
||
67 | -30x - | +||
12 | +! |
- checkmate::assert_flag(once)+ css_files <- list.files( |
|
68 | -30x - | +||
13 | +! |
- structure(+ system.file("css", package = "teal", mustWork = TRUE), |
|
69 | -30x - | +||
14 | +! |
- list(+ pattern = pattern, full.names = TRUE |
|
70 | -30x - | +||
15 | +
- ui = ui,+ ) |
||
71 | -30x - | +||
16 | +
- server = function(id) {+ |
||
72 | -23x - | +||
17 | +! |
- data_out <- server(id)+ singleton( |
|
73 | -22x - | +||
18 | +! |
- decorate_err_msg(+ tags$head(lapply(css_files, includeCSS)) |
|
74 | -22x - | +||
19 | +
- assert_reactive(data_out),+ ) |
||
75 | -22x - | +||
20 | +
- pre = sprintf("From: 'teal_data_module()':\nA 'teal_data_module' with \"%s\" label:", label),+ } |
||
76 | -22x - | +||
21 | +
- post = "Please make sure that this module returns a 'reactive` object containing 'teal_data' class of object." # nolint: line_length_linter.+ |
||
77 | +22 |
- )+ #' Include `JS` files from `/inst/js/` package directory to application header |
|
78 | +23 |
- }+ #' |
|
79 | +24 |
- ),+ #' `system.file` should not be used to access files in other packages, it does |
|
80 | -30x - | +||
25 | +
- label = label,+ #' not work with `devtools`. Therefore, we redefine this method in each package |
||
81 | -30x - | +||
26 | +
- class = "teal_data_module",+ #' as needed. Thus, we do not export this method |
||
82 | -30x - | +||
27 | +
- once = once+ #' |
||
83 | +28 |
- )+ #' @param pattern (`character`) pattern of files to be included, passed to `system.file` |
|
84 | +29 | ++ |
+ #' @param except (`character`) vector of basename filenames to be excluded+ |
+
30 | ++ |
+ #'+ |
+ |
31 | ++ |
+ #' @return HTML code that includes `JS` files.+ |
+ |
32 | ++ |
+ #' @keywords internal+ |
+ |
33 | ++ |
+ include_js_files <- function(pattern = NULL, except = NULL) {+ |
+ |
34 | +! | +
+ checkmate::assert_character(except, min.len = 1, any.missing = FALSE, null.ok = TRUE)+ |
+ |
35 | +! | +
+ js_files <- list.files(system.file("js", package = "teal", mustWork = TRUE), pattern = pattern, full.names = TRUE)+ |
+ |
36 | +! | +
+ js_files <- js_files[!(basename(js_files) %in% except)] # no-op if except is NULL+ |
+ |
37 | ++ | + + | +|
38 | +! | +
+ singleton(lapply(js_files, includeScript))+ |
+ |
39 |
} |
||
85 | +40 | ||
86 | +41 |
- #' Data module for `teal` transformers.+ #' Run `JS` file from `/inst/js/` package directory |
|
87 | +42 |
#' |
|
88 | +43 |
- #' @description+ #' This is triggered from the server to execute on the client+ |
+ |
44 | ++ |
+ #' rather than triggered directly on the client.+ |
+ |
45 | ++ |
+ #' Unlike `include_js_files` which includes `JavaScript` functions,+ |
+ |
46 | ++ |
+ #' the `run_js` actually executes `JavaScript` functions. |
|
89 | +47 |
- #' `r lifecycle::badge("experimental")`+ #' |
|
90 | +48 |
- #'+ #' `system.file` should not be used to access files in other packages, it does |
|
91 | +49 |
- #' Create a `teal_data_module` object for custom transformation of data for pre-processing+ #' not work with `devtools`. Therefore, we redefine this method in each package |
|
92 | +50 |
- #' before passing the data into the module.+ #' as needed. Thus, we do not export this method. |
|
93 | +51 |
#' |
|
94 | +52 |
- #' @details+ #' @param files (`character`) vector of filenames. |
|
95 | +53 |
- #' `teal_transform_module` creates a [`teal_data_module`] object to transform data in a `teal`+ #' |
|
96 | +54 |
- #' application. This transformation happens after the data has passed through the filtering activity+ #' @return `NULL`, invisibly. |
|
97 | +55 |
- #' in teal. The transformed data is then sent to the server of the [teal_module()].+ #' @keywords internal |
|
98 | +56 |
- #'+ run_js_files <- function(files) { |
|
99 | -+ | ||
57 | +81x |
- #' See vignette `vignette("data-transform-as-shiny-module", package = "teal")` for more details.+ checkmate::assert_character(files, min.len = 1, any.missing = FALSE) |
|
100 | -+ | ||
58 | +81x |
- #'+ lapply(files, function(file) { |
|
101 | -+ | ||
59 | +81x |
- #'+ shinyjs::runjs(paste0(readLines(system.file("js", file, package = "teal", mustWork = TRUE)), collapse = "\n")) |
|
102 | +60 |
- #' @inheritParams teal_data_module+ }) |
|
103 | -+ | ||
61 | +81x |
- #' @param server (`function(id, data)`)+ invisible(NULL) |
|
104 | +62 |
- #' `shiny` module server function; that takes `id` and `data` argument,+ } |
|
105 | +63 |
- #' where the `id` is the module id and `data` is the reactive `teal_data` input.+ |
|
106 | +64 |
- #' The server function must return reactive expression containing `teal_data` object.+ #' Code to include `teal` `CSS` and `JavaScript` files |
|
107 | +65 |
#' |
|
108 | +66 |
- #' The server function definition should not use `eventReactive` as it may lead to+ #' This is useful when you want to use the same `JavaScript` and `CSS` files that are |
|
109 | +67 |
- #' unexpected behavior.+ #' used with the `teal` application. |
|
110 | +68 |
- #' See `vignettes("data-transform-as-shiny-module")` for more information.+ #' This is also useful for running standalone modules in `teal` with the correct |
|
111 | +69 |
- #' @param datanames (`character`)+ #' styles. |
|
112 | +70 |
- #' Names of the datasets that are relevant for this module to evaluate. If set to `character(0)`+ #' Also initializes `shinyjs` so you can use it. |
|
113 | +71 |
- #' then module would receive [modules()] `datanames`.+ #' |
|
114 | +72 |
- #' @examples+ #' Simply add `include_teal_css_js()` as one of the UI elements. |
|
115 | +73 |
- #' my_transformers <- list(+ #' @return A `shiny.tag.list`. |
|
116 | +74 |
- #' teal_transform_module(+ #' @keywords internal |
|
117 | +75 |
- #' label = "Custom transform for iris",+ include_teal_css_js <- function() { |
|
118 | -+ | ||
76 | +! |
- #' datanames = "iris",+ tagList( |
|
119 | -+ | ||
77 | +! |
- #' ui = function(id) {+ shinyjs::useShinyjs(), |
|
120 | -+ | ||
78 | +! |
- #' ns <- NS(id)+ include_css_files(), |
|
121 | +79 |
- #' tags$div(+ # init.js is executed from the server |
|
122 | -+ | ||
80 | +! |
- #' numericInput(ns("n_rows"), "Subset n rows", value = 6, min = 1, max = 150, step = 1)+ include_js_files(except = "init.js"), |
|
123 | -+ | ||
81 | +! |
- #' )+ shinyjs::hidden(icon("fas fa-gear")), # add hidden icon to load font-awesome css for icons |
|
124 | +82 |
- #' },+ ) |
|
125 | +83 |
- #' server = function(id, data) {+ } |
126 | +1 |
- #' moduleServer(id, function(input, output, session) {+ #' `teal_data` utils |
||
127 | +2 |
- #' reactive({+ #' |
||
128 | +3 |
- #' within(data(),+ #' In `teal` we need to recreate the `teal_data` object due to two operations: |
||
129 | +4 |
- #' {+ #' - we need to append filter-data code and objects which have been evaluated in `FilteredData` and |
||
130 | +5 |
- #' iris <- head(iris, num_rows)+ #' we want to avoid double-evaluation. |
||
131 | +6 |
- #' },+ #' - we need to subset `teal_data` to `datanames` used by the module, to shorten obtainable R-code |
||
132 | +7 |
- #' num_rows = input$n_rows+ #' |
||
133 | +8 |
- #' )+ #' Due to above recreation of `teal_data` object can't be done simply by using public |
||
134 | +9 |
- #' })+ #' `teal.code` and `teal.data` methods. |
||
135 | +10 |
- #' })+ #' |
||
136 | +11 |
- #' }+ #' @param data (`teal_data`) |
||
137 | +12 |
- #' )+ #' @param code (`character`) code to append to `data@code` |
||
138 | +13 |
- #' )+ #' @param objects (`list`) objects to append to `data@env` |
||
139 | +14 |
- #'+ #' @param datanames (`character`) names of the datasets |
||
140 | +15 |
- #' @name teal_transform_module+ #' @return modified `teal_data` |
||
141 | +16 |
- #'+ #' @keywords internal |
||
142 | +17 |
- #' @export+ #' @name teal_data_utilities |
||
143 | +18 |
- teal_transform_module <- function(ui = function(id) NULL,+ NULL |
||
144 | +19 |
- server = function(id, data) data,+ |
||
145 | +20 |
- label = "transform module",+ #' @rdname teal_data_utilities |
||
146 | +21 |
- datanames = character(0)) {- |
- ||
147 | -19x - | -
- checkmate::assert_function(ui, args = "id", nargs = 1)+ .append_evaluated_code <- function(data, code) { |
||
148 | -19x - | +22 | +82x |
- checkmate::assert_function(server, args = c("id", "data"), nargs = 2)+ checkmate::assert_class(data, "teal_data") |
149 | -19x - | +23 | +82x |
- checkmate::assert_string(label)+ data@code <- c(data@code, code) |
150 | -19x - | +24 | +82x |
- checkmate::assert_character(datanames)+ data@id <- c(data@id, max(data@id) + 1L + seq_along(code)) |
151 | -19x - | +25 | +82x |
- if (identical(datanames, "all")) {+ data@messages <- c(data@messages, rep("", length(code))) |
152 | -1x - | +26 | +82x |
- stop(+ data@warnings <- c(data@warnings, rep("", length(code))) |
153 | -1x - | +27 | +82x |
- "teal_transform_module can't have datanames property equal to 'all'. Set `datanames = character(0)` instead.",+ methods::validObject(data) |
154 | -1x - | +28 | +82x |
- call. = FALSE+ data |
155 | +29 |
- )+ } |
||
156 | +30 |
- }- |
- ||
157 | -18x - | -
- structure(- |
- ||
158 | -18x - | -
- list(- |
- ||
159 | -18x - | -
- ui = ui,- |
- ||
160 | -18x - | -
- server = function(id, data) {- |
- ||
161 | -19x - | -
- data_out <- server(id, data)+ |
||
162 | +31 | - - | -||
163 | -19x - | -
- if (inherits(data_out, "reactive.event")) {+ #' @rdname teal_data_utilities |
||
164 | +32 |
- # This warning message partially detects when `eventReactive` is used in `data_module`.+ .append_modified_data <- function(data, objects) { |
||
165 | -1x - | +33 | +82x |
- warning(+ checkmate::assert_class(data, "teal_data") |
166 | -1x - | +34 | +82x |
- "teal_transform_module() ",+ checkmate::assert_class(objects, "list") |
167 | -1x - | +35 | +82x |
- "Using eventReactive in teal_transform module server code should be avoided as it ",+ new_env <- list2env(objects, parent = .GlobalEnv) |
168 | -1x - | +36 | +82x |
- "may lead to unexpected behavior. See the vignettes for more information ",+ rlang::env_coalesce(new_env, teal.code::get_env(data)) |
169 | -1x - | +37 | +82x |
- "(`vignette(\"data-transform-as-shiny-module\", package = \"teal\")`).",+ data@env <- new_env |
170 | -1x - | +38 | +82x |
- call. = FALSE+ data |
171 | +39 |
- )+ } |
||
172 | +40 |
- }+ |
||
173 | +41 |
-
+ #' @rdname teal_data_utilities |
||
174 | -19x - | +|||
42 | +
- decorate_err_msg(+ .subset_teal_data <- function(data, datanames) { |
|||
175 | -19x - | +43 | +81x |
- assert_reactive(data_out),+ checkmate::assert_class(data, "teal_data") |
176 | -19x - | +44 | +81x |
- pre = sprintf("From: 'teal_transform_module()':\nA 'teal_transform_module' with \"%s\" label:", label),+ checkmate::assert_class(datanames, "character") |
177 | -19x - | -
- post = "Please make sure that this module returns a 'reactive` object containing 'teal_data' class of object." # nolint: line_length_linter.- |
- ||
178 | -- |
- )- |
- ||
179 | -- |
- }- |
- ||
180 | -+ | 45 | +81x |
- ),+ datanames_corrected <- intersect(datanames, ls(teal.code::get_env(data))) |
181 | -18x - | +46 | +81x |
- label = label,+ datanames_corrected_with_raw <- c(datanames_corrected, ".raw_data") |
182 | -18x - | +47 | +81x |
- datanames = datanames,+ if (!length(datanames_corrected)) { |
183 | -18x - | +48 | +2x |
- class = c("teal_transform_module", "teal_data_module")+ return(teal_data()) |
184 | +49 |
- )+ } |
||
185 | +50 |
- }+ |
||
186 | -+ | |||
51 | +79x |
-
+ new_data <- do.call( |
||
187 | -+ | |||
52 | +79x |
-
+ teal.data::teal_data, |
||
188 | -+ | |||
53 | +79x |
- #' Extract all `transformers` from `modules`.+ args = c( |
||
189 | -+ | |||
54 | +79x |
- #'+ mget(x = datanames_corrected_with_raw, envir = teal.code::get_env(data)), |
||
190 | -+ | |||
55 | +79x |
- #' @param modules `teal_modules` or `teal_module`+ list( |
||
191 | -+ | |||
56 | +79x |
- #' @return A list of `teal_transform_module` nested in the same way as input `modules`.+ code = teal.code::get_code(data, names = datanames_corrected_with_raw), |
||
192 | -+ | |||
57 | +79x |
- #' @keywords internal+ join_keys = teal.data::join_keys(data)[datanames_corrected] |
||
193 | +58 |
- extract_transformers <- function(modules) {+ ) |
||
194 | -4x - | +|||
59 | +
- if (inherits(modules, "teal_module")) {+ ) |
|||
195 | -2x - | +|||
60 | +
- modules$transformers+ ) |
|||
196 | -2x - | +61 | +79x |
- } else if (inherits(modules, "teal_modules")) {+ new_data@verified <- data@verified |
197 | -2x - | +62 | +79x |
- lapply(modules$children, extract_transformers)+ teal.data::datanames(new_data) <- datanames_corrected |
198 | -+ | |||
63 | +79x |
- }+ new_data |
||
199 | +64 |
}@@ -41527,14 +39863,14 @@ teal coverage - 59.73% |
1 |
- #' Include `CSS` files from `/inst/css/` package directory to application header+ #' Filter settings for `teal` applications |
||
3 |
- #' `system.file` should not be used to access files in other packages, it does+ #' Specify initial filter states and filtering settings for a `teal` app. |
||
4 |
- #' not work with `devtools`. Therefore, we redefine this method in each package+ #' |
||
5 |
- #' as needed. Thus, we do not export this method.+ #' Produces a `teal_slices` object. |
||
6 |
- #'+ #' The `teal_slice` components will specify filter states that will be active when the app starts. |
||
7 |
- #' @param pattern (`character`) pattern of files to be included+ #' Attributes (created with the named arguments) will configure the way the app applies filters. |
||
8 |
- #'+ #' See argument descriptions for details. |
||
9 |
- #' @return HTML code that includes `CSS` files.+ #' |
||
10 |
- #' @keywords internal+ #' @inheritParams teal.slice::teal_slices |
||
11 |
- include_css_files <- function(pattern = "*") {+ #' |
||
12 | -! | +
- css_files <- list.files(+ #' @param module_specific (`logical(1)`) optional, |
|
13 | -! | +
- system.file("css", package = "teal", mustWork = TRUE),+ #' - `FALSE` (default) when one filter panel applied to all modules. |
|
14 | -! | +
- pattern = pattern, full.names = TRUE+ #' All filters will be shared by all modules. |
|
15 |
- )+ #' - `TRUE` when filter panel module-specific. |
||
16 |
-
+ #' Modules can have different set of filters specified - see `mapping` argument. |
||
17 | -! | +
- singleton(+ #' @param mapping `r lifecycle::badge("experimental")` |
|
18 | -! | +
- tags$head(lapply(css_files, includeCSS))+ #' _This is a new feature. Do kindly share your opinions on |
|
19 |
- )+ #' [`teal`'s GitHub repository](https://github.com/insightsengineering/teal/)._ |
||
20 |
- }+ #' |
||
21 |
-
+ #' (named `list`) specifies which filters will be active in which modules on app start. |
||
22 |
- #' Include `JS` files from `/inst/js/` package directory to application header+ #' Elements should contain character vector of `teal_slice` `id`s (see [`teal.slice::teal_slice`]). |
||
23 |
- #'+ #' Names of the list should correspond to `teal_module` `label` set in [module()] function. |
||
24 |
- #' `system.file` should not be used to access files in other packages, it does+ #' - `id`s listed under `"global_filters` will be active in all modules. |
||
25 |
- #' not work with `devtools`. Therefore, we redefine this method in each package+ #' - If missing, all filters will be applied to all modules. |
||
26 |
- #' as needed. Thus, we do not export this method+ #' - If empty list, all filters will be available to all modules but will start inactive. |
||
27 |
- #'+ #' - If `module_specific` is `FALSE`, only `global_filters` will be active on start. |
||
28 |
- #' @param pattern (`character`) pattern of files to be included, passed to `system.file`+ #' @param app_id (`character(1)`) |
||
29 |
- #' @param except (`character`) vector of basename filenames to be excluded+ #' For internal use only, do not set manually. |
||
30 |
- #'+ #' Added by `init` so that a `teal_slices` can be matched to the app in which it was used. |
||
31 |
- #' @return HTML code that includes `JS` files.+ #' Used for verifying snapshots uploaded from file. See `snapshot`. |
||
32 |
- #' @keywords internal+ #' |
||
33 |
- include_js_files <- function(pattern = NULL, except = NULL) {+ #' @param x (`list`) of lists to convert to `teal_slices` |
||
34 | -! | +
- checkmate::assert_character(except, min.len = 1, any.missing = FALSE, null.ok = TRUE)+ #' |
|
35 | -! | +
- js_files <- list.files(system.file("js", package = "teal", mustWork = TRUE), pattern = pattern, full.names = TRUE)+ #' @return |
|
36 | -! | +
- js_files <- js_files[!(basename(js_files) %in% except)] # no-op if except is NULL+ #' A `teal_slices` object. |
|
37 |
-
+ #' |
||
38 | -! | +
- singleton(lapply(js_files, includeScript))+ #' @seealso [`teal.slice::teal_slices`], [`teal.slice::teal_slice`], [slices_store()] |
|
39 |
- }+ #' |
||
40 |
-
+ #' @examples |
||
41 |
- #' Run `JS` file from `/inst/js/` package directory+ #' filter <- teal_slices( |
||
42 |
- #'+ #' teal_slice(dataname = "iris", varname = "Species", id = "species"), |
||
43 |
- #' This is triggered from the server to execute on the client+ #' teal_slice(dataname = "iris", varname = "Sepal.Length", id = "sepal_length"), |
||
44 |
- #' rather than triggered directly on the client.+ #' teal_slice( |
||
45 |
- #' Unlike `include_js_files` which includes `JavaScript` functions,+ #' dataname = "iris", id = "long_petals", title = "Long petals", expr = "Petal.Length > 5" |
||
46 |
- #' the `run_js` actually executes `JavaScript` functions.+ #' ), |
||
47 |
- #'+ #' teal_slice(dataname = "mtcars", varname = "mpg", id = "mtcars_mpg"), |
||
48 |
- #' `system.file` should not be used to access files in other packages, it does+ #' mapping = list( |
||
49 |
- #' not work with `devtools`. Therefore, we redefine this method in each package+ #' module1 = c("species", "sepal_length"), |
||
50 |
- #' as needed. Thus, we do not export this method.+ #' module2 = c("mtcars_mpg"), |
||
51 |
- #'+ #' global_filters = "long_petals" |
||
52 |
- #' @param files (`character`) vector of filenames.+ #' ) |
||
53 |
- #'+ #' ) |
||
54 |
- #' @return `NULL`, invisibly.+ #' |
||
55 |
- #' @keywords internal+ #' app <- init( |
||
56 |
- run_js_files <- function(files) {+ #' data = teal_data(iris = iris, mtcars = mtcars), |
||
57 | -78x - | +
- checkmate::assert_character(files, min.len = 1, any.missing = FALSE)+ #' modules = list( |
|
58 | -78x - | +
- lapply(files, function(file) {+ #' module("module1"), |
|
59 | -78x - | +
- shinyjs::runjs(paste0(readLines(system.file("js", file, package = "teal", mustWork = TRUE)), collapse = "\n"))+ #' module("module2") |
|
60 |
- })+ #' ), |
||
61 | -78x - | +
- invisible(NULL)+ #' filter = filter |
|
62 |
- }+ #' ) |
||
63 |
-
+ #' |
||
64 |
- #' Code to include `teal` `CSS` and `JavaScript` files+ #' if (interactive()) { |
||
65 |
- #'+ #' shinyApp(app$ui, app$server) |
||
66 |
- #' This is useful when you want to use the same `JavaScript` and `CSS` files that are+ #' } |
||
67 |
- #' used with the `teal` application.+ #' |
||
68 |
- #' This is also useful for running standalone modules in `teal` with the correct+ #' @export |
||
69 |
- #' styles.+ teal_slices <- function(..., |
||
70 |
- #' Also initializes `shinyjs` so you can use it.+ exclude_varnames = NULL, |
||
71 |
- #'+ include_varnames = NULL, |
||
72 |
- #' Simply add `include_teal_css_js()` as one of the UI elements.+ count_type = NULL, |
||
73 |
- #' @return A `shiny.tag.list`.+ allow_add = TRUE, |
||
74 |
- #' @keywords internal+ module_specific = FALSE, |
||
75 |
- include_teal_css_js <- function() {- |
- ||
76 | -! | -
- tagList(- |
- |
77 | -! | -
- shinyjs::useShinyjs(),- |
- |
78 | -! | -
- include_css_files(),- |
- |
79 | -- |
- # init.js is executed from the server- |
- |
80 | -! | -
- include_js_files(except = "init.js"),- |
- |
81 | -! | -
- shinyjs::hidden(icon("fas fa-gear")), # add hidden icon to load font-awesome css for icons- |
- |
82 | -- |
- )- |
- |
83 | -- |
- }- |
-
1 | -- |
- #' `teal_data` utils- |
- ||
2 | -- |
- #'- |
- ||
3 | -- |
- #' In `teal` we need to recreate the `teal_data` object due to two operations:- |
- ||
4 | -- |
- #' - we need to append filter-data code and objects which have been evaluated in `FilteredData` and- |
- ||
5 | -- |
- #' we want to avoid double-evaluation.- |
- ||
6 | -- |
- #' - we need to subset `teal_data` to `datanames` used by the module, to shorten obtainable R-code- |
- ||
7 | -- |
- #'- |
- ||
8 | -- |
- #' Due to above recreation of `teal_data` object can't be done simply by using public- |
- ||
9 | -- |
- #' `teal.code` and `teal.data` methods.- |
- ||
10 | -- |
- #'- |
- ||
11 | -- |
- #' @param data (`teal_data`)- |
- ||
12 | -- |
- #' @param code (`character`) code to append to `data@code`- |
- ||
13 | -- |
- #' @param objects (`list`) objects to append to `data@env`- |
- ||
14 | -- |
- #' @param datanames (`character`) names of the datasets- |
- ||
15 | -- |
- #' @return modified `teal_data`- |
- ||
16 | -- |
- #' @keywords internal- |
- ||
17 | -- |
- #' @name teal_data_utilities- |
- ||
18 | -- |
- NULL- |
- ||
19 | -- | - - | -||
20 | -- |
- #' @rdname teal_data_utilities- |
- ||
21 | -- |
- .append_evaluated_code <- function(data, code) {- |
- ||
22 | -79x - | -
- checkmate::assert_class(data, "teal_data")- |
- ||
23 | -79x - | -
- data@code <- c(data@code, code)- |
- ||
24 | -79x - | -
- data@id <- c(data@id, max(data@id) + 1L + seq_along(code))- |
- ||
25 | -79x - | -
- data@messages <- c(data@messages, rep("", length(code)))- |
- ||
26 | -79x - | -
- data@warnings <- c(data@warnings, rep("", length(code)))- |
- ||
27 | -79x - | -
- methods::validObject(data)- |
- ||
28 | -79x - | -
- data- |
- ||
29 | -- |
- }- |
- ||
30 | -- | - - | -||
31 | -- |
- #' @rdname teal_data_utilities+ mapping, |
||
32 | +76 |
- .append_modified_data <- function(data, objects) {- |
- ||
33 | -79x - | -
- checkmate::assert_class(data, "teal_data")+ app_id = NULL) { |
||
34 | -79x - | +77 | +159x |
- checkmate::assert_class(objects, "list")+ shiny::isolate({ |
35 | -79x - | +78 | +159x |
- new_env <- list2env(objects, parent = .GlobalEnv)+ checkmate::assert_flag(allow_add) |
36 | -79x - | +79 | +159x |
- rlang::env_coalesce(new_env, teal.code::get_env(data))+ checkmate::assert_flag(module_specific) |
37 | -79x - | +80 | +51x |
- data@env <- new_env+ if (!missing(mapping)) checkmate::assert_list(mapping, types = c("character", "NULL"), names = "named") |
38 | -79x - | +81 | +156x |
- data+ checkmate::assert_string(app_id, null.ok = TRUE) |
39 | +82 |
- }+ |
||
40 | -+ | |||
83 | +156x |
-
+ slices <- list(...) |
||
41 | -+ | |||
84 | +156x |
- #' @rdname teal_data_utilities+ all_slice_id <- vapply(slices, `[[`, character(1L), "id") |
||
42 | +85 |
- .subset_teal_data <- function(data, datanames) {+ |
||
43 | -78x - | +86 | +156x |
- checkmate::assert_class(data, "teal_data")+ if (missing(mapping)) { |
44 | -78x - | +87 | +108x |
- checkmate::assert_class(datanames, "character")+ mapping <- if (length(all_slice_id)) { |
45 | -78x - | +88 | +26x |
- datanames_corrected <- intersect(datanames, ls(teal.code::get_env(data)))+ list(global_filters = all_slice_id) |
46 | -78x - | +|||
89 | +
- datanames_corrected_with_raw <- c(datanames_corrected, ".raw_data")+ } else { |
|||
47 | -78x - | +90 | +82x |
- if (!length(datanames_corrected)) {+ list() |
48 | -1x - | +|||
91 | +
- return(teal_data())+ } |
|||
49 | +92 |
- }+ } |
||
50 | +93 | |||
51 | -77x - | +94 | +156x |
- new_data <- do.call(+ if (!module_specific) { |
52 | -77x - | +95 | +137x |
- teal.data::teal_data,+ mapping[setdiff(names(mapping), "global_filters")] <- NULL |
53 | -77x - | +|||
96 | +
- args = c(+ } |
|||
54 | -77x - | +|||
97 | +
- mget(x = datanames_corrected_with_raw, envir = teal.code::get_env(data)),+ |
|||
55 | -77x - | +98 | +156x |
- list(+ failed_slice_id <- setdiff(unlist(mapping), all_slice_id) |
56 | -77x - | +99 | +156x |
- code = teal.code::get_code(data, names = datanames_corrected_with_raw),+ if (length(failed_slice_id)) { |
57 | -77x - | -
- join_keys = teal.data::join_keys(data)[datanames_corrected]- |
- ||
58 | -- |
- )- |
- ||
59 | -- |
- )- |
- ||
60 | -+ | 100 | +1x |
- )+ stop(sprintf( |
61 | -77x - | +101 | +1x |
- new_data@verified <- data@verified+ "Filters in mapping don't match any available filter.\n %s not in %s", |
62 | -77x - | +102 | +1x |
- teal.data::datanames(new_data) <- datanames_corrected+ toString(failed_slice_id), |
63 | -77x - | +103 | +1x |
- new_data+ toString(all_slice_id) |
64 | +104 |
- }+ )) |
1 | +105 |
- #' Store and restore `teal_slices` object+ } |
||||
2 | +106 |
- #'+ |
||||
3 | -+ | |||||
107 | +155x |
- #' Functions that write a `teal_slices` object to a file in the `JSON` format,+ tss <- teal.slice::teal_slices( |
||||
4 | +108 |
- #' and also restore the object from disk.+ ..., |
||||
5 | -+ | |||||
109 | +155x |
- #'+ exclude_varnames = exclude_varnames, |
||||
6 | -+ | |||||
110 | +155x |
- #' Date and date time objects are stored in the following formats:+ include_varnames = include_varnames, |
||||
7 | -+ | |||||
111 | +155x |
- #'+ count_type = count_type, |
||||
8 | -+ | |||||
112 | +155x |
- #' - `Date` class is converted to the `"ISO8601"` standard (`YYYY-MM-DD`).+ allow_add = allow_add |
||||
9 | +113 |
- #' - `POSIX*t` classes are converted to character by using+ ) |
||||
10 | -+ | |||||
114 | +155x |
- #' `format.POSIX*t(usetz = TRUE, tz = "UTC")` (`YYYY-MM-DD HH:MM:SS UTC`, where+ attr(tss, "mapping") <- mapping |
||||
11 | -+ | |||||
115 | +155x |
- #' `UTC` is the `Coordinated Universal Time` timezone short-code).+ attr(tss, "module_specific") <- module_specific |
||||
12 | -+ | |||||
116 | +155x |
- #'+ attr(tss, "app_id") <- app_id |
||||
13 | -+ | |||||
117 | +155x |
- #' This format is assumed during `slices_restore`. All `POSIX*t` objects in+ class(tss) <- c("modules_teal_slices", class(tss)) |
||||
14 | -+ | |||||
118 | +155x |
- #' `selected` or `choices` fields of `teal_slice` objects are always printed in+ tss |
||||
15 | +119 |
- #' `UTC` timezone as well.+ }) |
||||
16 | +120 |
- #'+ } |
||||
17 | +121 |
- #' @param tss (`teal_slices`) object to be stored.+ |
||||
18 | +122 |
- #' @param file (`character(1)`) file path where `teal_slices` object will be+ |
||||
19 | +123 |
- #' saved and restored. The file extension should be `".json"`.+ #' @rdname teal_slices |
||||
20 | +124 |
- #'+ #' @export |
||||
21 | +125 |
- #' @return `slices_store` returns `NULL`, invisibly.+ #' @keywords internal |
||||
22 | +126 |
#' |
||||
23 | +127 |
- #' @seealso [teal_slices()]+ as.teal_slices <- function(x) { # nolint: object_name. |
||||
24 | -+ | |||||
128 | +13x |
- #'+ checkmate::assert_list(x) |
||||
25 | -+ | |||||
129 | +13x |
- #' @keywords internal+ lapply(x, checkmate::assert_list, names = "named", .var.name = "list element") |
||||
26 | +130 |
- #'+ |
||||
27 | -+ | |||||
131 | +13x |
- slices_store <- function(tss, file) {+ attrs <- attributes(unclass(x)) |
||||
28 | -9x - | +132 | +13x |
- checkmate::assert_class(tss, "teal_slices")+ ans <- lapply(x, function(x) if (is.teal_slice(x)) x else as.teal_slice(x)) |
||
29 | -9x - | +133 | +13x |
- checkmate::assert_path_for_output(file, overwrite = TRUE, extension = "json")+ do.call(teal_slices, c(ans, attrs)) |
||
30 | +134 |
-
+ } |
||||
31 | -9x - | +|||||
135 | +
- cat(format(tss, trim_lines = FALSE), "\n", file = file)+ |
|||||
32 | +136 |
- }+ |
||||
33 | +137 |
-
+ #' @rdname teal_slices |
||||
34 | +138 |
- #' @rdname slices_store+ #' @export |
||||
35 | +139 |
- #' @return `slices_restore` returns a `teal_slices` object restored from the file.+ #' @keywords internal |
||||
36 | +140 |
- #' @keywords internal+ #' |
||||
37 | +141 |
- slices_restore <- function(file) {+ c.teal_slices <- function(...) { |
||||
38 | -9x - | +142 | +6x |
- checkmate::assert_file_exists(file, access = "r", extension = "json")+ x <- list(...)+ |
+ ||
143 | +6x | +
+ checkmate::assert_true(all(vapply(x, is.teal_slices, logical(1L))), .var.name = "all arguments are teal_slices") |
||||
39 | +144 | |||||
40 | -9x - | +145 | +6x |
- tss_json <- jsonlite::fromJSON(file, simplifyDataFrame = FALSE)+ all_attributes <- lapply(x, attributes) |
||
41 | -9x - | +146 | +6x |
- tss_json$slices <-+ all_attributes <- coalesce_r(all_attributes) |
||
42 | -9x - | +147 | +6x |
- lapply(tss_json$slices, function(slice) {+ all_attributes <- all_attributes[names(all_attributes) != "class"]+ |
+ ||
148 | ++ | + | ||||
43 | -9x - | +149 | +6x |
- for (field in c("selected", "choices")) {+ do.call( |
||
44 | -18x - | +150 | +6x |
- if (!is.null(slice[[field]])) {+ teal_slices, |
||
45 | -12x - | +151 | +6x |
- if (length(slice[[field]]) > 0) {+ c( |
||
46 | -9x - | +152 | +6x |
- date_partial_regex <- "^[0-9]{4}-[0-9]{2}-[0-9]{2}"+ unique(unlist(x, recursive = FALSE)), |
||
47 | -9x - | +153 | +6x |
- time_stamp_regex <- paste0(date_partial_regex, "\\s[0-9]{2}:[0-9]{2}:[0-9]{2}\\sUTC$")+ all_attributes |
||
48 | +154 |
-
+ ) |
||||
49 | -9x - | +|||||
155 | +
- slice[[field]] <-+ ) |
|||||
50 | -9x - | +|||||
156 | +
- if (all(grepl(paste0(date_partial_regex, "$"), slice[[field]]))) {+ } |
|||||
51 | -3x - | +|||||
157 | +
- as.Date(slice[[field]])+ |
|||||
52 | -9x - | +|||||
158 | +
- } else if (all(grepl(time_stamp_regex, slice[[field]]))) {+ |
|||||
53 | -3x - | +|||||
159 | +
- as.POSIXct(slice[[field]], tz = "UTC")+ #' Deep copy `teal_slices` |
|||||
54 | +160 |
- } else {+ #' |
||||
55 | -3x - | +|||||
161 | +
- slice[[field]]+ #' it's important to create a new copy of `teal_slices` when |
|||||
56 | +162 |
- }+ #' starting a new `shiny` session. Otherwise, object will be shared |
||||
57 | +163 |
- } else {+ #' by multiple users as it is created in global environment before |
||||
58 | -3x - | +|||||
164 | +
- slice[[field]] <- character(0)+ #' `shiny` session starts. |
|||||
59 | +165 |
- }+ #' @param filter (`teal_slices`) |
||||
60 | +166 |
- }+ #' @return `teal_slices` |
||||
61 | +167 |
- }+ #' @keywords internal+ |
+ ||||
168 | ++ |
+ deep_copy_filter <- function(filter) { |
||||
62 | -9x - | +169 | +1x |
- slice+ checkmate::assert_class(filter, "teal_slices") |
||
63 | -+ | |||||
170 | +1x |
- })+ shiny::isolate({ |
||||
64 | -+ | |||||
171 | +1x |
-
+ filter_copy <- lapply(filter, function(slice) { |
||||
65 | -9x - | +172 | +2x |
- tss_elements <- lapply(tss_json$slices, as.teal_slice)+ teal.slice::as.teal_slice(as.list(slice)) |
||
66 | +173 |
-
+ }) |
||||
67 | -9x - | +174 | +1x |
- do.call(teal_slices, c(tss_elements, tss_json$attributes))+ attributes(filter_copy) <- attributes(filter)+ |
+ ||
175 | +1x | +
+ filter_copy |
||||
68 | +176 | ++ |
+ })+ |
+ |||
177 |
}@@ -43502,40 +41505,35 @@ teal coverage - 59.73% | |||||
57 | -76x - | +79x |
assert_reactive(teal_data) |
|||
58 | -76x - | +79x |
moduleServer( |
|||
59 | -76x - | +79x |
id = id, |
|||
60 | -76x - | +79x |
function(input, output, session) { |
|||
61 | -76x - | +79x |
logger::log_debug("srv_data_summary initializing") |
@@ -43549,32 +41547,28 @@ |||
63 | -76x - | +79x |
summary_table <- reactive({ |
|||
64 | -84x - | +87x |
req(inherits(teal_data(), "teal_data")) |
|||
65 | -78x - | +81x |
if (!length(ls(teal.code::get_env(teal_data())))) { |
|||
66 | -1x - | +2x |
return(NULL) |
@@ -43595,16 +41589,14 @@ |||
69 | -77x - | +79x |
filter_overview <- get_filter_overview(teal_data) |
|||
70 | -77x - | +79x |
names(filter_overview)[[1]] <- "Data Name" |
@@ -43618,32 +41610,28 @@ |||
72 | -77x - | +79x |
filter_overview$Obs <- ifelse( |
|||
73 | -77x - | +79x |
!is.na(filter_overview$obs), |
|||
74 | -77x - | +79x |
sprintf("%s/%s", filter_overview$obs_filtered, filter_overview$obs), |
|||
75 | -77x - | +79x |
ifelse(!is.na(filter_overview$obs_filtered), sprintf("%s", filter_overview$obs_filtered), "") |
@@ -43664,24 +41652,21 @@ |||
78 | -77x - | +79x |
filter_overview$Subjects <- ifelse( |
|||
79 | -77x - | +79x |
!is.na(filter_overview$subjects), |
|||
80 | -77x - | +79x |
sprintf("%s/%s", filter_overview$subjects_filtered, filter_overview$subjects), |
@@ -43709,16 +41694,14 @@ |||
84 | -77x - | +79x |
filter_overview <- filter_overview[, colnames(filter_overview) %in% c("Data Name", "Obs", "Subjects")] |
|||
85 | -77x - | +79x |
Filter(function(col) !all(col == ""), filter_overview) |
@@ -43739,24 +41722,21 @@ |||
88 | -76x - | +79x |
output$table <- renderUI({ |
|||
89 | -84x - | +87x |
summary_table_out <- try(summary_table(), silent = TRUE) |
|||
90 | -84x - | +87x |
if (inherits(summary_table_out, "try-error")) { |
@@ -43770,8 +41750,7 @@ |||
92 | -6x - | +6x |
if (!inherits(attr(summary_table_out, "condition"), "shiny.silent.error")) { |
@@ -43792,16 +41771,14 @@ |||
95 | -78x - | +81x |
} else if (is.null(summary_table_out)) { |
|||
96 | -1x - | +2x |
"no datasets to show" |
@@ -43815,112 +41792,98 @@ |||
98 | -77x - | +79x |
body_html <- apply( |
|||
99 | -77x - | +79x |
summary_table_out, |
|||
100 | -77x - | +79x |
1, |
|||
101 | -77x - | +79x |
function(x) { |
|||
102 | -139x - | +142x |
tags$tr( |
|||
103 | -139x - | +142x |
tagList( |
|||
104 | -139x - | +142x |
tags$td( |
|||
105 | -139x - | +142x |
if (all(x[-1] == "")) { |
|||
106 | -5x - | +5x |
icon( |
|||
107 | -5x - | +5x |
name = "fas fa-exclamation-triangle", |
|||
108 | -5x - | +5x |
title = "Unsupported dataset", |
|||
109 | -5x - | +5x |
`data-container` = "body", |
|||
110 | -5x - | +5x |
`data-toggle` = "popover", |
|||
111 | -5x - | +5x |
`data-content` = "object not supported by the data_summary module" |
@@ -43941,8 +41904,7 @@ |||
114 | -139x - | +142x |
x[1] |
@@ -43956,8 +41918,7 @@ |||
116 | -139x - | +142x |
lapply(x[-1], tags$td) |
@@ -43999,16 +41960,14 @@ |||
122 | -77x - | +79x |
header_labels <- names(summary_table()) |
|||
123 | -77x - | +79x |
header_html <- tags$tr(tagList(lapply(header_labels, tags$td))) |
@@ -44022,32 +41981,28 @@ |||
125 | -77x - | +79x |
table_html <- tags$table( |
|||
126 | -77x - | +79x |
class = "table custom-table", |
|||
127 | -77x - | +79x |
tags$thead(header_html), |
|||
128 | -77x - | +79x |
tags$tbody(body_html) |
@@ -44061,8 +42016,7 @@ |||
130 | -77x - | +79x |
table_html |
@@ -44090,8 +42044,7 @@ |||
134 | -76x - | +79x |
summary_table # testing purpose |
@@ -44140,16 +42093,14 @@ |||
141 | -77x - | +79x |
datanames <- teal.data::datanames(teal_data()) |
|||
142 | -77x - | +79x |
joinkeys <- teal.data::join_keys(teal_data()) |
@@ -44163,32 +42114,28 @@ |||
144 | -77x - | +79x |
filtered_data_objs <- sapply( |
|||
145 | -77x - | +79x |
datanames, |
|||
146 | -77x - | +79x |
function(name) teal.code::get_var(teal_data(), name), |
|||
147 | -77x - | +79x |
simplify = FALSE |
@@ -44202,8 +42149,7 @@ |||
149 | -77x - | +79x |
unfiltered_data_objs <- teal.code::get_var(teal_data(), ".raw_data") |
@@ -44217,32 +42163,28 @@ |||
151 | -77x - | +79x |
rows <- lapply( |
|||
152 | -77x - | +79x |
datanames, |
|||
153 | -77x - | +79x |
function(dataname) { |
|||
154 | -139x - | +142x |
parent <- teal.data::parent(joinkeys, dataname) |
@@ -44291,16 +42233,14 @@ |||
161 | -139x - | +142x |
subject_keys <- if (length(parent) > 0) { |
|||
162 | -7x - | +7x |
names(joinkeys[dataname, parent]) |
@@ -44314,8 +42254,7 @@ |||
164 | -132x - | +135x |
joinkeys[dataname, dataname] |
@@ -44329,40 +42268,35 @@ |||
166 | -139x - | +142x |
get_object_filter_overview( |
|||
167 | -139x - | +142x |
filtered_data = filtered_data_objs[[dataname]], |
|||
168 | -139x - | +142x |
unfiltered_data = unfiltered_data_objs[[dataname]], |
|||
169 | -139x - | +142x |
dataname = dataname, |
|||
170 | -139x - | +142x |
subject_keys = subject_keys |
@@ -44397,16 +42331,14 @@ |||
175 | -77x - | +79x |
unssuported_idx <- vapply(rows, function(x) all(is.na(x[-1])), logical(1)) # this is mainly for vectors |
|||
176 | -77x - | +79x |
do.call(rbind, c(rows[!unssuported_idx], rows[unssuported_idx])) |
@@ -44462,24 +42394,21 @@ |||
184 | -139x - | +142x |
if (inherits(filtered_data, c("data.frame", "DataFrame", "array", "Matrix", "SummarizedExperiment"))) { |
|||
185 | -134x - | +137x |
get_object_filter_overview_array(filtered_data, unfiltered_data, dataname, subject_keys) |
|||
186 | -5x - | +5x |
} else if (inherits(filtered_data, "MultiAssayExperiment")) { |
@@ -44500,48 +42429,42 @@ |||
189 | -5x - | +5x |
data.frame( |
|||
190 | -5x - | +5x |
dataname = dataname, |
|||
191 | -5x - | +5x |
obs = NA, |
|||
192 | -5x - | +5x |
obs_filtered = NA, |
|||
193 | -5x - | +5x |
subjects = NA, |
|||
194 | -5x - | +5x |
subjects_filtered = NA |
@@ -44611,56 +42534,49 @@ |||
204 | -134x - | +137x |
if (length(subject_keys) == 0) { |
|||
205 | -121x - | +124x |
data.frame( |
|||
206 | -121x - | +124x |
dataname = dataname, |
|||
207 | -121x - | +124x |
obs = ifelse(!is.null(nrow(unfiltered_data)), nrow(unfiltered_data), NA), |
|||
208 | -121x - | +124x |
obs_filtered = nrow(filtered_data), |
|||
209 | -121x - | +124x |
subjects = NA, |
|||
210 | -121x - | +124x |
subjects_filtered = NA |
@@ -44681,48 +42597,42 @@ |||
213 | -13x - | +13x |
data.frame( |
|||
214 | -13x - | +13x |
dataname = dataname, |
|||
215 | -13x - | +13x |
obs = ifelse(!is.null(nrow(unfiltered_data)), nrow(unfiltered_data), NA), |
|||
216 | -13x - | +13x |
obs_filtered = nrow(filtered_data), |
|||
217 | -13x - | +13x |
subjects = nrow(unique(unfiltered_data[subject_keys])), |
|||
218 | -13x - | +13x |
subjects_filtered = nrow(unique(filtered_data[subject_keys])) |
@@ -45004,74 +42914,353 @@ 258 | ! |
- function(experiment_name) {+ function(experiment_name) {+ |
+
259 | +! | +
+ data.frame(+ |
+ ||||
260 | +! | +
+ subjects = get_experiment_keys(filtered_data, unfiltered_data[[experiment_name]]),+ |
+ ||||
261 | +! | +
+ subjects_filtered = get_experiment_keys(filtered_data, filtered_data[[experiment_name]])+ |
+ ||||
262 | ++ |
+ )+ |
+ ||||
263 | ++ |
+ }+ |
+ ||||
264 | ++ |
+ ))+ |
+ ||||
265 | ++ | + + | +||||
266 | +! | +
+ experiment_info <- cbind(experiment_obs_info[, c("dataname", "obs", "obs_filtered")], experiment_subjects_info)+ |
+ ||||
267 | +! | +
+ rbind(mae_info, experiment_info)+ |
+ ||||
268 | ++ |
+ }+ |
+
1 | ++ |
+ #' UI and server modules of `teal`+ |
+ ||||
2 | ++ |
+ #'+ |
+ ||||
3 | ++ |
+ #' @description `r lifecycle::badge("deprecated")`+ |
+ ||||
4 | ++ |
+ #' Please use [`module_teal`] instead.+ |
+ ||||
5 | ++ |
+ #'+ |
+ ||||
6 | ++ |
+ #' @inheritParams ui_teal+ |
+ ||||
7 | ++ |
+ #' @inheritParams srv_teal+ |
+ ||||
8 | ++ |
+ #'+ |
+ ||||
9 | ++ |
+ #' @return+ |
+ ||||
10 | ++ |
+ #' Returns a `reactive` expression containing a `teal_data` object when data is loaded or `NULL` when it is not.+ |
+ ||||
11 | ++ |
+ #' @name module_teal_with_splash+ |
+ ||||
12 | ++ |
+ #'+ |
+ ||||
13 | ++ |
+ NULL+ |
+ ||||
14 | ++ | + + | +||||
15 | ++ |
+ #' @export+ |
+ ||||
16 | ++ |
+ #' @rdname module_teal_with_splash+ |
+ ||||
17 | ++ |
+ ui_teal_with_splash <- function(id,+ |
+ ||||
18 | ++ |
+ data,+ |
+ ||||
19 | ++ |
+ title = build_app_title(),+ |
+ ||||
20 | ++ |
+ header = tags$p(),+ |
+ ||||
21 | ++ |
+ footer = tags$p()) {+ |
+ ||||
22 | +! | +
+ lifecycle::deprecate_soft(+ |
+ ||||
23 | +! | +
+ when = "0.16",+ |
+ ||||
24 | +! | +
+ what = "ui_teal_with_splash()",+ |
+ ||||
25 | +! | +
+ details = "Deprecated, please use `ui_teal` instead"+ |
+ ||||
26 | ++ |
+ ) |
||||
259 | +27 | ! |
- data.frame(+ ui_teal(id = id, title = title, header = header, footer = footer) |
|||
260 | -! | +|||||
28 | +
- subjects = get_experiment_keys(filtered_data, unfiltered_data[[experiment_name]]),+ } |
|||||
261 | -! | +|||||
29 | +
- subjects_filtered = get_experiment_keys(filtered_data, filtered_data[[experiment_name]])+ |
|||||
262 | +30 |
- )+ #' @export |
||||
263 | +31 |
- }+ #' @rdname module_teal_with_splash |
||||
264 | +32 |
- ))+ srv_teal_with_splash <- function(id, data, modules, filter = teal_slices()) { |
||||
265 | -+ | |||||
33 | +! |
-
+ lifecycle::deprecate_soft( |
||||
266 | +34 | ! |
- experiment_info <- cbind(experiment_obs_info[, c("dataname", "obs", "obs_filtered")], experiment_subjects_info)+ when = "0.16", |
|||
267 | +35 | ! |
- rbind(mae_info, experiment_info)+ what = "srv_teal_with_splash()",+ |
+ |||
36 | +! | +
+ details = "Deprecated, please use `srv_teal` instead" |
||||
268 | +37 | ++ |
+ )+ |
+ |||
38 | +! | +
+ srv_teal(id = id, data = data, modules = modules, filter = filter)+ |
+ ||||
39 |
}@@ -45365,40 +43554,35 @@ teal coverage - 59.73% | |||||
41 | -9x - | +9x |
ns <- shiny::NS(id) |
|||
42 | -9x - | +9x |
shiny::div( |
|||
43 | -9x - | +9x |
id = ns("content"), |
|||
44 | -9x - | +9x |
style = "display: inline-block; width: 100%;", |
|||
45 | -9x - | +9x |
uiOutput(ns("data")) |
@@ -45440,16 +43624,14 @@ |||
51 | -78x - | +81x |
checkmate::assert_character(id, max.len = 1, any.missing = FALSE) |
|||
52 | -78x - | +81x |
checkmate::assert_multi_class(data, c("teal_data", "teal_data_module", "reactive")) |
@@ -45463,16 +43645,14 @@ |||
54 | -78x - | +81x |
moduleServer(id, function(input, output, session) { |
|||
55 | -78x - | +81x |
logger::log_debug("srv_data initializing.") |
@@ -45493,56 +43673,49 @@ |||
58 | -78x - | +81x |
data_out <- if (inherits(data, "teal_data_module")) { |
|||
59 | -10x - | +10x |
output$data <- renderUI(data$ui(id = session$ns("teal_data_module"))) |
|||
60 | -10x - | +10x |
data$server("teal_data_module") |
|||
61 | -78x - | +81x |
} else if (inherits(data, "teal_data")) { |
|||
62 | -39x - | +41x |
reactiveVal(data) |
|||
63 | -78x - | +81x |
} else if (test_reactive(data)) { |
|||
64 | -29x - | +30x |
data |
@@ -45563,16 +43736,14 @@ |||
67 | -77x - | +80x |
data_handled <- reactive({ |
|||
68 | -70x - | +73x |
tryCatch(data_out(), error = function(e) e) |
@@ -45600,72 +43771,63 @@ |||
72 | -77x - | +80x |
observeEvent(data_handled(), { |
|||
73 | -70x - | +73x |
if (inherits(data_handled(), "teal_data")) { |
|||
74 | -65x - | +68x |
app_session <- .subset2(shiny::getDefaultReactiveDomain(), "parent") |
|||
75 | -65x - | +68x |
setBookmarkExclude( |
|||
76 | -65x - | +68x |
session$ns( |
|||
77 | -65x - | +68x |
grep( |
|||
78 | -65x - | +68x |
pattern = "teal_data_module-", |
|||
79 | -65x - | +68x |
x = names(reactiveValuesToList(input)), |
|||
80 | -65x - | +68x |
value = TRUE |
@@ -45686,8 +43848,7 @@ |||
83 | -65x - | +68x |
session = app_session |
@@ -45722,8 +43883,7 @@ |||
88 | -77x - | +80x |
data_handled |
@@ -45786,8 +43946,7 @@ |||
97 | -65x - | +68x |
hashes <- .get_hashes_code(data) |
@@ -45801,80 +43960,70 @@ |||
99 | -65x - | +68x |
tdata <- do.call( |
|||
100 | -65x - | +68x |
teal.data::teal_data, |
|||
101 | -65x - | +68x |
c( |
|||
102 | -65x - | +68x |
list(code = trimws(c(teal.code::get_code(data), hashes), which = "right")), |
|||
103 | -65x - | +68x |
list(join_keys = teal.data::join_keys(data)), |
|||
104 | -65x - | +68x |
sapply( |
|||
105 | -65x - | +68x |
ls(teal.code::get_env(data)), |
|||
106 | -65x - | +68x |
teal.code::get_var, |
|||
107 | -65x - | +68x |
object = data, |
|||
108 | -65x - | +68x |
simplify = FALSE |
@@ -45909,16 +44058,14 @@ |||
113 | -65x - | +68x |
tdata@verified <- data@verified |
|||
114 | -65x - | +68x |
tdata |
@@ -45976,194 +44123,661 @@ 122 |
- #' @return A character vector with the code lines.+ #' @return A character vector with the code lines.+ |
+ |
123 | ++ |
+ #' @keywords internal+ |
+ ||||
124 | ++ |
+ #'+ |
+ ||||
125 | ++ |
+ .get_hashes_code <- function(data, datanames = ls(teal.code::get_env(data))) {+ |
+ ||||
126 | +68x | +
+ vapply(+ |
+ ||||
127 | +68x | +
+ datanames,+ |
+ ||||
128 | +68x | +
+ function(dataname, datasets) {+ |
+ ||||
129 | +119x | +
+ x <- data[[dataname]]+ |
+ ||||
130 | ++ | + + | +||||
131 | +119x | +
+ code <- if (is.function(x) && !is.primitive(x)) {+ |
+ ||||
132 | +4x | +
+ x <- deparse1(x)+ |
+ ||||
133 | +4x | +
+ bquote(rlang::hash(deparse1(.(as.name(dataname)))))+ |
+ ||||
134 | ++ |
+ } else {+ |
+ ||||
135 | +115x | +
+ bquote(rlang::hash(.(as.name(dataname))))+ |
+ ||||
136 | ++ |
+ }+ |
+ ||||
137 | +119x | +
+ sprintf(+ |
+ ||||
138 | +119x | +
+ "stopifnot(%s == %s) # @linksto %s",+ |
+ ||||
139 | +119x | +
+ deparse1(code),+ |
+ ||||
140 | +119x | +
+ deparse1(rlang::hash(x)),+ |
+ ||||
141 | +119x | +
+ dataname+ |
+ ||||
142 | ++ |
+ )+ |
+ ||||
143 | ++ |
+ },+ |
+ ||||
144 | +68x | +
+ character(1L),+ |
+ ||||
145 | +68x | +
+ USE.NAMES = TRUE+ |
+ ||||
146 | ++ |
+ )+ |
+ ||||
147 | ++ |
+ }+ |
+
1 | ++ |
+ #' Store and restore `teal_slices` object+ |
+ ||
2 | ++ |
+ #'+ |
+ ||
3 | ++ |
+ #' Functions that write a `teal_slices` object to a file in the `JSON` format,+ |
+ ||
4 | ++ |
+ #' and also restore the object from disk.+ |
+ ||
5 | ++ |
+ #'+ |
+ ||
6 | ++ |
+ #' Date and date time objects are stored in the following formats:+ |
+ ||
7 | ++ |
+ #'+ |
+ ||
8 | ++ |
+ #' - `Date` class is converted to the `"ISO8601"` standard (`YYYY-MM-DD`).+ |
+ ||
9 | ++ |
+ #' - `POSIX*t` classes are converted to character by using+ |
+ ||
10 | ++ |
+ #' `format.POSIX*t(usetz = TRUE, tz = "UTC")` (`YYYY-MM-DD HH:MM:SS UTC`, where+ |
+ ||
11 | ++ |
+ #' `UTC` is the `Coordinated Universal Time` timezone short-code).+ |
+ ||
12 | ++ |
+ #'+ |
+ ||
13 | ++ |
+ #' This format is assumed during `slices_restore`. All `POSIX*t` objects in+ |
+ ||
14 | ++ |
+ #' `selected` or `choices` fields of `teal_slice` objects are always printed in+ |
+ ||
15 | ++ |
+ #' `UTC` timezone as well.+ |
+ ||
16 | ++ |
+ #'+ |
+ ||
17 | ++ |
+ #' @param tss (`teal_slices`) object to be stored.+ |
+ ||
18 | ++ |
+ #' @param file (`character(1)`) file path where `teal_slices` object will be+ |
+ ||
19 | ++ |
+ #' saved and restored. The file extension should be `".json"`.+ |
+ ||
20 | ++ |
+ #'+ |
+ ||
21 | ++ |
+ #' @return `slices_store` returns `NULL`, invisibly.+ |
+ ||
22 | ++ |
+ #'+ |
+ ||
23 | ++ |
+ #' @seealso [teal_slices()]+ |
+ ||
24 | ++ |
+ #'+ |
+ ||
25 | ++ |
+ #' @keywords internal+ |
+ ||
26 | ++ |
+ #'+ |
+ ||
27 | ++ |
+ slices_store <- function(tss, file) {+ |
+ ||
28 | +9x | +
+ checkmate::assert_class(tss, "teal_slices")+ |
+ ||
29 | +9x | +
+ checkmate::assert_path_for_output(file, overwrite = TRUE, extension = "json")+ |
+ ||
30 | ++ | + + | +||
31 | +9x | +
+ cat(format(tss, trim_lines = FALSE), "\n", file = file)+ |
+ ||
32 | ++ |
+ }+ |
+ ||
33 | ++ | + + | +||
34 | ++ |
+ #' @rdname slices_store+ |
+ ||
35 | ++ |
+ #' @return `slices_restore` returns a `teal_slices` object restored from the file.+ |
+ ||
36 | ++ |
+ #' @keywords internal+ |
+ ||
37 | ++ |
+ slices_restore <- function(file) {+ |
+ ||
38 | +9x | +
+ checkmate::assert_file_exists(file, access = "r", extension = "json")+ |
+ ||
39 | ++ | + + | +||
40 | +9x | +
+ tss_json <- jsonlite::fromJSON(file, simplifyDataFrame = FALSE) |
||
123 | -+ | |||
41 | +9x |
- #' @keywords internal+ tss_json$slices <- |
||
124 | -+ | |||
42 | +9x |
- #'+ lapply(tss_json$slices, function(slice) { |
||
125 | -+ | |||
43 | +9x |
- .get_hashes_code <- function(data, datanames = ls(teal.code::get_env(data))) {+ for (field in c("selected", "choices")) { |
||
126 | -65x - | +44 | +18x |
- vapply(+ if (!is.null(slice[[field]])) { |
127 | -65x - | +45 | +12x |
- datanames,+ if (length(slice[[field]]) > 0) { |
128 | -65x - | +46 | +9x |
- function(dataname, datasets) {+ date_partial_regex <- "^[0-9]{4}-[0-9]{2}-[0-9]{2}" |
129 | -115x - | +47 | +9x |
- x <- data[[dataname]]+ time_stamp_regex <- paste0(date_partial_regex, "\\s[0-9]{2}:[0-9]{2}:[0-9]{2}\\sUTC$") |
130 | +48 | |||
131 | -115x - | +49 | +9x |
- code <- if (is.function(x) && !is.primitive(x)) {+ slice[[field]] <- |
132 | -4x - | +50 | +9x |
- x <- deparse1(x)+ if (all(grepl(paste0(date_partial_regex, "$"), slice[[field]]))) { |
133 | -4x - | +51 | +3x |
- bquote(rlang::hash(deparse1(.(as.name(dataname)))))+ as.Date(slice[[field]]) |
134 | -+ | |||
52 | +9x |
- } else {+ } else if (all(grepl(time_stamp_regex, slice[[field]]))) { |
||
135 | -111x - | +53 | +3x |
- bquote(rlang::hash(.(as.name(dataname))))+ as.POSIXct(slice[[field]], tz = "UTC") |
136 | +54 |
- }+ } else { |
||
137 | -115x - | +55 | +3x |
- sprintf(+ slice[[field]] |
138 | -115x - | +|||
56 | +
- "stopifnot(%s == %s) # @linksto %s",+ } |
|||
139 | -115x - | +|||
57 | +
- deparse1(code),+ } else { |
|||
140 | -115x - | +58 | +3x |
- deparse1(rlang::hash(x)),+ slice[[field]] <- character(0) |
141 | -115x - | +|||
59 | +
- dataname+ } |
|||
142 | +60 |
- )+ } |
||
143 | +61 |
- },+ } |
||
144 | -65x - | +62 | +9x |
- character(1L),+ slice+ |
+
63 | ++ |
+ })+ |
+ ||
64 | ++ | + | ||
145 | -65x - | +65 | +9x |
- USE.NAMES = TRUE+ tss_elements <- lapply(tss_json$slices, as.teal_slice) |
146 | +66 |
- )+ + |
+ ||
67 | +9x | +
+ do.call(teal_slices, c(tss_elements, tss_json$attributes)) |
||
147 | +68 |
}@@ -46226,32 +44840,28 @@ teal coverage - 59.73% | ||
8 | -1x - | +1x |
libraries <- vapply( |
|
9 | -1x - | +1x |
utils::sessionInfo()$otherPkgs, |
|
10 | -1x - | +1x |
function(x) { |
|
11 | -6x - | +6x |
paste0("library(", x$Package, ")") |
@@ -46265,8 +44875,7 @@ |
13 | -1x - | +1x |
character(1) |
@@ -46280,8 +44889,7 @@ |
15 | -1x - | +1x |
paste0(paste0(rev(libraries), sep = "\n"), collapse = "") |
@@ -46330,24 +44938,21 @@ |
22 | -5x - | +5x |
code_string <- getOption("teal.load_nest_code") |
|
23 | -5x - | +5x |
if (is.character(code_string)) { |
|
24 | -2x - | +2x |
code_string |
@@ -46361,8 +44966,7 @@ |
26 | -3x - | +3x |
"# Add any code to install/load your NEST environment here\n" |
@@ -46384,14 +44988,14 @@
1 |
- #' Evaluate expression on `teal_data_module`+ #' Create a `tdata` object |
||
3 |
- #' @details+ #' @description `r lifecycle::badge("superseded")` |
||
4 |
- #' `within` is a convenience function for evaluating inline code inside the environment of a `teal_data_module`.+ #' |
||
5 |
- #' It accepts only inline expressions (both simple and compound) and allows for injecting values into `expr` through+ #' Recent changes in `teal` cause modules to fail because modules expect a `tdata` object |
||
6 |
- #' the `...` argument: as `name:value` pairs are passed to `...`, `name` in `expr` will be replaced with `value.`+ #' to be passed to the `data` argument but instead they receive a `teal_data` object, |
||
7 |
- #'+ #' which is additionally wrapped in a reactive expression in the server functions. |
||
8 |
- #' @param data (`teal_data_module`) object+ #' In order to easily adapt such modules without a proper refactor, |
||
9 |
- #' @param expr (`expression`) to evaluate. Must be inline code. See+ #' use this function to downgrade the `data` argument. |
||
10 |
- #' @param ... See `Details`.+ #' |
||
11 |
- #'+ #' @name tdata |
||
12 |
- #' @return+ #' @param ... ignored |
||
13 |
- #' `within` returns a `teal_data_module` object with a delayed evaluation of `expr` when the module is run.+ #' @return nothing |
||
14 |
- #'+ NULL |
||
15 |
- #' @examples+ |
||
16 |
- #' within(tdm, dataset1 <- subset(dataset1, Species == "virginica"))+ #' @rdname tdata |
||
17 |
- #'+ #' @export |
||
18 |
- #' # use additional parameter for expression value substitution.+ new_tdata <- function(...) { |
||
19 | -+ | ! |
- #' valid_species <- "versicolor"+ .deprecate_tdata_msg() |
20 |
- #' within(tdm, dataset1 <- subset(dataset1, Species %in% species), species = valid_species)+ } |
||
21 |
- #' @include teal_data_module.R+ |
||
22 |
- #' @name within+ #' @rdname tdata |
||
23 |
- #' @rdname teal_data_module+ #' @export |
||
24 |
- #'+ tdata2env <- function(...) { |
||
25 | -+ | ! |
- #' @export+ .deprecate_tdata_msg() |
26 |
- #'+ } |
||
27 |
- within.teal_data_module <- function(data, expr, ...) {+ |
||
28 | -2x - | +
- expr <- substitute(expr)+ #' @rdname tdata |
|
29 | -2x - | +
- extras <- list(...)+ #' @export |
|
30 |
-
+ get_code_tdata <- function(...) { |
||
31 | -+ | ! |
- # Add braces for consistency.+ .deprecate_tdata_msg() |
32 | -2x - | +
- if (!identical(as.list(expr)[[1L]], as.symbol("{"))) {+ } |
|
33 | -2x - | +
- expr <- call("{", expr)+ |
|
34 |
- }+ #' @rdname tdata |
||
35 |
-
+ #' @export |
||
36 | -2x - | +
- calls <- as.list(expr)[-1]+ join_keys.tdata <- function(...) { |
|
37 | -+ | ! |
-
+ .deprecate_tdata_msg() |
38 |
- # Inject extra values into expressions.+ } |
||
39 | -2x - | +
- calls <- lapply(calls, function(x) do.call(substitute, list(x, env = extras)))+ |
|
40 |
-
+ #' @rdname tdata |
||
41 | -2x - | +
- eval_code(object = data, code = as.expression(calls))+ #' @export |
|
42 | + |
+ get_metadata <- function(...) {+ |
+ |
43 | +! | +
+ .deprecate_tdata_msg()+ |
+ |
44 | ++ |
+ }+ |
+ |
45 | ++ | + + | +|
46 | ++ |
+ #' @rdname tdata+ |
+ |
47 | ++ |
+ #' @export+ |
+ |
48 | ++ |
+ as_tdata <- function(...) {+ |
+ |
49 | +! | +
+ .deprecate_tdata_msg()+ |
+ |
50 | ++ |
+ }+ |
+ |
51 | ++ | + + | +|
52 | ++ | + + | +|
53 | ++ |
+ .deprecate_tdata_msg <- function() {+ |
+ |
54 | +! | +
+ lifecycle::deprecate_stop(+ |
+ |
55 | +! | +
+ when = "0.16",+ |
+ |
56 | +! | +
+ what = "tdata()",+ |
+ |
57 | +! | +
+ details = paste(+ |
+ |
58 | +! | +
+ "tdata has been removed in favour of `teal_data`.\n",+ |
+ |
59 | +! | +
+ "Please follow migration instructions https://github.com/insightsengineering/teal/discussions/987."+ |
+ |
60 | ++ |
+ )+ |
+ |
61 | ++ |
+ )+ |
+ |
62 | +
} |
@@ -46691,14 +45428,14 @@
1 |
- #' UI and server modules of `teal`+ #' Show `R` code modal |
||
4 |
- #' Please use [`module_teal`] instead.+ #' |
||
5 |
- #'+ #' Use the [shiny::showModal()] function to show the `R` code inside. |
||
6 |
- #' @inheritParams ui_teal+ #' |
||
7 |
- #' @inheritParams srv_teal+ #' @param title (`character(1)`) |
||
8 |
- #'+ #' Title of the modal, displayed in the first comment of the `R` code. |
||
9 |
- #' @return+ #' @param rcode (`character`) |
||
10 |
- #' Returns a `reactive` expression containing a `teal_data` object when data is loaded or `NULL` when it is not.+ #' vector with `R` code to show inside the modal. |
||
11 |
- #' @name module_teal_with_splash+ #' @param session (`ShinySession`) optional |
||
12 |
- #'+ #' `shiny` session object, defaults to [shiny::getDefaultReactiveDomain()]. |
||
13 |
- NULL+ #' |
||
14 |
-
+ #' @references [shiny::showModal()] |
||
16 |
- #' @rdname module_teal_with_splash+ show_rcode_modal <- function(title = NULL, rcode, session = getDefaultReactiveDomain()) { |
||
17 | -+ | ! |
- ui_teal_with_splash <- function(id,+ lifecycle::deprecate_soft( |
18 | -+ | ! |
- data,+ when = "0.16", |
19 | -+ | ! |
- title = build_app_title(),+ what = "show_rcode_modal()", |
20 | -+ | ! |
- header = tags$p(),+ details = "This function will be removed in the next release." |
21 |
- footer = tags$p()) {+ ) |
||
22 | -! | +
- lifecycle::deprecate_soft(+ |
|
23 | ! |
- when = "0.16",+ rcode <- paste(rcode, collapse = "\n") |
|
24 | -! | +
- what = "ui_teal_with_splash()",+ |
|
25 | ! |
- details = "Deprecated, please use `ui_teal` instead"+ ns <- session$ns |
|
26 | -+ | ! |
- )+ showModal(modalDialog( |
27 | ! |
- ui_teal(id = id, title = title, header = header, footer = footer)+ tagList( |
|
28 | -+ | ! |
- }+ tags$div( |
29 | -+ | ! |
-
+ actionButton(ns("copyRCode"), "Copy to Clipboard", `data-clipboard-target` = paste0("#", ns("r_code"))), |
30 | -+ | ! |
- #' @export+ modalButton("Dismiss"), |
31 | +! | +
+ style = "mb-4"+ |
+ |
32 |
- #' @rdname module_teal_with_splash+ ),+ |
+ ||
33 | +! | +
+ tags$div(tags$pre(id = ns("r_code"), rcode)), |
|
32 | +34 |
- srv_teal_with_splash <- function(id, data, modules, filter = teal_slices()) {+ ), |
|
33 | +35 | ! |
- lifecycle::deprecate_soft(+ title = title, |
34 | +36 | ! |
- when = "0.16",+ footer = tagList( |
35 | +37 | ! |
- what = "srv_teal_with_splash()",+ actionButton(ns("copyRCode"), "Copy to Clipboard", `data-clipboard-target` = paste0("#", ns("r_code"))), |
36 | +38 | ! |
- details = "Deprecated, please use `srv_teal` instead"+ modalButton("Dismiss") |
37 | +39 |
- )+ ), |
|
38 | +40 | ! |
- srv_teal(id = id, data = data, modules = modules, filter = filter)+ size = "l",+ |
+
41 | +! | +
+ easyClose = TRUE |
|
39 | +42 | ++ |
+ ))+ |
+
43 |
}@@ -47122,16 +45887,14 @@ teal coverage - 59.73% | ||
22 | -2x - | +2x |
packageStartupMessage( |
23 | -2x - | +2x |
"\nYou are using teal version ", |
@@ -47152,8 +45915,7 @@
26 | -2x - | +2x |
read.dcf(system.file("DESCRIPTION", package = "teal"))[, "Version"] |
@@ -47252,14 +46014,14 @@
1 |
- #' Create a `teal` module for previewing a report+ #' Evaluate expression on `teal_data_module` |
|||
3 |
- #' @description `r lifecycle::badge("experimental")`+ #' @details |
|||
4 |
- #'+ #' `within` is a convenience function for evaluating inline code inside the environment of a `teal_data_module`. |
|||
5 |
- #' This function wraps [teal.reporter::reporter_previewer_ui()] and+ #' It accepts only inline expressions (both simple and compound) and allows for injecting values into `expr` through |
|||
6 |
- #' [teal.reporter::reporter_previewer_srv()] into a `teal_module` to be+ #' the `...` argument: as `name:value` pairs are passed to `...`, `name` in `expr` will be replaced with `value.` |
|||
7 |
- #' used in `teal` applications.+ #' |
|||
8 |
- #'+ #' @param data (`teal_data_module`) object |
|||
9 |
- #' If you are creating a `teal` application using [init()] then this+ #' @param expr (`expression`) to evaluate. Must be inline code. See |
|||
10 |
- #' module will be added to your application automatically if any of your `teal_modules`+ #' @param ... See `Details`. |
|||
11 |
- #' support report generation.+ #' |
|||
12 |
- #'+ #' @return |
|||
13 |
- #' @inheritParams teal_modules+ #' `within` returns a `teal_data_module` object with a delayed evaluation of `expr` when the module is run. |
|||
14 |
- #' @param server_args (named `list`)+ #' |
|||
15 |
- #' Arguments passed to [teal.reporter::reporter_previewer_srv()].+ #' @examples |
|||
16 |
- #'+ #' within(tdm, dataset1 <- subset(dataset1, Species == "virginica")) |
|||
17 |
- #' @return+ #' |
|||
18 |
- #' `teal_module` (extended with `teal_module_previewer` class) containing the `teal.reporter` previewer functionality.+ #' # use additional parameter for expression value substitution. |
|||
19 |
- #'+ #' valid_species <- "versicolor" |
|||
20 |
- #' @export+ #' within(tdm, dataset1 <- subset(dataset1, Species %in% species), species = valid_species) |
|||
21 |
- #'+ #' @include teal_data_module.R |
|||
22 |
- reporter_previewer_module <- function(label = "Report previewer", server_args = list()) {+ #' @name within |
|||
23 | -7x - | +
- checkmate::assert_string(label)+ #' @rdname teal_data_module |
||
24 | -5x - | +
- checkmate::assert_list(server_args, names = "named")+ #' |
||
25 | -5x - | +
- checkmate::assert_true(all(names(server_args) %in% names(formals(teal.reporter::reporter_previewer_srv))))+ #' @export |
||
26 |
-
+ #' |
|||
27 | -3x - | +
- message("Initializing reporter_previewer_module")+ within.teal_data_module <- function(data, expr, ...) { |
||
28 | -+ | 2x |
-
+ expr <- substitute(expr) |
|
29 | -3x - | +2x |
- srv <- function(id, reporter, ...) {+ extras <- list(...) |
|
30 | -! | +
- teal.reporter::reporter_previewer_srv(id, reporter, ...)+ |
||
31 |
- }+ # Add braces for consistency. |
|||
32 | -+ | 2x |
-
+ if (!identical(as.list(expr)[[1L]], as.symbol("{"))) { |
|
33 | -3x - | -
- ui <- function(id, ...) {- |
- ||
34 | -! | +2x |
- teal.reporter::reporter_previewer_ui(id, ...)+ expr <- call("{", expr) |
|
35 | +34 |
} |
||
36 | +35 | |||
37 | -3x - | -
- module <- module(- |
- ||
38 | -3x - | -
- label = "temporary label",- |
- ||
39 | -3x - | -
- server = srv, ui = ui,- |
- ||
40 | -3x - | -
- server_args = server_args, ui_args = list(), datanames = NULL- |
- ||
41 | -+ | 36 | +2x |
- )+ calls <- as.list(expr)[-1] |
42 | +37 |
- # Module is created with a placeholder label and the label is changed later.+ |
||
43 | +38 |
- # This is to prevent another module being labeled "Report previewer".- |
- ||
44 | -3x - | -
- class(module) <- c(class(module), "teal_module_previewer")+ # Inject extra values into expressions. |
||
45 | -3x - | +39 | +2x |
- module$label <- label+ calls <- lapply(calls, function(x) do.call(substitute, list(x, env = extras))) |
46 | -3x - | +|||
40 | +
- attr(module, "teal_bookmarkable") <- TRUE+ |
|||
47 | -3x - | +41 | +2x |
- module+ eval_code(object = data, code = as.expression(calls)) |
48 | +42 |
}@@ -47655,48 +46361,42 @@ teal coverage - 59.73% | ||
7 | -938x - | +969x |
if (!isTRUE(checkmate::test_class(x, classes = "reactive", null.ok = null.ok))) { |
|
8 | -4x - | +4x |
cl <- class(x) |
|
9 | -4x - | +4x |
return(sprintf( |
|
10 | -4x - | +4x |
"Must be a reactive (i.e. inherit from 'reactive' class) but has class%s '%s'", |
|
11 | -4x - | +4x |
if (length(cl) > 1L) "es" else "", |
|
12 | -4x - | +4x |
paste0(cl, collapse = "','") |
@@ -47717,8 +46417,7 @@ |
15 | -934x - | +965x |
return(TRUE) |
@@ -47746,8 +46445,7 @@ |
19 | -29x - | +30x |
isTRUE(check_reactive(x, null.ok = null.ok)) |
@@ -47852,88 +46550,77 @@ |
34 | -40x - | +40x |
tryCatch( |
|
35 | -40x - | +40x |
x, |
|
36 | -40x - | +40x |
error = function(e) { |
|
37 | -2x - | +2x |
stop( |
|
38 | -2x - | +2x |
"\n", |
|
39 | -2x - | +2x |
pre, |
|
40 | -2x - | +2x |
"\n", |
|
41 | -2x - | +2x |
e$message, |
|
42 | -2x - | +2x |
"\n", |
|
43 | -2x - | +2x |
post, |
|
44 | -2x - | +2x |
call. = FALSE |
@@ -47961,8 +46648,7 @@ |
48 | -38x - | +38x |
x |
@@ -47977,313 +46663,6 @@
1 | -- |
- #' Show `R` code modal- |
-
2 | -- |
- #'- |
-
3 | -- |
- #' @description `r lifecycle::badge("deprecated")`- |
-
4 | -- |
- #'- |
-
5 | -- |
- #' Use the [shiny::showModal()] function to show the `R` code inside.- |
-
6 | -- |
- #'- |
-
7 | -- |
- #' @param title (`character(1)`)- |
-
8 | -- |
- #' Title of the modal, displayed in the first comment of the `R` code.- |
-
9 | -- |
- #' @param rcode (`character`)- |
-
10 | -- |
- #' vector with `R` code to show inside the modal.- |
-
11 | -- |
- #' @param session (`ShinySession`) optional- |
-
12 | -- |
- #' `shiny` session object, defaults to [shiny::getDefaultReactiveDomain()].- |
-
13 | -- |
- #'- |
-
14 | -- |
- #' @references [shiny::showModal()]- |
-
15 | -- |
- #' @export- |
-
16 | -- |
- show_rcode_modal <- function(title = NULL, rcode, session = getDefaultReactiveDomain()) {- |
-
17 | -! | -
- lifecycle::deprecate_soft(- |
-
18 | -! | -
- when = "0.16",- |
-
19 | -! | -
- what = "show_rcode_modal()",- |
-
20 | -! | -
- details = "This function will be removed in the next release."- |
-
21 | -- |
- )- |
-
22 | -- | - - | -
23 | -! | -
- rcode <- paste(rcode, collapse = "\n")- |
-
24 | -- | - - | -
25 | -! | -
- ns <- session$ns- |
-
26 | -! | -
- showModal(modalDialog(- |
-
27 | -! | -
- tagList(- |
-
28 | -! | -
- tags$div(- |
-
29 | -! | -
- actionButton(ns("copyRCode"), "Copy to Clipboard", `data-clipboard-target` = paste0("#", ns("r_code"))),- |
-
30 | -! | -
- modalButton("Dismiss"),- |
-
31 | -! | -
- style = "mb-4"- |
-
32 | -- |
- ),- |
-
33 | -! | -
- tags$div(tags$pre(id = ns("r_code"), rcode)),- |
-
34 | -- |
- ),- |
-
35 | -! | -
- title = title,- |
-
36 | -! | -
- footer = tagList(- |
-
37 | -! | -
- actionButton(ns("copyRCode"), "Copy to Clipboard", `data-clipboard-target` = paste0("#", ns("r_code"))),- |
-
38 | -! | -
- modalButton("Dismiss")- |
-
39 | -- |
- ),- |
-
40 | -! | -
- size = "l",- |
-
41 | -! | -
- easyClose = TRUE- |
-
42 | -- |
- ))- |
-
43 | -- |
- }- |
-