diff --git a/main/coverage-report/index.html b/main/coverage-report/index.html index 47f3129e93..d8d96df841 100644 --- a/main/coverage-report/index.html +++ b/main/coverage-report/index.html @@ -94,7 +94,7 @@ font-size: 11px; }
1 |
- #' Filter state snapshot management.+ setOldClass("teal_data_module") |
||
2 |
- #'+ |
||
3 |
- #' Capture and restore snapshots of the global (app) filter state.+ #' Evaluate the code in the qenv environment |
||
4 |
- #'+ #' @name eval_code |
||
5 |
- #' This module introduces snapshots: stored descriptions of the filter state of the entire application.+ #' @description |
||
6 |
- #' Snapshots allow the user to save the current filter state of the application for later use in the session,+ #' Given code is evaluated in the `qenv` environment of `teal_data` reactive defined in `teal_data_module`. |
||
7 |
- #' as well as to save it to file in order to share it with an app developer or other users,+ #' @param object (`teal_data_module`) |
||
8 |
- #' who in turn can upload it to their own session.+ #' @inheritParams teal.code::eval_code |
||
9 |
- #'+ #' @return Returns a `teal_data_module` object. |
||
10 |
- #' The snapshot manager is accessed through the filter manager, with the cog icon in the top right corner.+ #' @importMethodsFrom teal.code eval_code |
||
11 |
- #' At the beginning of a session it presents three icons: a camera, an upload, and an circular arrow.+ #' @importFrom methods setMethod |
||
12 |
- #' Clicking the camera captures a snapshot, clicking the upload adds a snapshot from a file+ NULL |
||
13 |
- #' and applies the filter states therein, and clicking the arrow resets initial application state.+ |
||
14 |
- #' As snapshots are added, they will show up as rows in a table and each will have a select button and a save button.+ #' @rdname eval_code |
||
15 |
- #'+ #' @export |
||
16 |
- #' @section Server logic:+ #' |
||
17 |
- #' Snapshots are basically `teal_slices` objects, however, since each module is served by a separate instance+ #' @examples |
||
18 |
- #' of `FilteredData` and these objects require shared state, `teal_slice` is a `reactiveVal` so `teal_slices`+ #' tdm <- teal_data_module( |
||
19 |
- #' cannot be stored as is. Therefore, `teal_slices` are reversibly converted to a list of lists representation+ #' ui = function(id) div(id = shiny::NS(id)("div_id")), |
||
20 |
- #' (attributes are maintained).+ #' server = function(id) { |
||
21 |
- #'+ #' shiny::moduleServer(id, function(input, output, session) { |
||
22 |
- #' Snapshots are stored in a `reactiveVal` as a named list.+ #' shiny::reactive(teal_data(IRIS = iris)) |
||
23 |
- #' The first snapshot is the initial state of the application and the user can add a snapshot whenever they see fit.+ #' }) |
||
24 |
- #'+ #' } |
||
25 |
- #' For every snapshot except the initial one, a piece of UI is generated that contains+ #' ) |
||
26 |
- #' the snapshot name, a select button to restore that snapshot, and a save button to save it to a file.+ #' eval_code(tdm, "IRIS <- subset(IRIS, Species == 'virginica')") |
||
27 |
- #' The initial snapshot is restored by a separate "reset" button.+ setMethod("eval_code", signature = c("teal_data_module", "character"), function(object, code) { |
||
28 | -+ | 13x |
- #' It cannot be saved directly but a user is welcome to capture the initial state as a snapshot and save that.+ teal_data_module( |
29 | -+ | 13x |
- #'+ ui = function(id) { |
30 | -+ | 1x |
- #' @section Snapshot mechanics:+ ns <- NS(id) |
31 | -+ | 1x |
- #' When a snapshot is captured, the user is prompted to name it.+ object$ui(ns("mutate_inner")) |
32 |
- #' Names are displayed as is but since they are used to create button ids,+ }, |
||
33 | -+ | 13x |
- #' under the hood they are converted to syntactically valid strings.+ server = function(id) { |
34 | -+ | 11x |
- #' New snapshot names are validated so that their valid versions are unique.+ moduleServer(id, function(input, output, session) { |
35 | -+ | 11x |
- #' Leading and trailing white space is trimmed.+ teal_data_rv <- object$server("mutate_inner") |
36 |
- #'+ |
||
37 | -+ | 11x |
- #' The module can read the global state of the application from `slices_global` and `mapping_matrix`.+ if (!is.reactive(teal_data_rv)) { |
38 | -+ | 1x |
- #' The former provides a list of all existing `teal_slice`s and the latter says which slice is active in which module.+ stop("The `teal_data_module` must return a reactive expression.", call. = FALSE) |
39 |
- #' Once a name has been accepted, `slices_global` is converted to a list of lists - a snapshot.+ } |
||
40 |
- #' The snapshot contains the `mapping` attribute of the initial application state+ |
||
41 | -+ | 10x |
- #' (or one that has been restored), which may not reflect the current one,+ td <- eventReactive(teal_data_rv(), |
42 |
- #' so `mapping_matrix` is transformed to obtain the current mapping, i.e. a list that,+ { |
||
43 | -+ | 10x |
- #' when passed to the `mapping` argument of [`teal::teal_slices`], would result in the current mapping.+ if (inherits(teal_data_rv(), c("teal_data", "qenv.error"))) { |
44 | -+ | 6x |
- #' This is substituted as the snapshot's `mapping` attribute and the snapshot is added to the snapshot list.+ eval_code(teal_data_rv(), code) |
45 |
- #'+ } else { |
||
46 | -+ | 4x |
- #' To restore app state, a snapshot is retrieved from storage and rebuilt into a `teal_slices` object.+ teal_data_rv() |
47 |
- #' Then state of all `FilteredData` objects (provided in `filtered_data_list`) is cleared+ } |
||
48 |
- #' and set anew according to the `mapping` attribute of the snapshot.+ }, |
||
49 | -+ | 10x |
- #' The snapshot is then set as the current content of `slices_global`.+ ignoreNULL = FALSE |
50 |
- #'+ ) |
||
51 | -+ | 10x |
- #' To save a snapshot, the snapshot is retrieved and reassembled just like for restoring,+ td |
52 |
- #' and then saved to file with [`slices_store`].+ }) |
||
53 |
- #'+ } |
||
54 |
- #' When a snapshot is uploaded, it will first be added to storage just like a newly created one,+ ) |
||
55 |
- #' and then used to restore app state much like a snapshot taken from storage.+ }) |
||
56 |
- #' Upon clicking the upload icon the user will be prompted for a file to upload+ |
||
57 |
- #' and may choose to name the new snapshot. The name defaults to the name of the file (the extension is dropped)+ #' @rdname eval_code |
||
58 |
- #' and normal naming rules apply. Loading the file yields a `teal_slices` object,+ #' @export |
||
59 |
- #' which is disassembled for storage and used directly for restoring app state.+ setMethod("eval_code", signature = c("teal_data_module", "language"), function(object, code) { |
||
60 | -+ | 1x |
- #'+ eval_code(object, code = paste(lang2calls(code), collapse = "\n")) |
61 |
- #' @section Transferring snapshots:+ }) |
||
62 |
- #' Snapshots uploaded from disk should only be used in the same application they come from,+ |
||
63 |
- #' _i.e._ an application that uses the same data and the same modules.+ #' @rdname eval_code |
||
64 |
- #' To ensure this is the case, `init` stamps `teal_slices` with an app id that is stored in the `app_id` attribute of+ #' @export |
||
65 |
- #' a `teal_slices` object. When a snapshot is restored from file, its `app_id` is compared to that+ setMethod("eval_code", signature = c("teal_data_module", "expression"), function(object, code) { |
||
66 | -+ | 6x |
- #' of the current app state and only if the match is the snapshot admitted to the session.+ eval_code(object, code = paste(lang2calls(code), collapse = "\n")) |
67 |
- #'+ }) |
||
68 |
- #' @param id (`character(1)`) `shiny` module id+ |
||
69 |
- #' @param slices_global (`reactiveVal`) that contains a `teal_slices` object+ #' @inherit teal.code::within.qenv params title details |
||
70 |
- #' containing all `teal_slice`s existing in the app, both active and inactive+ #' @description |
||
71 |
- #' @param mapping_matrix (`reactive`) that contains a `data.frame` representation+ #' Convenience function for evaluating inline code inside the environment of a |
||
72 |
- #' of the mapping of filter state ids (rows) to modules labels (columns);+ #' `teal_data_module` |
||
73 |
- #' all columns are `logical` vectors+ #' |
||
74 |
- #' @param filtered_data_list non-nested (`named list`) that contains `FilteredData` objects+ #' @param data (`teal_data_module`) object |
||
75 |
- #'+ #' @return Returns a `teal_data_module` object with a delayed evaluation of `expr` |
||
76 |
- #' @return Nothing is returned.+ #' when module. |
||
77 |
- #'+ #' @export |
||
78 |
- #' @name snapshot_manager_module+ #' @seealso [base::within()], [teal_data_module()] |
||
79 |
- #' @aliases snapshot snapshot_manager+ #' @examples |
||
80 |
- #'+ #' tdm <- teal_data_module( |
||
81 |
- #' @author Aleksander Chlebowski+ #' ui = function(id) div(id = shiny::NS(id)("div_id")), |
||
82 |
- #'+ #' server = function(id) { |
||
83 |
- #' @rdname snapshot_manager_module+ #' shiny::moduleServer(id, function(input, output, session) { |
||
84 |
- #' @keywords internal+ #' shiny::reactive(teal_data(IRIS = iris)) |
||
85 |
- #'+ #' }) |
||
86 |
- snapshot_manager_ui <- function(id) {+ #' } |
||
87 | -! | +
- ns <- NS(id)+ #' ) |
|
88 | -! | +
- div(+ #' within(tdm, IRIS <- subset(IRIS, Species == "virginica")) |
|
89 | -! | +
- class = "snapshot_manager_content",+ within.teal_data_module <- function(data, expr, ...) { |
|
90 | -! | +6x |
- div(+ expr <- substitute(expr) |
91 | -! | +6x |
- class = "snapshot_table_row",+ extras <- list(...) |
92 | -! | +
- span(tags$b("Snapshot manager")),+ |
|
93 | -! | +
- actionLink(ns("snapshot_add"), label = NULL, icon = icon("camera"), title = "add snapshot"),+ # Add braces for consistency. |
|
94 | -! | +6x |
- actionLink(ns("snapshot_load"), label = NULL, icon = icon("upload"), title = "upload snapshot"),+ if (!identical(as.list(expr)[[1L]], as.symbol("{"))) { |
95 | -! | +6x |
- actionLink(ns("snapshot_reset"), label = NULL, icon = icon("undo"), title = "reset initial state"),+ expr <- call("{", expr) |
96 | -! | +
- NULL+ } |
|
97 |
- ),+ |
||
98 | -! | +6x |
- uiOutput(ns("snapshot_list"))+ calls <- as.list(expr)[-1] |
99 |
- )+ |
||
100 |
- }+ # Inject extra values into expressions. |
||
101 | -+ | 6x |
-
+ calls <- lapply(calls, function(x) do.call(substitute, list(x, env = extras))) |
102 |
- #' @rdname snapshot_manager_module+ |
||
103 | -+ | 6x |
- #' @keywords internal+ eval_code(object = data, code = as.expression(calls)) |
104 |
- #'+ } |
105 | +1 |
- snapshot_manager_srv <- function(id, slices_global, mapping_matrix, filtered_data_list) {+ #' Filter state snapshot management. |
|
106 | -7x | +||
2 | +
- checkmate::assert_character(id)+ #' |
||
107 | -7x | +||
3 | +
- checkmate::assert_true(is.reactive(slices_global))+ #' Capture and restore snapshots of the global (app) filter state. |
||
108 | -7x | +||
4 | +
- checkmate::assert_class(isolate(slices_global()), "teal_slices")+ #' |
||
109 | -7x | +||
5 | +
- checkmate::assert_true(is.reactive(mapping_matrix))+ #' This module introduces snapshots: stored descriptions of the filter state of the entire application. |
||
110 | -7x | +||
6 | +
- checkmate::assert_data_frame(isolate(mapping_matrix()), null.ok = TRUE)+ #' Snapshots allow the user to save the current filter state of the application for later use in the session, |
||
111 | -7x | +||
7 | +
- checkmate::assert_list(filtered_data_list, types = "FilteredData", any.missing = FALSE, names = "named")+ #' as well as to save it to file in order to share it with an app developer or other users, |
||
112 | +8 |
-
+ #' who in turn can upload it to their own session. |
|
113 | -7x | +||
9 | +
- moduleServer(id, function(input, output, session) {+ #' |
||
114 | -7x | +||
10 | +
- ns <- session$ns+ #' The snapshot manager is accessed through the filter manager, with the cog icon in the top right corner. |
||
115 | +11 |
-
+ #' At the beginning of a session it presents three icons: a camera, an upload, and an circular arrow. |
|
116 | +12 |
- # Store global filter states ----+ #' Clicking the camera captures a snapshot, clicking the upload adds a snapshot from a file |
|
117 | -7x | +||
13 | +
- filter <- isolate(slices_global())+ #' and applies the filter states therein, and clicking the arrow resets initial application state. |
||
118 | -7x | +||
14 | +
- snapshot_history <- reactiveVal({+ #' As snapshots are added, they will show up as rows in a table and each will have a select button and a save button. |
||
119 | -7x | +||
15 | +
- list(+ #' |
||
120 | -7x | +||
16 | +
- "Initial application state" = as.list(filter, recursive = TRUE)+ #' @section Server logic: |
||
121 | +17 |
- )+ #' Snapshots are basically `teal_slices` objects, however, since each module is served by a separate instance |
|
122 | +18 |
- })+ #' of `FilteredData` and these objects require shared state, `teal_slice` is a `reactiveVal` so `teal_slices` |
|
123 | +19 |
-
+ #' cannot be stored as is. Therefore, `teal_slices` are reversibly converted to a list of lists representation |
|
124 | +20 |
- # Snapshot current application state ----+ #' (attributes are maintained). |
|
125 | +21 |
- # Name snaphsot.+ #' |
|
126 | -7x | +||
22 | +
- observeEvent(input$snapshot_add, {+ #' Snapshots are stored in a `reactiveVal` as a named list. |
||
127 | -! | +||
23 | +
- showModal(+ #' The first snapshot is the initial state of the application and the user can add a snapshot whenever they see fit. |
||
128 | -! | +||
24 | +
- modalDialog(+ #' |
||
129 | -! | +||
25 | +
- textInput(ns("snapshot_name"), "Name the snapshot", width = "100%", placeholder = "Meaningful, unique name"),+ #' For every snapshot except the initial one, a piece of UI is generated that contains |
||
130 | -! | +||
26 | +
- footer = tagList(+ #' the snapshot name, a select button to restore that snapshot, and a save button to save it to a file. |
||
131 | -! | +||
27 | +
- actionButton(ns("snapshot_name_accept"), "Accept", icon = icon("thumbs-up")),+ #' The initial snapshot is restored by a separate "reset" button. |
||
132 | -! | +||
28 | +
- modalButton(label = "Cancel", icon = icon("thumbs-down"))+ #' It cannot be saved directly but a user is welcome to capture the initial state as a snapshot and save that. |
||
133 | +29 |
- ),+ #' |
|
134 | -! | +||
30 | +
- size = "s"+ #' @section Snapshot mechanics: |
||
135 | +31 |
- )+ #' When a snapshot is captured, the user is prompted to name it. |
|
136 | +32 |
- )+ #' Names are displayed as is but since they are used to create button ids, |
|
137 | +33 |
- })+ #' under the hood they are converted to syntactically valid strings. |
|
138 | +34 |
- # Store snaphsot.+ #' New snapshot names are validated so that their valid versions are unique. |
|
139 | -7x | +||
35 | +
- observeEvent(input$snapshot_name_accept, {+ #' Leading and trailing white space is trimmed. |
||
140 | -! | +||
36 | +
- snapshot_name <- trimws(input$snapshot_name)+ #' |
||
141 | -! | +||
37 | +
- if (identical(snapshot_name, "")) {+ #' The module can read the global state of the application from `slices_global` and `mapping_matrix`. |
||
142 | -! | +||
38 | +
- showNotification(+ #' The former provides a list of all existing `teal_slice`s and the latter says which slice is active in which module. |
||
143 | -! | +||
39 | +
- "Please name the snapshot.",+ #' Once a name has been accepted, `slices_global` is converted to a list of lists - a snapshot. |
||
144 | -! | +||
40 | +
- type = "message"+ #' The snapshot contains the `mapping` attribute of the initial application state |
||
145 | +41 |
- )+ #' (or one that has been restored), which may not reflect the current one, |
|
146 | -! | +||
42 | +
- updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name")+ #' so `mapping_matrix` is transformed to obtain the current mapping, i.e. a list that, |
||
147 | -! | +||
43 | +
- } else if (is.element(make.names(snapshot_name), make.names(names(snapshot_history())))) {+ #' when passed to the `mapping` argument of [`teal::teal_slices`], would result in the current mapping. |
||
148 | -! | +||
44 | +
- showNotification(+ #' This is substituted as the snapshot's `mapping` attribute and the snapshot is added to the snapshot list. |
||
149 | -! | +||
45 | +
- "This name is in conflict with other snapshot names. Please choose a different one.",+ #' |
||
150 | -! | +||
46 | +
- type = "message"+ #' To restore app state, a snapshot is retrieved from storage and rebuilt into a `teal_slices` object. |
||
151 | +47 |
- )+ #' Then state of all `FilteredData` objects (provided in `filtered_data_list`) is cleared |
|
152 | -! | +||
48 | +
- updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name")+ #' and set anew according to the `mapping` attribute of the snapshot. |
||
153 | +49 |
- } else {+ #' The snapshot is then set as the current content of `slices_global`. |
|
154 | -! | +||
50 | +
- snapshot <- as.list(slices_global(), recursive = TRUE)+ #' |
||
155 | -! | +||
51 | +
- attr(snapshot, "mapping") <- matrix_to_mapping(mapping_matrix())+ #' To save a snapshot, the snapshot is retrieved and reassembled just like for restoring, |
||
156 | -! | +||
52 | +
- snapshot_update <- c(snapshot_history(), list(snapshot))+ #' and then saved to file with [`slices_store`]. |
||
157 | -! | +||
53 | +
- names(snapshot_update)[length(snapshot_update)] <- snapshot_name+ #' |
||
158 | -! | +||
54 | +
- snapshot_history(snapshot_update)+ #' When a snapshot is uploaded, it will first be added to storage just like a newly created one, |
||
159 | -! | +||
55 | +
- removeModal()+ #' and then used to restore app state much like a snapshot taken from storage. |
||
160 | +56 |
- # Reopen filter manager modal by clicking button in the main application.+ #' Upon clicking the upload icon the user will be prompted for a file to upload |
|
161 | -! | +||
57 | +
- shinyjs::click(id = "teal-main_ui-filter_manager-show", asis = TRUE)+ #' and may choose to name the new snapshot. The name defaults to the name of the file (the extension is dropped) |
||
162 | +58 |
- }+ #' and normal naming rules apply. Loading the file yields a `teal_slices` object, |
|
163 | +59 |
- })+ #' which is disassembled for storage and used directly for restoring app state. |
|
164 | +60 |
-
+ #' |
|
165 | +61 |
- # Upload a snapshot file ----+ #' @section Transferring snapshots: |
|
166 | +62 |
- # Select file.+ #' Snapshots uploaded from disk should only be used in the same application they come from, |
|
167 | -7x | +||
63 | +
- observeEvent(input$snapshot_load, {+ #' _i.e._ an application that uses the same data and the same modules. |
||
168 | -! | +||
64 | +
- showModal(+ #' To ensure this is the case, `init` stamps `teal_slices` with an app id that is stored in the `app_id` attribute of |
||
169 | -! | +||
65 | +
- modalDialog(+ #' a `teal_slices` object. When a snapshot is restored from file, its `app_id` is compared to that |
||
170 | -! | +||
66 | +
- fileInput(ns("snapshot_file"), "Choose snapshot file", accept = ".json", width = "100%"),+ #' of the current app state and only if the match is the snapshot admitted to the session. |
||
171 | -! | +||
67 | +
- textInput(+ #' |
||
172 | -! | +||
68 | +
- ns("snapshot_name"),+ #' @param id (`character(1)`) `shiny` module id |
||
173 | -! | +||
69 | +
- "Name the snapshot (optional)",+ #' @param slices_global (`reactiveVal`) that contains a `teal_slices` object |
||
174 | -! | +||
70 | +
- width = "100%",+ #' containing all `teal_slice`s existing in the app, both active and inactive |
||
175 | -! | +||
71 | +
- placeholder = "Meaningful, unique name"+ #' @param mapping_matrix (`reactive`) that contains a `data.frame` representation |
||
176 | +72 |
- ),+ #' of the mapping of filter state ids (rows) to modules labels (columns); |
|
177 | -! | +||
73 | +
- footer = tagList(+ #' all columns are `logical` vectors |
||
178 | -! | +||
74 | +
- actionButton(ns("snaphot_file_accept"), "Accept", icon = icon("thumbs-up")),+ #' @param filtered_data_list non-nested (`named list`) that contains `FilteredData` objects |
||
179 | -! | +||
75 | +
- modalButton(label = "Cancel", icon = icon("thumbs-down"))+ #' |
||
180 | +76 |
- )+ #' @return Nothing is returned. |
|
181 | +77 |
- )+ #' |
|
182 | +78 |
- )+ #' @name snapshot_manager_module |
|
183 | +79 |
- })+ #' @aliases snapshot snapshot_manager |
|
184 | +80 |
- # Store new snapshot to list and restore filter states.+ #' |
|
185 | -7x | +||
81 | +
- observeEvent(input$snaphot_file_accept, {+ #' @author Aleksander Chlebowski |
||
186 | -! | +||
82 | +
- snapshot_name <- trimws(input$snapshot_name)+ #' |
||
187 | -! | +||
83 | +
- if (identical(snapshot_name, "")) {+ #' @rdname snapshot_manager_module |
||
188 | -! | +||
84 | +
- snapshot_name <- tools::file_path_sans_ext(input$snapshot_file$name)+ #' @keywords internal |
||
189 | +85 |
- }+ #' |
|
190 | -! | +||
86 | +
- if (is.element(make.names(snapshot_name), make.names(names(snapshot_history())))) {+ snapshot_manager_ui <- function(id) { |
||
191 | +87 | ! |
- showNotification(+ ns <- NS(id) |
192 | +88 | ! |
- "This name is in conflict with other snapshot names. Please choose a different one.",+ div( |
193 | +89 | ! |
- type = "message"- |
-
194 | -- |
- )+ class = "snapshot_manager_content", |
|
195 | +90 | ! |
- updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name")- |
-
196 | -- |
- } else {+ div( |
|
197 | -+ | ||
91 | +! |
- # Restore snapshot and verify app compatibility.+ class = "snapshot_table_row", |
|
198 | +92 | ! |
- snapshot_state <- try(slices_restore(input$snapshot_file$datapath))+ span(tags$b("Snapshot manager")), |
199 | +93 | ! |
- if (!inherits(snapshot_state, "modules_teal_slices")) {+ actionLink(ns("snapshot_add"), label = NULL, icon = icon("camera"), title = "add snapshot"), |
200 | +94 | ! |
- showNotification(+ actionLink(ns("snapshot_load"), label = NULL, icon = icon("upload"), title = "upload snapshot"), |
201 | +95 | ! |
- "File appears to be corrupt.",+ actionLink(ns("snapshot_reset"), label = NULL, icon = icon("undo"), title = "reset initial state"), |
202 | +96 | ! |
- type = "error"+ NULL |
203 | +97 |
- )+ ), |
|
204 | +98 | ! |
- } else if (!identical(attr(snapshot_state, "app_id"), attr(slices_global(), "app_id"))) {+ uiOutput(ns("snapshot_list")) |
205 | -! | +||
99 | +
- showNotification(+ ) |
||
206 | -! | +||
100 | +
- "This snapshot file is not compatible with the app and cannot be loaded.",+ } |
||
207 | -! | +||
101 | +
- type = "warning"+ |
||
208 | +102 |
- )+ #' @rdname snapshot_manager_module |
|
209 | +103 |
- } else {+ #' @keywords internal |
|
210 | +104 |
- # Add to snapshot history.+ #' |
|
211 | -! | +||
105 | +
- snapshot <- as.list(snapshot_state, recursive = TRUE)+ snapshot_manager_srv <- function(id, slices_global, mapping_matrix, filtered_data_list) { |
||
212 | -! | +||
106 | +7x |
- snapshot_update <- c(snapshot_history(), list(snapshot))+ checkmate::assert_character(id) |
|
213 | -! | +||
107 | +7x |
- names(snapshot_update)[length(snapshot_update)] <- snapshot_name+ checkmate::assert_true(is.reactive(slices_global)) |
|
214 | -! | +||
108 | +7x |
- snapshot_history(snapshot_update)+ checkmate::assert_class(isolate(slices_global()), "teal_slices") |
|
215 | -+ | ||
109 | +7x |
- ### Begin simplified restore procedure. ###+ checkmate::assert_true(is.reactive(mapping_matrix)) |
|
216 | -! | +||
110 | +7x |
- mapping_unfolded <- unfold_mapping(attr(snapshot_state, "mapping"), names(filtered_data_list))+ checkmate::assert_data_frame(isolate(mapping_matrix()), null.ok = TRUE) |
|
217 | -! | +||
111 | +7x |
- mapply(+ checkmate::assert_list(filtered_data_list, types = "FilteredData", any.missing = FALSE, names = "named") |
|
218 | -! | +||
112 | +
- function(filtered_data, filter_ids) {+ |
||
219 | -! | +||
113 | +7x |
- filtered_data$clear_filter_states(force = TRUE)+ moduleServer(id, function(input, output, session) { |
|
220 | -! | +||
114 | +7x |
- slices <- Filter(function(x) x$id %in% filter_ids, snapshot_state)- |
- |
221 | -! | -
- filtered_data$set_filter_state(slices)+ ns <- session$ns |
|
222 | +115 |
- },- |
- |
223 | -! | -
- filtered_data = filtered_data_list,- |
- |
224 | -! | -
- filter_ids = mapping_unfolded+ |
|
225 | +116 |
- )+ # Store global filter states ---- |
|
226 | -! | +||
117 | +7x |
- slices_global(snapshot_state)+ filter <- isolate(slices_global()) |
|
227 | -! | +||
118 | +7x |
- removeModal()+ snapshot_history <- reactiveVal({ |
|
228 | -+ | ||
119 | +7x |
- ### End simplified restore procedure. ###+ list( |
|
229 | -+ | ||
120 | +7x |
- }+ "Initial application state" = as.list(filter, recursive = TRUE) |
|
230 | +121 |
- }+ ) |
|
231 | +122 |
}) |
|
232 | +123 |
- # Apply newly added snapshot.+ |
|
233 | +124 |
-
+ # Snapshot current application state ---- |
|
234 | +125 |
- # Restore initial state ----+ # Name snaphsot. |
|
235 | +126 | 7x |
- observeEvent(input$snapshot_reset, {+ observeEvent(input$snapshot_add, { |
236 | +127 | ! |
- s <- "Initial application state"+ showModal( |
237 | -+ | ||
128 | +! |
- ### Begin restore procedure. ###+ modalDialog( |
|
238 | +129 | ! |
- snapshot <- snapshot_history()[[s]]+ textInput(ns("snapshot_name"), "Name the snapshot", width = "100%", placeholder = "Meaningful, unique name"), |
239 | +130 | ! |
- snapshot_state <- as.teal_slices(snapshot)+ footer = tagList( |
240 | +131 | ! |
- mapping_unfolded <- unfold_mapping(attr(snapshot_state, "mapping"), names(filtered_data_list))+ actionButton(ns("snapshot_name_accept"), "Accept", icon = icon("thumbs-up")), |
241 | +132 | ! |
- mapply(+ modalButton(label = "Cancel", icon = icon("thumbs-down")) |
242 | -! | +||
133 | +
- function(filtered_data, filter_ids) {+ ), |
||
243 | +134 | ! |
- filtered_data$clear_filter_states(force = TRUE)+ size = "s"+ |
+
135 | ++ |
+ )+ |
+ |
136 | ++ |
+ )+ |
+ |
137 | ++ |
+ })+ |
+ |
138 | ++ |
+ # Store snaphsot.+ |
+ |
139 | +7x | +
+ observeEvent(input$snapshot_name_accept, { |
|
244 | +140 | ! |
- slices <- Filter(function(x) x$id %in% filter_ids, snapshot_state)+ snapshot_name <- trimws(input$snapshot_name) |
245 | +141 | ! |
- filtered_data$set_filter_state(slices)+ if (identical(snapshot_name, "")) { |
246 | -+ | ||
142 | +! |
- },+ showNotification( |
|
247 | +143 | ! |
- filtered_data = filtered_data_list,+ "Please name the snapshot.", |
248 | +144 | ! |
- filter_ids = mapping_unfolded+ type = "message" |
249 | +145 |
- )+ ) |
|
250 | +146 | ! |
- slices_global(snapshot_state)+ updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name") |
251 | +147 | ! |
- removeModal()+ } else if (is.element(make.names(snapshot_name), make.names(names(snapshot_history())))) { |
252 | -+ | ||
148 | +! |
- ### End restore procedure. ###+ showNotification( |
|
253 | -+ | ||
149 | +! |
- })+ "This name is in conflict with other snapshot names. Please choose a different one.", |
|
254 | -+ | ||
150 | +! |
-
+ type = "message" |
|
255 | +151 |
- # Build snapshot table ----+ ) |
|
256 | -+ | ||
152 | +! |
- # Create UI elements and server logic for the snapshot table.+ updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name") |
|
257 | +153 |
- # Observers must be tracked to avoid duplication and excess reactivity.+ } else { |
|
258 | -+ | ||
154 | +! |
- # Remaining elements are tracked likewise for consistency and a slight speed margin.+ snapshot <- as.list(slices_global(), recursive = TRUE) |
|
259 | -7x | +||
155 | +! |
- observers <- reactiveValues()+ attr(snapshot, "mapping") <- matrix_to_mapping(mapping_matrix()) |
|
260 | -7x | +||
156 | +! |
- handlers <- reactiveValues()+ snapshot_update <- c(snapshot_history(), list(snapshot)) |
|
261 | -7x | -
- divs <- reactiveValues()- |
- |
262 | -- | - - | -|
263 | -7x | +||
157 | +! |
- observeEvent(snapshot_history(), {+ names(snapshot_update)[length(snapshot_update)] <- snapshot_name |
|
264 | -3x | +||
158 | +! |
- lapply(names(snapshot_history())[-1L], function(s) {+ snapshot_history(snapshot_update) |
|
265 | +159 | ! |
- id_pickme <- sprintf("pickme_%s", make.names(s))+ removeModal() |
266 | -! | +||
160 | +
- id_saveme <- sprintf("saveme_%s", make.names(s))+ # Reopen filter manager modal by clicking button in the main application. |
||
267 | +161 | ! |
- id_rowme <- sprintf("rowme_%s", make.names(s))+ shinyjs::click(id = "teal-main_ui-filter_manager-show", asis = TRUE) |
268 | +162 |
-
+ } |
|
269 | +163 |
- # Observer for restoring snapshot.+ }) |
|
270 | -! | +||
164 | +
- if (!is.element(id_pickme, names(observers))) {+ |
||
271 | -! | +||
165 | +
- observers[[id_pickme]] <- observeEvent(input[[id_pickme]], {+ # Upload a snapshot file ---- |
||
272 | +166 |
- ### Begin restore procedure. ###+ # Select file.+ |
+ |
167 | +7x | +
+ observeEvent(input$snapshot_load, { |
|
273 | +168 | ! |
- snapshot <- snapshot_history()[[s]]+ showModal( |
274 | +169 | ! |
- snapshot_state <- as.teal_slices(snapshot)+ modalDialog( |
275 | +170 | ! |
- mapping_unfolded <- unfold_mapping(attr(snapshot_state, "mapping"), names(filtered_data_list))+ fileInput(ns("snapshot_file"), "Choose snapshot file", accept = ".json", width = "100%"), |
276 | +171 | ! |
- mapply(+ textInput( |
277 | +172 | ! |
- function(filtered_data, filter_ids) {+ ns("snapshot_name"), |
278 | +173 | ! |
- filtered_data$clear_filter_states(force = TRUE)+ "Name the snapshot (optional)", |
279 | +174 | ! |
- slices <- Filter(function(x) x$id %in% filter_ids, snapshot_state)+ width = "100%", |
280 | +175 | ! |
- filtered_data$set_filter_state(slices)+ placeholder = "Meaningful, unique name" |
281 | +176 |
- },+ ), |
|
282 | +177 | ! |
- filtered_data = filtered_data_list,+ footer = tagList( |
283 | +178 | ! |
- filter_ids = mapping_unfolded- |
-
284 | -- |
- )+ actionButton(ns("snaphot_file_accept"), "Accept", icon = icon("thumbs-up")), |
|
285 | +179 | ! |
- slices_global(snapshot_state)+ modalButton(label = "Cancel", icon = icon("thumbs-down")) |
286 | -! | +||
180 | +
- removeModal()+ ) |
||
287 | +181 |
- ### End restore procedure. ###+ ) |
|
288 | +182 |
- })+ ) |
|
289 | +183 |
- }+ }) |
|
290 | +184 |
- # Create handler for downloading snapshot.+ # Store new snapshot to list and restore filter states. |
|
291 | -! | +||
185 | +7x |
- if (!is.element(id_saveme, names(handlers))) {+ observeEvent(input$snaphot_file_accept, { |
|
292 | +186 | ! |
- output[[id_saveme]] <- downloadHandler(+ snapshot_name <- trimws(input$snapshot_name) |
293 | +187 | ! |
- filename = function() {+ if (identical(snapshot_name, "")) { |
294 | +188 | ! |
- sprintf("teal_snapshot_%s_%s.json", s, Sys.Date())+ snapshot_name <- tools::file_path_sans_ext(input$snapshot_file$name) |
295 | +189 |
- },+ } |
|
296 | +190 | ! |
- content = function(file) {+ if (is.element(make.names(snapshot_name), make.names(names(snapshot_history())))) { |
297 | +191 | ! |
- snapshot <- snapshot_history()[[s]]+ showNotification( |
298 | +192 | ! |
- snapshot_state <- as.teal_slices(snapshot)+ "This name is in conflict with other snapshot names. Please choose a different one.", |
299 | +193 | ! |
- slices_store(tss = snapshot_state, file = file)- |
-
300 | -- |
- }+ type = "message" |
|
301 | +194 |
- )+ ) |
|
302 | +195 | ! |
- handlers[[id_saveme]] <- id_saveme+ updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name") |
303 | +196 |
- }+ } else { |
|
304 | +197 |
- # Create a row for the snapshot table.- |
- |
305 | -! | -
- if (!is.element(id_rowme, names(divs))) {+ # Restore snapshot and verify app compatibility. |
|
306 | +198 | ! |
- divs[[id_rowme]] <- div(+ snapshot_state <- try(slices_restore(input$snapshot_file$datapath)) |
307 | +199 | ! |
- class = "snapshot_table_row",+ if (!inherits(snapshot_state, "modules_teal_slices")) { |
308 | +200 | ! |
- span(h5(s)),+ showNotification( |
309 | +201 | ! |
- actionLink(inputId = ns(id_pickme), label = icon("circle-check"), title = "select"),+ "File appears to be corrupt.", |
310 | +202 | ! |
- downloadLink(outputId = ns(id_saveme), label = icon("save"), title = "save to file")+ type = "error" |
311 | +203 |
) |
|
312 | -- |
- }+ | |
204 | +! | +
+ } else if (!identical(attr(snapshot_state, "app_id"), attr(slices_global(), "app_id"))) { |
|
313 | -+ | ||
205 | +! |
- })+ showNotification( |
|
314 | -+ | ||
206 | +! |
- })+ "This snapshot file is not compatible with the app and cannot be loaded.", |
|
315 | -+ | ||
207 | +! |
-
+ type = "warning" |
|
316 | +208 |
- # Create table to display list of snapshots and their actions.+ ) |
|
317 | -7x | +||
209 | +
- output$snapshot_list <- renderUI({+ } else { |
||
318 | -3x | +||
210 | +
- rows <- lapply(rev(reactiveValuesToList(divs)), function(d) d)+ # Add to snapshot history. |
||
319 | -3x | +||
211 | +! |
- if (length(rows) == 0L) {+ snapshot <- as.list(snapshot_state, recursive = TRUE) |
|
320 | -3x | +||
212 | +! |
- div(+ snapshot_update <- c(snapshot_history(), list(snapshot)) |
|
321 | -3x | +||
213 | +! |
- class = "snapshot_manager_placeholder",+ names(snapshot_update)[length(snapshot_update)] <- snapshot_name |
|
322 | -3x | +||
214 | +! |
- "Snapshots will appear here."+ snapshot_history(snapshot_update) |
|
323 | +215 |
- )+ ### Begin simplified restore procedure. ### |
|
324 | -+ | ||
216 | +! |
- } else {+ mapping_unfolded <- unfold_mapping(attr(snapshot_state, "mapping"), names(filtered_data_list)) |
|
325 | +217 | ! |
- rows+ mapply( |
326 | -+ | ||
218 | +! |
- }+ function(filtered_data, filter_ids) { |
|
327 | -+ | ||
219 | +! |
- })+ filtered_data$clear_filter_states(force = TRUE) |
|
328 | -+ | ||
220 | +! |
- })+ slices <- Filter(function(x) x$id %in% filter_ids, snapshot_state) |
|
329 | -+ | ||
221 | +! |
- }+ filtered_data$set_filter_state(slices) |
|
330 | +222 |
-
+ }, |
|
331 | -+ | ||
223 | +! |
-
+ filtered_data = filtered_data_list, |
|
332 | -+ | ||
224 | +! |
-
+ filter_ids = mapping_unfolded |
|
333 | +225 |
-
+ ) |
|
334 | -+ | ||
226 | +! |
- ### utility functions ----+ slices_global(snapshot_state) |
|
335 | -+ | ||
227 | +! |
-
+ removeModal() |
|
336 | +228 |
- #' Explicitly enumerate global filters.+ ### End simplified restore procedure. ### |
|
337 | +229 |
- #'+ } |
|
338 | +230 |
- #' Transform module mapping such that global filters are explicitly specified for every module.+ } |
|
339 | +231 |
- #'+ }) |
|
340 | +232 |
- #' @param mapping (`named list`) as stored in mapping parameter of `teal_slices`+ # Apply newly added snapshot. |
|
341 | +233 |
- #' @param module_names (`character`) vector containing names of all modules in the app+ |
|
342 | +234 |
- #' @return A `named_list` with one element per module, each element containing all filters applied to that module.+ # Restore initial state ---- |
|
343 | -+ | ||
235 | +7x |
- #' @keywords internal+ observeEvent(input$snapshot_reset, { |
|
344 | -+ | ||
236 | +! |
- #'+ s <- "Initial application state" |
|
345 | +237 |
- unfold_mapping <- function(mapping, module_names) {+ ### Begin restore procedure. ### |
|
346 | +238 | ! |
- module_names <- structure(module_names, names = module_names)+ snapshot <- snapshot_history()[[s]] |
347 | +239 | ! |
- lapply(module_names, function(x) c(mapping[[x]], mapping[["global_filters"]]))+ snapshot_state <- as.teal_slices(snapshot) |
348 | -+ | ||
240 | +! |
- }+ mapping_unfolded <- unfold_mapping(attr(snapshot_state, "mapping"), names(filtered_data_list)) |
|
349 | -+ | ||
241 | +! |
-
+ mapply( |
|
350 | -+ | ||
242 | +! |
- #' Convert mapping matrix to filter mapping specification.+ function(filtered_data, filter_ids) { |
|
351 | -+ | ||
243 | +! |
- #'+ filtered_data$clear_filter_states(force = TRUE) |
|
352 | -+ | ||
244 | +! |
- #' Transform a mapping matrix, i.e. a data frame that maps each filter state to each module,+ slices <- Filter(function(x) x$id %in% filter_ids, snapshot_state) |
|
353 | -+ | ||
245 | +! |
- #' to a list specification like the one used in the `mapping` attribute of `teal_slices`.+ filtered_data$set_filter_state(slices) |
|
354 | +246 |
- #' Global filters are gathered in one list element.+ }, |
|
355 | -+ | ||
247 | +! |
- #' If a module has no active filters but the global ones, it will not be mentioned in the output.+ filtered_data = filtered_data_list,+ |
+ |
248 | +! | +
+ filter_ids = mapping_unfolded |
|
356 | +249 |
- #'+ )+ |
+ |
250 | +! | +
+ slices_global(snapshot_state)+ |
+ |
251 | +! | +
+ removeModal() |
|
357 | +252 |
- #' @param mapping_matrix (`data.frame`) of logical vectors where+ ### End restore procedure. ### |
|
358 | +253 |
- #' columns represent modules and row represent `teal_slice`s+ }) |
|
359 | +254 |
- #' @return `named list` like that in the `mapping` attribute of a `teal_slices` object.+ |
|
360 | +255 |
- #' @keywords internal+ # Build snapshot table ---- |
|
361 | +256 |
- #'+ # Create UI elements and server logic for the snapshot table. |
|
362 | +257 |
- matrix_to_mapping <- function(mapping_matrix) {+ # Observers must be tracked to avoid duplication and excess reactivity. |
|
363 | -! | +||
258 | +
- mapping_matrix[] <- lapply(mapping_matrix, function(x) x | is.na(x))+ # Remaining elements are tracked likewise for consistency and a slight speed margin. |
||
364 | -! | +||
259 | +7x |
- global <- vapply(as.data.frame(t(mapping_matrix)), all, logical(1L))+ observers <- reactiveValues() |
|
365 | -! | +||
260 | +7x |
- global_filters <- names(global[global])+ handlers <- reactiveValues() |
|
366 | -! | +||
261 | +7x |
- local_filters <- mapping_matrix[!rownames(mapping_matrix) %in% global_filters, ]+ divs <- reactiveValues() |
|
367 | +262 | ||
368 | -! | +||
263 | +7x |
- mapping <- c(lapply(local_filters, function(x) rownames(local_filters)[x]), list(global_filters = global_filters))+ observeEvent(snapshot_history(), {+ |
+ |
264 | +3x | +
+ lapply(names(snapshot_history())[-1L], function(s) { |
|
369 | +265 | ! |
- Filter(function(x) length(x) != 0L, mapping)+ id_pickme <- sprintf("pickme_%s", make.names(s)) |
370 | -+ | ||
266 | +! |
- }+ id_saveme <- sprintf("saveme_%s", make.names(s)) |
1 | -+ | ||
267 | +! |
- # This module is the main teal module that puts everything together.+ id_rowme <- sprintf("rowme_%s", make.names(s)) |
|
2 | +268 | ||
3 | +269 |
- #' teal main app module+ # Observer for restoring snapshot. |
|
4 | -+ | ||
270 | +! |
- #'+ if (!is.element(id_pickme, names(observers))) { |
|
5 | -+ | ||
271 | +! |
- #' This is the main teal app that puts everything together.+ observers[[id_pickme]] <- observeEvent(input[[id_pickme]], { |
|
6 | +272 |
- #'+ ### Begin restore procedure. ### |
|
7 | -+ | ||
273 | +! |
- #' It displays the splash UI which is used to fetch the data, possibly+ snapshot <- snapshot_history()[[s]] |
|
8 | -+ | ||
274 | +! |
- #' prompting for a password input to fetch the data. Once the data is ready,+ snapshot_state <- as.teal_slices(snapshot) |
|
9 | -+ | ||
275 | +! |
- #' the splash screen is replaced by the actual teal UI that is tabsetted and+ mapping_unfolded <- unfold_mapping(attr(snapshot_state, "mapping"), names(filtered_data_list)) |
|
10 | -+ | ||
276 | +! |
- #' has a filter panel with `datanames` that are relevant for the current tab.+ mapply( |
|
11 | -+ | ||
277 | +! |
- #' Nested tabs are possible, but we limit it to two nesting levels for reasons+ function(filtered_data, filter_ids) { |
|
12 | -+ | ||
278 | +! |
- #' of clarity of the UI.+ filtered_data$clear_filter_states(force = TRUE) |
|
13 | -+ | ||
279 | +! |
- #'+ slices <- Filter(function(x) x$id %in% filter_ids, snapshot_state) |
|
14 | -+ | ||
280 | +! |
- #' The splash screen functionality can also be used+ filtered_data$set_filter_state(slices) |
|
15 | +281 |
- #' for non-delayed data which takes time to load into memory, avoiding+ }, |
|
16 | -+ | ||
282 | +! |
- #' Shiny session timeouts.+ filtered_data = filtered_data_list, |
|
17 | -+ | ||
283 | +! |
- #'+ filter_ids = mapping_unfolded |
|
18 | +284 |
- #' Server evaluates the `teal_data_rv` (delayed data mechanism) and creates the+ ) |
|
19 | -+ | ||
285 | +! |
- #' `datasets` object that is shared across modules.+ slices_global(snapshot_state) |
|
20 | -+ | ||
286 | +! |
- #' Once it is ready and non-`NULL`, the splash screen is replaced by the+ removeModal() |
|
21 | +287 |
- #' main teal UI that depends on the data.+ ### End restore procedure. ### |
|
22 | +288 |
- #' The currently active tab is tracked and the right filter panel+ }) |
|
23 | +289 |
- #' updates the displayed datasets to filter for according to the active `datanames`+ } |
|
24 | +290 |
- #' of the tab.+ # Create handler for downloading snapshot. |
|
25 | -+ | ||
291 | +! |
- #'+ if (!is.element(id_saveme, names(handlers))) { |
|
26 | -+ | ||
292 | +! |
- #' It is written as a Shiny module so it can be added into other apps as well.+ output[[id_saveme]] <- downloadHandler( |
|
27 | -+ | ||
293 | +! |
- #'+ filename = function() { |
|
28 | -+ | ||
294 | +! |
- #' @name module_teal+ sprintf("teal_snapshot_%s_%s.json", s, Sys.Date()) |
|
29 | +295 |
- #'+ }, |
|
30 | -+ | ||
296 | +! |
- #' @inheritParams ui_teal_with_splash+ content = function(file) { |
|
31 | -+ | ||
297 | +! |
- #'+ snapshot <- snapshot_history()[[s]] |
|
32 | -+ | ||
298 | +! |
- #' @param splash_ui (`shiny.tag`)\cr UI to display initially,+ snapshot_state <- as.teal_slices(snapshot) |
|
33 | -+ | ||
299 | +! |
- #' can be a splash screen or a Shiny module UI. For the latter, see+ slices_store(tss = snapshot_state, file = file) |
|
34 | +300 |
- #' [init()] about how to call the corresponding server function.+ } |
|
35 | +301 |
- #'+ ) |
|
36 | -+ | ||
302 | +! |
- #' @param teal_data_rv (`reactive`)\cr+ handlers[[id_saveme]] <- id_saveme |
|
37 | +303 |
- #' returns the `teal_data`, only evaluated once, `NULL` value is ignored+ } |
|
38 | +304 |
- #'+ # Create a row for the snapshot table. |
|
39 | -+ | ||
305 | +! |
- #' @return+ if (!is.element(id_rowme, names(divs))) { |
|
40 | -+ | ||
306 | +! |
- #' `ui_teal` returns `HTML` for Shiny module UI.+ divs[[id_rowme]] <- div( |
|
41 | -+ | ||
307 | +! |
- #' `srv_teal` returns `reactive` which returns the currently active module.+ class = "snapshot_table_row", |
|
42 | -+ | ||
308 | +! |
- #'+ span(h5(s)), |
|
43 | -+ | ||
309 | +! |
- #' @keywords internal+ actionLink(inputId = ns(id_pickme), label = icon("circle-check"), title = "select"), |
|
44 | -+ | ||
310 | +! |
- #'+ downloadLink(outputId = ns(id_saveme), label = icon("save"), title = "save to file") |
|
45 | +311 |
- #' @examples+ ) |
|
46 | +312 |
- #' mods <- teal:::example_modules()+ } |
|
47 | +313 |
- #' teal_data_rv <- reactive(teal:::example_cdisc_data())+ }) |
|
48 | +314 |
- #' app <- shinyApp(+ }) |
|
49 | +315 |
- #' ui = function() {+ |
|
50 | +316 |
- #' teal:::ui_teal("dummy")+ # Create table to display list of snapshots and their actions. |
|
51 | -+ | ||
317 | +7x |
- #' },+ output$snapshot_list <- renderUI({ |
|
52 | -+ | ||
318 | +3x |
- #' server = function(input, output, session) {+ rows <- lapply(rev(reactiveValuesToList(divs)), function(d) d) |
|
53 | -+ | ||
319 | +3x |
- #' active_module <- teal:::srv_teal(id = "dummy", modules = mods, teal_data_rv = teal_data_rv)+ if (length(rows) == 0L) { |
|
54 | -+ | ||
320 | +3x |
- #' }+ div( |
|
55 | -+ | ||
321 | +3x |
- #' )+ class = "snapshot_manager_placeholder", |
|
56 | -+ | ||
322 | +3x |
- #' if (interactive()) {+ "Snapshots will appear here." |
|
57 | +323 |
- #' shinyApp(app$ui, app$server)+ ) |
|
58 | +324 |
- #' }+ } else { |
|
59 | -+ | ||
325 | +! |
- NULL+ rows |
|
60 | +326 |
-
+ } |
|
61 | +327 |
- #' @rdname module_teal+ }) |
|
62 | +328 |
- ui_teal <- function(id,+ }) |
|
63 | +329 |
- splash_ui = tags$h2("Starting the Teal App"),+ } |
|
64 | +330 |
- title = NULL,+ |
|
65 | +331 |
- header = tags$p(""),+ |
|
66 | +332 |
- footer = tags$p("")) {- |
- |
67 | -22x | -
- if (checkmate::test_string(header)) {- |
- |
68 | -! | -
- header <- tags$h1(header)+ |
|
69 | +333 |
- }- |
- |
70 | -22x | -
- if (checkmate::test_string(footer)) {- |
- |
71 | -! | -
- footer <- tags$p(footer)+ |
|
72 | +334 |
- }+ ### utility functions ---- |
|
73 | -22x | +||
335 | +
- checkmate::assert(+ |
||
74 | -22x | +||
336 | +
- checkmate::check_class(splash_ui, "shiny.tag"),+ #' Explicitly enumerate global filters. |
||
75 | -22x | +||
337 | +
- checkmate::check_class(splash_ui, "shiny.tag.list"),+ #' |
||
76 | -22x | +||
338 | +
- checkmate::check_class(splash_ui, "html")+ #' Transform module mapping such that global filters are explicitly specified for every module. |
||
77 | +339 |
- )+ #' |
|
78 | -22x | +||
340 | +
- checkmate::assert(+ #' @param mapping (`named list`) as stored in mapping parameter of `teal_slices` |
||
79 | -22x | +||
341 | +
- checkmate::check_class(header, "shiny.tag"),+ #' @param module_names (`character`) vector containing names of all modules in the app |
||
80 | -22x | +||
342 | +
- checkmate::check_class(header, "shiny.tag.list"),+ #' @return A `named_list` with one element per module, each element containing all filters applied to that module. |
||
81 | -22x | +||
343 | +
- checkmate::check_class(header, "html")+ #' @keywords internal |
||
82 | +344 |
- )+ #' |
|
83 | -22x | +||
345 | +
- checkmate::assert(+ unfold_mapping <- function(mapping, module_names) { |
||
84 | -22x | +||
346 | +! |
- checkmate::check_class(footer, "shiny.tag"),+ module_names <- structure(module_names, names = module_names) |
|
85 | -22x | +||
347 | +! |
- checkmate::check_class(footer, "shiny.tag.list"),+ lapply(module_names, function(x) c(mapping[[x]], mapping[["global_filters"]])) |
|
86 | -22x | +||
348 | +
- checkmate::check_class(footer, "html")+ } |
||
87 | +349 |
- )+ |
|
88 | +350 |
-
+ #' Convert mapping matrix to filter mapping specification. |
|
89 | -22x | +||
351 | +
- ns <- NS(id)+ #' |
||
90 | +352 |
- # Once the data is loaded, we will remove this element and add the real teal UI instead+ #' Transform a mapping matrix, i.e. a data frame that maps each filter state to each module, |
|
91 | -22x | +||
353 | +
- splash_ui <- div(+ #' to a list specification like the one used in the `mapping` attribute of `teal_slices`. |
||
92 | +354 |
- # id so we can remove the splash screen once ready, which is the first child of this container+ #' Global filters are gathered in one list element. |
|
93 | -22x | +||
355 | +
- id = ns("main_ui_container"),+ #' If a module has no active filters but the global ones, it will not be mentioned in the output. |
||
94 | +356 |
- # we put it into a div, so it can easily be removed as a whole, also when it is a tagList (and not+ #' |
|
95 | +357 |
- # just the first item of the tagList)+ #' @param mapping_matrix (`data.frame`) of logical vectors where |
|
96 | -22x | +||
358 | +
- div(splash_ui)+ #' columns represent modules and row represent `teal_slice`s |
||
97 | +359 |
- )+ #' @return `named list` like that in the `mapping` attribute of a `teal_slices` object. |
|
98 | +360 |
-
+ #' @keywords internal |
|
99 | +361 |
- # show busy icon when shiny session is busy computing stuff+ #' |
|
100 | +362 |
- # based on https://stackoverflow.com/questions/17325521/r-shiny-display-loading-message-while-function-is-running/22475216#22475216 #nolint+ matrix_to_mapping <- function(mapping_matrix) { |
|
101 | -22x | +||
363 | +! |
- shiny_busy_message_panel <- conditionalPanel(+ mapping_matrix[] <- lapply(mapping_matrix, function(x) x | is.na(x)) |
|
102 | -22x | +||
364 | +! |
- condition = "(($('html').hasClass('shiny-busy')) && (document.getElementById('shiny-notification-panel') == null))", # nolint+ global <- vapply(as.data.frame(t(mapping_matrix)), all, logical(1L)) |
|
103 | -22x | +||
365 | +! |
- div(+ global_filters <- names(global[global]) |
|
104 | -22x | +||
366 | +! |
- icon("arrows-rotate", "spin fa-spin"),+ local_filters <- mapping_matrix[!rownames(mapping_matrix) %in% global_filters, ] |
|
105 | -22x | +||
367 | +
- "Computing ...",+ |
||
106 | -+ | ||
368 | +! |
- # CSS defined in `custom.css`+ mapping <- c(lapply(local_filters, function(x) rownames(local_filters)[x]), list(global_filters = global_filters)) |
|
107 | -22x | +||
369 | +! |
- class = "shinybusymessage"+ Filter(function(x) length(x) != 0L, mapping) |
|
108 | +370 |
- )+ } |
109 | +1 |
- )+ # This module is the main teal module that puts everything together. |
||
110 | +2 | |||
111 | -22x | +|||
3 | +
- res <- fluidPage(+ #' teal main app module |
|||
112 | -22x | +|||
4 | +
- title = title,+ #' |
|||
113 | -22x | +|||
5 | +
- theme = get_teal_bs_theme(),+ #' This is the main teal app that puts everything together. |
|||
114 | -22x | +|||
6 | +
- include_teal_css_js(),+ #' |
|||
115 | -22x | +|||
7 | +
- tags$header(header),+ #' It displays the splash UI which is used to fetch the data, possibly |
|||
116 | -22x | +|||
8 | +
- tags$hr(class = "my-2"),+ #' prompting for a password input to fetch the data. Once the data is ready, |
|||
117 | -22x | +|||
9 | +
- shiny_busy_message_panel,+ #' the splash screen is replaced by the actual teal UI that is tabsetted and |
|||
118 | -22x | +|||
10 | +
- splash_ui,+ #' has a filter panel with `datanames` that are relevant for the current tab. |
|||
119 | -22x | +|||
11 | +
- tags$hr(),+ #' Nested tabs are possible, but we limit it to two nesting levels for reasons |
|||
120 | -22x | +|||
12 | +
- tags$footer(+ #' of clarity of the UI. |
|||
121 | -22x | +|||
13 | +
- div(+ #' |
|||
122 | -22x | +|||
14 | +
- footer,+ #' The splash screen functionality can also be used |
|||
123 | -22x | +|||
15 | +
- teal.widgets::verbatim_popup_ui(ns("sessionInfo"), "Session Info", type = "link"),+ #' for non-delayed data which takes time to load into memory, avoiding |
|||
124 | -22x | +|||
16 | +
- textOutput(ns("identifier"))+ #' Shiny session timeouts. |
|||
125 | +17 |
- )+ #' |
||
126 | +18 |
- )+ #' Server evaluates the `teal_data_rv` (delayed data mechanism) and creates the |
||
127 | +19 |
- )+ #' `datasets` object that is shared across modules. |
||
128 | -22x | +|||
20 | +
- return(res)+ #' Once it is ready and non-`NULL`, the splash screen is replaced by the |
|||
129 | +21 |
- }+ #' main teal UI that depends on the data. |
||
130 | +22 |
-
+ #' The currently active tab is tracked and the right filter panel |
||
131 | +23 |
-
+ #' updates the displayed datasets to filter for according to the active `datanames` |
||
132 | +24 |
- #' @rdname module_teal+ #' of the tab. |
||
133 | +25 |
- srv_teal <- function(id, modules, teal_data_rv, filter = teal_slices()) {+ #' |
||
134 | -17x | +|||
26 | +
- stopifnot(is.reactive(teal_data_rv))+ #' It is written as a Shiny module so it can be added into other apps as well. |
|||
135 | -16x | +|||
27 | +
- moduleServer(id, function(input, output, session) {+ #' |
|||
136 | -16x | +|||
28 | +
- logger::log_trace("srv_teal initializing the module.")+ #' @name module_teal |
|||
137 | +29 |
-
+ #' |
||
138 | -16x | -
- output$identifier <- renderText(+ | ||
30 | ++ |
+ #' @inheritParams ui_teal_with_splash |
||
139 | -16x | +|||
31 | +
- paste0("Pid:", Sys.getpid(), " Token:", substr(session$token, 25, 32))+ #' |
|||
140 | +32 |
- )+ #' @param splash_ui (`shiny.tag`)\cr UI to display initially, |
||
141 | +33 |
-
+ #' can be a splash screen or a Shiny module UI. For the latter, see |
||
142 | -16x | +|||
34 | +
- teal.widgets::verbatim_popup_srv(+ #' [init()] about how to call the corresponding server function. |
|||
143 | -16x | +|||
35 | +
- "sessionInfo",+ #' |
|||
144 | -16x | +|||
36 | +
- verbatim_content = utils::capture.output(utils::sessionInfo()),+ #' @param teal_data_rv (`reactive`)\cr |
|||
145 | -16x | +|||
37 | +
- title = "SessionInfo"+ #' returns the `teal_data`, only evaluated once, `NULL` value is ignored |
|||
146 | +38 |
- )+ #' |
||
147 | +39 |
-
+ #' @return |
||
148 | +40 |
- # `JavaScript` code+ #' `ui_teal` returns `HTML` for Shiny module UI. |
||
149 | -16x | +|||
41 | +
- run_js_files(files = "init.js") # `JavaScript` code to make the clipboard accessible+ #' `srv_teal` returns `reactive` which returns the currently active module. |
|||
150 | +42 |
- # set timezone in shiny app+ #' |
||
151 | +43 |
- # timezone is set in the early beginning so it will be available also+ #' @keywords internal |
||
152 | +44 |
- # for `DDL` and all shiny modules+ #' |
||
153 | -16x | +|||
45 | +
- get_client_timezone(session$ns)+ #' @examples |
|||
154 | -16x | +|||
46 | +
- observeEvent(+ #' mods <- teal:::example_modules() |
|||
155 | -16x | +|||
47 | +
- eventExpr = input$timezone,+ #' teal_data_rv <- reactive(teal:::example_cdisc_data()) |
|||
156 | -16x | +|||
48 | +
- once = TRUE,+ #' app <- shinyApp( |
|||
157 | -16x | +|||
49 | +
- handlerExpr = {+ #' ui = function() { |
|||
158 | -! | +|||
50 | +
- session$userData$timezone <- input$timezone+ #' teal:::ui_teal("dummy") |
|||
159 | -! | +|||
51 | +
- logger::log_trace("srv_teal@1 Timezone set to client's timezone: { input$timezone }.")+ #' }, |
|||
160 | +52 |
- }+ #' server = function(input, output, session) { |
||
161 | +53 |
- )+ #' active_module <- teal:::srv_teal(id = "dummy", modules = mods, teal_data_rv = teal_data_rv) |
||
162 | +54 |
-
+ #' } |
||
163 | -16x | +|||
55 | +
- reporter <- teal.reporter::Reporter$new()+ #' ) |
|||
164 | -16x | +|||
56 | +
- if (is_arg_used(modules, "reporter") && length(extract_module(modules, "teal_module_previewer")) == 0) {+ #' if (interactive()) { |
|||
165 | -! | +|||
57 | +
- modules <- append_module(modules, reporter_previewer_module())+ #' shinyApp(app$ui, app$server) |
|||
166 | +58 |
- }+ #' } |
||
167 | +59 |
-
+ NULL |
||
168 | -16x | +|||
60 | +
- env <- environment()+ |
|||
169 | -16x | +|||
61 | +
- datasets_reactive <- eventReactive(teal_data_rv(), {+ #' @rdname module_teal |
|||
170 | -5x | +|||
62 | +
- env$progress <- shiny::Progress$new(session)+ ui_teal <- function(id, |
|||
171 | -5x | +|||
63 | +
- env$progress$set(0.25, message = "Setting data")+ splash_ui = tags$h2("Starting the Teal App"), |
|||
172 | +64 |
-
+ title = NULL, |
||
173 | +65 |
- # create a list of data following structure of the nested modules list structure.+ header = tags$p(""), |
||
174 | +66 |
- # Because it's easier to unpack modules and datasets when they follow the same nested structure.+ footer = tags$p("")) { |
||
175 | -5x | +67 | +22x |
- datasets_singleton <- teal_data_to_filtered_data(teal_data_rv())+ if (checkmate::test_string(header)) { |
176 | -+ | |||
68 | +! |
-
+ header <- tags$h1(header) |
||
177 | +69 |
- # Singleton starts with only global filters active.+ } |
||
178 | -5x | +70 | +22x |
- filter_global <- Filter(function(x) x$id %in% attr(filter, "mapping")$global_filters, filter)+ if (checkmate::test_string(footer)) { |
179 | -5x | +|||
71 | +! |
- datasets_singleton$set_filter_state(filter_global)+ footer <- tags$p(footer) |
||
180 | +72 |
-
+ } |
||
181 | -5x | +73 | +22x |
- module_datasets <- function(modules) {+ checkmate::assert( |
182 | -20x | +74 | +22x |
- if (inherits(modules, "teal_modules")) {+ checkmate::check_class(splash_ui, "shiny.tag"), |
183 | -8x | +75 | +22x |
- datasets <- lapply(modules$children, module_datasets)+ checkmate::check_class(splash_ui, "shiny.tag.list"), |
184 | -8x | +76 | +22x |
- labels <- vapply(modules$children, `[[`, character(1), "label")+ checkmate::check_class(splash_ui, "html")+ |
+
77 | ++ |
+ ) |
||
185 | -8x | +78 | +22x |
- names(datasets) <- labels+ checkmate::assert( |
186 | -8x | +79 | +22x |
- datasets+ checkmate::check_class(header, "shiny.tag"), |
187 | -12x | +80 | +22x |
- } else if (isTRUE(attr(filter, "module_specific"))) {+ checkmate::check_class(header, "shiny.tag.list"), |
188 | -+ | |||
81 | +22x |
- # we should create FilteredData even if modules$datanames is null+ checkmate::check_class(header, "html") |
||
189 | +82 |
- # null controls a display of filter panel but data should be still passed+ ) |
||
190 | -3x | +83 | +22x |
- datanames <- if (is.null(modules$datanames) || modules$datanames == "all") {+ checkmate::assert( |
191 | -3x | +84 | +22x |
- include_parent_datanames(+ checkmate::check_class(footer, "shiny.tag"), |
192 | -3x | +85 | +22x |
- teal.data::datanames(teal_data_rv()),+ checkmate::check_class(footer, "shiny.tag.list"), |
193 | -3x | +86 | +22x |
- teal_data_rv()@join_keys+ checkmate::check_class(footer, "html") |
194 | +87 |
- )+ ) |
||
195 | +88 |
- } else {- |
- ||
196 | -! | -
- modules$datanames+ |
||
197 | -+ | |||
89 | +22x |
- }+ ns <- NS(id) |
||
198 | +90 |
- # todo: subset teal_data to datanames+ # Once the data is loaded, we will remove this element and add the real teal UI instead |
||
199 | -3x | +91 | +22x |
- datasets_module <- teal_data_to_filtered_data(teal_data_rv(), datanames = datanames)+ splash_ui <- div( |
200 | +92 |
-
+ # id so we can remove the splash screen once ready, which is the first child of this container+ |
+ ||
93 | +22x | +
+ id = ns("main_ui_container"), |
||
201 | +94 |
- # set initial filters+ # we put it into a div, so it can easily be removed as a whole, also when it is a tagList (and not |
||
202 | +95 |
- # - filtering filters for this module+ # just the first item of the tagList) |
||
203 | -3x | +96 | +22x |
- slices <- Filter(x = filter, f = function(x) {+ div(splash_ui) |
204 | -! | +|||
97 | +
- x$id %in% unique(unlist(attr(filter, "mapping")[c(modules$label, "global_filters")])) &&+ ) |
|||
205 | -! | +|||
98 | +
- x$dataname %in% datanames+ |
|||
206 | +99 |
- })+ # show busy icon when shiny session is busy computing stuff |
||
207 | -3x | +|||
100 | +
- include_varnames <- attr(slices, "include_varnames")[names(attr(slices, "include_varnames")) %in% datanames]+ # based on https://stackoverflow.com/questions/17325521/r-shiny-display-loading-message-while-function-is-running/22475216#22475216 #nolint |
|||
208 | -3x | +101 | +22x |
- exclude_varnames <- attr(slices, "exclude_varnames")[names(attr(slices, "exclude_varnames")) %in% datanames]+ shiny_busy_message_panel <- conditionalPanel( |
209 | -3x | +102 | +22x |
- slices$include_varnames <- include_varnames+ condition = "(($('html').hasClass('shiny-busy')) && (document.getElementById('shiny-notification-panel') == null))", # nolint |
210 | -3x | +103 | +22x |
- slices$exclude_varnames <- exclude_varnames+ div( |
211 | -3x | +104 | +22x |
- datasets_module$set_filter_state(slices)+ icon("arrows-rotate", "spin fa-spin"), |
212 | -3x | +105 | +22x |
- datasets_module+ "Computing ...", |
213 | +106 |
- } else {+ # CSS defined in `custom.css` |
||
214 | -9x | +107 | +22x |
- datasets_singleton+ class = "shinybusymessage" |
215 | +108 |
- }+ ) |
||
216 | +109 |
- }- |
- ||
217 | -5x | -
- module_datasets(modules)+ ) |
||
218 | +110 |
- })+ |
||
219 | -+ | |||
111 | +22x |
-
+ res <- fluidPage( |
||
220 | -+ | |||
112 | +22x |
- # Replace splash / welcome screen once data is loaded ----+ title = title, |
||
221 | -- |
- # ignoreNULL to not trigger at the beginning when data is NULL- |
- ||
222 | -- |
- # just handle it once because data obtained through delayed loading should- |
- ||
223 | -- |
- # usually not change afterwards- |
- ||
224 | -- |
- # if restored from bookmarked state, `filter` is ignored- |
- ||
225 | -+ | |||
113 | +22x |
-
+ theme = get_teal_bs_theme(), |
||
226 | -16x | +114 | +22x |
- observeEvent(datasets_reactive(), once = TRUE, {+ include_teal_css_js(), |
227 | -1x | +115 | +22x |
- logger::log_trace("srv_teal@5 setting main ui after data was pulled")+ tags$header(header), |
228 | -1x | +116 | +22x |
- on.exit(env$progress$close())+ tags$hr(class = "my-2"), |
229 | -1x | +117 | +22x |
- env$progress$set(0.5, message = "Setting up main UI")+ shiny_busy_message_panel, |
230 | -1x | +118 | +22x |
- datasets <- datasets_reactive()+ splash_ui, |
231 | -+ | |||
119 | +22x |
-
+ tags$hr(), |
||
232 | -+ | |||
120 | +22x |
- # main_ui_container contains splash screen first and we remove it and replace it by the real UI+ tags$footer( |
||
233 | -1x | +121 | +22x |
- removeUI(sprintf("#%s > div:nth-child(1)", session$ns("main_ui_container")))+ div( |
234 | -1x | +122 | +22x |
- insertUI(+ footer, |
235 | -1x | +123 | +22x |
- selector = paste0("#", session$ns("main_ui_container")),+ teal.widgets::verbatim_popup_ui(ns("sessionInfo"), "Session Info", type = "link"), |
236 | -1x | +124 | +22x |
- where = "beforeEnd",+ textOutput(ns("identifier")) |
237 | +125 |
- # we put it into a div, so it can easily be removed as a whole, also when it is a tagList (and not+ ) |
||
238 | +126 |
- # just the first item of the tagList)+ ) |
||
239 | -1x | +|||
127 | +
- ui = div(ui_tabs_with_filters(+ ) |
|||
240 | -1x | +128 | +22x |
- session$ns("main_ui"),+ return(res) |
241 | -1x | +|||
129 | +
- modules = modules,+ } |
|||
242 | -1x | +|||
130 | +
- datasets = datasets,+ |
|||
243 | -1x | +|||
131 | +
- filter = filter+ |
|||
244 | +132 |
- )),+ #' @rdname module_teal |
||
245 | +133 |
- # needed so that the UI inputs are available and can be immediately updated, otherwise, updating may not+ srv_teal <- function(id, modules, teal_data_rv, filter = teal_slices()) { |
||
246 | -+ | |||
134 | +21x |
- # have any effect as they are ignored when not present+ stopifnot(is.reactive(teal_data_rv)) |
||
247 | -1x | +135 | +20x |
- immediate = TRUE+ moduleServer(id, function(input, output, session) { |
248 | -+ | |||
136 | +20x |
- )+ logger::log_trace("srv_teal initializing the module.") |
||
249 | +137 | |||
138 | +20x | +
+ output$identifier <- renderText(+ |
+ ||
139 | +20x | +
+ paste0("Pid:", Sys.getpid(), " Token:", substr(session$token, 25, 32))+ |
+ ||
250 | +140 |
- # must make sure that this is only executed once as modules assume their observers are only+ ) |
||
251 | +141 |
- # registered once (calling server functions twice would trigger observers twice each time)+ |
||
252 | -1x | +142 | +20x |
- active_module <- srv_tabs_with_filters(+ teal.widgets::verbatim_popup_srv( |
253 | -1x | +143 | +20x |
- id = "main_ui",+ "sessionInfo", |
254 | -1x | +144 | +20x |
- datasets = datasets,+ verbatim_content = utils::capture.output(utils::sessionInfo()), |
255 | -1x | +145 | +20x |
- modules = modules,+ title = "SessionInfo" |
256 | -1x | +|||
146 | +
- reporter = reporter,+ ) |
|||
257 | -1x | +|||
147 | +
- filter = filter+ |
|||
258 | +148 |
- )+ # `JavaScript` code |
||
259 | -1x | +149 | +20x |
- return(active_module)+ run_js_files(files = "init.js") # `JavaScript` code to make the clipboard accessible |
260 | +150 |
- })+ # set timezone in shiny app |
||
261 | +151 |
- })+ # timezone is set in the early beginning so it will be available also |
||
262 | +152 |
- }+ # for `DDL` and all shiny modules |
1 | -+ | |||
153 | +20x |
- #' Generates library calls from current session info+ get_client_timezone(session$ns) |
||
2 | -+ | |||
154 | +20x |
- #'+ observeEvent( |
||
3 | -+ | |||
155 | +20x |
- #' Function to create multiple library calls out of current session info to make reproducible code works.+ eventExpr = input$timezone, |
||
4 | -+ | |||
156 | +20x |
- #'+ once = TRUE, |
||
5 | -+ | |||
157 | +20x |
- #' @return Character object contain code+ handlerExpr = {+ |
+ ||
158 | +! | +
+ session$userData$timezone <- input$timezone+ |
+ ||
159 | +! | +
+ logger::log_trace("srv_teal@1 Timezone set to client's timezone: { input$timezone }.") |
||
6 | +160 |
- #' @keywords internal+ } |
||
7 | +161 |
- get_rcode_libraries <- function() {+ ) |
||
8 | -16x | +|||
162 | +
- vapply(+ |
|||
9 | -16x | +163 | +20x |
- utils::sessionInfo()$otherPkgs,+ reporter <- teal.reporter::Reporter$new() |
10 | -16x | +164 | +20x |
- function(x) {+ if (is_arg_used(modules, "reporter") && length(extract_module(modules, "teal_module_previewer")) == 0) { |
11 | -272x | +|||
165 | +! |
- paste0("library(", x$Package, ")")+ modules <- append_module(modules, reporter_previewer_module()) |
||
12 | +166 |
- },- |
- ||
13 | -16x | -
- character(1)+ } |
||
14 | +167 |
- ) %>%+ |
||
15 | -+ | |||
168 | +20x |
- # put it into reverse order to correctly simulate executed code+ env <- environment() |
||
16 | -16x | +169 | +20x |
- rev() %>%+ datasets_reactive <- eventReactive(teal_data_rv(), { |
17 | -16x | +170 | +5x |
- paste0(sep = "\n") %>%+ env$progress <- shiny::Progress$new(session) |
18 | -16x | +171 | +5x |
- paste0(collapse = "")+ env$progress$set(0.25, message = "Setting data") |
19 | +172 |
- }+ |
||
20 | +173 |
-
+ # create a list of data following structure of the nested modules list structure. |
||
21 | +174 |
-
+ # Because it's easier to unpack modules and datasets when they follow the same nested structure.+ |
+ ||
175 | +5x | +
+ datasets_singleton <- teal_data_to_filtered_data(teal_data_rv()) |
||
22 | +176 | |||
23 | +177 |
- get_rcode_str_install <- function() {+ # Singleton starts with only global filters active. |
||
24 | -20x | +178 | +5x |
- code_string <- getOption("teal.load_nest_code")+ filter_global <- Filter(function(x) x$id %in% attr(filter, "mapping")$global_filters, filter)+ |
+
179 | +5x | +
+ datasets_singleton$set_filter_state(filter_global) |
||
25 | +180 | |||
26 | -20x | +181 | +5x |
- if (!is.null(code_string) && is.character(code_string)) {+ module_datasets <- function(modules) { |
27 | -2x | +182 | +20x |
- return(code_string)+ if (inherits(modules, "teal_modules")) { |
28 | -+ | |||
183 | +8x |
- }+ datasets <- lapply(modules$children, module_datasets) |
||
29 | -+ | |||
184 | +8x |
-
+ labels <- vapply(modules$children, `[[`, character(1), "label") |
||
30 | -18x | +185 | +8x |
- return("# Add any code to install/load your NEST environment here\n")+ names(datasets) <- labels |
31 | -+ | |||
186 | +8x |
- }+ datasets |
||
32 | -+ | |||
187 | +12x |
-
+ } else if (isTRUE(attr(filter, "module_specific"))) { |
||
33 | +188 |
- #' Get datasets code+ # we should create FilteredData even if modules$datanames is null |
||
34 | +189 |
- #'+ # null controls a display of filter panel but data should be still passed |
||
35 | -+ | |||
190 | +3x |
- #' Get combined code from `FilteredData` and from `CodeClass` object.+ datanames <- if (is.null(modules$datanames) || modules$datanames == "all") { |
||
36 | -+ | |||
191 | +3x |
- #'+ include_parent_datanames( |
||
37 | -+ | |||
192 | +3x |
- #' @param datanames (`character`) names of datasets to extract code from+ teal.data::datanames(teal_data_rv()), |
||
38 | -+ | |||
193 | +3x |
- #' @param datasets (`FilteredData`) object+ teal_data_rv()@join_keys |
||
39 | +194 |
- #' @param hashes named (`list`) of hashes per dataset+ ) |
||
40 | +195 |
- #'+ } else { |
||
41 | -+ | |||
196 | +! |
- #' @return `character(3)` containing the following elements:+ modules$datanames |
||
42 | +197 |
- #' - data pre-processing code (from `data` argument in `init`)+ } |
||
43 | +198 |
- #' - hash check of loaded objects+ # todo: subset teal_data to datanames |
||
44 | -+ | |||
199 | +3x |
- #' - filter code+ datasets_module <- teal_data_to_filtered_data(teal_data_rv(), datanames = datanames) |
||
45 | +200 |
- #'+ |
||
46 | +201 |
- #' @keywords internal+ # set initial filters |
||
47 | +202 |
- get_datasets_code <- function(datanames, datasets, hashes) {- |
- ||
48 | -14x | -
- str_prepro <- teal.data:::get_code_dependency(attr(datasets, "preprocessing_code"), names = datanames)+ # - filtering filters for this module |
||
49 | -14x | +203 | +3x |
- if (length(str_prepro) == 0) {+ slices <- Filter(x = filter, f = function(x) { |
50 | +204 | ! |
- str_prepro <- "message('Preprocessing is empty')"- |
- |
51 | -14x | -
- } else if (length(str_prepro) > 0) {- |
- ||
52 | -14x | -
- str_prepro <- paste0(str_prepro, "\n\n")+ x$id %in% unique(unlist(attr(filter, "mapping")[c(modules$label, "global_filters")])) && |
||
53 | -+ | |||
205 | +! |
- }+ x$dataname %in% datanames |
||
54 | +206 | - - | -||
55 | -14x | -
- str_hash <- paste(+ }) |
||
56 | -14x | +207 | +3x |
- paste0(+ include_varnames <- attr(slices, "include_varnames")[names(attr(slices, "include_varnames")) %in% datanames] |
57 | -14x | +208 | +3x |
- vapply(+ exclude_varnames <- attr(slices, "exclude_varnames")[names(attr(slices, "exclude_varnames")) %in% datanames] |
58 | -14x | +209 | +3x |
- datanames,+ slices$include_varnames <- include_varnames |
59 | -14x | +210 | +3x |
- function(dataname) {+ slices$exclude_varnames <- exclude_varnames |
60 | -17x | +211 | +3x |
- sprintf(+ datasets_module$set_filter_state(slices) |
61 | -17x | +212 | +3x |
- "stopifnot(%s == %s)",+ datasets_module |
62 | -17x | +|||
213 | +
- deparse1(bquote(rlang::hash(.(as.name(dataname))))),+ } else { |
|||
63 | -17x | +214 | +9x |
- deparse1(hashes[[dataname]])+ datasets_singleton |
64 | +215 |
- )+ } |
||
65 | +216 |
- },+ } |
||
66 | -14x | +217 | +5x |
- character(1)+ module_datasets(modules) |
67 | +218 |
- ),+ }) |
||
68 | -14x | +|||
219 | +
- collapse = "\n"+ |
|||
69 | +220 |
- ),+ # Replace splash / welcome screen once data is loaded ---- |
||
70 | -14x | +|||
221 | +
- "\n\n"+ # ignoreNULL to not trigger at the beginning when data is NULL |
|||
71 | +222 |
- )+ # just handle it once because data obtained through delayed loading should |
||
72 | +223 |
-
+ # usually not change afterwards |
||
73 | -14x | +|||
224 | +
- str_filter <- teal.slice::get_filter_expr(datasets, datanames)+ # if restored from bookmarked state, `filter` is ignored |
|||
74 | +225 | |||
75 | -14x | +226 | +20x |
- c(str_prepro, str_hash, str_filter)+ observeEvent(datasets_reactive(), once = TRUE, { |
76 | -+ | |||
227 | +1x |
- }+ logger::log_trace("srv_teal@5 setting main ui after data was pulled") |
1 | -+ | |
228 | +1x |
- #' Get dummy `CDISC` data+ on.exit(env$progress$close()) |
2 | -+ | |
229 | +1x | +
+ env$progress$set(0.5, message = "Setting up main UI")+ |
+
230 | +1x | +
+ datasets <- datasets_reactive()+ |
+
231 | ++ | + + | +
232 | ++ |
+ # main_ui_container contains splash screen first and we remove it and replace it by the real UI+ |
+
233 | +1x | +
+ removeUI(sprintf("#%s > div:nth-child(1)", session$ns("main_ui_container")))+ |
+
234 | +1x | +
+ insertUI(+ |
+
235 | +1x | +
+ selector = paste0("#", session$ns("main_ui_container")),+ |
+
236 | +1x | +
+ where = "beforeEnd",+ |
+
237 | ++ |
+ # we put it into a div, so it can easily be removed as a whole, also when it is a tagList (and not+ |
+
238 | ++ |
+ # just the first item of the tagList)+ |
+
239 | +1x | +
+ ui = div(ui_tabs_with_filters(+ |
+
240 | +1x | +
+ session$ns("main_ui"),+ |
+
241 | +1x | +
+ modules = modules,+ |
+
242 | +1x | +
+ datasets = datasets,+ |
+
243 | +1x | +
+ filter = filter+ |
+
244 | ++ |
+ )),+ |
+
245 | ++ |
+ # needed so that the UI inputs are available and can be immediately updated, otherwise, updating may not+ |
+
246 | ++ |
+ # have any effect as they are ignored when not present+ |
+
247 | +1x | +
+ immediate = TRUE+ |
+
248 | ++ |
+ )+ |
+
249 | ++ | + + | +
250 | ++ |
+ # must make sure that this is only executed once as modules assume their observers are only+ |
+
251 | ++ |
+ # registered once (calling server functions twice would trigger observers twice each time)+ |
+
252 | +1x | +
+ active_module <- srv_tabs_with_filters(+ |
+
253 | +1x | +
+ id = "main_ui",+ |
+
254 | +1x | +
+ datasets = datasets,+ |
+
255 | +1x | +
+ modules = modules,+ |
+
256 | +1x | +
+ reporter = reporter,+ |
+
257 | +1x | +
+ filter = filter+ |
+
258 | ++ |
+ )+ |
+
259 | +1x | +
+ return(active_module)+ |
+
260 | ++ |
+ })+ |
+
261 | ++ |
+ })+ |
+
262 | ++ |
+ }+ |
+
1 | ++ |
+ #' Generates library calls from current session info+ |
+ |||
2 | +
#' |
@@ -5106,133 +5302,133 @@ 3 |
- #' Get dummy `CDISC` data including `ADSL`, `ADAE` and `ADLB`.+ #' Function to create multiple library calls out of current session info to make reproducible code works. |
||
4 |
- #' Some NAs are also introduced to stress test.+ #' |
||||
5 |
- #'+ #' @return Character object contain code |
||||
6 |
- #' @return `cdisc_data`+ #' @keywords internal |
||||
7 |
- #' @keywords internal+ get_rcode_libraries <- function() { |
||||
8 | -+ | 16x |
- example_cdisc_data <- function() { # nolint+ vapply( |
||
9 | -! | +16x |
- ADSL <- data.frame( # nolint+ utils::sessionInfo()$otherPkgs, |
||
10 | -! | +16x |
- STUDYID = "study",+ function(x) { |
||
11 | -! | +272x |
- USUBJID = 1:10,+ paste0("library(", x$Package, ")") |
||
12 | -! | +
- SEX = sample(c("F", "M"), 10, replace = TRUE),+ }, |
|||
13 | -! | +16x |
- AGE = stats::rpois(10, 40)+ character(1) |
||
14 |
- )+ ) %>% |
||||
15 | -! | +
- ADTTE <- rbind(ADSL, ADSL, ADSL) # nolint+ # put it into reverse order to correctly simulate executed code |
|||
16 | -! | +16x |
- ADTTE$PARAMCD <- rep(c("OS", "EFS", "PFS"), each = 10) # nolint+ rev() %>% |
||
17 | -! | +16x |
- ADTTE$AVAL <- c( # nolint+ paste0(sep = "\n") %>% |
||
18 | -! | +16x |
- stats::rnorm(10, mean = 700, sd = 200), # dummy OS level+ paste0(collapse = "") |
||
19 | -! | +
- stats::rnorm(10, mean = 400, sd = 100), # dummy EFS level+ } |
|||
20 | -! | +
- stats::rnorm(10, mean = 450, sd = 200) # dummy PFS level+ |
|||
21 |
- )+ |
||||
23 | -! | +
- ADSL$logical_test <- sample(c(TRUE, FALSE, NA), size = nrow(ADSL), replace = TRUE) # nolint+ get_rcode_str_install <- function() { |
|||
24 | -! | +20x |
- ADSL$SEX[c(2, 5)] <- NA # nolint+ code_string <- getOption("teal.load_nest_code") |
||
26 | -! | +20x |
- res <- teal.data::cdisc_data(+ if (!is.null(code_string) && is.character(code_string)) { |
||
27 | -! | +2x |
- ADSL = ADSL,+ return(code_string) |
||
28 | -! | +
- ADTTE = ADTTE,+ } |
|||
29 | -! | +
- code = '+ |
|||
30 | -! | +18x |
- ADSL <- data.frame(+ return("# Add any code to install/load your NEST environment here\n") |
||
31 | -! | ++ |
+ }+ |
+ ||
32 | ++ | + + | +|||
33 | ++ |
+ #' Get datasets code+ |
+ |||
34 | ++ |
+ #'+ |
+ |||
35 | ++ |
+ #' Get combined code from `FilteredData` and from `CodeClass` object.+ |
+ |||
36 | ++ |
+ #'+ |
+ |||
37 | ++ |
+ #' @param datanames (`character`) names of datasets to extract code from+ |
+ |||
38 | ++ |
+ #' @param datasets (`FilteredData`) object+ |
+ |||
39 | ++ |
+ #' @param hashes named (`list`) of hashes per dataset+ |
+ |||
40 | ++ |
+ #'+ |
+ |||
41 | ++ |
+ #' @return `character(3)` containing the following elements:+ |
+ |||
42 | ++ |
+ #' - data pre-processing code (from `data` argument in `init`)+ |
+ |||
43 | ++ |
+ #' - hash check of loaded objects+ |
+ |||
44 | ++ |
+ #' - filter code+ |
+ |||
45 | ++ |
+ #'+ |
+ |||
46 | ++ |
+ #' @keywords internal+ |
+ |||
47 | ++ |
+ get_datasets_code <- function(datanames, datasets, hashes) {+ |
+ |||
48 | +14x | +
+ str_prepro <- teal.data:::get_code_dependency(attr(datasets, "preprocessing_code"), names = datanames)+ |
+ |||
49 | +14x |
- STUDYID = "study",+ if (length(str_prepro) == 0) { |
|||
32 | +50 | ! |
- USUBJID = 1:10,+ str_prepro <- "message('Preprocessing is empty')" |
||
33 | -! | +||||
51 | +14x |
- SEX = sample(c("F", "M"), 10, replace = TRUE),+ } else if (length(str_prepro) > 0) { |
|||
34 | -! | +||||
52 | +14x |
- AGE = rpois(10, 40)+ str_prepro <- paste0(str_prepro, "\n\n") |
|||
35 | +53 |
- )+ } |
|||
36 | -! | +||||
54 | +
- ADTTE <- rbind(ADSL, ADSL, ADSL)+ |
||||
37 | -! | +||||
55 | +14x |
- ADTTE$PARAMCD <- rep(c("OS", "EFS", "PFS"), each = 10)+ str_hash <- paste( |
|||
38 | -! | +||||
56 | +14x |
- ADTTE$AVAL <- c(+ paste0( |
|||
39 | -! | +||||
57 | +14x |
- rnorm(10, mean = 700, sd = 200),+ vapply( |
|||
40 | -! | +||||
58 | +14x |
- rnorm(10, mean = 400, sd = 100),+ datanames, |
|||
41 | -! | +||||
59 | +14x |
- rnorm(10, mean = 450, sd = 200)+ function(dataname) { |
|||
42 | -+ | ||||
60 | +17x |
- )+ sprintf( |
|||
43 | -+ | ||||
61 | +17x |
-
+ "stopifnot(%s == %s)", |
|||
44 | -! | +||||
62 | +17x |
- ADSL$logical_test <- sample(c(TRUE, FALSE, NA), size = nrow(ADSL), replace = TRUE)+ deparse1(bquote(rlang::hash(.(as.name(dataname))))), |
|||
45 | -! | +||||
63 | +17x |
- ADSL$SEX[c(2, 5)] <- NA+ deparse1(hashes[[dataname]]) |
|||
46 | +64 |
- '+ ) |
|||
47 | +65 |
- )+ }, |
|||
48 | -! | +||||
66 | +14x |
- return(res)+ character(1) |
|||
49 | +67 |
- }+ ), |
|||
50 | -+ | ||||
68 | +14x |
-
+ collapse = "\n" |
|||
51 | +69 |
- #' Get datasets to go with example modules.+ ), |
|||
52 | -+ | ||||
70 | +14x |
- #'+ "\n\n" |
|||
53 | +71 |
- #' Creates a nested list, the structure of which matches the module hierarchy created by `example_modules`.+ ) |
|||
54 | +72 |
- #' Each list leaf is the same `FilteredData` object.+ |
|||
55 | -+ | ||||
73 | +14x |
- #'+ str_filter <- teal.slice::get_filter_expr(datasets, datanames) |
|||
56 | +74 |
- #' @return named list of `FilteredData` objects, each with `ADSL` set.+ |
|||
57 | -+ | ||||
75 | +14x |
- #' @keywords internal+ c(str_prepro, str_hash, str_filter) |
|||
58 | +76 |
- example_datasets <- function() { # nolint- |
- |||
59 | -! | -
- dummy_cdisc_data <- example_cdisc_data()+ } |
|||
60 | -! | +
1 | +
- datasets <- teal_data_to_filtered_data(dummy_cdisc_data)+ #' Filter settings for teal applications |
|||
61 | -! | +|||
2 | +
- list(+ #' |
|||
62 | -! | +|||
3 | +
- "d2" = list(+ #' Specify initial filter states and filtering settings for a `teal` app. |
|||
63 | -! | +|||
4 | +
- "d3" = list(+ #' |
|||
64 | -! | +|||
5 | +
- "aaa1" = datasets,+ #' Produces a `teal_slices` object. |
|||
65 | -! | +|||
6 | +
- "aaa2" = datasets,+ #' The `teal_slice` components will specify filter states that will be active when the app starts. |
|||
66 | -! | +|||
7 | +
- "aaa3" = datasets+ #' Attributes (created with the named arguments) will configure the way the app applies filters. |
|||
67 | +8 |
- ),+ #' See argument descriptions for details. |
||
68 | -! | +|||
9 | +
- "bbb" = datasets+ #' |
|||
69 | +10 |
- ),+ #' @inheritParams teal.slice::teal_slices |
||
70 | -! | +|||
11 | +
- "ccc" = datasets+ #' |
|||
71 | +12 |
- )+ #' @param module_specific optional (`logical(1)`)\cr |
||
72 | +13 |
- }+ #' - `FALSE` (default) when one filter panel applied to all modules. |
||
73 | +14 |
-
+ #' All filters will be shared by all modules. |
||
74 | +15 |
- #' An example `teal` module+ #' - `TRUE` when filter panel module-specific. |
||
75 | +16 |
- #'+ #' Modules can have different set of filters specified - see `mapping` argument. |
||
76 | +17 |
- #' @description `r lifecycle::badge("experimental")`+ #' @param mapping `r lifecycle::badge("experimental")` _This is a new feature. Do kindly share your opinions.\cr_ |
||
77 | +18 |
- #' @inheritParams module+ #' (`named list`)\cr |
||
78 | +19 |
- #' @return A `teal` module which can be included in the `modules` argument to [teal::init()].+ #' Specifies which filters will be active in which modules on app start. |
||
79 | +20 |
- #' @examples+ #' Elements should contain character vector of `teal_slice` `id`s (see [teal.slice::teal_slice()]). |
||
80 | +21 |
- #' app <- init(+ #' Names of the list should correspond to `teal_module` `label` set in [module()] function. |
||
81 | +22 |
- #' data = teal_data(+ #' `id`s listed under `"global_filters` will be active in all modules. |
||
82 | +23 |
- #' dataset("IRIS", iris),+ #' If missing, all filters will be applied to all modules. |
||
83 | +24 |
- #' dataset("MTCARS", mtcars)+ #' If empty list, all filters will be available to all modules but will start inactive. |
||
84 | +25 |
- #' ),+ #' If `module_specific` is `FALSE`, only `global_filters` will be active on start. |
||
85 | +26 |
- #' modules = example_module()+ #' @param app_id (`character(1)`)\cr |
||
86 | +27 |
- #' )+ #' For internal use only, do not set manually. |
||
87 | +28 |
- #' if (interactive()) {+ #' Added by `init` so that a `teal_slices` can be matched to the app in which it was used. |
||
88 | +29 |
- #' shinyApp(app$ui, app$server)+ #' Used for verifying snapshots uploaded from file. See `snapshot`. |
||
89 | +30 |
- #' }+ #' |
||
90 | +31 |
- #' @export+ #' @param x (`list`) of lists to convert to `teal_slices` |
||
91 | +32 |
- example_module <- function(label = "example teal module", datanames = "all") {+ #' |
||
92 | -64x | +|||
33 | +
- checkmate::assert_string(label)+ #' @return |
|||
93 | -64x | +|||
34 | +
- module(+ #' A `teal_slices` object. |
|||
94 | -64x | +|||
35 | +
- label,+ #' |
|||
95 | -64x | +|||
36 | +
- server = function(id, data) {+ #' @seealso [`teal.slice::teal_slices`], [`teal.slice::teal_slice`], [`slices_store`] |
|||
96 | -1x | +|||
37 | +
- checkmate::assert_class(data, "tdata")+ #' |
|||
97 | -1x | +|||
38 | +
- moduleServer(id, function(input, output, session) {+ #' @examples |
|||
98 | -1x | +|||
39 | +
- output$text <- renderPrint(data[[input$dataname]]())+ #' filter <- teal_slices( |
|||
99 | -1x | +|||
40 | +
- teal.widgets::verbatim_popup_srv(+ #' teal.slice::teal_slice(dataname = "iris", varname = "Species", id = "species"), |
|||
100 | -1x | +|||
41 | +
- id = "rcode",+ #' teal.slice::teal_slice(dataname = "iris", varname = "Sepal.Length", id = "sepal_length"), |
|||
101 | -1x | +|||
42 | +
- verbatim_content = attr(data, "code")(),+ #' teal.slice::teal_slice( |
|||
102 | -1x | +|||
43 | +
- title = "Association Plot"+ #' dataname = "iris", id = "long_petals", title = "Long petals", expr = "Petal.Length > 5" |
|||
103 | +44 |
- )+ #' ), |
||
104 | +45 |
- })+ #' teal.slice::teal_slice(dataname = "mtcars", varname = "mpg", id = "mtcars_mpg"), |
||
105 | +46 |
- },+ #' mapping = list( |
||
106 | -64x | +|||
47 | +
- ui = function(id, data) {+ #' module1 = c("species", "sepal_length"), |
|||
107 | -1x | +|||
48 | +
- ns <- NS(id)+ #' module2 = c("mtcars_mpg"), |
|||
108 | -1x | +|||
49 | +
- teal.widgets::standard_layout(+ #' global_filters = "long_petals" |
|||
109 | -1x | +|||
50 | +
- output = verbatimTextOutput(ns("text")),+ #' ) |
|||
110 | -1x | +|||
51 | +
- encoding = div(+ #' ) |
|||
111 | -1x | +|||
52 | +
- selectInput(ns("dataname"), "Choose a dataset", choices = names(data)),+ #' |
|||
112 | -1x | +|||
53 | +
- teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")+ #' app <- teal::init( |
|||
113 | +54 |
- )+ #' modules = list( |
||
114 | +55 |
- )+ #' module("module1"), |
||
115 | +56 |
- },+ #' module("module2") |
||
116 | -64x | +|||
57 | +
- datanames = datanames+ #' ), |
|||
117 | +58 |
- )+ #' data = list(iris, mtcars), |
||
118 | +59 |
- }+ #' filter = filter |
||
119 | +60 |
-
+ #' ) |
||
120 | +61 |
-
+ #' |
||
121 | +62 |
- #' Get example modules.+ #' if (interactive()) { |
||
122 | +63 |
- #'+ #' shinyApp(app$ui, app$server) |
||
123 | +64 |
- #' Creates an example hierarchy of `teal_modules` from which a `teal` app can be created.+ #' } |
||
124 | +65 |
- #' @param datanames (`character`)\cr+ #' |
||
125 | +66 |
- #' names of the datasets to be used in the example modules. Possible choices are `ADSL`, `ADTTE`.+ #' @export |
||
126 | +67 |
- #' @return `teal_modules`+ teal_slices <- function(..., |
||
127 | +68 |
- #' @keywords internal+ exclude_varnames = NULL, |
||
128 | +69 |
- example_modules <- function(datanames = c("ADSL", "ADTTE")) {+ include_varnames = NULL, |
||
129 | -3x | +|||
70 | +
- checkmate::assert_subset(datanames, c("ADSL", "ADTTE"))+ count_type = NULL, |
|||
130 | -3x | +|||
71 | +
- mods <- modules(+ allow_add = TRUE, |
|||
131 | -3x | +|||
72 | +
- label = "d1",+ module_specific = FALSE, |
|||
132 | -3x | +|||
73 | +
- modules(+ mapping, |
|||
133 | -3x | +|||
74 | +
- label = "d2",+ app_id = NULL) { |
|||
134 | -3x | +75 | +91x |
- modules(+ shiny::isolate({ |
135 | -3x | +76 | +91x |
- label = "d3",+ checkmate::assert_flag(allow_add) |
136 | -3x | +77 | +91x |
- example_module(label = "aaa1", datanames = datanames),+ checkmate::assert_flag(module_specific) |
137 | -3x | +78 | +44x |
- example_module(label = "aaa2", datanames = datanames),+ if (!missing(mapping)) checkmate::assert_list(mapping, types = c("character", "NULL"), names = "named") |
138 | -3x | +79 | +88x |
- example_module(label = "aaa3", datanames = datanames)+ checkmate::assert_string(app_id, null.ok = TRUE) |
139 | +80 |
- ),+ |
||
140 | -3x | -
- example_module(label = "bbb", datanames = datanames)- |
- ||
141 | -+ | 81 | +88x |
- ),+ slices <- list(...) |
142 | -3x | +82 | +88x |
- example_module(label = "ccc", datanames = datanames)+ all_slice_id <- vapply(slices, `[[`, character(1L), "id") |
143 | +83 |
- )+ |
||
144 | -3x | -
- return(mods)- |
- ||
145 | -+ | 84 | +88x |
- }+ if (missing(mapping)) { |
1 | -+ | |||
85 | +47x |
- #' Store teal_slices object to a file+ mapping <- list(global_filters = all_slice_id) |
||
2 | +86 |
- #'+ } |
||
3 | -+ | |||
87 | +88x |
- #' This function takes a `teal_slices` object and saves it to a file in `JSON` format.+ if (!module_specific) { |
||
4 | -+ | |||
88 | +84x |
- #' The `teal_slices` object contains information about filter states and can be used to+ mapping[setdiff(names(mapping), "global_filters")] <- NULL |
||
5 | +89 |
- #' create, modify, and delete filter states. The saved file can be later loaded using+ } |
||
6 | +90 |
- #' the `slices_restore` function.+ |
||
7 | -+ | |||
91 | +88x |
- #'+ failed_slice_id <- setdiff(unlist(mapping), all_slice_id) |
||
8 | -+ | |||
92 | +88x |
- #' @param tss (`teal_slices`) object to be stored.+ if (length(failed_slice_id)) { |
||
9 | -+ | |||
93 | +1x |
- #' @param file (`character(1)`) The file path where `teal_slices` object will be saved.+ stop(sprintf( |
||
10 | -+ | |||
94 | +1x |
- #' The file extension should be `".json"`.+ "Filters in mapping don't match any available filter.\n %s not in %s", |
||
11 | -+ | |||
95 | +1x |
- #'+ toString(failed_slice_id), |
||
12 | -+ | |||
96 | +1x |
- #' @details `Date` class is stored in `"ISO8601"` format (`YYYY-MM-DD`). `POSIX*t` classes are converted to a+ toString(all_slice_id) |
||
13 | +97 |
- #' character by using `format.POSIX*t(usetz = TRUE, tz = "UTC")` (`YYYY-MM-DD {N}{N}:{N}{N}:{N}{N} UTC`, where+ )) |
||
14 | +98 |
- #' `{N} = [0-9]` is a number and `UTC` is `Coordinated Universal Time` timezone short-code).+ } |
||
15 | +99 |
- #' This format is assumed during `slices_restore`. All `POSIX*t` objects in `selected` or `choices` fields of+ |
||
16 | -+ | |||
100 | +87x |
- #' `teal_slice` objects are always printed in `UTC` timezone as well.+ tss <- teal.slice::teal_slices( |
||
17 | +101 |
- #'+ ..., |
||
18 | -+ | |||
102 | +87x |
- #' @return `NULL`, invisibly.+ exclude_varnames = exclude_varnames, |
||
19 | -+ | |||
103 | +87x |
- #'+ include_varnames = include_varnames, |
||
20 | -+ | |||
104 | +87x |
- #' @keywords internal+ count_type = count_type, |
||
21 | -+ | |||
105 | +87x |
- #'+ allow_add = allow_add |
||
22 | +106 |
- #' @examples+ ) |
||
23 | -+ | |||
107 | +87x |
- #' # Create a teal_slices object+ attr(tss, "mapping") <- mapping |
||
24 | -+ | |||
108 | +87x |
- #' tss <- teal_slices(+ attr(tss, "module_specific") <- module_specific |
||
25 | -+ | |||
109 | +87x |
- #' teal_slice(dataname = "data", varname = "var"),+ attr(tss, "app_id") <- app_id |
||
26 | -+ | |||
110 | +87x |
- #' teal_slice(dataname = "data", expr = "x > 0", id = "positive_x", title = "Positive x")+ class(tss) <- c("modules_teal_slices", class(tss)) |
||
27 | -+ | |||
111 | +87x |
- #' )+ tss |
||
28 | +112 |
- #'+ }) |
||
29 | +113 |
- #' if (interactive()) {+ } |
||
30 | +114 |
- #' # Store the teal_slices object to a file+ |
||
31 | +115 |
- #' slices_store(tss, "path/to/file.json")+ |
||
32 | +116 |
- #' }+ #' @rdname teal_slices |
||
33 | +117 |
- #'+ #' @export |
||
34 | +118 |
- slices_store <- function(tss, file) {- |
- ||
35 | -6x | -
- checkmate::assert_class(tss, "teal_slices")+ #' @keywords internal |
||
36 | -6x | +|||
119 | +
- checkmate::assert_path_for_output(file, overwrite = TRUE, extension = "json")+ #' |
|||
37 | +120 |
-
+ as.teal_slices <- function(x) { # nolint |
||
38 | -6x | +121 | +25x |
- cat(format(tss, trim_lines = FALSE), "\n", file = file)+ checkmate::assert_list(x) |
39 | -+ | |||
122 | +25x |
- }+ lapply(x, checkmate::assert_list, names = "named", .var.name = "list element") |
||
40 | +123 | |||
41 | -+ | |||
124 | +25x |
- #' Restore teal_slices object from a file+ attrs <- attributes(unclass(x)) |
||
42 | -+ | |||
125 | +25x |
- #'+ ans <- lapply(x, function(x) if (is.teal_slice(x)) x else as.teal_slice(x)) |
||
43 | -+ | |||
126 | +25x |
- #' This function takes a file path to a `JSON` file containing a `teal_slices` object+ do.call(teal_slices, c(ans, attrs)) |
||
44 | +127 |
- #' and restores it to its original form. The restored `teal_slices` object can be used+ } |
||
45 | +128 |
- #' to access filter states and their corresponding attributes.+ |
||
46 | +129 |
- #'+ |
||
47 | +130 |
- #' @param file Path to file where `teal_slices` is stored. Must have a `.json` extension and read access.+ #' @rdname teal_slices |
||
48 | +131 |
- #'+ #' @export |
||
49 | +132 |
- #' @return A `teal_slices` object restored from the file.+ #' @keywords internal |
||
50 | +133 |
#' |
||
51 | -- |
- #' @keywords internal- |
- ||
52 | +134 |
- #'+ c.teal_slices <- function(...) { |
||
53 | -+ | |||
135 | +! |
- #' @examples+ x <- list(...) |
||
54 | -+ | |||
136 | +! |
- #' if (interactive()) {+ checkmate::assert_true(all(vapply(x, is.teal_slices, logical(1L))), .var.name = "all arguments are teal_slices") |
||
55 | +137 |
- #' # Restore a teal_slices object from a file+ |
||
56 | -+ | |||
138 | +! |
- #' tss_restored <- slices_restore("path/to/file.json")+ all_attributes <- lapply(x, attributes) |
||
57 | -+ | |||
139 | +! |
- #' }+ all_attributes <- coalesce_r(all_attributes) |
||
58 | -+ | |||
140 | +! |
- #'+ all_attributes <- all_attributes[names(all_attributes) != "class"] |
||
59 | +141 |
- slices_restore <- function(file) {+ |
||
60 | -6x | +|||
142 | +! |
- checkmate::assert_file_exists(file, access = "r", extension = "json")+ do.call( |
||
61 | -+ | |||
143 | +! |
-
+ teal_slices, |
||
62 | -6x | +|||
144 | +! |
- tss_json <- jsonlite::fromJSON(file, simplifyDataFrame = FALSE)+ c( |
||
63 | -6x | +|||
145 | +! |
- tss_json$slices <-+ unique(unlist(x, recursive = FALSE)), |
||
64 | -6x | +|||
146 | +! |
- lapply(tss_json$slices, function(slice) {+ all_attributes |
||
65 | -6x | +|||
147 | +
- for (field in c("selected", "choices")) {+ ) |
|||
66 | -12x | +|||
148 | +
- if (!is.null(slice[[field]])) {+ ) |
|||
67 | -9x | +|||
149 | +
- date_partial_regex <- "^[0-9]{4}-[0-9]{2}-[0-9]{2}"+ } |
|||
68 | -9x | +|||
150 | +
- time_stamp_regex <- paste0(date_partial_regex, "\\s[0-9]{2}:[0-9]{2}:[0-9]{2}\\sUTC$")+ |
|||
69 | +151 | |||
70 | -9x | +|||
152 | +
- slice[[field]] <-+ #' Deep copy `teal_slices` |
|||
71 | -9x | +|||
153 | +
- if (all(grepl(paste0(date_partial_regex, "$"), slice[[field]]))) {+ #' |
|||
72 | -3x | +|||
154 | +
- as.Date(slice[[field]])+ #' it's important to create a new copy of `teal_slices` when |
|||
73 | -9x | +|||
155 | +
- } else if (all(grepl(time_stamp_regex, slice[[field]]))) {+ #' starting a new `shiny` session. Otherwise, object will be shared |
|||
74 | -3x | +|||
156 | +
- as.POSIXct(slice[[field]], tz = "UTC")+ #' by multiple users as it is created in global environment before |
|||
75 | +157 |
- } else {+ #' `shiny` session starts. |
||
76 | -3x | +|||
158 | +
- slice[[field]]+ #' @param filter (`teal_slices`) |
|||
77 | +159 |
- }+ #' @return `teal_slices` |
||
78 | +160 |
- }+ #' @keywords internal |
||
79 | +161 |
- }+ deep_copy_filter <- function(filter) { |
||
80 | -6x | +162 | +1x |
- slice+ checkmate::assert_class(filter, "teal_slices") |
81 | -+ | |||
163 | +1x |
- })+ shiny::isolate({ |
||
82 | -+ | |||
164 | +1x |
-
+ filter_copy <- lapply(filter, function(slice) { |
||
83 | -6x | +165 | +2x |
- tss_elements <- lapply(tss_json$slices, as.teal_slice)+ teal.slice::as.teal_slice(as.list(slice)) |
84 | +166 |
-
+ }) |
||
85 | -6x | +167 | +1x |
- do.call(teal_slices, c(tss_elements, tss_json$attributes))+ attributes(filter_copy) <- attributes(filter)+ |
+
168 | +1x | +
+ filter_copy |
||
86 | +169 | ++ |
+ })+ |
+ |
170 |
}@@ -6714,14 +7015,14 @@ teal coverage - 71.59% |
1 |
- #' Get Client Timezone+ #' Get dummy `CDISC` data |
||
3 |
- #' Local timezone in the browser may differ from the system timezone from the server.+ #' Get dummy `CDISC` data including `ADSL`, `ADAE` and `ADLB`. |
||
4 |
- #' This script can be run to register a shiny input which contains information about+ #' Some NAs are also introduced to stress test. |
||
5 |
- #' the timezone in the browser.+ #' |
||
6 |
- #'+ #' @return `cdisc_data` |
||
7 |
- #' @param ns (`function`) namespace function passed from the `session` object in the+ #' @keywords internal |
||
8 |
- #' Shiny server. For Shiny modules this will allow for proper name spacing of the+ example_cdisc_data <- function() { # nolint |
||
9 | -+ | ! |
- #' registered input.+ ADSL <- data.frame( # nolint |
10 | -+ | ! |
- #'+ STUDYID = "study", |
11 | -+ | ! |
- #' @return (`Shiny`) input variable accessible with `input$tz` which is a (`character`)+ USUBJID = 1:10, |
12 | -+ | ! |
- #' string containing the timezone of the browser/client.+ SEX = sample(c("F", "M"), 10, replace = TRUE), |
13 | -+ | ! |
- #' @keywords internal+ AGE = stats::rpois(10, 40) |
14 |
- get_client_timezone <- function(ns) {+ ) |
||
15 | -16x | +! |
- script <- sprintf(+ ADTTE <- rbind(ADSL, ADSL, ADSL) # nolint |
16 | -16x | +! |
- "Shiny.setInputValue(`%s`, Intl.DateTimeFormat().resolvedOptions().timeZone)",+ ADTTE$PARAMCD <- rep(c("OS", "EFS", "PFS"), each = 10) # nolint |
17 | -16x | +! |
- ns("timezone")+ ADTTE$AVAL <- c( # nolint |
18 | -+ | ! |
- )+ stats::rnorm(10, mean = 700, sd = 200), # dummy OS level |
19 | -16x | +! |
- shinyjs::runjs(script) # function does not return anything+ stats::rnorm(10, mean = 400, sd = 100), # dummy EFS level |
20 | -16x | +! |
- return(invisible(NULL))+ stats::rnorm(10, mean = 450, sd = 200) # dummy PFS level |
21 |
- }+ ) |
||
23 | -+ | ! |
- #' Resolve the expected bootstrap theme+ ADSL$logical_test <- sample(c(TRUE, FALSE, NA), size = nrow(ADSL), replace = TRUE) # nolint |
24 | -+ | ! |
- #' @keywords internal+ ADSL$SEX[c(2, 5)] <- NA # nolint |
25 |
- get_teal_bs_theme <- function() {+ |
||
26 | -26x | +! |
- bs_theme <- getOption("teal.bs_theme")+ res <- teal.data::cdisc_data( |
27 | -26x | +! |
- if (is.null(bs_theme)) {+ ADSL = ADSL, |
28 | -23x | +! |
- NULL+ ADTTE = ADTTE, |
29 | -3x | +! |
- } else if (!inherits(bs_theme, "bs_theme")) {+ code = ' |
30 | -2x | +! |
- warning("teal.bs_theme has to be of a bslib::bs_theme class, the default shiny bootstrap is used.")+ ADSL <- data.frame( |
31 | -2x | +! |
- NULL+ STUDYID = "study", |
32 | -+ | ! |
- } else {+ USUBJID = 1:10, |
33 | -1x | +! |
- bs_theme+ SEX = sample(c("F", "M"), 10, replace = TRUE), |
34 | -+ | ! |
- }+ AGE = rpois(10, 40) |
35 |
- }+ ) |
||
36 | -+ | ! |
-
+ ADTTE <- rbind(ADSL, ADSL, ADSL) |
37 | -+ | ! |
- include_parent_datanames <- function(dataname, join_keys) {+ ADTTE$PARAMCD <- rep(c("OS", "EFS", "PFS"), each = 10) |
38 | -3x | +! |
- parents <- character(0)+ ADTTE$AVAL <- c( |
39 | -3x | +! |
- for (i in dataname) {+ rnorm(10, mean = 700, sd = 200), |
40 | -6x | +! |
- while (length(i) > 0) {+ rnorm(10, mean = 400, sd = 100), |
41 | -6x | +! |
- parent_i <- teal.data::parent(join_keys, i)+ rnorm(10, mean = 450, sd = 200) |
42 | -6x | +
- parents <- c(parent_i, parents)+ ) |
|
43 | -6x | +
- i <- parent_i+ |
|
44 | -+ | ! |
- }+ ADSL$logical_test <- sample(c(TRUE, FALSE, NA), size = nrow(ADSL), replace = TRUE) |
45 | -+ | ! |
- }+ ADSL$SEX[c(2, 5)] <- NA |
46 |
-
+ ' |
||
47 | -3x | +
- return(unique(c(parents, dataname)))+ ) |
|
48 | -+ | ! |
- }+ return(res) |
49 |
-
+ } |
||
51 |
-
+ #' Get datasets to go with example modules. |
||
52 |
- #' Create a `FilteredData`+ #' |
||
53 |
- #'+ #' Creates a nested list, the structure of which matches the module hierarchy created by `example_modules`. |
||
54 |
- #' Create a `FilteredData` object from a `teal_data` object+ #' Each list leaf is the same `FilteredData` object. |
||
55 |
- #' @param x (`teal_data`) object+ #' |
||
56 |
- #' @param datanames (`character`) vector of data set names to include; must be subset of `datanames(x)`+ #' @return named list of `FilteredData` objects, each with `ADSL` set. |
||
57 |
- #' @return (`FilteredData`) object+ #' @keywords internal |
||
58 |
- #' @keywords internal+ example_datasets <- function() { # nolint |
||
59 | -+ | ! |
- teal_data_to_filtered_data <- function(x, datanames = teal.data::datanames(x)) {+ dummy_cdisc_data <- example_cdisc_data() |
60 | -15x | +! |
- checkmate::assert_class(x, "teal_data")+ datasets <- teal_data_to_filtered_data(dummy_cdisc_data) |
61 | -15x | +! |
- checkmate::assert_character(datanames, min.len = 1L, any.missing = FALSE)+ list( |
62 | -15x | +! |
- checkmate::assert_subset(datanames, teal.data::datanames(x))+ "d2" = list( |
63 | -+ | ! |
-
+ "d3" = list( |
64 | -15x | +! |
- ans <- teal.slice::init_filtered_data(+ "aaa1" = datasets, |
65 | -15x | +! |
- x = sapply(datanames, function(dn) x[[dn]], simplify = FALSE),+ "aaa2" = datasets, |
66 | -15x | +! |
- join_keys = teal.data::join_keys(x)+ "aaa3" = datasets |
67 |
- )+ ), |
||
68 | -+ | ! |
- # Piggy-back entire pre-processing code so that filtering code can be appended later.+ "bbb" = datasets |
69 | -15x | +
- attr(ans, "preprocessing_code") <- teal.code::get_code(x)+ ), |
|
70 | -15x | +! |
- ans+ "ccc" = datasets |
71 |
- }+ ) |
||
72 |
-
+ } |
||
73 |
- #' Template Function for `TealReportCard` Creation and Customization+ |
||
74 |
- #'+ #' An example `teal` module |
||
75 |
- #' This function generates a report card with a title,+ #' |
||
76 |
- #' an optional description, and the option to append the filter state list.+ #' @description `r lifecycle::badge("experimental")` |
||
77 |
- #'+ #' @inheritParams module |
||
78 |
- #' @param title (`character(1)`) title of the card (unless overwritten by label)+ #' @return A `teal` module which can be included in the `modules` argument to [teal::init()]. |
||
79 |
- #' @param label (`character(1)`) label provided by the user when adding the card+ #' @examples |
||
80 |
- #' @param description (`character(1)`) optional additional description+ #' app <- init( |
||
81 |
- #' @param with_filter (`logical(1)`) flag indicating to add filter state+ #' data = teal_data( |
||
82 |
- #' @param filter_panel_api (`FilterPanelAPI`) object with API that allows the generation+ #' dataset("IRIS", iris), |
||
83 |
- #' of the filter state in the report+ #' dataset("MTCARS", mtcars) |
||
84 |
- #'+ #' ), |
||
85 |
- #' @return (`TealReportCard`) populated with a title, description and filter state+ #' modules = example_module() |
||
86 |
- #'+ #' ) |
||
87 |
- #' @export+ #' if (interactive()) { |
||
88 |
- report_card_template <- function(title, label, description = NULL, with_filter, filter_panel_api) {+ #' shinyApp(app$ui, app$server) |
||
89 | -2x | +
- checkmate::assert_string(title)+ #' } |
|
90 | -2x | +
- checkmate::assert_string(label)+ #' @export |
|
91 | -2x | +
- checkmate::assert_string(description, null.ok = TRUE)+ example_module <- function(label = "example teal module", datanames = "all") { |
|
92 | -2x | +68x |
- checkmate::assert_flag(with_filter)+ checkmate::assert_string(label) |
93 | -2x | +68x |
- checkmate::assert_class(filter_panel_api, classes = "FilterPanelAPI")+ module( |
94 | -+ | 68x |
-
+ label, |
95 | -2x | +68x |
- card <- teal::TealReportCard$new()+ server = function(id, data) { |
96 | -2x | +1x |
- title <- if (label == "") title else label+ checkmate::assert_class(data, "tdata") |
97 | -2x | +1x |
- card$set_name(title)+ moduleServer(id, function(input, output, session) { |
98 | -2x | +1x |
- card$append_text(title, "header2")+ output$text <- renderPrint(data[[input$dataname]]()) |
99 | 1x |
- if (!is.null(description)) card$append_text(description, "header3")+ teal.widgets::verbatim_popup_srv( |
|
100 | 1x |
- if (with_filter) card$append_fs(filter_panel_api$get_filter_state())+ id = "rcode", |
|
101 | -2x | +1x |
- card+ verbatim_content = attr(data, "code")(), |
102 | -+ | 1x |
- }+ title = "Association Plot" |
103 |
- #' Resolve `datanames` for the modules+ ) |
||
104 |
- #'+ }) |
||
105 |
- #' Modifies `module$datanames` to include names of the parent dataset (taken from `join_keys`).+ }, |
||
106 | +68x | +
+ ui = function(id, data) {+ |
+ |
107 | +1x | +
+ ns <- NS(id)+ |
+ |
108 | +1x | +
+ teal.widgets::standard_layout(+ |
+ |
109 | +1x | +
+ output = verbatimTextOutput(ns("text")),+ |
+ |
110 | +1x | +
+ encoding = div(+ |
+ |
111 | +1x | +
+ selectInput(ns("dataname"), "Choose a dataset", choices = names(data)),+ |
+ |
112 | +1x | +
+ teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")+ |
+ |
113 |
- #' When `datanames` is set to `"all"` it is replaced with all available datasets names.+ ) |
||
107 | +114 |
- #' @param modules (`teal_modules`) object+ ) |
|
108 | +115 |
- #' @param datanames (`character`) names of datasets available in the `data` object+ },+ |
+ |
116 | +68x | +
+ datanames = datanames |
|
109 | +117 |
- #' @param join_keys (`join_keys`) object+ ) |
|
110 | +118 |
- #' @return `teal_modules` with resolved `datanames`+ } |
|
111 | +119 |
- #' @keywords internal+ |
|
112 | +120 |
- resolve_modules_datanames <- function(modules, datanames, join_keys) {+ |
|
113 | -! | +||
121 | +
- if (inherits(modules, "teal_modules")) {+ #' Get example modules. |
||
114 | -! | +||
122 | +
- modules$children <- sapply(+ #' |
||
115 | -! | +||
123 | ++ |
+ #' Creates an example hierarchy of `teal_modules` from which a `teal` app can be created.+ |
+ |
124 | ++ |
+ #' @param datanames (`character`)\cr+ |
+ |
125 | ++ |
+ #' names of the datasets to be used in the example modules. Possible choices are `ADSL`, `ADTTE`.+ |
+ |
126 | ++ |
+ #' @return `teal_modules`+ |
+ |
127 | ++ |
+ #' @keywords internal+ |
+ |
128 | +
- modules$children,+ example_modules <- function(datanames = c("ADSL", "ADTTE")) { |
||
116 | -! | +||
129 | +3x |
- resolve_modules_datanames,+ checkmate::assert_subset(datanames, c("ADSL", "ADTTE")) |
|
117 | -! | +||
130 | +3x |
- simplify = FALSE,+ mods <- modules( |
|
118 | -! | +||
131 | +3x |
- datanames = datanames,+ label = "d1", |
|
119 | -! | +||
132 | +3x |
- join_keys = join_keys+ modules( |
|
120 | -+ | ||
133 | +3x |
- )+ label = "d2", |
|
121 | -! | +||
134 | +3x |
- modules+ modules( |
|
122 | -+ | ||
135 | +3x |
- } else {+ label = "d3", |
|
123 | -! | +||
136 | +3x |
- modules$datanames <- if (identical(modules$datanames, "all")) {+ example_module(label = "aaa1", datanames = datanames), |
|
124 | -! | +||
137 | +3x |
- datanames+ example_module(label = "aaa2", datanames = datanames), |
|
125 | -! | +||
138 | +3x |
- } else if (is.character(modules$datanames)) {+ example_module(label = "aaa3", datanames = datanames) |
|
126 | -! | +||
139 | +
- extra_datanames <- setdiff(modules$datanames, datanames)+ ), |
||
127 | -! | +||
140 | +3x |
- if (length(extra_datanames)) {+ example_module(label = "bbb", datanames = datanames) |
|
128 | -! | +||
141 | +
- stop(+ ), |
||
129 | -! | +||
142 | +3x |
- sprintf(+ example_module(label = "ccc", datanames = datanames) |
|
130 | -! | +||
143 | +
- "Module %s has datanames that are not available in a 'data':\n %s not in %s",+ ) |
||
131 | -! | +||
144 | +3x |
- modules$label,+ return(mods) |
|
132 | -! | +||
145 | +
- toString(extra_datanames),+ } |
||
133 | -! | +
1 | +
- toString(datanames)+ #' Get Client Timezone |
|||
134 | +2 |
- )+ #' |
||
135 | +3 |
- )+ #' Local timezone in the browser may differ from the system timezone from the server. |
||
136 | +4 |
- }+ #' This script can be run to register a shiny input which contains information about |
||
137 | -! | +|||
5 | +
- datanames_adjusted <- intersect(modules$datanames, datanames)+ #' the timezone in the browser. |
|||
138 | -! | +|||
6 | +
- include_parent_datanames(dataname = datanames_adjusted, join_keys = join_keys)+ #' |
|||
139 | +7 |
- }+ #' @param ns (`function`) namespace function passed from the `session` object in the |
||
140 | -! | +|||
8 | +
- modules+ #' Shiny server. For Shiny modules this will allow for proper name spacing of the |
|||
141 | +9 |
- }+ #' registered input. |
||
142 | +10 |
- }+ #' |
||
143 | +11 |
-
+ #' @return (`Shiny`) input variable accessible with `input$tz` which is a (`character`) |
||
144 | +12 |
- #' Check `datanames` in modules+ #' string containing the timezone of the browser/client. |
||
145 | +13 |
- #'+ #' @keywords internal |
||
146 | +14 |
- #' This function ensures specified `datanames` in modules match those in the data object,+ get_client_timezone <- function(ns) {+ |
+ ||
15 | +20x | +
+ script <- sprintf(+ |
+ ||
16 | +20x | +
+ "Shiny.setInputValue(`%s`, Intl.DateTimeFormat().resolvedOptions().timeZone)",+ |
+ ||
17 | +20x | +
+ ns("timezone") |
||
147 | +18 |
- #' returning error messages or `TRUE` for successful validation.+ )+ |
+ ||
19 | +20x | +
+ shinyjs::runjs(script) # function does not return anything+ |
+ ||
20 | +20x | +
+ return(invisible(NULL)) |
||
148 | +21 |
- #'+ } |
||
149 | +22 |
- #' @param modules (`teal_modules`) object+ |
||
150 | +23 |
- #' @param datanames (`character`) names of datasets available in the `data` object+ #' Resolve the expected bootstrap theme |
||
151 | +24 |
- #'+ #' @keywords internal |
||
152 | +25 |
- #' @return A `character(1)` containing error message or `TRUE` if validation passes.+ get_teal_bs_theme <- function() { |
||
153 | -+ | |||
26 | +26x |
- #' @keywords internal+ bs_theme <- getOption("teal.bs_theme") |
||
154 | -+ | |||
27 | +26x |
- check_modules_datanames <- function(modules, datanames) {+ if (is.null(bs_theme)) { |
||
155 | -7x | +28 | +23x |
- checkmate::assert_class(modules, "teal_modules")+ NULL |
156 | -7x | +29 | +3x |
- checkmate::assert_character(datanames)+ } else if (!inherits(bs_theme, "bs_theme")) { |
157 | -+ | |||
30 | +2x |
-
+ warning("teal.bs_theme has to be of a bslib::bs_theme class, the default shiny bootstrap is used.") |
||
158 | -7x | +31 | +2x |
- recursive_check_datanames <- function(modules, datanames) {+ NULL |
159 | +32 |
- # check teal_modules against datanames+ } else { |
||
160 | -14x | +33 | +1x |
- if (inherits(modules, "teal_modules")) {+ bs_theme |
161 | -7x | +|||
34 | +
- sapply(modules$children, function(module) recursive_check_datanames(module, datanames = datanames))+ } |
|||
162 | +35 |
- } else {+ } |
||
163 | -7x | +|||
36 | +
- extra_datanames <- setdiff(modules$datanames, c("all", datanames))+ |
|||
164 | -7x | +|||
37 | +
- if (length(extra_datanames)) {+ include_parent_datanames <- function(dataname, join_keys) { |
|||
165 | -2x | +38 | +3x |
- sprintf(+ parents <- character(0) |
166 | -2x | +39 | +3x |
- "- Module '%s' uses datanames not available in 'data': (%s) not in (%s)",+ for (i in dataname) { |
167 | -2x | +40 | +6x |
- modules$label,+ while (length(i) > 0) { |
168 | -2x | +41 | +6x |
- toString(dQuote(extra_datanames, q = FALSE)),+ parent_i <- teal.data::parent(join_keys, i) |
169 | -2x | +42 | +6x |
- toString(dQuote(datanames, q = FALSE))+ parents <- c(parent_i, parents) |
170 | -+ | |||
43 | +6x |
- )+ i <- parent_i |
||
171 | +44 |
- }+ } |
||
172 | +45 |
- }+ } |
||
173 | +46 |
- }+ |
||
174 | -7x | +47 | +3x |
- check_datanames <- unlist(recursive_check_datanames(modules, datanames))+ return(unique(c(parents, dataname))) |
175 | -7x | +|||
48 | +
- if (length(check_datanames)) {+ } |
|||
176 | -2x | +|||
49 | +
- paste(check_datanames, collapse = "\n")+ |
|||
177 | +50 |
- } else {+ |
||
178 | -5x | +|||
51 | +
- TRUE+ |
|||
179 | +52 |
- }+ #' Create a `FilteredData` |
||
180 | +53 |
- }+ #' |
||
181 | +54 |
-
+ #' Create a `FilteredData` object from a `teal_data` object |
||
182 | +55 |
- #' Check `datanames` in filters+ #' @param x (`teal_data`) object |
||
183 | +56 |
- #'+ #' @param datanames (`character`) vector of data set names to include; must be subset of `datanames(x)` |
||
184 | +57 |
- #' This function checks whether `datanames` in filters correspond to those in `data`,+ #' @return (`FilteredData`) object |
||
185 | +58 |
- #' returning character vector with error messages or TRUE if all checks pass.+ #' @keywords internal |
||
186 | +59 |
- #'+ teal_data_to_filtered_data <- function(x, datanames = teal.data::datanames(x)) { |
||
187 | -+ | |||
60 | +15x |
- #' @param filters (`teal_slices`) object+ checkmate::assert_class(x, "teal_data") |
||
188 | -+ | |||
61 | +15x |
- #' @param datanames (`character`) names of datasets available in the `data` object+ checkmate::assert_character(datanames, min.len = 1L, any.missing = FALSE)+ |
+ ||
62 | +15x | +
+ checkmate::assert_subset(datanames, teal.data::datanames(x)) |
||
189 | +63 |
- #'+ + |
+ ||
64 | +15x | +
+ ans <- teal.slice::init_filtered_data(+ |
+ ||
65 | +15x | +
+ x = sapply(datanames, function(dn) x[[dn]], simplify = FALSE), |
||
190 | -+ | |||
66 | +15x |
- #' @return A `character(1)` containing error message or TRUE if validation passes.+ join_keys = teal.data::join_keys(x) |
||
191 | +67 |
- #' @keywords internal+ ) |
||
192 | +68 |
- check_filter_datanames <- function(filters, datanames) {+ # Piggy-back entire pre-processing code so that filtering code can be appended later. |
||
193 | -5x | +69 | +15x |
- checkmate::assert_class(filters, "teal_slices")+ attr(ans, "preprocessing_code") <- teal.code::get_code(x) |
194 | -5x | +70 | +15x |
- checkmate::assert_character(datanames)+ ans |
195 | +71 |
-
+ } |
||
196 | +72 |
- # check teal_slices against datanames+ |
||
197 | -5x | +|||
73 | +
- out <- unlist(sapply(+ #' Template Function for `TealReportCard` Creation and Customization |
|||
198 | -5x | +|||
74 | +
- filters, function(filter) {+ #' |
|||
199 | -2x | +|||
75 | +
- dataname <- shiny::isolate(filter$dataname)+ #' This function generates a report card with a title, |
|||
200 | -2x | +|||
76 | +
- if (!dataname %in% datanames) {+ #' an optional description, and the option to append the filter state list. |
|||
201 | -2x | +|||
77 | +
- sprintf(+ #' |
|||
202 | -2x | +|||
78 | +
- "- Filter '%s' refers to dataname not available in 'data':\n %s not in (%s)",+ #' @param title (`character(1)`) title of the card (unless overwritten by label) |
|||
203 | -2x | +|||
79 | +
- shiny::isolate(filter$id),+ #' @param label (`character(1)`) label provided by the user when adding the card |
|||
204 | -2x | +|||
80 | +
- dQuote(dataname, q = FALSE),+ #' @param description (`character(1)`) optional additional description |
|||
205 | -2x | +|||
81 | +
- toString(dQuote(datanames, q = FALSE))+ #' @param with_filter (`logical(1)`) flag indicating to add filter state |
|||
206 | +82 |
- )+ #' @param filter_panel_api (`FilterPanelAPI`) object with API that allows the generation |
||
207 | +83 |
- }+ #' of the filter state in the report |
||
208 | +84 |
- }+ #' |
||
209 | +85 |
- ))+ #' @return (`TealReportCard`) populated with a title, description and filter state |
||
210 | +86 |
-
+ #' |
||
211 | +87 |
-
+ #' @export |
||
212 | -5x | +|||
88 | +
- if (length(out)) {+ report_card_template <- function(title, label, description = NULL, with_filter, filter_panel_api) { |
|||
213 | +89 | 2x |
- paste(out, collapse = "\n")+ checkmate::assert_string(title) |
|
214 | -+ | |||
90 | +2x |
- } else {+ checkmate::assert_string(label) |
||
215 | -3x | +91 | +2x |
- TRUE+ checkmate::assert_string(description, null.ok = TRUE) |
216 | -+ | |||
92 | +2x |
- }+ checkmate::assert_flag(with_filter) |
||
217 | -+ | |||
93 | +2x |
- }+ checkmate::assert_class(filter_panel_api, classes = "FilterPanelAPI") |
1 | +94 |
- #' Create a `tdata` Object+ |
||
2 | -+ | |||
95 | +2x |
- #'+ card <- teal::TealReportCard$new() |
||
3 | -+ | |||
96 | +2x |
- #' Create a new object called `tdata` which contains `data`, a `reactive` list of data.frames+ title <- if (label == "") title else label |
||
4 | -+ | |||
97 | +2x |
- #' (or `MultiAssayExperiment`), with attributes:+ card$set_name(title) |
||
5 | -+ | |||
98 | +2x |
- #' \itemize{+ card$append_text(title, "header2") |
||
6 | -+ | |||
99 | +1x |
- #' \item{`code` (`reactive`) containing code used to generate the data}+ if (!is.null(description)) card$append_text(description, "header3") |
||
7 | -+ | |||
100 | +1x |
- #' \item{join_keys (`join_keys`) containing the relationships between the data}+ if (with_filter) card$append_fs(filter_panel_api$get_filter_state()) |
||
8 | -+ | |||
101 | +2x |
- #' \item{metadata (`named list`) containing any metadata associated with the data frames}+ card |
||
9 | +102 |
- #' }+ } |
||
10 | +103 |
- #' @name tdata+ #' Resolve `datanames` for the modules |
||
11 | +104 |
- #' @param data A `named list` of `data.frames` (or `MultiAssayExperiment`)+ #' |
||
12 | +105 |
- #' which optionally can be `reactive`.+ #' Modifies `module$datanames` to include names of the parent dataset (taken from `join_keys`). |
||
13 | +106 |
- #' Inside this object all of these items will be made `reactive`.+ #' When `datanames` is set to `"all"` it is replaced with all available datasets names. |
||
14 | +107 |
- #' @param code A `character` (or `reactive` which evaluates to a `character`) containing+ #' @param modules (`teal_modules`) object |
||
15 | +108 |
- #' the code used to generate the data. This should be `reactive` if the code is changing+ #' @param datanames (`character`) names of datasets available in the `data` object |
||
16 | +109 |
- #' during a reactive context (e.g. if filtering changes the code). Inside this+ #' @param join_keys (`join_keys`) object |
||
17 | +110 |
- #' object `code` will be made reactive+ #' @return `teal_modules` with resolved `datanames` |
||
18 | +111 |
- #' @param join_keys A `teal.data::join_keys` object containing relationships between the+ #' @keywords internal |
||
19 | +112 |
- #' datasets.+ resolve_modules_datanames <- function(modules, datanames, join_keys) { |
||
20 | -+ | |||
113 | +! |
- #' @param metadata A `named list` each element contains a list of metadata about the named data.frame+ if (inherits(modules, "teal_modules")) { |
||
21 | -+ | |||
114 | +! |
- #' Each element of these list should be atomic and length one.+ modules$children <- sapply( |
||
22 | -+ | |||
115 | +! |
- #' @return A `tdata` object+ modules$children, |
||
23 | -+ | |||
116 | +! |
- #' @examples+ resolve_modules_datanames, |
||
24 | -+ | |||
117 | +! |
- #'+ simplify = FALSE, |
||
25 | -+ | |||
118 | +! |
- #' data <- new_tdata(+ datanames = datanames, |
||
26 | -+ | |||
119 | +! |
- #' data = list(iris = iris, mtcars = reactive(mtcars), dd = data.frame(x = 1:10)),+ join_keys = join_keys |
||
27 | +120 |
- #' code = "iris <- iris+ ) |
||
28 | -+ | |||
121 | +! |
- #' mtcars <- mtcars+ modules |
||
29 | +122 |
- #' dd <- data.frame(x = 1:10)",+ } else { |
||
30 | -+ | |||
123 | +! |
- #' metadata = list(dd = list(author = "NEST"), iris = list(version = 1))+ modules$datanames <- if (identical(modules$datanames, "all")) { |
||
31 | -+ | |||
124 | +! |
- #' )+ datanames |
||
32 | -+ | |||
125 | +! |
- #'+ } else if (is.character(modules$datanames)) { |
||
33 | -+ | |||
126 | +! |
- #' # Extract a data.frame+ extra_datanames <- setdiff(modules$datanames, datanames) |
||
34 | -+ | |||
127 | +! |
- #' isolate(data[["iris"]]())+ if (length(extra_datanames)) { |
||
35 | -+ | |||
128 | +! |
- #'+ stop( |
||
36 | -+ | |||
129 | +! |
- #' # Get code+ sprintf( |
||
37 | -+ | |||
130 | +! |
- #' isolate(get_code(data))+ "Module %s has datanames that are not available in a 'data':\n %s not in %s", |
||
38 | -+ | |||
131 | +! |
- #'+ modules$label, |
||
39 | -+ | |||
132 | +! |
- #' # Get metadata+ toString(extra_datanames), |
||
40 | -+ | |||
133 | +! |
- #' get_metadata(data, "iris")+ toString(datanames) |
||
41 | +134 |
- #'+ ) |
||
42 | +135 |
- #' @export+ ) |
||
43 | +136 |
- new_tdata <- function(data, code = "", join_keys = NULL, metadata = NULL) {+ } |
||
44 | -42x | +|||
137 | +! |
- checkmate::assert_list(+ datanames_adjusted <- intersect(modules$datanames, datanames) |
||
45 | -42x | +|||
138 | +! |
- data,+ include_parent_datanames(dataname = datanames_adjusted, join_keys = join_keys) |
||
46 | -42x | +|||
139 | +
- any.missing = FALSE, names = "unique",+ } |
|||
47 | -42x | +|||
140 | +! |
- types = c("data.frame", "reactive", "MultiAssayExperiment")+ modules |
||
48 | +141 |
- )+ } |
||
49 | -38x | +|||
142 | +
- checkmate::assert_class(join_keys, "join_keys", null.ok = TRUE)+ } |
|||
50 | -37x | +|||
143 | +
- checkmate::assert_multi_class(code, c("character", "reactive"))+ |
|||
51 | +144 |
-
+ #' Check `datanames` in modules |
||
52 | -36x | +|||
145 | +
- checkmate::assert_list(metadata, names = "unique", null.ok = TRUE)+ #' |
|||
53 | -34x | +|||
146 | +
- checkmate::assert_subset(names(metadata), names(data))+ #' This function ensures specified `datanames` in modules match those in the data object, |
|||
54 | -22x | +|||
147 | +
- for (m in metadata) teal.data::validate_metadata(m)+ #' returning error messages or `TRUE` for successful validation. |
|||
55 | +148 |
-
+ #' |
||
56 | -33x | +|||
149 | +
- if (is.reactive(code)) {+ #' @param modules (`teal_modules`) object |
|||
57 | -17x | +|||
150 | +
- isolate(checkmate::assert_class(code(), "character", .var.name = "code"))+ #' @param datanames (`character`) names of datasets available in the `data` object |
|||
58 | +151 |
- }+ #' |
||
59 | +152 |
-
+ #' @return A `character(1)` containing error message or `TRUE` if validation passes. |
||
60 | +153 |
- # create reactive data.frames+ #' @keywords internal |
||
61 | -32x | +|||
154 | +
- for (x in names(data)) {+ check_modules_datanames <- function(modules, datanames) { |
|||
62 | -51x | +155 | +8x |
- if (!is.reactive(data[[x]])) {+ checkmate::assert_class(modules, "teal_modules") |
63 | -29x | +156 | +8x |
- data[[x]] <- do.call(reactive, list(as.name(x)), envir = list2env(data[x]))+ checkmate::assert_character(datanames) |
64 | +157 |
- } else {+ |
||
65 | -22x | +158 | +8x |
- isolate(+ recursive_check_datanames <- function(modules, datanames) { |
66 | -22x | +|||
159 | +
- checkmate::assert_multi_class(+ # check teal_modules against datanames |
|||
67 | -22x | +160 | +16x |
- data[[x]](), c("data.frame", "MultiAssayExperiment"),+ if (inherits(modules, "teal_modules")) { |
68 | -22x | +161 | +8x |
- .var.name = "data"+ sapply(modules$children, function(module) recursive_check_datanames(module, datanames = datanames)) |
69 | +162 |
- )+ } else { |
||
70 | -+ | |||
163 | +8x |
- )+ extra_datanames <- setdiff(modules$datanames, c("all", datanames)) |
||
71 | -+ | |||
164 | +8x |
- }+ if (length(extra_datanames)) { |
||
72 | -+ | |||
165 | +2x |
- }+ sprintf( |
||
73 | -+ | |||
166 | +2x |
-
+ "- Module '%s' uses datanames not available in 'data': (%s) not in (%s)", |
||
74 | -+ | |||
167 | +2x |
- # set attributes+ modules$label, |
||
75 | -31x | +168 | +2x |
- attr(data, "code") <- if (is.reactive(code)) code else reactive(code)+ toString(dQuote(extra_datanames, q = FALSE)), |
76 | -31x | +169 | +2x |
- attr(data, "join_keys") <- join_keys+ toString(dQuote(datanames, q = FALSE)) |
77 | -31x | +|||
170 | +
- attr(data, "metadata") <- metadata+ ) |
|||
78 | +171 |
-
+ } |
||
79 | +172 |
- # set class+ } |
||
80 | -31x | +|||
173 | +
- class(data) <- c("tdata", class(data))+ } |
|||
81 | -31x | +174 | +8x |
- data+ check_datanames <- unlist(recursive_check_datanames(modules, datanames)) |
82 | -+ | |||
175 | +8x |
- }+ if (length(check_datanames)) { |
||
83 | -+ | |||
176 | +2x |
-
+ paste(check_datanames, collapse = "\n") |
||
84 | +177 |
- #' Function to convert a `tdata` object to an `environment`+ } else { |
||
85 | -+ | |||
178 | +6x |
- #' Any `reactives` inside `tdata` are first evaluated+ TRUE |
||
86 | +179 |
- #' @param data a `tdata` object+ } |
||
87 | +180 |
- #' @return an `environment`+ } |
||
88 | +181 |
- #' @examples+ |
||
89 | +182 |
- #'+ #' Check `datanames` in filters |
||
90 | +183 |
- #' data <- new_tdata(+ #' |
||
91 | +184 |
- #' data = list(iris = iris, mtcars = reactive(mtcars)),+ #' This function checks whether `datanames` in filters correspond to those in `data`, |
||
92 | +185 |
- #' code = "iris <- iris+ #' returning character vector with error messages or TRUE if all checks pass. |
||
93 | +186 |
- #' mtcars = mtcars"+ #' |
||
94 | +187 |
- #' )+ #' @param filters (`teal_slices`) object |
||
95 | +188 |
- #'+ #' @param datanames (`character`) names of datasets available in the `data` object |
||
96 | +189 |
- #' my_env <- isolate(tdata2env(data))+ #' |
||
97 | +190 |
- #'+ #' @return A `character(1)` containing error message or TRUE if validation passes. |
||
98 | +191 |
- #' @export+ #' @keywords internal |
||
99 | +192 |
- tdata2env <- function(data) { # nolint+ check_filter_datanames <- function(filters, datanames) { |
||
100 | -2x | +193 | +6x |
- checkmate::assert_class(data, "tdata")+ checkmate::assert_class(filters, "teal_slices") |
101 | -1x | +194 | +6x |
- list2env(lapply(data, function(x) if (is.reactive(x)) x() else x))+ checkmate::assert_character(datanames) |
102 | +195 |
- }+ |
||
103 | +196 |
-
+ # check teal_slices against datanames |
||
104 | -+ | |||
197 | +6x |
- #' @rdname tdata+ out <- unlist(sapply( |
||
105 | -+ | |||
198 | +6x |
- #' @param x a `tdata` object+ filters, function(filter) { |
||
106 | -+ | |||
199 | +2x |
- #' @param ... additional arguments for the generic+ dataname <- shiny::isolate(filter$dataname) |
||
107 | -+ | |||
200 | +2x |
- #' @export+ if (!dataname %in% datanames) { |
||
108 | -+ | |||
201 | +2x |
- get_code.tdata <- function(x, ...) { # nolint+ sprintf( |
||
109 | -+ | |||
202 | +2x |
- # note teal.data which teal depends on defines the get_code method+ "- Filter '%s' refers to dataname not available in 'data':\n %s not in (%s)", |
||
110 | -6x | +203 | +2x |
- attr(x, "code")()+ shiny::isolate(filter$id), |
111 | -+ | |||
204 | +2x |
- }+ dQuote(dataname, q = FALSE), |
||
112 | -+ | |||
205 | +2x |
-
+ toString(dQuote(datanames, q = FALSE)) |
||
113 | +206 |
-
+ ) |
||
114 | +207 |
- #' Wrapper for `get_code.tdata`+ } |
||
115 | +208 |
- #' This wrapper is to be used by downstream packages to extract the code of a `tdata` object+ } |
||
116 | +209 |
- #'+ )) |
||
117 | +210 |
- #' @param data (`tdata`) object+ |
||
118 | +211 |
- #'+ |
||
119 | -+ | |||
212 | +6x |
- #' @return (`character`) code used in the `tdata` object.+ if (length(out)) { |
||
120 | -+ | |||
213 | +2x |
- #' @export+ paste(out, collapse = "\n") |
||
121 | +214 |
- get_code_tdata <- function(data) {+ } else { |
||
122 | +215 | 4x |
- checkmate::assert_class(data, "tdata")- |
- |
123 | -2x | -
- get_code(data)+ TRUE |
||
124 | +216 |
- }+ } |
||
125 | +217 |
-
+ } |
126 | +1 |
- #' Extract `join_keys` from `tdata`+ #' Validate that dataset has a minimum number of observations |
|
127 | +2 |
- #' @param data A `tdata` object+ #' |
|
128 | +3 |
- #' @param ... Additional arguments (not used)+ #' @description `r lifecycle::badge("stable")` |
|
129 | +4 |
- #' @export+ #' @param x a data.frame |
|
130 | +5 |
- join_keys.tdata <- function(data, ...) {- |
- |
131 | -3x | -
- attr(data, "join_keys")+ #' @param min_nrow minimum number of rows in \code{x} |
|
132 | +6 |
- }+ #' @param complete \code{logical} default \code{FALSE} when set to \code{TRUE} then complete cases are checked. |
|
133 | +7 |
-
+ #' @param allow_inf \code{logical} default \code{TRUE} when set to \code{FALSE} then error thrown if any values are |
|
134 | +8 |
-
+ #' infinite. |
|
135 | +9 |
- #' Function to get metadata from a `tdata` object+ #' @param msg (`character(1)`) additional message to display alongside the default message. |
|
136 | +10 |
- #' @param data `tdata` - object to extract the data from+ #' |
|
137 | +11 |
- #' @param dataname `character(1)` the dataset name whose metadata is requested+ #' @details This function is a wrapper for `shiny::validate`. |
|
138 | +12 |
- #' @return Either list of metadata or NULL if no metadata+ #' |
|
139 | +13 |
#' @export |
|
140 | +14 |
- get_metadata <- function(data, dataname) {- |
- |
141 | -4x | -
- checkmate::assert_string(dataname)- |
- |
142 | -4x | -
- UseMethod("get_metadata", data)+ #' |
|
143 | +15 |
- }+ #' @examples |
|
144 | +16 |
-
+ #' library(teal) |
|
145 | +17 |
- #' @rdname get_metadata+ #' ui <- fluidPage( |
|
146 | +18 |
- #' @export+ #' sliderInput("len", "Max Length of Sepal", |
|
147 | +19 |
- get_metadata.tdata <- function(data, dataname) {+ #' min = 4.3, max = 7.9, value = 5 |
|
148 | -4x | +||
20 | +
- metadata <- attr(data, "metadata")+ #' ), |
||
149 | -4x | +||
21 | +
- if (is.null(metadata)) {+ #' plotOutput("plot") |
||
150 | -1x | +||
22 | +
- return(NULL)+ #' ) |
||
151 | +23 |
- }+ #' |
|
152 | -3x | +||
24 | +
- metadata[[dataname]]+ #' server <- function(input, output) { |
||
153 | +25 |
- }+ #' output$plot <- renderPlot({ |
|
154 | +26 |
-
+ #' df <- iris[iris$Sepal.Length <= input$len, ] |
|
155 | +27 |
- #' @rdname get_metadata+ #' validate_has_data( |
|
156 | +28 |
- #' @export+ #' iris_f, |
|
157 | +29 |
- get_metadata.default <- function(data, dataname) {+ #' min_nrow = 10, |
|
158 | -! | +||
30 | +
- stop("get_metadata function not implemented for this object")+ #' complete = FALSE, |
||
159 | +31 |
- }+ #' msg = "Please adjust Max Length of Sepal" |
1 | +32 |
- #' Filter manager modal+ #' ) |
|
2 | +33 |
#' |
|
3 | +34 |
- #' Opens modal containing the filter manager UI.+ #' hist(iris_f$Sepal.Length, breaks = 5) |
|
4 | +35 |
- #'+ #' }) |
|
5 | +36 |
- #' @name module_filter_manager_modal+ #' } |
|
6 | +37 |
- #' @inheritParams filter_manager_srv+ #' if (interactive()) { |
|
7 | +38 |
- #' @examples+ #' shinyApp(ui, server) |
|
8 | +39 |
- #' fd1 <- teal.slice::init_filtered_data(list(iris = list(dataset = iris)))+ #' } |
|
9 | +40 |
- #' fd2 <- teal.slice::init_filtered_data(+ #' |
|
10 | +41 |
- #' list(iris = list(dataset = iris), mtcars = list(dataset = mtcars))+ validate_has_data <- function(x, |
|
11 | +42 |
- #' )+ min_nrow = NULL, |
|
12 | +43 |
- #' fd3 <- teal.slice::init_filtered_data(+ complete = FALSE, |
|
13 | +44 |
- #' list(iris = list(dataset = iris), women = list(dataset = women))+ allow_inf = TRUE, |
|
14 | +45 |
- #' )+ msg = NULL) { |
|
15 | -+ | ||
46 | +17x |
- #' filter <- teal_slices(+ checkmate::assert_string(msg, null.ok = TRUE) |
|
16 | -+ | ||
47 | +15x |
- #' teal.slice::teal_slice(dataname = "iris", varname = "Sepal.Length"),+ checkmate::assert_data_frame(x) |
|
17 | -+ | ||
48 | +15x |
- #' teal.slice::teal_slice(dataname = "iris", varname = "Species"),+ if (!is.null(min_nrow)) { |
|
18 | -+ | ||
49 | +15x |
- #' teal.slice::teal_slice(dataname = "mtcars", varname = "mpg"),+ if (complete) { |
|
19 | -+ | ||
50 | +5x |
- #' teal.slice::teal_slice(dataname = "women", varname = "height"),+ complete_index <- stats::complete.cases(x) |
|
20 | -+ | ||
51 | +5x |
- #' mapping = list(+ validate(need( |
|
21 | -+ | ||
52 | +5x |
- #' module2 = c("mtcars mpg"),+ sum(complete_index) > 0 && nrow(x[complete_index, , drop = FALSE]) >= min_nrow, |
|
22 | -+ | ||
53 | +5x |
- #' module3 = c("women height"),+ paste(c(paste("Number of complete cases is less than:", min_nrow), msg), collapse = "\n") |
|
23 | +54 |
- #' global_filters = "iris Species"+ )) |
|
24 | +55 |
- #' )+ } else { |
|
25 | -+ | ||
56 | +10x |
- #' )+ validate(need( |
|
26 | -+ | ||
57 | +10x |
- #'+ nrow(x) >= min_nrow, |
|
27 | -+ | ||
58 | +10x | +
+ paste(+ |
+ |
59 | +10x | +
+ c(paste("Minimum number of records not met: >=", min_nrow, "records required."), msg),+ |
+ |
60 | +10x |
- #' app <- shinyApp(+ collapse = "\n" |
|
28 | +61 |
- #' ui = fluidPage(+ ) |
|
29 | +62 |
- #' teal:::filter_manager_modal_ui("manager")+ )) |
|
30 | +63 |
- #' ),+ } |
|
31 | +64 |
- #' server = function(input, output, session) {+ |
|
32 | -+ | ||
65 | +10x |
- #' teal:::filter_manager_modal_srv(+ if (!allow_inf) { |
|
33 | -+ | ||
66 | +6x |
- #' "manager",+ validate(need( |
|
34 | -+ | ||
67 | +6x |
- #' filtered_data_list = list(module1 = fd1, module2 = fd2, module3 = fd3),+ all(vapply(x, function(col) !is.numeric(col) || !any(is.infinite(col)), logical(1))), |
|
35 | -+ | ||
68 | +6x |
- #' filter = filter+ "Dataframe contains Inf values which is not allowed." |
|
36 | +69 |
- #' )+ )) |
|
37 | +70 |
- #' }+ } |
|
38 | +71 |
- #' )+ } |
|
39 | +72 |
- #' if (interactive()) {+ } |
|
40 | +73 |
- #' shinyApp(app$ui, app$server)+ |
|
41 | +74 |
- #' }+ #' Validate that dataset has unique rows for key variables |
|
42 | +75 |
#' |
|
43 | +76 |
- #' @keywords internal+ #' @description `r lifecycle::badge("stable")` |
|
44 | +77 |
- #'+ #' @param x a data.frame |
|
45 | +78 |
- NULL+ #' @param key a vector of ID variables from \code{x} that identify unique records |
|
46 | +79 |
-
+ #' |
|
47 | +80 |
- #' @rdname module_filter_manager_modal+ #' @details This function is a wrapper for `shiny::validate`. |
|
48 | +81 |
- filter_manager_modal_ui <- function(id) {- |
- |
49 | -1x | -
- ns <- NS(id)- |
- |
50 | -1x | -
- tags$button(- |
- |
51 | -1x | -
- id = ns("show"),- |
- |
52 | -1x | -
- class = "btn action-button filter_manager_button",- |
- |
53 | -1x | -
- title = "Show filters manager modal",- |
- |
54 | -1x | -
- icon("gear")+ #' |
|
55 | +82 |
- )+ #' @export |
|
56 | +83 |
- }+ #' |
|
57 | +84 |
-
+ #' @examples |
|
58 | +85 |
- #' @rdname module_filter_manager_modal+ #' iris$id <- rep(1:50, times = 3) |
|
59 | +86 |
- filter_manager_modal_srv <- function(id, filtered_data_list, filter) {- |
- |
60 | -4x | -
- moduleServer(id, function(input, output, session) {- |
- |
61 | -4x | -
- observeEvent(input$show, {+ #' ui <- fluidPage( |
|
62 | -! | +||
87 | +
- logger::log_trace("filter_manager_modal_srv@1 show button has been clicked.")+ #' selectInput( |
||
63 | -! | +||
88 | +
- showModal(+ #' inputId = "species", |
||
64 | -! | +||
89 | +
- modalDialog(+ #' label = "Select species", |
||
65 | -! | +||
90 | +
- filter_manager_ui(session$ns("filter_manager")),+ #' choices = c("setosa", "versicolor", "virginica"), |
||
66 | -! | +||
91 | +
- size = "l",+ #' selected = "setosa", |
||
67 | -! | +||
92 | +
- footer = NULL,+ #' multiple = TRUE |
||
68 | -! | +||
93 | +
- easyClose = TRUE+ #' ), |
||
69 | +94 |
- )+ #' plotOutput("plot") |
|
70 | +95 |
- )+ #' ) |
|
71 | +96 |
- })+ #' server <- function(input, output) { |
|
72 | +97 |
-
+ #' output$plot <- renderPlot({ |
|
73 | -4x | +||
98 | +
- filter_manager_srv("filter_manager", filtered_data_list, filter)+ #' iris_f <- iris[iris$Species %in% input$species, ] |
||
74 | +99 |
- })+ #' validate_one_row_per_id(iris_f, key = c("id")) |
|
75 | +100 |
- }+ #' |
|
76 | +101 |
-
+ #' hist(iris_f$Sepal.Length, breaks = 5) |
|
77 | +102 |
- #' @rdname module_filter_manager+ #' }) |
|
78 | +103 |
- filter_manager_ui <- function(id) {+ #' } |
|
79 | -! | +||
104 | +
- ns <- NS(id)+ #' if (interactive()) { |
||
80 | -! | +||
105 | +
- div(+ #' shinyApp(ui, server) |
||
81 | -! | +||
106 | +
- class = "filter_manager_content",+ #' } |
||
82 | -! | +||
107 | +
- tableOutput(ns("slices_table")),+ #' |
||
83 | -! | +||
108 | +
- snapshot_manager_ui(ns("snapshot_manager"))+ validate_one_row_per_id <- function(x, key = c("USUBJID", "STUDYID")) { |
||
84 | -+ | ||
109 | +! |
- )+ validate(need(!any(duplicated(x[key])), paste("Found more than one row per id."))) |
|
85 | +110 |
} |
|
86 | +111 | ||
87 | +112 |
- #' Manage multiple `FilteredData` objects+ #' Validates that vector includes all expected values |
|
88 | +113 |
#' |
|
89 | +114 |
- #' Oversee filter states in the whole application.+ #' @description `r lifecycle::badge("stable")` |
|
90 | +115 |
- #'+ #' @param x values to test. All must be in \code{choices} |
|
91 | +116 |
- #' @rdname module_filter_manager+ #' @param choices a vector to test for values of \code{x} |
|
92 | +117 |
- #' @details+ #' @param msg warning message to display |
|
93 | +118 |
- #' This module observes the changes of the filters in each `FilteredData` object+ #' |
|
94 | +119 |
- #' and keeps track of all filters used. A mapping of filters to modules+ #' @details This function is a wrapper for `shiny::validate`. |
|
95 | +120 |
- #' is kept in the `mapping_matrix` object (which is actually a `data.frame`)+ #' |
|
96 | +121 |
- #' that tracks which filters (rows) are active in which modules (columns).+ #' @export |
|
97 | +122 |
#' |
|
98 | +123 |
- #' @param id (`character(1)`)\cr+ #' @examples |
|
99 | +124 |
- #' `shiny` module id.+ #' ui <- fluidPage( |
|
100 | +125 |
- #' @param filtered_data_list (`named list`)\cr+ #' selectInput( |
|
101 | +126 |
- #' A list, possibly nested, of `FilteredData` objects.+ #' "species", |
|
102 | +127 |
- #' Each `FilteredData` will be served to one module in the `teal` application.+ #' "Select species", |
|
103 | +128 |
- #' The structure of the list must reflect the nesting of modules in tabs+ #' choices = c("setosa", "versicolor", "virginica", "unknown species"), |
|
104 | +129 |
- #' and names of the list must be the same as labels of their respective modules.+ #' selected = "setosa", |
|
105 | +130 |
- #' @inheritParams init+ #' multiple = FALSE |
|
106 | +131 |
- #' @return A list of `reactive`s, each holding a `teal_slices`, as returned by `filter_manager_module_srv`.+ #' ), |
|
107 | +132 |
- #' @keywords internal+ #' verbatimTextOutput("summary") |
|
108 | +133 |
- #'+ #' ) |
|
109 | +134 |
- filter_manager_srv <- function(id, filtered_data_list, filter) {+ #' |
|
110 | -6x | +||
135 | +
- moduleServer(id, function(input, output, session) {+ #' server <- function(input, output) { |
||
111 | -6x | +||
136 | +
- logger::log_trace("filter_manager_srv initializing for: { paste(names(filtered_data_list), collapse = ', ')}.")+ #' output$summary <- renderPrint({ |
||
112 | +137 |
-
+ #' validate_in(input$species, iris$Species, "Species does not exist.") |
|
113 | -6x | +||
138 | +
- is_module_specific <- isTRUE(attr(filter, "module_specific"))+ #' nrow(iris[iris$Species == input$species, ]) |
||
114 | +139 |
-
+ #' }) |
|
115 | +140 |
- # Create global list of slices.+ #' } |
|
116 | +141 |
- # Contains all available teal_slice objects available to all modules.+ #' if (interactive()) { |
|
117 | +142 |
- # Passed whole to instances of FilteredData used for individual modules.+ #' shinyApp(ui, server) |
|
118 | +143 |
- # Down there a subset that pertains to the data sets used in that module is applied and displayed.+ #' } |
|
119 | -6x | +||
144 | +
- slices_global <- reactiveVal(filter)+ #' |
||
120 | +145 |
-
+ validate_in <- function(x, choices, msg) { |
|
121 | -6x | +||
146 | +! |
- filtered_data_list <-+ validate(need(length(x) > 0 && length(choices) > 0 && all(x %in% choices), msg)) |
|
122 | -6x | +||
147 | +
- if (!is_module_specific) {+ } |
||
123 | +148 |
- # Retrieve the first FilteredData from potentially nested list.+ |
|
124 | +149 |
- # List of length one is named "global_filters" because that name is forbidden for a module label.+ #' Validates that vector has length greater than 0 |
|
125 | -5x | +||
150 | +
- list(global_filters = unlist(filtered_data_list)[[1]])+ #' |
||
126 | +151 |
- } else {+ #' @description `r lifecycle::badge("stable")` |
|
127 | +152 |
- # Flatten potentially nested list of FilteredData objects while maintaining useful names.+ #' @param x vector |
|
128 | +153 |
- # Simply using `unlist` would result in concatenated names.+ #' @param msg message to display |
|
129 | -1x | +||
154 | +
- flatten_nested <- function(x, name = NULL) {+ #' |
||
130 | -5x | +||
155 | +
- if (inherits(x, "FilteredData")) {+ #' @details This function is a wrapper for `shiny::validate`. |
||
131 | -3x | +||
156 | +
- setNames(list(x), name)+ #' |
||
132 | +157 |
- } else {+ #' @export |
|
133 | -2x | +||
158 | +
- unlist(lapply(names(x), function(name) flatten_nested(x[[name]], name)))+ #' |
||
134 | +159 |
- }+ #' @examples |
|
135 | +160 |
- }+ #' data <- data.frame( |
|
136 | -1x | +||
161 | +
- flatten_nested(filtered_data_list)+ #' id = c(1:10, 11:20, 1:10), |
||
137 | +162 |
- }+ #' strata = rep(c("A", "B"), each = 15) |
|
138 | +163 |
-
+ #' ) |
|
139 | +164 |
- # Create mapping fo filters to modules in matrix form (presented as data.frame).+ #' ui <- fluidPage( |
|
140 | +165 |
- # Modules get NAs for filters that cannot be set for them.+ #' selectInput("ref1", "Select strata1 to compare", |
|
141 | -6x | +||
166 | +
- mapping_matrix <- reactive({+ #' choices = c("A", "B", "C"), selected = "A" |
||
142 | -6x | +||
167 | +
- state_ids_global <- vapply(slices_global(), `[[`, character(1L), "id")+ #' ), |
||
143 | -6x | +||
168 | +
- mapping_smooth <- lapply(filtered_data_list, function(x) {+ #' selectInput("ref2", "Select strata2 to compare", |
||
144 | -8x | +||
169 | +
- state_ids_local <- vapply(x$get_filter_state(), `[[`, character(1L), "id")+ #' choices = c("A", "B", "C"), selected = "B" |
||
145 | -8x | +||
170 | +
- state_ids_allowed <- vapply(x$get_available_teal_slices()(), `[[`, character(1L), "id")+ #' ), |
||
146 | -8x | +||
171 | +
- states_active <- state_ids_global %in% state_ids_local+ #' verbatimTextOutput("arm_summary") |
||
147 | -8x | +||
172 | +
- ifelse(state_ids_global %in% state_ids_allowed, states_active, NA)+ #' ) |
||
148 | +173 |
- })+ #' |
|
149 | +174 |
-
+ #' server <- function(input, output) { |
|
150 | -6x | +||
175 | +
- as.data.frame(mapping_smooth, row.names = state_ids_global, check.names = FALSE)+ #' output$arm_summary <- renderText({ |
||
151 | +176 |
- })+ #' sample_1 <- data$id[data$strata == input$ref1] |
|
152 | +177 |
-
+ #' sample_2 <- data$id[data$strata == input$ref2] |
|
153 | -6x | +||
178 | +
- output$slices_table <- renderTable(+ #' |
||
154 | -6x | +||
179 | +
- expr = {+ #' validate_has_elements(sample_1, "No subjects in strata1.") |
||
155 | +180 |
- # Display logical values as UTF characters.+ #' validate_has_elements(sample_2, "No subjects in strata2.") |
|
156 | -3x | +||
181 | +
- mm <- mapping_matrix()+ #' |
||
157 | -3x | +||
182 | +
- mm[] <- lapply(mm, ifelse, yes = intToUtf8(9989), no = intToUtf8(10060))+ #' paste0( |
||
158 | -3x | +||
183 | +
- mm[] <- lapply(mm, function(x) ifelse(is.na(x), intToUtf8(128306), x))+ #' "Number of samples in: strata1=", length(sample_1), |
||
159 | -3x | +||
184 | +
- if (!is_module_specific) colnames(mm) <- "Global Filters"+ #' " comparions strata2=", length(sample_2) |
||
160 | +185 |
-
+ #' ) |
|
161 | +186 |
- # Display placeholder if no filters defined.+ #' }) |
|
162 | -3x | +||
187 | +
- if (nrow(mm) == 0L) {+ #' } |
||
163 | -3x | +||
188 | +
- mm <- data.frame(`Filter manager` = "No filters specified.", check.names = FALSE)+ #' if (interactive()) { |
||
164 | -3x | +||
189 | +
- rownames(mm) <- ""+ #' shinyApp(ui, server) |
||
165 | +190 |
- }+ #' } |
|
166 | +191 |
-
+ validate_has_elements <- function(x, msg) {+ |
+ |
192 | +! | +
+ validate(need(length(x) > 0, msg)) |
|
167 | +193 |
- # Report Previewer will not be displayed.+ } |
|
168 | -3x | +||
194 | +
- mm[names(mm) != "Report previewer"]+ |
||
169 | +195 |
- },+ #' Validates no intersection between two vectors |
|
170 | -6x | +||
196 | +
- align = paste(c("l", rep("c", sum(names(filtered_data_list) != "Report previewer"))), collapse = ""),+ #' |
||
171 | -6x | +||
197 | +
- rownames = TRUE+ #' @description `r lifecycle::badge("stable")` |
||
172 | +198 |
- )+ #' @param x vector |
|
173 | +199 |
-
+ #' @param y vector |
|
174 | +200 |
- # Create list of module calls.+ #' @param msg message to display if \code{x} and \code{y} intersect |
|
175 | -6x | +||
201 | +
- modules_out <- lapply(names(filtered_data_list), function(module_name) {+ #' |
||
176 | -8x | +||
202 | +
- filter_manager_module_srv(+ #' @details This function is a wrapper for `shiny::validate`. |
||
177 | -8x | +||
203 | +
- id = module_name,+ #' |
||
178 | -8x | +||
204 | +
- module_fd = filtered_data_list[[module_name]],+ #' @export |
||
179 | -8x | +||
205 | +
- slices_global = slices_global+ #' |
||
180 | +206 |
- )+ #' @examples |
|
181 | +207 |
- })+ #' data <- data.frame( |
|
182 | +208 |
-
+ #' id = c(1:10, 11:20, 1:10), |
|
183 | +209 |
- # Call snapshot manager.+ #' strata = rep(c("A", "B", "C"), each = 10) |
|
184 | -6x | +||
210 | +
- snapshot_manager_srv("snapshot_manager", slices_global, mapping_matrix, filtered_data_list)+ #' ) |
||
185 | +211 |
-
+ #' |
|
186 | -6x | +||
212 | +
- modules_out # returned for testing purpose+ #' ui <- fluidPage( |
||
187 | +213 |
- })+ #' selectInput("ref1", "Select strata1 to compare", |
|
188 | +214 |
- }+ #' choices = c("A", "B", "C"), |
|
189 | +215 |
-
+ #' selected = "A" |
|
190 | +216 |
- #' Module specific filter manager+ #' ), |
|
191 | +217 |
- #'+ #' selectInput("ref2", "Select strata2 to compare", |
|
192 | +218 |
- #' Track filter states in single module.+ #' choices = c("A", "B", "C"), |
|
193 | +219 |
- #'+ #' selected = "B" |
|
194 | +220 |
- #' This module tracks the state of a single `FilteredData` object and global `teal_slices`+ #' ), |
|
195 | +221 |
- #' and updates both objects as necessary. Filter states added in different modules+ #' verbatimTextOutput("summary") |
|
196 | +222 |
- #' Filter states added any individual module are added to global `teal_slices`+ #' ) |
|
197 | +223 |
- #' and from there become available in other modules+ #' |
|
198 | +224 |
- #' by setting `private$available_teal_slices` in each `FilteredData`.+ #' server <- function(input, output) { |
|
199 | +225 |
- #'+ #' output$summary <- renderText({ |
|
200 | +226 |
- #' @param id (`character(1)`)\cr+ #' sample_1 <- data$id[data$strata == input$ref1] |
|
201 | +227 |
- #' `shiny` module id.+ #' sample_2 <- data$id[data$strata == input$ref2] |
|
202 | +228 |
- #' @param module_fd (`FilteredData`)\cr+ #' |
|
203 | +229 |
- #' object to filter data in the teal-module+ #' validate_no_intersection( |
|
204 | +230 |
- #' @param slices_global (`reactiveVal`)\cr+ #' sample_1, sample_2, |
|
205 | +231 |
- #' stores `teal_slices` with all available filters; allows the following actions:+ #' "subjects within strata1 and strata2 cannot overlap" |
|
206 | +232 |
- #' - to disable/enable a specific filter in a module+ #' ) |
|
207 | +233 |
- #' - to restore saved filter settings+ #' paste0( |
|
208 | +234 |
- #' - to save current filter panel settings+ #' "Number of subject in: reference treatment=", length(sample_1), |
|
209 | +235 |
- #' @return A `reactive` expression containing the slices active in this module.+ #' " comparions treatment=", length(sample_2) |
|
210 | +236 |
- #' @keywords internal+ #' ) |
|
211 | +237 |
- #'+ #' }) |
|
212 | +238 |
- filter_manager_module_srv <- function(id, module_fd, slices_global) {+ #' } |
|
213 | -8x | +||
239 | +
- moduleServer(id, function(input, output, session) {+ #' if (interactive()) { |
||
214 | +240 |
- # Only operate on slices that refer to data sets present in this module.+ #' shinyApp(ui, server) |
|
215 | -8x | +||
241 | +
- module_fd$set_available_teal_slices(reactive(slices_global()))+ #' } |
||
216 | +242 |
-
+ #' |
|
217 | +243 |
- # Track filter state of this module.+ validate_no_intersection <- function(x, y, msg) { |
|
218 | -8x | +||
244 | +! |
- slices_module <- reactive(module_fd$get_filter_state())+ validate(need(length(intersect(x, y)) == 0, msg)) |
|
219 | +245 |
-
+ } |
|
220 | +246 |
- # Reactive values for comparing states.+ |
|
221 | -8x | +||
247 | +
- previous_slices <- reactiveVal(isolate(slices_module()))+ |
||
222 | -8x | +||
248 | +
- slices_added <- reactiveVal(NULL)+ #' Validates that dataset contains specific variable |
||
223 | +249 |
-
+ #' |
|
224 | +250 |
- # Observe changes in module filter state and trigger appropriate actions.+ #' @description `r lifecycle::badge("stable")` |
|
225 | -8x | +||
251 | +
- observeEvent(slices_module(), ignoreNULL = FALSE, {+ #' @param data a data.frame |
||
226 | -3x | +||
252 | +
- logger::log_trace("filter_manager_srv@1 detecting states deltas in module: { id }.")+ #' @param varname name of variable in \code{data} |
||
227 | -3x | +||
253 | +
- added <- setdiff_teal_slices(slices_module(), slices_global())+ #' @param msg message to display if \code{data} does not include \code{varname} |
||
228 | -! | +||
254 | +
- if (length(added)) slices_added(added)+ #' |
||
229 | -3x | +||
255 | +
- previous_slices(slices_module())+ #' @details This function is a wrapper for `shiny::validate`. |
||
230 | +256 |
- })+ #' |
|
231 | +257 |
-
+ #' @export |
|
232 | -8x | +||
258 | +
- observeEvent(slices_added(), ignoreNULL = TRUE, {+ #' |
||
233 | -! | +||
259 | +
- logger::log_trace("filter_manager_srv@2 added filter in module: { id }.")+ #' @examples |
||
234 | +260 |
- # In case the new state has the same id as an existing state, add a suffix to it.+ #' data <- data.frame( |
|
235 | -! | +||
261 | +
- global_ids <- vapply(slices_global(), `[[`, character(1L), "id")+ #' one = rep("a", length.out = 20), |
||
236 | -! | +||
262 | +
- lapply(+ #' two = rep(c("a", "b"), length.out = 20) |
||
237 | -! | +||
263 | +
- slices_added(),+ #' ) |
||
238 | -! | +||
264 | +
- function(slice) {+ #' ui <- fluidPage( |
||
239 | -! | +||
265 | +
- if (slice$id %in% global_ids) {+ #' selectInput( |
||
240 | -! | +||
266 | +
- slice$id <- utils::tail(make.unique(c(global_ids, slice$id), sep = "_"), 1)+ #' "var", |
||
241 | +267 |
- }+ #' "Select variable", |
|
242 | +268 |
- }+ #' choices = c("one", "two", "three", "four"), |
|
243 | +269 |
- )+ #' selected = "one" |
|
244 | -! | +||
270 | +
- slices_global_new <- c(slices_global(), slices_added())+ #' ), |
||
245 | -! | +||
271 | +
- slices_global(slices_global_new)+ #' verbatimTextOutput("summary") |
||
246 | -! | +||
272 | +
- slices_added(NULL)+ #' ) |
||
247 | +273 |
- })+ #' |
|
248 | +274 |
-
+ #' server <- function(input, output) { |
|
249 | -8x | +||
275 | +
- slices_module # returned for testing purpose+ #' output$summary <- renderText({ |
||
250 | +276 |
- })+ #' validate_has_variable(data, input$var) |
|
251 | +277 |
- }+ #' paste0("Selected treatment variables: ", paste(input$var, collapse = ", ")) |
1 | +278 |
- #' Include `CSS` files from `/inst/css/` package directory to application header+ #' }) |
|
2 | +279 |
- #'+ #' } |
|
3 | +280 |
- #' `system.file` should not be used to access files in other packages, it does+ #' if (interactive()) { |
|
4 | +281 |
- #' not work with `devtools`. Therefore, we redefine this method in each package+ #' shinyApp(ui, server) |
|
5 | +282 |
- #' as needed. Thus, we do not export this method+ #' } |
|
6 | +283 |
- #'+ validate_has_variable <- function(data, varname, msg) { |
|
7 | -+ | ||
284 | +! |
- #' @param pattern (`character`) pattern of files to be included+ if (length(varname) != 0) { |
|
8 | -+ | ||
285 | +! |
- #'+ has_vars <- varname %in% names(data) |
|
9 | +286 |
- #' @return HTML code that includes `CSS` files+ |
|
10 | -+ | ||
287 | +! |
- #' @keywords internal+ if (!all(has_vars)) { |
|
11 | -+ | ||
288 | +! |
- include_css_files <- function(pattern = "*") {+ if (missing(msg)) { |
|
12 | -22x | +||
289 | +! |
- css_files <- list.files(+ msg <- sprintf( |
|
13 | -22x | +||
290 | +! |
- system.file("css", package = "teal", mustWork = TRUE),+ "%s does not have the required variables: %s.", |
|
14 | -22x | +||
291 | +! |
- pattern = pattern, full.names = TRUE+ deparse(substitute(data)), |
|
15 | -+ | ||
292 | +! |
- )+ toString(varname[!has_vars]) |
|
16 | -22x | +||
293 | +
- return(+ ) |
||
17 | -22x | +||
294 | +
- shiny::singleton(+ } |
||
18 | -22x | +||
295 | +! |
- shiny::tags$head(lapply(css_files, shiny::includeCSS))+ validate(need(FALSE, msg)) |
|
19 | +296 |
- )+ } |
|
20 | +297 |
- )+ } |
|
21 | +298 |
} |
|
22 | +299 | ||
23 | +300 |
- #' Include `JS` files from `/inst/js/` package directory to application header+ #' Validate that variables has expected number of levels |
|
24 | +301 |
#' |
|
25 | +302 |
- #' `system.file` should not be used to access files in other packages, it does+ #' @description `r lifecycle::badge("stable")` |
|
26 | +303 |
- #' not work with `devtools`. Therefore, we redefine this method in each package+ #' @param x variable name. If \code{x} is not a factor, the unique values |
|
27 | +304 |
- #' as needed. Thus, we do not export this method+ #' are treated as levels. |
|
28 | +305 |
- #'+ #' @param min_levels cutoff for minimum number of levels of \code{x} |
|
29 | +306 |
- #' @param pattern (`character`) pattern of files to be included, passed to `system.file`+ #' @param max_levels cutoff for maximum number of levels of \code{x} |
|
30 | +307 |
- #' @param except (`character`) vector of basename filenames to be excluded+ #' @param var_name name of variable being validated for use in |
|
31 | +308 |
- #'+ #' validation message |
|
32 | +309 |
- #' @return HTML code that includes `JS` files+ #' |
|
33 | +310 |
- #' @keywords internal+ #' @details If the number of levels of \code{x} is less than \code{min_levels} |
|
34 | +311 |
- include_js_files <- function(pattern = NULL, except = NULL) {- |
- |
35 | -22x | -
- checkmate::assert_character(except, min.len = 1, any.missing = FALSE, null.ok = TRUE)- |
- |
36 | -22x | -
- js_files <- list.files(system.file("js", package = "teal", mustWork = TRUE), pattern = pattern, full.names = TRUE)+ #' or greater than \code{max_levels} the validation will fail. |
|
37 | -22x | +||
312 | +
- js_files <- js_files[!(basename(js_files) %in% except)] # no-op if except is NULL+ #' This function is a wrapper for `shiny::validate`. |
||
38 | +313 |
-
+ #' |
|
39 | -22x | +||
314 | +
- return(singleton(lapply(js_files, includeScript)))+ #' @export |
||
40 | +315 |
- }+ #' @examples |
|
41 | +316 |
-
+ #' data <- data.frame( |
|
42 | +317 |
- #' Run `JS` file from `/inst/js/` package directory+ #' one = rep("a", length.out = 20), |
|
43 | +318 |
- #'+ #' two = rep(c("a", "b"), length.out = 20), |
|
44 | +319 |
- #' This is triggered from the server to execute on the client+ #' three = rep(c("a", "b", "c"), length.out = 20), |
|
45 | +320 |
- #' rather than triggered directly on the client.+ #' four = rep(c("a", "b", "c", "d"), length.out = 20), |
|
46 | +321 |
- #' Unlike `include_js_files` which includes `JavaScript` functions,+ #' stringsAsFactors = TRUE |
|
47 | +322 |
- #' the `run_js` actually executes `JavaScript` functions.+ #' ) |
|
48 | +323 |
- #'+ #' ui <- fluidPage( |
|
49 | +324 |
- #' `system.file` should not be used to access files in other packages, it does+ #' selectInput( |
|
50 | +325 |
- #' not work with `devtools`. Therefore, we redefine this method in each package+ #' "var", |
|
51 | +326 |
- #' as needed. Thus, we do not export this method+ #' "Select variable", |
|
52 | +327 |
- #'+ #' choices = c("one", "two", "three", "four"), |
|
53 | +328 |
- #' @param files (`character`) vector of filenames+ #' selected = "one" |
|
54 | +329 |
- #' @keywords internal+ #' ), |
|
55 | +330 |
- run_js_files <- function(files) {+ #' verbatimTextOutput("summary") |
|
56 | -16x | +||
331 | +
- checkmate::assert_character(files, min.len = 1, any.missing = FALSE)+ #' ) |
||
57 | -16x | +||
332 | +
- lapply(files, function(file) {+ #' |
||
58 | -16x | +||
333 | +
- shinyjs::runjs(paste0(readLines(system.file("js", file, package = "teal", mustWork = TRUE)), collapse = "\n"))+ #' server <- function(input, output) { |
||
59 | +334 |
- })+ #' output$summary <- renderText({ |
|
60 | -16x | +||
335 | +
- return(invisible(NULL))+ #' validate_n_levels(data[[input$var]], min_levels = 2, max_levels = 15, var_name = input$var) |
||
61 | +336 |
- }+ #' paste0( |
|
62 | +337 |
-
+ #' "Levels of selected treatment variable: ", |
|
63 | +338 |
- #' Code to include teal `CSS` and `JavaScript` files+ #' paste(levels(data[[input$var]]), |
|
64 | +339 |
- #'+ #' collapse = ", " |
|
65 | +340 |
- #' This is useful when you want to use the same `JavaScript` and `CSS` files that are+ #' ) |
|
66 | +341 |
- #' used with the teal application.+ #' ) |
|
67 | +342 |
- #' This is also useful for running standalone modules in teal with the correct+ #' }) |
|
68 | +343 |
- #' styles.+ #' } |
|
69 | +344 |
- #' Also initializes `shinyjs` so you can use it.+ #' if (interactive()) { |
|
70 | +345 |
- #'+ #' shinyApp(ui, server) |
|
71 | +346 |
- #' @return HTML code to include+ #' } |
|
72 | +347 |
- #' @examples+ validate_n_levels <- function(x, min_levels = 1, max_levels = 12, var_name) { |
|
73 | -+ | ||
348 | +! |
- #' shiny_ui <- tagList(+ x_levels <- if (is.factor(x)) {+ |
+ |
349 | +! | +
+ levels(x) |
|
74 | +350 |
- #' teal:::include_teal_css_js(),+ } else {+ |
+ |
351 | +! | +
+ unique(x) |
|
75 | +352 |
- #' p("Hello")+ } |
|
76 | +353 |
- #' )+ + |
+ |
354 | +! | +
+ if (!is.null(min_levels) && !(is.null(max_levels))) {+ |
+ |
355 | +! | +
+ validate(need(+ |
+ |
356 | +! | +
+ length(x_levels) >= min_levels && length(x_levels) <= max_levels,+ |
+ |
357 | +! | +
+ sprintf(+ |
+ |
358 | +! | +
+ "%s variable needs minimum %s level(s) and maximum %s level(s).",+ |
+ |
359 | +! | +
+ var_name, min_levels, max_levels |
|
77 | +360 |
- #' @keywords internal+ ) |
|
78 | +361 |
- include_teal_css_js <- function() {+ )) |
|
79 | -22x | +||
362 | +! |
- tagList(+ } else if (!is.null(min_levels)) { |
|
80 | -22x | +||
363 | +! |
- shinyjs::useShinyjs(),+ validate(need( |
|
81 | -22x | +||
364 | +! |
- include_css_files(),+ length(x_levels) >= min_levels,+ |
+ |
365 | +! | +
+ sprintf("%s variable needs minimum %s levels(s)", var_name, min_levels) |
|
82 | +366 |
- # init.js is executed from the server+ )) |
|
83 | -22x | +||
367 | +! |
- include_js_files(except = "init.js"),+ } else if (!is.null(max_levels)) { |
|
84 | -22x | +||
368 | +! |
- shinyjs::hidden(icon("gear")), # add hidden icon to load font-awesome css for icons+ validate(need(+ |
+ |
369 | +! | +
+ length(x_levels) <= max_levels,+ |
+ |
370 | +! | +
+ sprintf("%s variable needs maximum %s level(s)", var_name, max_levels) |
|
85 | +371 |
- )+ )) |
|
86 | +372 | ++ |
+ }+ |
+
373 |
}@@ -11729,5119 +12178,5300 @@ teal coverage - 71.59% |
1 |
- # This is the main function from teal to be used by the end-users. Although it delegates+ #' Send input validation messages to output. |
|||
2 |
- # directly to `module_teal_with_splash.R`, we keep it in a separate file because its doc is quite large+ #' |
|||
3 |
- # and it is very end-user oriented. It may also perform more argument checking with more informative+ #' Captures messages from `InputValidator` objects and collates them |
|||
4 |
- # error messages.+ #' into one message passed to `validate`. |
|||
5 |
-
+ #' |
|||
6 |
-
+ #' `shiny::validate` is used to withhold rendering of an output element until |
|||
7 |
- #' Create the Server and UI Function For the Shiny App+ #' certain conditions are met and to print a validation message in place |
|||
8 |
- #'+ #' of the output element. |
|||
9 |
- #' @description `r lifecycle::badge("stable")`+ #' `shinyvalidate::InputValidator` allows to validate input elements |
|||
10 |
- #' End-users: This is the most important function for you to start a+ #' and to display specific messages in their respective input widgets. |
|||
11 |
- #' teal app that is composed out of teal modules.+ #' `validate_inputs` provides a hybrid solution. |
|||
12 |
- #'+ #' Given an `InputValidator` object, messages corresponding to inputs that fail validation |
|||
13 |
- #' @param data (`TealData` or `TealDataset` or `TealDatasetConnector` or `list` or `data.frame`+ #' are extracted and placed in one validation message that is passed to a `validate`/`need` call. |
|||
14 |
- #' or `MultiAssayExperiment`, `teal_data`, `teal_data_module`)\cr+ #' This way the input `validator` messages are repeated in the output. |
|||
15 |
- #' `R6` object as returned by [teal.data::cdisc_data()], [teal.data::teal_data()],+ #' |
|||
16 |
- #' [teal.data::cdisc_dataset()], [teal.data::dataset()], [teal.data::dataset_connector()] or+ #' The `...` argument accepts any number of `InputValidator` objects |
|||
17 |
- #' [teal.data::cdisc_dataset_connector()] or [teal_data_module()] or a single `data.frame` or+ #' or a nested list of such objects. |
|||
18 |
- #' a `MultiAssayExperiment`+ #' If `validators` are passed directly, all their messages are printed together |
|||
19 |
- #' or a list of the previous objects or function returning a named list.+ #' under one (optional) header message specified by `header`. If a list is passed, |
|||
20 |
- #' NOTE: teal does not guarantee reproducibility of the code when names of the list elements+ #' messages are grouped by `validator`. The list's names are used as headers |
|||
21 |
- #' do not match the original object names. To ensure reproducibility please use [teal.data::teal_data()]+ #' for their respective message groups. |
|||
22 |
- #' or [teal.data::cdisc_data()] with `check = TRUE` enabled.+ #' If neither of the nested list elements is named, a header message is taken from `header`. |
|||
23 |
- #' @param modules (`list`, `teal_modules` or `teal_module`)\cr+ #' |
|||
24 |
- #' nested list of `teal_modules` or `teal_module` objects or a single+ #' @param ... either any number of `InputValidator` objects |
|||
25 |
- #' `teal_modules` or `teal_module` object. These are the specific output modules which+ #' or an optionally named, possibly nested `list` of `InputValidator` |
|||
26 |
- #' will be displayed in the teal application. See [modules()] and [module()] for+ #' objects, see `Details` |
|||
27 |
- #' more details.+ #' @param header `character(1)` generic validation message; set to NULL to omit |
|||
28 |
- #' @param title (`NULL` or `character`)\cr+ #' |
|||
29 |
- #' The browser window title (defaults to the host URL of the page).+ #' @return |
|||
30 |
- #' @param filter (`teal_slices`)\cr+ #' Returns NULL if the final validation call passes and a `shiny.silent.error` if it fails. |
|||
31 |
- #' Specification of initial filter. Filters can be specified using [teal::teal_slices()].+ #' |
|||
32 |
- #' Old way of specifying filters through a list is deprecated and will be removed in the+ #' @seealso [`shinyvalidate::InputValidator`], [`shiny::validate`] |
|||
33 |
- #' next release. Please fix your applications to use [teal::teal_slices()].+ #' |
|||
34 |
- #' @param header (`shiny.tag` or `character`) \cr+ #' @examples |
|||
35 |
- #' the header of the app. Note shiny code placed here (and in the footer+ #' library(shiny) |
|||
36 |
- #' argument) will be placed in the app's `ui` function so code which needs to be placed in the `ui` function+ #' library(shinyvalidate) |
|||
37 |
- #' (such as loading `CSS` via [htmltools::htmlDependency()]) should be included here.+ #' |
|||
38 |
- #' @param footer (`shiny.tag` or `character`)\cr+ #' ui <- fluidPage( |
|||
39 |
- #' the footer of the app+ #' selectInput("method", "validation method", c("sequential", "combined", "grouped")), |
|||
40 |
- #' @param id (`character`)\cr+ #' sidebarLayout( |
|||
41 |
- #' module id to embed it, if provided,+ #' sidebarPanel( |
|||
42 |
- #' the server function must be called with [shiny::moduleServer()];+ #' selectInput("letter", "select a letter:", c(letters[1:3], LETTERS[4:6])), |
|||
43 |
- #' See the vignette for an example. However, [ui_teal_with_splash()]+ #' selectInput("number", "select a number:", 1:6), |
|||
44 |
- #' is then preferred to this function.+ #' br(), |
|||
45 |
- #'+ #' selectInput("color", "select a color:", |
|||
46 |
- #' @return named list with `server` and `ui` function+ #' c("black", "indianred2", "springgreen2", "cornflowerblue"), |
|||
47 |
- #'+ #' multiple = TRUE |
|||
48 |
- #' @export+ #' ), |
|||
49 |
- #'+ #' sliderInput("size", "select point size:", |
|||
50 |
- #' @include modules.R+ #' min = 0.1, max = 4, value = 0.25 |
|||
51 |
- #'+ #' ) |
|||
52 |
- #' @examples+ #' ), |
|||
53 |
- #' new_iris <- transform(iris, id = seq_len(nrow(iris)))+ #' mainPanel(plotOutput("plot")) |
|||
54 |
- #' new_mtcars <- transform(mtcars, id = seq_len(nrow(mtcars)))+ #' ) |
|||
55 |
- #'+ #' ) |
|||
56 |
- #' app <- init(+ #' |
|||
57 |
- #' data = teal_data(+ #' server <- function(input, output) { |
|||
58 |
- #' dataset("new_iris", new_iris),+ #' # set up input validation |
|||
59 |
- #' dataset("new_mtcars", new_mtcars),+ #' iv <- InputValidator$new() |
|||
60 |
- #' code = "+ #' iv$add_rule("letter", sv_in_set(LETTERS, "choose a capital letter")) |
|||
61 |
- #' new_iris <- transform(iris, id = seq_len(nrow(iris)))+ #' iv$add_rule("number", ~ if (as.integer(.) %% 2L == 1L) "choose an even number") |
|||
62 |
- #' new_mtcars <- transform(mtcars, id = seq_len(nrow(mtcars)))+ #' iv$enable() |
|||
63 |
- #' "+ #' # more input validation |
|||
64 |
- #' ),+ #' iv_par <- InputValidator$new() |
|||
65 |
- #' modules = modules(+ #' iv_par$add_rule("color", sv_required(message = "choose a color")) |
|||
66 |
- #' module(+ #' iv_par$add_rule("color", ~ if (length(.) > 1L) "choose only one color") |
|||
67 |
- #' label = "data source",+ #' iv_par$add_rule( |
|||
68 |
- #' server = function(input, output, session, data) {},+ #' "size", |
|||
69 |
- #' ui = function(id, ...) div(p("information about data source")),+ #' sv_between( |
|||
70 |
- #' datanames = "all"+ #' left = 0.5, right = 3, |
|||
71 |
- #' ),+ #' message_fmt = "choose a value between {left} and {right}" |
|||
72 |
- #' example_module(label = "example teal module"),+ #' ) |
|||
73 |
- #' module(+ #' ) |
|||
74 |
- #' "Iris Sepal.Length histogram",+ #' iv_par$enable() |
|||
75 |
- #' server = function(input, output, session, data) {+ #' |
|||
76 |
- #' output$hist <- renderPlot(+ #' output$plot <- renderPlot({ |
|||
77 |
- #' hist(data[["new_iris"]]()$Sepal.Length)+ #' # validate output |
|||
78 |
- #' )+ #' switch(input[["method"]], |
|||
79 |
- #' },+ #' "sequential" = { |
|||
80 |
- #' ui = function(id, ...) {+ #' validate_inputs(iv) |
|||
81 |
- #' ns <- NS(id)+ #' validate_inputs(iv_par, header = "Set proper graphical parameters") |
|||
82 |
- #' plotOutput(ns("hist"))+ #' }, |
|||
83 |
- #' },+ #' "combined" = validate_inputs(iv, iv_par), |
|||
84 |
- #' datanames = "new_iris"+ #' "grouped" = validate_inputs(list( |
|||
85 |
- #' )+ #' "Some inputs require attention" = iv, |
|||
86 |
- #' ),+ #' "Set proper graphical parameters" = iv_par |
|||
87 |
- #' title = "App title",+ #' )) |
|||
88 |
- #' filter = teal_slices(+ #' ) |
|||
89 |
- #' teal_slice(dataname = "new_iris", varname = "Species"),+ #' |
|||
90 |
- #' teal_slice(dataname = "new_iris", varname = "Sepal.Length"),+ #' plot(eruptions ~ waiting, faithful, |
|||
91 |
- #' teal_slice(dataname = "new_mtcars", varname = "cyl"),+ #' las = 1, pch = 16, |
|||
92 |
- #' exclude_varnames = list(new_iris = c("Sepal.Width", "Petal.Width")),+ #' col = input[["color"]], cex = input[["size"]] |
|||
93 |
- #' mapping = list(+ #' ) |
|||
94 |
- #' `example teal module` = "new_iris Species",+ #' }) |
|||
95 |
- #' `Iris Sepal.Length histogram` = "new_iris Species",+ #' } |
|||
96 |
- #' global_filters = "new_mtcars cyl"+ #' |
|||
97 |
- #' )+ #' if (interactive()) { |
|||
98 |
- #' ),+ #' shinyApp(ui, server) |
|||
99 |
- #' header = tags$h1("Sample App"),+ #' } |
|||
100 |
- #' footer = tags$p("Copyright 2017 - 2023")+ #' |
|||
101 |
- #' )+ #' @export |
|||
102 |
- #' if (interactive()) {+ #' |
|||
103 |
- #' shinyApp(app$ui, app$server)+ validate_inputs <- function(..., header = "Some inputs require attention") { |
|||
104 | -+ | 36x |
- #' }+ dots <- list(...) |
|
105 | -+ | 2x |
- #'+ if (!is_validators(dots)) stop("validate_inputs accepts validators or a list thereof") |
|
106 |
- init <- function(data,+ |
|||
107 | -+ | 34x |
- modules,+ messages <- extract_validator(dots, header) |
|
108 | -+ | 34x |
- title = NULL,+ failings <- if (!any_names(dots)) { |
|
109 | -+ | 29x |
- filter = teal_slices(),+ add_header(messages, header) |
|
110 |
- header = tags$p(),+ } else { |
|||
111 | -+ | 5x |
- footer = tags$p(),+ unlist(messages) |
|
112 |
- id = character(0)) {+ } |
|||
113 | -30x | +
- logger::log_trace("init initializing teal app with: data ({ class(data)[1] }).")+ |
||
114 | -30x | +34x |
- if (!inherits(data, c("TealData", "teal_data", "teal_data_module"))) {+ shiny::validate(shiny::need(is.null(failings), failings)) |
|
115 | -24x | +
- data <- teal.data::to_relational_data(data = data)+ } |
||
116 |
- }+ |
|||
117 |
-
+ ### internal functions |
|||
118 | -25x | +
- checkmate::assert_multi_class(data, c("TealData", "teal_data", "teal_data_module"))+ |
||
119 | -25x | +
- checkmate::assert_multi_class(modules, c("teal_module", "list", "teal_modules"))+ #' @keywords internal |
||
120 | -25x | +
- checkmate::assert_string(title, null.ok = TRUE)+ # recursive object type test |
||
121 | -25x | +
- checkmate::assert(+ # returns logical of length 1 |
||
122 | -25x | +
- checkmate::check_class(filter, "teal_slices"),+ is_validators <- function(x) { |
||
123 | -25x | +118x |
- checkmate::check_list(filter, names = "named")+ all(if (is.list(x)) unlist(lapply(x, is_validators)) else inherits(x, "InputValidator")) |
|
124 |
- )+ } |
|||
125 | -24x | +
- checkmate::assert_multi_class(header, c("shiny.tag", "character"))+ |
||
126 | -24x | +
- checkmate::assert_multi_class(footer, c("shiny.tag", "character"))+ #' @keywords internal |
||
127 | -24x | +
- checkmate::assert_character(id, max.len = 1, any.missing = FALSE)+ # test if an InputValidator object is enabled |
||
128 |
-
+ # returns logical of length 1 |
|||
129 | -24x | +
- teal.logger::log_system_info()+ # official method requested at https://github.com/rstudio/shinyvalidate/issues/64 |
||
130 |
-
+ validator_enabled <- function(x) { |
|||
131 | -24x | +49x |
- if (inherits(modules, "teal_module")) {+ x$.__enclos_env__$private$enabled |
|
132 | -1x | +
- modules <- list(modules)+ } |
||
133 |
- }+ |
|||
134 | -24x | +
- if (inherits(modules, "list")) {+ #' @keywords internal |
||
135 | -4x | +
- modules <- do.call(teal::modules, modules)+ # recursively extract messages from validator list |
||
136 |
- }+ # returns character vector or a list of character vectors, possibly nested and named |
|||
137 |
-
+ extract_validator <- function(iv, header) { |
|||
138 | -24x | +113x |
- landing <- extract_module(modules, "teal_module_landing")+ if (inherits(iv, "InputValidator")) { |
|
139 | -! | +49x |
- if (length(landing) > 1L) stop("Only one `landing_popup_module` can be used.")+ add_header(gather_messages(iv), header) |
|
140 | -24x | +
- modules <- drop_module(modules, "teal_module_landing")+ } else { |
||
141 | -+ | 58x |
-
+ if (is.null(names(iv))) names(iv) <- rep("", length(iv)) |
|
142 | -+ | 64x |
- # Calculate app id that will be used to stamp filter state snapshots.+ mapply(extract_validator, iv = iv, header = names(iv), SIMPLIFY = FALSE) |
|
143 |
- # App id is a hash of the app's data and modules.+ } |
|||
144 |
- # See "transferring snapshots" section in ?snapshot.+ } |
|||
145 | -24x | +
- hashables <- mget(c("data", "modules"))+ |
||
146 | -24x | +
- hashables$data <- if (inherits(hashables$data, "teal_data")) {+ #' @keywords internal |
||
147 | -4x | +
- as.list(hashables$data@env)+ # collate failing messages from validator |
||
148 | -24x | +
- } else if (inherits(data, "teal_data_module")) {+ # returns list |
||
149 | -1x | +
- body(data$server)+ gather_messages <- function(iv) { |
||
150 | -24x | +49x |
- } else if (hashables$data$is_pulled()) {+ if (validator_enabled(iv)) { |
|
151 | -17x | +46x |
- sapply(get_dataname(hashables$data), simplify = FALSE, function(dn) {+ status <- iv$validate() |
|
152 | -23x | +46x |
- hashables$data$get_dataset(dn)$get_raw_data()+ failing_inputs <- Filter(Negate(is.null), status) |
|
153 | +46x | +
+ unique(lapply(failing_inputs, function(x) x[["message"]]))+ |
+ ||
154 |
- })+ } else {+ |
+ |||
155 | +3x | +
+ logger::log_warn("Validator is disabled and will be omitted.")+ |
+ ||
156 | +3x | +
+ list()+ |
+ ||
157 | ++ |
+ }+ |
+ ||
158 | ++ |
+ }+ |
+ ||
159 | ++ | + + | +||
160 | ++ |
+ #' @keywords internal+ |
+ ||
161 | ++ |
+ # add optional header to failing messages+ |
+ ||
162 | ++ |
+ add_header <- function(messages, header = "") {+ |
+ ||
163 | +78x | +
+ ans <- unlist(messages)+ |
+ ||
164 | +78x | +
+ if (length(ans) != 0L && isTRUE(nchar(header) > 0L)) {+ |
+ ||
165 | +31x | +
+ ans <- c(paste0(header, "\n"), ans, "\n")+ |
+ ||
166 | ++ |
+ }+ |
+ ||
167 | +78x | +
+ ans+ |
+ ||
168 | ++ |
+ } |
||
154 | +169 |
- } else {+ |
||
155 | -2x | +|||
170 | +
- hashables$data$get_code()+ #' @keywords internal |
|||
156 | +171 |
- }+ # recursively check if the object contains a named list |
||
157 | +172 |
-
+ any_names <- function(x) { |
||
158 | -24x | +173 | +103x |
- attr(filter, "app_id") <- rlang::hash(hashables)+ any( |
159 | -+ | |||
174 | +103x |
-
+ if (is.list(x)) {+ |
+ ||
175 | +58x | +
+ if (!is.null(names(x)) && any(names(x) != "")) TRUE else unlist(lapply(x, any_names)) |
||
160 | +176 |
- # convert teal.slice::teal_slices to teal::teal_slices+ } else { |
||
161 | -24x | +177 | +40x |
- filter <- as.teal_slices(as.list(filter))+ FALSE |
162 | +178 |
-
+ } |
||
163 | -24x | +|||
179 | +
- if (isTRUE(attr(filter, "module_specific"))) {+ ) |
|||
164 | -! | +|||
180 | +
- module_names <- unlist(c(module_labels(modules), "global_filters"))+ } |
|||
165 | -! | +
1 | +
- failed_mod_names <- setdiff(names(attr(filter, "mapping")), module_names)+ #' @title `TealReportCard` |
|||
166 | -! | +|||
2 | +
- if (length(failed_mod_names)) {+ #' @description `r lifecycle::badge("experimental")` |
|||
167 | -! | +|||
3 | +
- stop(+ #' A child of [`ReportCard`] that is used for teal specific applications. |
|||
168 | -! | +|||
4 | +
- sprintf(+ #' In addition to the parent methods, it supports rendering teal specific elements such as |
|||
169 | -! | +|||
5 | +
- "Some module names in the mapping arguments don't match module labels.\n %s not in %s",+ #' the source code, the encodings panel content and the filter panel content as part of the |
|||
170 | -! | +|||
6 | +
- toString(failed_mod_names),+ #' meta data. |
|||
171 | -! | +|||
7 | +
- toString(unique(module_names))+ #' @export |
|||
172 | +8 |
- )+ #' |
||
173 | +9 |
- )+ TealReportCard <- R6::R6Class( # nolint: object_name_linter. |
||
174 | +10 |
- }+ classname = "TealReportCard", |
||
175 | +11 |
-
+ inherit = teal.reporter::ReportCard, |
||
176 | -! | +|||
12 | +
- if (anyDuplicated(module_names)) {+ public = list( |
|||
177 | +13 |
- # In teal we are able to set nested modules with duplicated label.+ #' @description Appends the source code to the `content` meta data of this `TealReportCard`. |
||
178 | +14 |
- # Because mapping argument bases on the relationship between module-label and filter-id,+ #' |
||
179 | +15 |
- # it is possible that module-label in mapping might refer to multiple teal_module (identified by the same label)+ #' @param src (`character(1)`) code as text. |
||
180 | -! | +|||
16 | +
- stop(+ #' @param ... any `rmarkdown` R chunk parameter and its value. |
|||
181 | -! | +|||
17 | +
- sprintf(+ #' But `eval` parameter is always set to `FALSE`. |
|||
182 | -! | +|||
18 | +
- "Module labels should be unique when teal_slices(mapping = TRUE). Duplicated labels:\n%s ",+ #' @return invisibly self |
|||
183 | -! | +|||
19 | +
- toString(module_names[duplicated(module_names)])+ #' @examples |
|||
184 | +20 |
- )+ #' card <- TealReportCard$new()$append_src( |
||
185 | +21 |
- )+ #' "plot(iris)" |
||
186 | +22 |
- }+ #' ) |
||
187 | +23 |
- }+ #' card$get_content()[[1]]$get_content() |
||
188 | +24 |
-
+ append_src = function(src, ...) {+ |
+ ||
25 | +4x | +
+ checkmate::assert_character(src, min.len = 0, max.len = 1)+ |
+ ||
26 | +4x | +
+ params <- list(...)+ |
+ ||
27 | +4x | +
+ params$eval <- FALSE+ |
+ ||
28 | +4x | +
+ rblock <- RcodeBlock$new(src)+ |
+ ||
29 | +4x | +
+ rblock$set_params(params) |
||
189 | -24x | +30 | +4x |
- if (inherits(data, "teal_data")) {+ self$append_content(rblock) |
190 | +31 | 4x |
- if (length(teal.data::datanames(data)) == 0) {+ self$append_metadata("SRC", src) |
|
191 | -1x | +32 | +4x |
- stop("`data` object has no datanames. Specify `datanames(data)` and try again.")+ invisible(self) |
192 | +33 |
- }+ }, |
||
193 | +34 |
-
+ #' @description Appends the filter state list to the `content` and `metadata` of this `TealReportCard`. |
||
194 | +35 |
- # in case of teal_data_module this check is postponed to the srv_teal_with_splash+ #' If the filter state list has an attribute named `formatted`, it appends it to the card otherwise it uses |
||
195 | -3x | +|||
36 | +
- is_modules_ok <- check_modules_datanames(modules, teal.data::datanames(data))+ #' the default `yaml::as.yaml` to format the list. |
|||
196 | -3x | +|||
37 | +
- if (!isTRUE(is_modules_ok)) {+ #' If the filter state list is empty, nothing is appended to the `content`. |
|||
197 | -1x | +|||
38 | +
- logger::log_error(is_modules_ok)+ #' |
|||
198 | -1x | +|||
39 | +
- checkmate::assert(is_modules_ok, .var.name = "modules")+ #' @param fs (`teal_slices`) object returned from [teal_slices()] function. |
|||
199 | +40 |
- }+ #' @return invisibly self |
||
200 | +41 |
-
+ append_fs = function(fs) { |
||
201 | -+ | |||
42 | +5x |
-
+ checkmate::assert_class(fs, "teal_slices") |
||
202 | -2x | +43 | +4x |
- is_filter_ok <- check_filter_datanames(filter, teal.data::datanames(data))+ self$append_text("Filter State", "header3") |
203 | -2x | +44 | +4x |
- if (!isTRUE(is_filter_ok)) {+ self$append_content(TealSlicesBlock$new(fs)) |
204 | -1x | +45 | +4x |
- logger::log_warn(is_filter_ok)+ invisible(self) |
205 | +46 |
- # we allow app to continue if applied filters are outside+ }, |
||
206 | +47 |
- # of possible data range+ #' @description Appends the encodings list to the `content` and `metadata` of this `TealReportCard`. |
||
207 | +48 |
- }+ #' |
||
208 | +49 |
- }+ #' @param encodings (`list`) list of encodings selections of the teal app. |
||
209 | +50 |
-
+ #' @return invisibly self |
||
210 | +51 |
- # Note regarding case `id = character(0)`:+ #' @examples |
||
211 | +52 |
- # rather than using `callModule` and creating a submodule of this module, we directly modify+ #' card <- TealReportCard$new()$append_encodings(list(variable1 = "X")) |
||
212 | +53 |
- # the `ui` and `server` with `id = character(0)` and calling the server function directly+ #' card$get_content()[[1]]$get_content() |
||
213 | +54 |
- # rather than through `callModule`+ #'+ |
+ ||
55 | ++ |
+ append_encodings = function(encodings) { |
||
214 | -22x | +56 | +4x |
- res <- list(+ checkmate::assert_list(encodings) |
215 | -22x | +57 | +4x |
- ui = ui_teal_with_splash(id = id, data = data, title = title, header = header, footer = footer),+ self$append_text("Selected Options", "header3") |
216 | -22x | +58 | +4x |
- server = function(input, output, session) {+ if (requireNamespace("yaml", quietly = TRUE)) { |
217 | -! | +|||
59 | +4x |
- if (length(landing) == 1L) {+ self$append_text(yaml::as.yaml(encodings, handlers = list( |
||
218 | -! | +|||
60 | +4x |
- landing_module <- landing[[1L]]+ POSIXct = function(x) format(x, "%Y-%m-%d"), |
||
219 | -! | +|||
61 | +4x |
- do.call(landing_module$server, c(list(id = "landing_module_shiny_id"), landing_module$server_args))+ POSIXlt = function(x) format(x, "%Y-%m-%d"), |
||
220 | -+ | |||
62 | +4x |
- }+ Date = function(x) format(x, "%Y-%m-%d") |
||
221 | -! | +|||
63 | +4x |
- if (inherits(data, "TealDataAbstract")) {+ )), "verbatim") |
||
222 | +64 |
- # copy TealData so that load won't be shared between the session+ } else { |
||
223 | +65 | ! |
- data <- data$copy(deep = TRUE)+ stop("yaml package is required to format the encodings list") |
|
224 | +66 |
} |
||
225 | -! | +|||
67 | +4x |
- filter <- deep_copy_filter(filter)+ self$append_metadata("Encodings", encodings) |
||
226 | -! | +|||
68 | +4x |
- srv_teal_with_splash(id = id, data = data, modules = modules, filter = filter)+ invisible(self) |
||
227 | +69 |
} |
||
228 | -- |
- )- |
- ||
229 | -22x | -
- logger::log_trace("init teal app has been initialized.")- |
- ||
230 | -22x | -
- return(res)- |
- ||
231 | +70 |
- }+ ), |
1 | +71 |
- #' Create a UI of nested tabs of `teal_modules`+ private = list() |
||
2 | +72 |
- #'+ ) |
||
3 | +73 |
- #' @section `ui_nested_tabs`:+ |
||
4 | +74 |
- #' Each `teal_modules` is translated to a `tabsetPanel` and each+ #' @title `RcodeBlock` |
||
5 | +75 |
- #' of its children is another tab-module called recursively. The UI of a+ #' @keywords internal |
||
6 | +76 |
- #' `teal_module` is obtained by calling the `ui` function on it.+ TealSlicesBlock <- R6::R6Class( # nolint: object_name_linter. |
||
7 | +77 |
- #'+ classname = "TealSlicesBlock", |
||
8 | +78 |
- #' The `datasets` argument is required to resolve the teal arguments in an+ inherit = teal.reporter:::TextBlock, |
||
9 | +79 |
- #' isolated context (with respect to reactivity)+ public = list( |
||
10 | +80 |
- #'+ #' @description Returns a `TealSlicesBlock` object. |
||
11 | +81 |
- #' @section `srv_nested_tabs`:+ #' |
||
12 | +82 |
- #' This module calls recursively all elements of the `modules` returns one which+ #' @details Returns a `TealSlicesBlock` object with no content and no parameters. |
||
13 | +83 |
- #' is currently active.+ #' |
||
14 | +84 |
- #' - `teal_module` returns self as a active module.+ #' @param content (`teal_slices`) object returned from [teal_slices()] function. |
||
15 | +85 |
- #' - `teal_modules` also returns module active within self which is determined by the `input$active_tab`.+ #' @param style (`character(1)`) string specifying style to apply. |
||
16 | +86 |
- #'+ #' |
||
17 | +87 |
- #' @name module_nested_tabs+ #' @return `TealSlicesBlock` |
||
18 | +88 |
- #'+ #' @examples |
||
19 | +89 |
- #' @inheritParams module_tabs_with_filters+ #' block <- teal:::TealSlicesBlock$new() |
||
20 | +90 |
- #'+ #' |
||
21 | +91 |
- #' @param depth (`integer(1)`)\cr+ initialize = function(content = teal_slices(), style = "verbatim") { |
||
22 | -+ | |||
92 | +10x |
- #' number which helps to determine depth of the modules nesting.+ self$set_content(content) |
||
23 | -+ | |||
93 | +9x |
- #' @param is_module_specific (`logical(1)`)\cr+ self$set_style(style) |
||
24 | -+ | |||
94 | +9x |
- #' flag determining if the filter panel is global or module-specific.+ invisible(self) |
||
25 | +95 |
- #' When set to `TRUE`, a filter panel is called inside of each module tab.+ }, |
||
26 | +96 |
- #' @return depending on class of `modules`, `ui_nested_tabs` returns:+ |
||
27 | +97 |
- #' - `teal_module`: instantiated UI of the module+ #' @description Sets content of this `TealSlicesBlock`. |
||
28 | +98 |
- #' - `teal_modules`: `tabsetPanel` with each tab corresponding to recursively+ #' Sets content as `YAML` text which represents a list generated from `teal_slices`. |
||
29 | +99 |
- #' calling this function on it.\cr+ #' The list displays limited number of fields from `teal_slice` objects, but this list is |
||
30 | +100 |
- #' `srv_nested_tabs` returns a reactive which returns the active module that corresponds to the selected tab.+ #' sufficient to conclude which filters were applied. |
||
31 | +101 |
- #'+ #' When `selected` field in `teal_slice` object is a range, then it is displayed as a "min" |
||
32 | +102 |
- #' @examples+ #' |
||
33 | +103 |
- #' mods <- teal:::example_modules()+ #' |
||
34 | +104 |
- #' datasets <- teal:::example_datasets()+ #' @param content (`teal_slices`) object returned from [teal_slices()] function. |
||
35 | +105 |
- #' app <- shinyApp(+ #' @return invisibly self |
||
36 | +106 |
- #' ui = function() {+ set_content = function(content) { |
||
37 | -+ | |||
107 | +11x |
- #' tagList(+ checkmate::assert_class(content, "teal_slices") |
||
38 | -+ | |||
108 | +10x |
- #' teal:::include_teal_css_js(),+ if (length(content) != 0) { |
||
39 | -+ | |||
109 | +7x |
- #' textOutput("info"),+ states_list <- lapply(content, function(x) { |
||
40 | -+ | |||
110 | +7x |
- #' fluidPage( # needed for nice tabs+ x_list <- shiny::isolate(as.list(x)) |
||
41 | -+ | |||
111 | +7x |
- #' teal:::ui_nested_tabs("dummy", modules = mods, datasets = datasets)+ if ( |
||
42 | -+ | |||
112 | +7x |
- #' )+ inherits(x_list$choices, c("integer", "numeric", "Date", "POSIXct", "POSIXlt")) && |
||
43 | -+ | |||
113 | +7x |
- #' )+ length(x_list$choices) == 2 && |
||
44 | -+ | |||
114 | +7x |
- #' },+ length(x_list$selected) == 2 |
||
45 | +115 |
- #' server = function(input, output, session) {+ ) { |
||
46 | -+ | |||
116 | +! |
- #' active_module <- teal:::srv_nested_tabs(+ x_list$range <- paste(x_list$selected, collapse = " - ") |
||
47 | -+ | |||
117 | +! |
- #' "dummy",+ x_list["selected"] <- NULL |
||
48 | +118 |
- #' datasets = datasets,+ } |
||
49 | -+ | |||
119 | +7x |
- #' modules = mods+ if (!is.null(x_list$arg)) { |
||
50 | -+ | |||
120 | +! |
- #' )+ x_list$arg <- if (x_list$arg == "subset") "Genes" else "Samples" |
||
51 | +121 |
- #' output$info <- renderText({+ } |
||
52 | +122 |
- #' paste0("The currently active tab name is ", active_module()$label)+ |
||
53 | -+ | |||
123 | +7x |
- #' })+ x_list <- x_list[ |
||
54 | -+ | |||
124 | +7x |
- #' }+ c("dataname", "varname", "experiment", "arg", "expr", "selected", "range", "keep_na", "keep_inf") |
||
55 | +125 |
- #' )+ ] |
||
56 | -+ | |||
126 | +7x |
- #' if (interactive()) {+ names(x_list) <- c( |
||
57 | -+ | |||
127 | +7x |
- #' shinyApp(app$ui, app$server)+ "Dataset name", "Variable name", "Experiment", "Filtering by", "Applied expression", |
||
58 | -+ | |||
128 | +7x |
- #' }+ "Selected Values", "Selected range", "Include NA values", "Include Inf values" |
||
59 | +129 |
- #' @keywords internal+ ) |
||
60 | +130 |
- NULL+ |
||
61 | -+ | |||
131 | +7x |
-
+ Filter(Negate(is.null), x_list) |
||
62 | +132 |
- #' @rdname module_nested_tabs+ }) |
||
63 | +133 |
- ui_nested_tabs <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) {+ |
||
64 | -2x | +134 | +7x |
- checkmate::assert_multi_class(modules, c("teal_modules", "teal_module"))+ if (requireNamespace("yaml", quietly = TRUE)) { |
65 | -2x | +135 | +7x |
- checkmate::assert_count(depth)+ super$set_content(yaml::as.yaml(states_list)) |
66 | -2x | +|||
136 | +
- UseMethod("ui_nested_tabs", modules)+ } else { |
|||
67 | -+ | |||
137 | +! |
- }+ stop("yaml package is required to format the filter state list") |
||
68 | +138 |
-
+ } |
||
69 | +139 |
- #' @rdname module_nested_tabs+ } |
||
70 | -+ | |||
140 | +10x |
- #' @export+ private$teal_slices <- content |
||
71 | -+ | |||
141 | +10x |
- ui_nested_tabs.default <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) {+ invisible(self) |
||
72 | -! | +|||
142 | +
- stop("Modules class not supported: ", paste(class(modules), collapse = " "))+ }, |
|||
73 | +143 |
- }+ #' @description Create the `RcodeBlock` from a list. |
||
74 | +144 |
-
+ #' @param x `named list` with two fields `c("text", "params")`. |
||
75 | +145 |
- #' @rdname module_nested_tabs+ #' Use the `get_available_params` method to get all possible parameters. |
||
76 | +146 |
- #' @export+ #' @return invisibly self |
||
77 | +147 |
- ui_nested_tabs.teal_modules <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) {+ from_list = function(x) { |
||
78 | +148 | 1x |
- checkmate::assert_list(datasets, types = c("list", "FilteredData"))+ checkmate::assert_list(x) |
|
79 | +149 | 1x |
- ns <- NS(id)+ checkmate::assert_names(names(x), must.include = c("teal_slices")) |
|
80 | +150 | 1x |
- do.call(+ self$set_content(x$teal_slices) |
|
81 | +151 | 1x |
- tabsetPanel,+ invisible(self) |
|
82 | -1x | +|||
152 | +
- c(+ }, |
|||
83 | +153 |
- # by giving an id, we can reactively respond to tab changes+ #' @description Convert the `RcodeBlock` to a list. |
||
84 | -1x | +|||
154 | +
- list(+ #' @return `named list` with a text and `params`. |
|||
85 | -1x | +|||
155 | +
- id = ns("active_tab"),+ + |
+ |||
156 | ++ |
+ to_list = function() { |
||
86 | -1x | +157 | +2x |
- type = if (modules$label == "root") "pills" else "tabs"+ list(teal_slices = private$teal_slices) |
87 | +158 |
- ),+ } |
||
88 | -1x | +|||
159 | +
- lapply(+ ), |
|||
89 | -1x | +|||
160 | +
- names(modules$children),+ private = list( |
|||
90 | -1x | +|||
161 | +
- function(module_id) {+ style = "verbatim", |
|||
91 | -1x | +|||
162 | +
- module_label <- modules$children[[module_id]]$label+ teal_slices = NULL # teal_slices |
|||
92 | -1x | +|||
163 | +
- tabPanel(+ ) |
|||
93 | -1x | +|||
164 | +
- title = module_label,+ ) |
|||
94 | -1x | +
1 | +
- value = module_id, # when clicked this tab value changes input$<tabset panel id>+ #' Create a `tdata` Object |
|||
95 | -1x | +|||
2 | +
- ui_nested_tabs(+ #' |
|||
96 | -1x | +|||
3 | +
- id = ns(module_id),+ #' Create a new object called `tdata` which contains `data`, a `reactive` list of data.frames |
|||
97 | -1x | +|||
4 | +
- modules = modules$children[[module_id]],+ #' (or `MultiAssayExperiment`), with attributes: |
|||
98 | -1x | +|||
5 | +
- datasets = datasets[[module_label]],+ #' \itemize{ |
|||
99 | -1x | +|||
6 | +
- depth = depth + 1L,+ #' \item{`code` (`reactive`) containing code used to generate the data} |
|||
100 | -1x | +|||
7 | +
- is_module_specific = is_module_specific+ #' \item{join_keys (`join_keys`) containing the relationships between the data} |
|||
101 | +8 |
- )+ #' \item{metadata (`named list`) containing any metadata associated with the data frames} |
||
102 | +9 |
- )+ #' } |
||
103 | +10 |
- }+ #' @name tdata |
||
104 | +11 |
- )+ #' @param data A `named list` of `data.frames` (or `MultiAssayExperiment`) |
||
105 | +12 |
- )+ #' which optionally can be `reactive`. |
||
106 | +13 |
- )+ #' Inside this object all of these items will be made `reactive`. |
||
107 | +14 |
- }+ #' @param code A `character` (or `reactive` which evaluates to a `character`) containing |
||
108 | +15 |
-
+ #' the code used to generate the data. This should be `reactive` if the code is changing |
||
109 | +16 |
- #' @rdname module_nested_tabs+ #' during a reactive context (e.g. if filtering changes the code). Inside this |
||
110 | +17 |
- #' @export+ #' object `code` will be made reactive |
||
111 | +18 |
- ui_nested_tabs.teal_module <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) {+ #' @param join_keys A `teal.data::join_keys` object containing relationships between the |
||
112 | -1x | +|||
19 | +
- checkmate::assert_class(datasets, classes = "FilteredData")+ #' datasets. |
|||
113 | -1x | +|||
20 | +
- ns <- NS(id)+ #' @param metadata A `named list` each element contains a list of metadata about the named data.frame |
|||
114 | +21 |
-
+ #' Each element of these list should be atomic and length one. |
||
115 | -1x | +|||
22 | +
- args <- isolate(teal.transform::resolve_delayed(modules$ui_args, datasets))+ #' @return A `tdata` object |
|||
116 | -1x | +|||
23 | +
- args <- c(list(id = ns("module")), args)+ #' @examples |
|||
117 | +24 |
-
+ #' |
||
118 | -1x | +|||
25 | +
- if (is_arg_used(modules$ui, "datasets")) {+ #' data <- new_tdata( |
|||
119 | -! | +|||
26 | +
- args <- c(args, datasets = datasets)+ #' data = list(iris = iris, mtcars = reactive(mtcars), dd = data.frame(x = 1:10)), |
|||
120 | +27 |
- }+ #' code = "iris <- iris |
||
121 | +28 |
-
+ #' mtcars <- mtcars |
||
122 | -1x | +|||
29 | +
- if (is_arg_used(modules$ui, "data")) {+ #' dd <- data.frame(x = 1:10)", |
|||
123 | -1x | +|||
30 | +
- data <- .datasets_to_data(modules, datasets)+ #' metadata = list(dd = list(author = "NEST"), iris = list(version = 1)) |
|||
124 | -1x | +|||
31 | +
- args <- c(args, data = list(data))+ #' ) |
|||
125 | +32 |
- }+ #' |
||
126 | +33 |
-
+ #' # Extract a data.frame |
||
127 | -1x | +|||
34 | +
- teal_ui <- tags$div(+ #' isolate(data[["iris"]]()) |
|||
128 | -1x | +|||
35 | +
- id = id,+ #' |
|||
129 | -1x | +|||
36 | +
- class = "teal_module",+ #' # Get code |
|||
130 | -1x | +|||
37 | +
- uiOutput(ns("data_reactive"), inline = TRUE),+ #' isolate(get_code(data)) |
|||
131 | -1x | +|||
38 | +
- tagList(+ #' |
|||
132 | -1x | +|||
39 | +
- if (depth >= 2L) div(style = "mt-6"),+ #' # Get metadata |
|||
133 | -1x | +|||
40 | +
- do.call(modules$ui, args)+ #' get_metadata(data, "iris") |
|||
134 | +41 |
- )+ #' |
||
135 | +42 |
- )+ #' @export |
||
136 | +43 |
-
+ new_tdata <- function(data, code = "", join_keys = NULL, metadata = NULL) { |
||
137 | -1x | +44 | +42x |
- if (!is.null(modules$datanames) && is_module_specific) {+ checkmate::assert_list( |
138 | -! | +|||
45 | +42x |
- fluidRow(+ data, |
||
139 | -! | +|||
46 | +42x |
- column(width = 9, teal_ui, class = "teal_primary_col"),+ any.missing = FALSE, names = "unique", |
||
140 | -! | +|||
47 | +42x |
- column(+ types = c("data.frame", "reactive", "MultiAssayExperiment") |
||
141 | -! | +|||
48 | +
- width = 3,+ ) |
|||
142 | -! | +|||
49 | +38x |
- datasets$ui_filter_panel(ns("module_filter_panel")),+ checkmate::assert_class(join_keys, "join_keys", null.ok = TRUE) |
||
143 | -! | +|||
50 | +37x |
- class = "teal_secondary_col"+ checkmate::assert_multi_class(code, c("character", "reactive")) |
||
144 | +51 |
- )+ |
||
145 | -+ | |||
52 | +36x |
- )+ checkmate::assert_list(metadata, names = "unique", null.ok = TRUE) |
||
146 | -+ | |||
53 | +34x |
- } else {+ checkmate::assert_subset(names(metadata), names(data)) |
||
147 | -1x | +54 | +22x |
- teal_ui+ for (m in metadata) teal.data::validate_metadata(m) |
148 | +55 |
- }+ |
||
149 | -+ | |||
56 | +33x |
- }+ if (is.reactive(code)) { |
||
150 | -+ | |||
57 | +17x |
-
+ isolate(checkmate::assert_class(code(), "character", .var.name = "code")) |
||
151 | +58 |
- #' @rdname module_nested_tabs+ } |
||
152 | +59 |
- srv_nested_tabs <- function(id, datasets, modules, is_module_specific = FALSE,+ |
||
153 | +60 |
- reporter = teal.reporter::Reporter$new()) {+ # create reactive data.frames |
||
154 | -54x | +61 | +32x |
- checkmate::assert_multi_class(modules, c("teal_modules", "teal_module"))+ for (x in names(data)) { |
155 | -54x | +62 | +51x |
- checkmate::assert_class(reporter, "Reporter")+ if (!is.reactive(data[[x]])) { |
156 | -53x | +63 | +29x |
- UseMethod("srv_nested_tabs", modules)+ data[[x]] <- do.call(reactive, list(as.name(x)), envir = list2env(data[x])) |
157 | +64 |
- }+ } else { |
||
158 | -+ | |||
65 | +22x |
-
+ isolate( |
||
159 | -+ | |||
66 | +22x |
- #' @rdname module_nested_tabs+ checkmate::assert_multi_class( |
||
160 | -+ | |||
67 | +22x |
- #' @export+ data[[x]](), c("data.frame", "MultiAssayExperiment"), |
||
161 | -+ | |||
68 | +22x |
- srv_nested_tabs.default <- function(id, datasets, modules, is_module_specific = FALSE,+ .var.name = "data" |
||
162 | +69 |
- reporter = teal.reporter::Reporter$new()) {+ ) |
||
163 | -! | +|||
70 | +
- stop("Modules class not supported: ", paste(class(modules), collapse = " "))+ ) |
|||
164 | +71 |
- }+ } |
||
165 | +72 |
-
+ } |
||
166 | +73 |
- #' @rdname module_nested_tabs+ |
||
167 | +74 |
- #' @export+ # set attributes |
||
168 | -+ | |||
75 | +31x |
- srv_nested_tabs.teal_modules <- function(id, datasets, modules, is_module_specific = FALSE,+ attr(data, "code") <- if (is.reactive(code)) code else reactive(code) |
||
169 | -+ | |||
76 | +31x |
- reporter = teal.reporter::Reporter$new()) {+ attr(data, "join_keys") <- join_keys |
||
170 | -24x | +77 | +31x |
- checkmate::assert_list(datasets, types = c("list", "FilteredData"))+ attr(data, "metadata") <- metadata |
171 | +78 | |||
79 | ++ |
+ # set class+ |
+ ||
172 | -24x | +80 | +31x |
- moduleServer(id = id, module = function(input, output, session) {+ class(data) <- c("tdata", class(data)) |
173 | -24x | +81 | +31x |
- logger::log_trace("srv_nested_tabs.teal_modules initializing the module { deparse1(modules$label) }.")+ data |
174 | +82 |
-
+ } |
||
175 | -24x | +|||
83 | +
- labels <- vapply(modules$children, `[[`, character(1), "label")+ |
|||
176 | -24x | +|||
84 | +
- modules_reactive <- sapply(+ #' Function to convert a `tdata` object to an `environment` |
|||
177 | -24x | +|||
85 | +
- names(modules$children),+ #' Any `reactives` inside `tdata` are first evaluated |
|||
178 | -24x | +|||
86 | +
- function(module_id) {+ #' @param data a `tdata` object |
|||
179 | -35x | +|||
87 | +
- srv_nested_tabs(+ #' @return an `environment` |
|||
180 | -35x | +|||
88 | +
- id = module_id,+ #' @examples |
|||
181 | -35x | +|||
89 | +
- datasets = datasets[[labels[module_id]]],+ #' |
|||
182 | -35x | +|||
90 | +
- modules = modules$children[[module_id]],+ #' data <- new_tdata( |
|||
183 | -35x | +|||
91 | +
- is_module_specific = is_module_specific,+ #' data = list(iris = iris, mtcars = reactive(mtcars)), |
|||
184 | -35x | +|||
92 | +
- reporter = reporter+ #' code = "iris <- iris |
|||
185 | +93 |
- )+ #' mtcars = mtcars" |
||
186 | +94 |
- },+ #' ) |
||
187 | -24x | +|||
95 | +
- simplify = FALSE+ #' |
|||
188 | +96 |
- )+ #' my_env <- isolate(tdata2env(data)) |
||
189 | +97 |
-
+ #' |
||
190 | +98 |
- # when not ready input$active_tab would return NULL - this would fail next reactive+ #' @export |
||
191 | -24x | +|||
99 | +
- input_validated <- eventReactive(input$active_tab, input$active_tab, ignoreNULL = TRUE)+ tdata2env <- function(data) { # nolint |
|||
192 | -24x | +100 | +2x |
- get_active_module <- reactive({+ checkmate::assert_class(data, "tdata") |
193 | -13x | +101 | +1x |
- if (length(modules$children) == 1L) {+ list2env(lapply(data, function(x) if (is.reactive(x)) x() else x)) |
194 | +102 |
- # single tab is active by default+ } |
||
195 | -2x | +|||
103 | +
- modules_reactive[[1]]()+ |
|||
196 | +104 |
- } else {+ #' @rdname tdata |
||
197 | +105 |
- # switch to active tab+ #' @param x a `tdata` object |
||
198 | -11x | +|||
106 | +
- modules_reactive[[input_validated()]]()+ #' @param ... additional arguments for the generic |
|||
199 | +107 |
- }+ #' @export |
||
200 | +108 |
- })+ get_code.tdata <- function(x, ...) { # nolint |
||
201 | +109 |
-
+ # note teal.data which teal depends on defines the get_code method |
||
202 | -24x | +110 | +6x |
- get_active_module+ attr(x, "code")() |
203 | +111 |
- })+ } |
||
204 | +112 |
- }+ |
||
205 | +113 | |||
206 | +114 |
- #' @rdname module_nested_tabs+ #' Wrapper for `get_code.tdata` |
||
207 | +115 |
- #' @export+ #' This wrapper is to be used by downstream packages to extract the code of a `tdata` object |
||
208 | +116 |
- srv_nested_tabs.teal_module <- function(id, datasets, modules, is_module_specific = TRUE,+ #' |
||
209 | +117 |
- reporter = teal.reporter::Reporter$new()) {+ #' @param data (`tdata`) object |
||
210 | -29x | +|||
118 | +
- checkmate::assert_class(datasets, "FilteredData")+ #' |
|||
211 | -29x | +|||
119 | +
- logger::log_trace("srv_nested_tabs.teal_module initializing the module: { deparse1(modules$label) }.")+ #' @return (`character`) code used in the `tdata` object. |
|||
212 | +120 |
-
+ #' @export |
||
213 | -29x | +|||
121 | +
- moduleServer(id = id, module = function(input, output, session) {+ get_code_tdata <- function(data) { |
|||
214 | -29x | +122 | +4x |
- modules$server_args <- teal.transform::resolve_delayed(modules$server_args, datasets)+ checkmate::assert_class(data, "tdata") |
215 | -29x | -
- if (!is.null(modules$datanames) && is_module_specific) {- |
- ||
216 | -! | +123 | +2x |
- datasets$srv_filter_panel("module_filter_panel")+ get_code(data) |
217 | +124 |
- }+ } |
||
218 | +125 | |||
219 | +126 |
- # Create two triggers to limit reactivity between filter-panel and modules.+ #' Extract `join_keys` from `tdata` |
||
220 | +127 |
- # We want to recalculate only visible modules+ #' @param data A `tdata` object |
||
221 | +128 |
- # - trigger the data when the tab is selected+ #' @param ... Additional arguments (not used) |
||
222 | +129 |
- # - trigger module to be called when the tab is selected for the first time- |
- ||
223 | -29x | -
- trigger_data <- reactiveVal(1L)- |
- ||
224 | -29x | -
- trigger_module <- reactiveVal(NULL)+ #' @export |
||
225 | -29x | +|||
130 | +
- output$data_reactive <- renderUI({+ join_keys.tdata <- function(data, ...) { |
|||
226 | -18x | +131 | +3x |
- lapply(datasets$datanames(), function(x) {+ attr(data, "join_keys") |
227 | -22x | +|||
132 | +
- datasets$get_data(x, filtered = TRUE)+ } |
|||
228 | +133 |
- })+ |
||
229 | -18x | +|||
134 | +
- isolate(trigger_data(trigger_data() + 1))+ |
|||
230 | -18x | +|||
135 | +
- isolate(trigger_module(TRUE))+ #' Function to get metadata from a `tdata` object |
|||
231 | +136 |
-
+ #' @param data `tdata` - object to extract the data from |
||
232 | -18x | +|||
137 | +
- NULL+ #' @param dataname `character(1)` the dataset name whose metadata is requested |
|||
233 | +138 |
- })+ #' @return Either list of metadata or NULL if no metadata |
||
234 | +139 |
-
+ #' @export |
||
235 | +140 |
- # collect arguments to run teal_module+ get_metadata <- function(data, dataname) { |
||
236 | -29x | +141 | +4x |
- args <- c(list(id = "module"), modules$server_args)+ checkmate::assert_string(dataname) |
237 | -29x | -
- if (is_arg_used(modules$server, "reporter")) {- |
- ||
238 | -! | +142 | +4x |
- args <- c(args, list(reporter = reporter))+ UseMethod("get_metadata", data) |
239 | +143 |
- }+ } |
||
240 | +144 | |||
241 | -29x | -
- if (is_arg_used(modules$server, "datasets")) {- |
- ||
242 | -2x | +|||
145 | +
- args <- c(args, datasets = datasets)+ #' @rdname get_metadata |
|||
243 | +146 |
- }+ #' @export |
||
244 | +147 |
-
+ get_metadata.tdata <- function(data, dataname) { |
||
245 | -29x | +148 | +4x |
- if (is_arg_used(modules$server, "data")) {+ metadata <- attr(data, "metadata") |
246 | -9x | +149 | +4x |
- data <- .datasets_to_data(modules, datasets, trigger_data)+ if (is.null(metadata)) { |
247 | -9x | +150 | +1x |
- args <- c(args, data = list(data))+ return(NULL) |
248 | +151 |
- }+ } |
||
249 | -+ | |||
152 | +3x |
-
+ metadata[[dataname]] |
||
250 | -29x | +|||
153 | +
- if (is_arg_used(modules$server, "filter_panel_api")) {+ } |
|||
251 | -2x | +|||
154 | +
- filter_panel_api <- teal.slice::FilterPanelAPI$new(datasets)+ |
|||
252 | -2x | +|||
155 | +
- args <- c(args, filter_panel_api = filter_panel_api)+ #' @rdname get_metadata |
|||
253 | +156 |
- }+ #' @export |
||
254 | +157 |
-
+ get_metadata.default <- function(data, dataname) { |
||
255 | -29x | +|||
158 | +! |
- if (is_arg_used(modules$server, "datasets") && is_arg_used(modules$server, "data")) {+ stop("get_metadata function not implemented for this object") |
||
256 | -1x | +|||
159 | +
- warning(+ } |
|||
257 | -1x | +
1 | +
- "Module '", modules$label, "' has `data` and `datasets` arguments in the formals.",+ #' Filter manager modal |
|||
258 | -1x | +|||
2 | ++ |
+ #'+ |
+ ||
3 | +
- "\nIt's recommended to use `data` to work with filtered objects."+ #' Opens modal containing the filter manager UI. |
|||
259 | +4 |
- )+ #' |
||
260 | +5 |
- }+ #' @name module_filter_manager_modal |
||
261 | +6 |
-
+ #' @inheritParams filter_manager_srv |
||
262 | +7 |
- # observe the trigger_module above to induce the module once the renderUI is triggered+ #' @examples |
||
263 | -29x | +|||
8 | +
- observeEvent(+ #' fd1 <- teal.slice::init_filtered_data(list(iris = list(dataset = iris))) |
|||
264 | -29x | +|||
9 | +
- ignoreNULL = TRUE,+ #' fd2 <- teal.slice::init_filtered_data( |
|||
265 | -29x | +|||
10 | +
- once = TRUE,+ #' list(iris = list(dataset = iris), mtcars = list(dataset = mtcars)) |
|||
266 | -29x | +|||
11 | +
- eventExpr = trigger_module(),+ #' ) |
|||
267 | -29x | +|||
12 | +
- handlerExpr = {+ #' fd3 <- teal.slice::init_filtered_data( |
|||
268 | -18x | +|||
13 | +
- module_output <- if (is_arg_used(modules$server, "id")) {+ #' list(iris = list(dataset = iris), women = list(dataset = women)) |
|||
269 | -18x | +|||
14 | +
- do.call(modules$server, args)+ #' ) |
|||
270 | +15 |
- } else {+ #' filter <- teal_slices( |
||
271 | -! | +|||
16 | +
- do.call(callModule, c(args, list(module = modules$server)))+ #' teal.slice::teal_slice(dataname = "iris", varname = "Sepal.Length"), |
|||
272 | +17 |
- }+ #' teal.slice::teal_slice(dataname = "iris", varname = "Species"), |
||
273 | +18 |
- }+ #' teal.slice::teal_slice(dataname = "mtcars", varname = "mpg"), |
||
274 | +19 |
- )+ #' teal.slice::teal_slice(dataname = "women", varname = "height"), |
||
275 | +20 |
-
+ #' mapping = list( |
||
276 | -29x | +|||
21 | +
- reactive(modules)+ #' module2 = c("mtcars mpg"), |
|||
277 | +22 |
- })+ #' module3 = c("women height"), |
||
278 | +23 |
- }+ #' global_filters = "iris Species" |
||
279 | +24 |
-
+ #' ) |
||
280 | +25 |
- #' Convert `FilteredData` to reactive list of datasets of the `tdata` type.+ #' ) |
||
281 | +26 |
#' |
||
282 | +27 |
- #' Converts `FilteredData` object to `tdata` object containing datasets needed for a specific module.+ #' app <- shinyApp( |
||
283 | +28 |
- #' Please note that if module needs dataset which has a parent, then parent will be also returned.+ #' ui = fluidPage( |
||
284 | +29 |
- #' A hash per `dataset` is calculated internally and returned in the code.+ #' teal:::filter_manager_modal_ui("manager") |
||
285 | +30 |
- #'+ #' ), |
||
286 | +31 |
- #' @param module (`teal_module`) module where needed filters are taken from+ #' server = function(input, output, session) { |
||
287 | +32 |
- #' @param datasets (`FilteredData`) object where needed data are taken from+ #' teal:::filter_manager_modal_srv( |
||
288 | +33 |
- #' @param trigger_data (`reactiveVal`) to trigger getting the filtered data+ #' "manager", |
||
289 | +34 |
- #' @return list of reactive datasets with following attributes:+ #' filtered_data_list = list(module1 = fd1, module2 = fd2, module3 = fd3), |
||
290 | +35 |
- #' - `code` (`character`) containing datasets reproducible code.+ #' filter = filter |
||
291 | +36 |
- #' - `join_keys` (`join_keys`) containing relationships between datasets.+ #' ) |
||
292 | +37 |
- #' - `metadata` (`list`) containing metadata of datasets.+ #' } |
||
293 | +38 |
- #'+ #' ) |
||
294 | +39 |
- #' @keywords internal+ #' if (interactive()) { |
||
295 | +40 |
- .datasets_to_data <- function(module, datasets, trigger_data = reactiveVal(1L)) {- |
- ||
296 | -15x | -
- checkmate::assert_class(module, "teal_module")+ #' shinyApp(app$ui, app$server) |
||
297 | -15x | +|||
41 | +
- checkmate::assert_class(datasets, "FilteredData")+ #' } |
|||
298 | -15x | +|||
42 | +
- checkmate::assert_class(trigger_data, "reactiveVal")+ #' |
|||
299 | +43 |
-
+ #' @keywords internal |
||
300 | -14x | +|||
44 | +
- datanames <- if (is.null(module$datanames) || identical(module$datanames, "all")) {+ #' |
|||
301 | -5x | +|||
45 | +
- datasets$datanames()+ NULL |
|||
302 | +46 |
- } else {+ |
||
303 | -9x | +|||
47 | +
- unique(module$datanames) # todo: include parents! unique shouldn't be needed here!+ #' @rdname module_filter_manager_modal |
|||
304 | +48 |
- }+ filter_manager_modal_ui <- function(id) { |
||
305 | -+ | |||
49 | +1x |
-
+ ns <- NS(id) |
||
306 | -+ | |||
50 | +1x |
- # list of reactive filtered data+ tags$button( |
||
307 | -14x | +51 | +1x |
- data <- sapply(+ id = ns("show"), |
308 | -14x | +52 | +1x |
- datanames,+ class = "btn action-button filter_manager_button", |
309 | -14x | +53 | +1x |
- function(x) eventReactive(trigger_data(), datasets$get_data(x, filtered = TRUE)),+ title = "Show filters manager modal", |
310 | -14x | +54 | +1x |
- simplify = FALSE+ icon("gear") |
311 | +55 |
) |
||
312 | +56 |
-
+ } |
||
313 | -14x | +|||
57 | +
- hashes <- calculate_hashes(datanames, datasets)+ |
|||
314 | -14x | +|||
58 | +
- metadata <- sapply(datanames, datasets$get_metadata, simplify = FALSE)+ #' @rdname module_filter_manager_modal |
|||
315 | +59 |
-
+ filter_manager_modal_srv <- function(id, filtered_data_list, filter) { |
||
316 | -14x | +60 | +4x |
- new_tdata(+ moduleServer(id, function(input, output, session) { |
317 | -14x | +61 | +4x |
- data,+ observeEvent(input$show, { |
318 | -14x | +|||
62 | +! |
- eventReactive(+ logger::log_trace("filter_manager_modal_srv@1 show button has been clicked.") |
||
319 | -14x | +|||
63 | +! |
- trigger_data(),+ showModal( |
||
320 | -+ | |||
64 | +! |
- {+ modalDialog( |
||
321 | -14x | +|||
65 | +! |
- c(+ filter_manager_ui(session$ns("filter_manager")), |
||
322 | -14x | +|||
66 | +! |
- get_rcode_str_install(),+ size = "l", |
||
323 | -14x | +|||
67 | +! |
- get_rcode_libraries(),+ footer = NULL, |
||
324 | -14x | +|||
68 | +! |
- get_datasets_code(datanames, datasets, hashes)+ easyClose = TRUE |
||
325 | +69 |
) |
||
326 | +70 |
- }+ ) |
||
327 | +71 |
- ),+ }) |
||
328 | -14x | +|||
72 | +
- datasets$get_join_keys(),+ |
|||
329 | -14x | +73 | +4x |
- metadata+ filter_manager_srv("filter_manager", filtered_data_list, filter) |
330 | +74 |
- )+ }) |
||
331 | +75 |
} |
||
332 | +76 | |||
333 | +77 |
- #' Get the hash of a dataset+ #' @rdname module_filter_manager |
||
334 | +78 |
- #'+ filter_manager_ui <- function(id) {+ |
+ ||
79 | +! | +
+ ns <- NS(id)+ |
+ ||
80 | +! | +
+ div(+ |
+ ||
81 | +! | +
+ class = "filter_manager_content",+ |
+ ||
82 | +! | +
+ tableOutput(ns("slices_table")),+ |
+ ||
83 | +! | +
+ snapshot_manager_ui(ns("snapshot_manager")) |
||
335 | +84 |
- #' @param datanames (`character`) names of datasets+ ) |
||
336 | +85 |
- #' @param datasets (`FilteredData`) object holding the data+ } |
||
337 | +86 |
- #'+ |
||
338 | +87 |
- #' @return A list of hashes per dataset+ #' Manage multiple `FilteredData` objects |
||
339 | +88 |
- #' @keywords internal+ #' |
||
340 | +89 | ++ |
+ #' Oversee filter states in the whole application.+ |
+ |
90 |
#' |
|||
341 | +91 |
- calculate_hashes <- function(datanames, datasets) {+ #' @rdname module_filter_manager |
||
342 | -17x | +|||
92 | +
- sapply(datanames, function(x) rlang::hash(datasets$get_data(x, filtered = FALSE)), simplify = FALSE)+ #' @details |
|||
343 | +93 |
- }+ #' This module observes the changes of the filters in each `FilteredData` object |
1 | +94 | ++ |
+ #' and keeps track of all filters used. A mapping of filters to modules+ |
+ |
95 |
- #' Add right filter panel into each of the top-level `teal_modules` UIs.+ #' is kept in the `mapping_matrix` object (which is actually a `data.frame`) |
|||
2 | +96 |
- #'+ #' that tracks which filters (rows) are active in which modules (columns). |
||
3 | +97 |
- #' The [ui_nested_tabs] function returns a nested tabbed UI corresponding+ #' |
||
4 | +98 |
- #' to the nested modules.+ #' @param id (`character(1)`)\cr |
||
5 | +99 |
- #' This function adds the right filter panel to each main tab.+ #' `shiny` module id. |
||
6 | +100 |
- #'+ #' @param filtered_data_list (`named list`)\cr |
||
7 | +101 |
- #' The right filter panel's filter choices affect the `datasets` object. Therefore,+ #' A list, possibly nested, of `FilteredData` objects. |
||
8 | +102 |
- #' all modules using the same `datasets` share the same filters.+ #' Each `FilteredData` will be served to one module in the `teal` application. |
||
9 | +103 |
- #'+ #' The structure of the list must reflect the nesting of modules in tabs |
||
10 | +104 |
- #' This works with nested modules of depth greater than 2, though the filter+ #' and names of the list must be the same as labels of their respective modules. |
||
11 | +105 |
- #' panel is inserted at the right of the modules at depth 1 and not at the leaves.+ #' @inheritParams init |
||
12 | +106 |
- #'+ #' @return A list of `reactive`s, each holding a `teal_slices`, as returned by `filter_manager_module_srv`. |
||
13 | +107 |
- #' @name module_tabs_with_filters+ #' @keywords internal |
||
14 | +108 |
#' |
||
15 | +109 |
- #' @inheritParams module_teal+ filter_manager_srv <- function(id, filtered_data_list, filter) { |
||
16 | -+ | |||
110 | +6x |
- #'+ moduleServer(id, function(input, output, session) { |
||
17 | -+ | |||
111 | +6x |
- #' @param datasets (`named list` of `FilteredData`)\cr+ logger::log_trace("filter_manager_srv initializing for: { paste(names(filtered_data_list), collapse = ', ')}.") |
||
18 | +112 |
- #' object to store filter state and filtered datasets, shared across modules. For more+ |
||
19 | -+ | |||
113 | +6x |
- #' details see [`teal.slice::FilteredData`]. Structure of the list must be the same as structure+ is_module_specific <- isTRUE(attr(filter, "module_specific")) |
||
20 | +114 |
- #' of the `modules` argument and list names must correspond to the labels in `modules`.+ |
||
21 | +115 |
- #' When filter is not module-specific then list contains the same object in all elements.+ # Create global list of slices. |
||
22 | +116 |
- #' @param reporter (`Reporter`) object from `teal.reporter`+ # Contains all available teal_slice objects available to all modules. |
||
23 | +117 |
- #'+ # Passed whole to instances of FilteredData used for individual modules. |
||
24 | +118 |
- #' @return A `tagList` of The main menu, place holders for filters and+ # Down there a subset that pertains to the data sets used in that module is applied and displayed. |
||
25 | -+ | |||
119 | +6x |
- #' place holders for the teal modules+ slices_global <- reactiveVal(filter) |
||
26 | +120 |
- #'+ |
||
27 | -+ | |||
121 | +6x |
- #'+ filtered_data_list <- |
||
28 | -+ | |||
122 | +6x |
- #' @keywords internal+ if (!is_module_specific) { |
||
29 | +123 |
- #'+ # Retrieve the first FilteredData from potentially nested list. |
||
30 | +124 |
- #' @examples+ # List of length one is named "global_filters" because that name is forbidden for a module label. |
||
31 | -+ | |||
125 | +5x |
- #'+ list(global_filters = unlist(filtered_data_list)[[1]]) |
||
32 | +126 |
- #' mods <- teal:::example_modules()+ } else { |
||
33 | +127 |
- #' datasets <- teal:::example_datasets()+ # Flatten potentially nested list of FilteredData objects while maintaining useful names. |
||
34 | +128 |
- #'+ # Simply using `unlist` would result in concatenated names. |
||
35 | -+ | |||
129 | +1x |
- #' app <- shinyApp(+ flatten_nested <- function(x, name = NULL) {+ |
+ ||
130 | +5x | +
+ if (inherits(x, "FilteredData")) {+ |
+ ||
131 | +3x | +
+ setNames(list(x), name) |
||
36 | +132 |
- #' ui = function() {+ } else {+ |
+ ||
133 | +2x | +
+ unlist(lapply(names(x), function(name) flatten_nested(x[[name]], name))) |
||
37 | +134 |
- #' tagList(+ } |
||
38 | +135 |
- #' teal:::include_teal_css_js(),+ }+ |
+ ||
136 | +1x | +
+ flatten_nested(filtered_data_list) |
||
39 | +137 |
- #' textOutput("info"),+ } |
||
40 | +138 |
- #' fluidPage( # needed for nice tabs+ |
||
41 | +139 |
- #' ui_tabs_with_filters("dummy", modules = mods, datasets = datasets)+ # Create mapping fo filters to modules in matrix form (presented as data.frame). |
||
42 | +140 |
- #' )+ # Modules get NAs for filters that cannot be set for them.+ |
+ ||
141 | +6x | +
+ mapping_matrix <- reactive({+ |
+ ||
142 | +6x | +
+ state_ids_global <- vapply(slices_global(), `[[`, character(1L), "id")+ |
+ ||
143 | +6x | +
+ mapping_smooth <- lapply(filtered_data_list, function(x) {+ |
+ ||
144 | +8x | +
+ state_ids_local <- vapply(x$get_filter_state(), `[[`, character(1L), "id")+ |
+ ||
145 | +8x | +
+ state_ids_allowed <- vapply(x$get_available_teal_slices()(), `[[`, character(1L), "id") |
||
43 | -+ | |||
146 | +8x |
- #' )+ states_active <- state_ids_global %in% state_ids_local |
||
44 | -+ | |||
147 | +8x |
- #' },+ ifelse(state_ids_global %in% state_ids_allowed, states_active, NA) |
||
45 | +148 |
- #' server = function(input, output, session) {+ }) |
||
46 | +149 |
- #' output$info <- renderText({+ |
||
47 | -+ | |||
150 | +6x |
- #' paste0("The currently active tab name is ", active_module()$label)+ as.data.frame(mapping_smooth, row.names = state_ids_global, check.names = FALSE) |
||
48 | +151 |
- #' })+ }) |
||
49 | +152 |
- #' active_module <- srv_tabs_with_filters(id = "dummy", datasets = datasets, modules = mods)+ |
||
50 | -+ | |||
153 | +6x |
- #' }+ output$slices_table <- renderTable( |
||
51 | -+ | |||
154 | +6x |
- #' )+ expr = { |
||
52 | +155 |
- #' if (interactive()) {+ # Display logical values as UTF characters. |
||
53 | -+ | |||
156 | +3x |
- #' shinyApp(app$ui, app$server)+ mm <- mapping_matrix() |
||
54 | -+ | |||
157 | +3x |
- #' }+ mm[] <- lapply(mm, ifelse, yes = intToUtf8(9989), no = intToUtf8(10060)) |
||
55 | -+ | |||
158 | +3x |
- #'+ mm[] <- lapply(mm, function(x) ifelse(is.na(x), intToUtf8(128306), x)) |
||
56 | -+ | |||
159 | +3x |
- NULL+ if (!is_module_specific) colnames(mm) <- "Global Filters" |
||
57 | +160 | |||
58 | +161 |
- #' @rdname module_tabs_with_filters+ # Display placeholder if no filters defined. |
||
59 | -+ | |||
162 | +3x |
- ui_tabs_with_filters <- function(id, modules, datasets, filter = teal_slices()) {+ if (nrow(mm) == 0L) { |
||
60 | -1x | +163 | +3x |
- checkmate::assert_class(modules, "teal_modules")+ mm <- data.frame(`Filter manager` = "No filters specified.", check.names = FALSE) |
61 | -1x | +164 | +3x |
- checkmate::assert_list(datasets, types = c("list", "FilteredData"))+ rownames(mm) <- "" |
62 | -1x | +|||
165 | +
- checkmate::assert_class(filter, "teal_slices")+ } |
|||
63 | +166 | |||
64 | -1x | +|||
167 | +
- ns <- NS(id)+ # Report Previewer will not be displayed. |
|||
65 | -1x | +168 | +3x |
- is_module_specific <- isTRUE(attr(filter, "module_specific"))+ mm[names(mm) != "Report previewer"] |
66 | +169 |
-
+ }, |
||
67 | -1x | +170 | +6x |
- teal_ui <- ui_nested_tabs(ns("root"), modules = modules, datasets, is_module_specific = is_module_specific)+ align = paste(c("l", rep("c", sum(names(filtered_data_list) != "Report previewer"))), collapse = ""), |
68 | -1x | +171 | +6x |
- filter_panel_btns <- tags$li(+ rownames = TRUE |
69 | -1x | +|||
172 | +
- class = "flex-grow",+ ) |
|||
70 | -1x | +|||
173 | +
- tags$button(+ |
|||
71 | -1x | +|||
174 | +
- class = "btn action-button filter_hamburger", # see sidebar.css for style filter_hamburger+ # Create list of module calls. |
|||
72 | -1x | +175 | +6x |
- href = "javascript:void(0)",+ modules_out <- lapply(names(filtered_data_list), function(module_name) { |
73 | -1x | +176 | +8x |
- onclick = "toggleFilterPanel();", # see sidebar.js+ filter_manager_module_srv( |
74 | -1x | +177 | +8x |
- title = "Toggle filter panels",+ id = module_name, |
75 | -1x | -
- icon("fas fa-bars")- |
- ||
76 | -+ | 178 | +8x |
- ),+ module_fd = filtered_data_list[[module_name]], |
77 | -1x | +179 | +8x |
- filter_manager_modal_ui(ns("filter_manager"))+ slices_global = slices_global |
78 | +180 |
- )+ ) |
||
79 | -1x | +|||
181 | +
- teal_ui$children[[1]] <- tagAppendChild(teal_ui$children[[1]], filter_panel_btns)+ }) |
|||
80 | +182 | |||
81 | -1x | -
- if (!is_module_specific) {- |
- ||
82 | +183 |
- # need to rearrange html so that filter panel is within tabset- |
- ||
83 | -1x | -
- tabset_bar <- teal_ui$children[[1]]+ # Call snapshot manager. |
||
84 | -1x | +184 | +6x |
- teal_modules <- teal_ui$children[[2]]+ snapshot_manager_srv("snapshot_manager", slices_global, mapping_matrix, filtered_data_list) |
85 | -1x | +|||
185 | +
- filter_ui <- unlist(datasets)[[1]]$ui_filter_panel(ns("filter_panel"))+ |
|||
86 | -1x | +186 | +6x |
- list(+ modules_out # returned for testing purpose |
87 | -1x | +|||
187 | +
- tabset_bar,+ }) |
|||
88 | -1x | +|||
188 | +
- tags$hr(class = "my-2"),+ } |
|||
89 | -1x | +|||
189 | +
- fluidRow(+ |
|||
90 | -1x | +|||
190 | +
- column(width = 9, teal_modules, class = "teal_primary_col"),+ #' Module specific filter manager |
|||
91 | -1x | +|||
191 | +
- column(width = 3, filter_ui, class = "teal_secondary_col")+ #' |
|||
92 | +192 |
- )+ #' Track filter states in single module. |
||
93 | +193 |
- )+ #' |
||
94 | +194 |
- } else {+ #' This module tracks the state of a single `FilteredData` object and global `teal_slices` |
||
95 | -! | +|||
195 | +
- teal_ui+ #' and updates both objects as necessary. Filter states added in different modules |
|||
96 | +196 |
- }+ #' Filter states added any individual module are added to global `teal_slices` |
||
97 | +197 |
- }+ #' and from there become available in other modules |
||
98 | +198 |
-
+ #' by setting `private$available_teal_slices` in each `FilteredData`. |
||
99 | +199 |
- #' @rdname module_tabs_with_filters+ #' |
||
100 | +200 |
- srv_tabs_with_filters <- function(id,+ #' @param id (`character(1)`)\cr |
||
101 | +201 |
- datasets,+ #' `shiny` module id. |
||
102 | +202 |
- modules,+ #' @param module_fd (`FilteredData`)\cr |
||
103 | +203 |
- reporter = teal.reporter::Reporter$new(),+ #' object to filter data in the teal-module |
||
104 | +204 |
- filter = teal_slices()) {+ #' @param slices_global (`reactiveVal`)\cr |
||
105 | -6x | +|||
205 | +
- checkmate::assert_class(modules, "teal_modules")+ #' stores `teal_slices` with all available filters; allows the following actions: |
|||
106 | -6x | +|||
206 | +
- checkmate::assert_list(datasets, types = c("list", "FilteredData"))+ #' - to disable/enable a specific filter in a module |
|||
107 | -6x | +|||
207 | +
- checkmate::assert_class(reporter, "Reporter")+ #' - to restore saved filter settings |
|||
108 | -4x | +|||
208 | +
- checkmate::assert_class(filter, "teal_slices")+ #' - to save current filter panel settings |
|||
109 | +209 |
-
+ #' @return A `reactive` expression containing the slices active in this module. |
||
110 | -4x | +|||
210 | +
- moduleServer(id, function(input, output, session) {+ #' @keywords internal |
|||
111 | -4x | +|||
211 | +
- logger::log_trace("srv_tabs_with_filters initializing the module.")+ #' |
|||
112 | +212 |
-
+ filter_manager_module_srv <- function(id, module_fd, slices_global) { |
||
113 | -4x | +213 | +8x |
- is_module_specific <- isTRUE(attr(filter, "module_specific"))+ moduleServer(id, function(input, output, session) {+ |
+
214 | ++ |
+ # Only operate on slices that refer to data sets present in this module. |
||
114 | -4x | +215 | +8x |
- manager_out <- filter_manager_modal_srv("filter_manager", filtered_data_list = datasets, filter = filter)+ module_fd$set_available_teal_slices(reactive(slices_global())) |
115 | +216 | |||
116 | -4x | +|||
217 | +
- active_module <- srv_nested_tabs(+ # Track filter state of this module. |
|||
117 | -4x | +218 | +8x |
- id = "root",+ slices_module <- reactive(module_fd$get_filter_state()) |
118 | -4x | +|||
219 | +
- datasets = datasets,+ |
|||
119 | -4x | +|||
220 | +
- modules = modules,+ # Reactive values for comparing states. |
|||
120 | -4x | +221 | +8x |
- reporter = reporter,+ previous_slices <- reactiveVal(isolate(slices_module())) |
121 | -4x | +222 | +8x |
- is_module_specific = is_module_specific+ slices_added <- reactiveVal(NULL) |
122 | +223 |
- )+ |
||
123 | +224 | - - | -||
124 | -4x | -
- if (!is_module_specific) {+ # Observe changes in module filter state and trigger appropriate actions. |
||
125 | -4x | +225 | +8x |
- active_datanames <- reactive({+ observeEvent(slices_module(), ignoreNULL = FALSE, { |
126 | -7x | +226 | +3x |
- if (identical(active_module()$datanames, "all")) {+ logger::log_trace("filter_manager_srv@1 detecting states deltas in module: { id }.") |
127 | -1x | +227 | +3x |
- singleton$datanames()+ added <- setdiff_teal_slices(slices_module(), slices_global()) |
128 | -+ | |||
228 | +! |
- } else {+ if (length(added)) slices_added(added) |
||
129 | -5x | +229 | +3x |
- active_module()$datanames+ previous_slices(slices_module()) |
130 | +230 |
- }+ }) |
||
131 | +231 |
- })+ |
||
132 | -4x | +232 | +8x |
- singleton <- unlist(datasets)[[1]]+ observeEvent(slices_added(), ignoreNULL = TRUE, { |
133 | -4x | +|||
233 | +! |
- singleton$srv_filter_panel("filter_panel", active_datanames = active_datanames)+ logger::log_trace("filter_manager_srv@2 added filter in module: { id }.") |
||
134 | +234 |
-
+ # In case the new state has the same id as an existing state, add a suffix to it. |
||
135 | -4x | +|||
235 | +! |
- observeEvent(+ global_ids <- vapply(slices_global(), `[[`, character(1L), "id") |
||
136 | -4x | +|||
236 | +! |
- eventExpr = active_datanames(),+ lapply( |
||
137 | -4x | +|||
237 | +! |
- handlerExpr = {+ slices_added(), |
||
138 | -5x | +|||
238 | +! |
- script <- if (length(active_datanames()) == 0 || is.null(active_datanames())) {+ function(slice) { |
||
139 | -+ | |||
239 | +! |
- # hide the filter panel and disable the burger button+ if (slice$id %in% global_ids) { |
||
140 | +240 | ! |
- "handleNoActiveDatasets();"+ slice$id <- utils::tail(make.unique(c(global_ids, slice$id), sep = "_"), 1) |
|
141 | +241 |
- } else {+ } |
||
142 | +242 |
- # show the filter panel and enable the burger button- |
- ||
143 | -5x | -
- "handleActiveDatasetsPresent();"+ } |
||
144 | +243 |
- }- |
- ||
145 | -5x | -
- shinyjs::runjs(script)+ ) |
||
146 | -+ | |||
244 | +! |
- },+ slices_global_new <- c(slices_global(), slices_added()) |
||
147 | -4x | +|||
245 | +! |
- ignoreNULL = FALSE+ slices_global(slices_global_new) |
||
148 | -+ | |||
246 | +! |
- )+ slices_added(NULL) |
||
149 | +247 |
- }+ }) |
||
150 | +248 | |||
151 | -4x | -
- showNotification("Data loaded - App fully started up")- |
- ||
152 | -4x | -
- logger::log_trace("srv_tabs_with_filters initialized the module")- |
- ||
153 | -4x | +249 | +8x |
- return(active_module)+ slices_module # returned for testing purpose |
154 | +250 |
}) |
||
155 | +251 |
}@@ -16850,10904 +17480,11008 @@ teal coverage - 71.59% |
1 |
- # This file adds a splash screen for delayed data loading on top of teal+ #' Include `CSS` files from `/inst/css/` package directory to application header |
|||
2 |
-
+ #' |
|||
3 |
- #' UI to show a splash screen in the beginning, then delegate to [srv_teal()]+ #' `system.file` should not be used to access files in other packages, it does |
|||
4 |
- #'+ #' not work with `devtools`. Therefore, we redefine this method in each package |
|||
5 |
- #' @description `r lifecycle::badge("stable")`+ #' as needed. Thus, we do not export this method |
|||
6 |
- #' The splash screen could be used to query for a password to fetch the data.+ #' |
|||
7 |
- #' [init()] is a very thin wrapper around this module useful for end-users which+ #' @param pattern (`character`) pattern of files to be included |
|||
8 |
- #' assumes that it is a top-level module and cannot be embedded.+ #' |
|||
9 |
- #' This function instead adheres to the Shiny module conventions.+ #' @return HTML code that includes `CSS` files |
|||
10 |
- #'+ #' @keywords internal |
|||
11 |
- #' If data is obtained through delayed loading, its splash screen is used. Otherwise,+ include_css_files <- function(pattern = "*") { |
|||
12 | -+ | 22x |
- #' a default splash screen is shown.+ css_files <- list.files( |
|
13 | -+ | 22x |
- #'+ system.file("css", package = "teal", mustWork = TRUE), |
|
14 | -+ | 22x |
- #' Please also refer to the doc of [init()].+ pattern = pattern, full.names = TRUE |
|
15 |
- #'+ ) |
|||
16 | -+ | 22x |
- #' @param id (`character(1)`)\cr+ return( |
|
17 | -+ | 22x |
- #' module id+ shiny::singleton( |
|
18 | -+ | 22x |
- #' @inheritParams init+ shiny::tags$head(lapply(css_files, shiny::includeCSS)) |
|
19 |
- #' @export+ ) |
|||
20 |
- ui_teal_with_splash <- function(id,+ ) |
|||
21 |
- data,+ } |
|||
22 |
- title,+ |
|||
23 |
- header = tags$p("Add Title Here"),+ #' Include `JS` files from `/inst/js/` package directory to application header |
|||
24 |
- footer = tags$p("Add Footer Here")) {+ #' |
|||
25 | -22x | +
- checkmate::assert_multi_class(data, c("TealData", "teal_data", "teal_data_module"))+ #' `system.file` should not be used to access files in other packages, it does |
||
26 | -22x | +
- ns <- NS(id)+ #' not work with `devtools`. Therefore, we redefine this method in each package |
||
27 |
-
+ #' as needed. Thus, we do not export this method |
|||
28 |
- # Startup splash screen for delayed loading+ #' |
|||
29 |
- # We use delayed loading in all cases, even when the data does not need to be fetched.+ #' @param pattern (`character`) pattern of files to be included, passed to `system.file` |
|||
30 |
- # This has the benefit that when filtering the data takes a lot of time initially, the+ #' @param except (`character`) vector of basename filenames to be excluded |
|||
31 |
- # Shiny app does not time out.+ #' |
|||
32 |
-
+ #' @return HTML code that includes `JS` files |
|||
33 | -22x | +
- splash_ui <- if (inherits(data, "teal_data_module")) {+ #' @keywords internal |
||
34 | -1x | +
- data$ui(ns("teal_data_module"))+ include_js_files <- function(pattern = NULL, except = NULL) { |
||
35 | 22x |
- } else if (inherits(data, "teal_data")) {+ checkmate::assert_character(except, min.len = 1, any.missing = FALSE, null.ok = TRUE) |
||
36 | -2x | +22x |
- div()+ js_files <- list.files(system.file("js", package = "teal", mustWork = TRUE), pattern = pattern, full.names = TRUE) |
|
37 | 22x |
- } else if (inherits(data, "TealDataAbstract") && teal.data::is_pulled(data)) {+ js_files <- js_files[!(basename(js_files) %in% except)] # no-op if except is NULL |
||
38 | -17x | +
- div()+ |
||
39 | -+ | 22x |
- } else {+ return(singleton(lapply(js_files, includeScript))) |
|
40 | -2x | +
- message("App was initialized with delayed data loading.")+ } |
||
41 | -2x | +
- data$get_ui(ns("startapp_module"))+ |
||
42 |
- }+ #' Run `JS` file from `/inst/js/` package directory |
|||
43 | -22x | +
- ui_teal(+ #' |
||
44 | -22x | +
- id = ns("teal"),+ #' This is triggered from the server to execute on the client |
||
45 | -22x | +
- splash_ui = div(splash_ui, uiOutput(ns("error"))),+ #' rather than triggered directly on the client. |
||
46 | -22x | +
- title = title,+ #' Unlike `include_js_files` which includes `JavaScript` functions, |
||
47 | -22x | +
- header = header,+ #' the `run_js` actually executes `JavaScript` functions. |
||
48 | -22x | +
- footer = footer+ #' |
||
49 |
- )+ #' `system.file` should not be used to access files in other packages, it does |
|||
50 |
- }+ #' not work with `devtools`. Therefore, we redefine this method in each package |
|||
51 |
-
+ #' as needed. Thus, we do not export this method |
|||
52 |
- #' Server function that loads the data through reactive loading and then delegates+ #' |
|||
53 |
- #' to [srv_teal()].+ #' @param files (`character`) vector of filenames |
|||
54 |
- #'+ #' @keywords internal |
|||
55 |
- #' @description `r lifecycle::badge("stable")`+ run_js_files <- function(files) { |
|||
56 | -+ | 20x |
- #' Please also refer to the doc of [init()].+ checkmate::assert_character(files, min.len = 1, any.missing = FALSE) |
|
57 | -+ | 20x |
- #'+ lapply(files, function(file) { |
|
58 | -+ | 20x |
- #' @inheritParams init+ shinyjs::runjs(paste0(readLines(system.file("js", file, package = "teal", mustWork = TRUE)), collapse = "\n")) |
|
59 |
- #' @param modules `teal_modules` object containing the output modules which+ }) |
|||
60 | -+ | 20x |
- #' will be displayed in the teal application. See [modules()] and [module()] for+ return(invisible(NULL)) |
|
61 |
- #' more details.+ } |
|||
62 |
- #' @inheritParams shiny::moduleServer+ |
|||
63 |
- #' @return `reactive` containing `teal_data` object when data is loaded.+ #' Code to include teal `CSS` and `JavaScript` files |
|||
64 |
- #' If data is not loaded yet, `reactive` returns `NULL`.+ #' |
|||
65 |
- #' @export+ #' This is useful when you want to use the same `JavaScript` and `CSS` files that are |
|||
66 |
- srv_teal_with_splash <- function(id, data, modules, filter = teal_slices()) {+ #' used with the teal application. |
|||
67 | -13x | +
- checkmate::check_multi_class(data, c("TealData", "teal_data", "teal_data_module"))+ #' This is also useful for running standalone modules in teal with the correct |
||
68 |
-
+ #' styles. |
|||
69 | -13x | +
- moduleServer(id, function(input, output, session) {+ #' Also initializes `shinyjs` so you can use it. |
||
70 | -13x | +
- logger::log_trace("srv_teal_with_splash initializing module with data.")+ #' |
||
71 |
-
+ #' @return HTML code to include |
|||
72 | -13x | +
- if (getOption("teal.show_js_log", default = FALSE)) {+ #' @examples |
||
73 | -! | +
- shinyjs::showLog()+ #' shiny_ui <- tagList( |
||
74 |
- }+ #' teal:::include_teal_css_js(), |
|||
75 |
-
+ #' p("Hello") |
|||
76 |
- # teal_data_rv contains teal_data object+ #' ) |
|||
77 |
- # either passed to teal::init or returned from teal_data_module- |
- |||
78 | -13x | -
- teal_data_rv <- if (inherits(data, "teal_data_module")) {- |
- ||
79 | -6x | -
- data <- data$server(id = "teal_data_module")- |
- ||
80 | -6x | -
- if (!is.reactive(data)) {- |
- ||
81 | -1x | -
- stop("The `teal_data_module` must return a reactive expression.", call. = FALSE)+ #' @keywords internal |
||
82 | +78 |
- }- |
- ||
83 | -5x | -
- data- |
- ||
84 | -13x | -
- } else if (inherits(data, "teal_data")) {- |
- ||
85 | -5x | -
- reactiveVal(data)+ include_teal_css_js <- function() { |
||
86 | -13x | -
- } else if (inherits(data, "TealDataAbstract") && teal.data::is_pulled(data)) {- |
- ||
87 | -! | +79 | +22x |
- new_data <- do.call(+ tagList( |
88 | -! | +|||
80 | +22x |
- teal.data::teal_data,+ shinyjs::useShinyjs(), |
||
89 | -! | +|||
81 | +22x |
- c(+ include_css_files(), |
||
90 | -! | +|||
82 | +
- lapply(data$get_datasets(), function(x) x$get_raw_data()),+ # init.js is executed from the server |
|||
91 | -! | +|||
83 | +22x |
- list(code = data$get_code()),+ include_js_files(except = "init.js"), |
||
92 | -! | +|||
84 | +22x |
- list(join_keys = teal.data::join_keys(data))+ shinyjs::hidden(icon("gear")), # add hidden icon to load font-awesome css for icons |
||
93 | +85 |
- )+ ) |
||
94 | +86 |
- )+ } |
||
95 | -! | +
1 | +
- reactiveVal(new_data) # will trigger by setting it+ # This is the main function from teal to be used by the end-users. Although it delegates |
||
96 | +2 |
- } else {+ # directly to `module_teal_with_splash.R`, we keep it in a separate file because its doc is quite large |
|
97 | -2x | +||
3 | +
- raw_data_old <- data$get_server()(id = "startapp_module")+ # and it is very end-user oriented. It may also perform more argument checking with more informative |
||
98 | -2x | +||
4 | +
- raw_data <- reactive({+ # error messages. |
||
99 | -3x | +||
5 | +
- data <- raw_data_old()+ |
||
100 | -3x | +||
6 | +
- if (!is.null(data)) {+ |
||
101 | +7 |
- # raw_data is a reactive which returns data only when submit button clicked+ #' Create the Server and UI Function For the Shiny App |
|
102 | +8 |
- # otherwise it returns NULL+ #' |
|
103 | -1x | +||
9 | +
- do.call(+ #' @description `r lifecycle::badge("stable")` |
||
104 | -1x | +||
10 | +
- teal.data::teal_data,+ #' End-users: This is the most important function for you to start a |
||
105 | -1x | +||
11 | +
- c(+ #' teal app that is composed out of teal modules. |
||
106 | -1x | +||
12 | +
- lapply(data$get_datasets(), function(x) x$get_raw_data()),+ #' |
||
107 | -1x | +||
13 | +
- list(code = data$get_code()),+ #' @param data (`TealData` or `TealDataset` or `TealDatasetConnector` or `list` or `data.frame` |
||
108 | -1x | +||
14 | +
- list(join_keys = teal.data::join_keys(data))+ #' or `MultiAssayExperiment`, `teal_data`, `teal_data_module`)\cr |
||
109 | +15 |
- )+ #' `R6` object as returned by [teal.data::cdisc_data()], [teal.data::teal_data()], |
|
110 | +16 |
- )+ #' [teal.data::cdisc_dataset()], [teal.data::dataset()], [teal.data::dataset_connector()] or |
|
111 | +17 |
- }+ #' [teal.data::cdisc_dataset_connector()] or [teal_data_module()] or a single `data.frame` or |
|
112 | +18 |
- })+ #' a `MultiAssayExperiment` |
|
113 | -2x | +||
19 | +
- raw_data+ #' or a list of the previous objects or function returning a named list. |
||
114 | +20 |
- }+ #' NOTE: teal does not guarantee reproducibility of the code when names of the list elements |
|
115 | +21 |
-
+ #' do not match the original object names. To ensure reproducibility please use [teal.data::teal_data()] |
|
116 | -12x | +||
22 | +
- teal_data_rv_validate <- reactive({+ #' or [teal.data::cdisc_data()] with `check = TRUE` enabled. |
||
117 | +23 |
- # custom module can return error+ #' @param modules (`list`, `teal_modules` or `teal_module`)\cr |
|
118 | -8x | +||
24 | +
- data <- tryCatch(teal_data_rv(), error = function(e) e)+ #' nested list of `teal_modules` or `teal_module` objects or a single |
||
119 | +25 |
-
+ #' `teal_modules` or `teal_module` object. These are the specific output modules which |
|
120 | +26 |
- # there is an empty reactive event on init!+ #' will be displayed in the teal application. See [modules()] and [module()] for |
|
121 | -8x | +||
27 | +
- if (inherits(data, "shiny.silent.error") && identical(data$message, "")) {+ #' more details. |
||
122 | -! | +||
28 | +
- return(NULL)+ #' @param title (`NULL` or `character`)\cr |
||
123 | +29 |
- }+ #' The browser window title (defaults to the host URL of the page). |
|
124 | +30 |
-
+ #' @param filter (`teal_slices`)\cr |
|
125 | +31 |
- # to handle qenv.error+ #' Specification of initial filter. Filters can be specified using [teal::teal_slices()]. |
|
126 | -8x | +||
32 | +
- if (inherits(data, "qenv.error")) {+ #' Old way of specifying filters through a list is deprecated and will be removed in the |
- ||
127 | -1x | +||
33 | +
- validate(+ #' next release. Please fix your applications to use [teal::teal_slices()]. |
||
128 | -1x | +||
34 | +
- need(+ #' @param header (`shiny.tag` or `character`) \cr |
||
129 | -1x | +||
35 | +
- FALSE,+ #' the header of the app. Note shiny code placed here (and in the footer |
||
130 | -1x | +||
36 | +
- paste(+ #' argument) will be placed in the app's `ui` function so code which needs to be placed in the `ui` function |
||
131 | -1x | +||
37 | +
- "Error when executing `teal_data_module`:\n ",+ #' (such as loading `CSS` via [htmltools::htmlDependency()]) should be included here. |
||
132 | -1x | +||
38 | +
- paste(data$message, collapse = "\n"),+ #' @param footer (`shiny.tag` or `character`)\cr |
||
133 | -1x | +||
39 | +
- "\n Check your inputs or contact app developer if error persists."+ #' the footer of the app |
||
134 | +40 |
- )+ #' @param id (`character`)\cr |
|
135 | +41 |
- )+ #' module id to embed it, if provided, |
|
136 | +42 |
- )+ #' the server function must be called with [shiny::moduleServer()]; |
|
137 | +43 |
- }+ #' See the vignette for an example. However, [ui_teal_with_splash()] |
|
138 | +44 |
-
+ #' is then preferred to this function. |
|
139 | +45 |
- # to handle module non-qenv errors+ #' |
|
140 | -7x | +||
46 | +
- if (inherits(data, "error")) {+ #' @return named list with `server` and `ui` function |
||
141 | -1x | +||
47 | +
- validate(+ #' |
||
142 | -1x | +||
48 | +
- need(+ #' @export |
||
143 | -1x | +||
49 | +
- FALSE,+ #' |
||
144 | -1x | +||
50 | +
- paste(+ #' @include modules.R |
||
145 | -1x | +||
51 | +
- "Error when executing `teal_data_module`:\n ",+ #' |
||
146 | -1x | +||
52 | +
- paste(data$message, collpase = "\n"),+ #' @examples |
||
147 | -1x | +||
53 | +
- "\n Check your inputs or contact app developer if error persists."+ #' new_iris <- transform(iris, id = seq_len(nrow(iris))) |
||
148 | +54 |
- )+ #' new_mtcars <- transform(mtcars, id = seq_len(nrow(mtcars))) |
|
149 | +55 |
- )+ #' |
|
150 | +56 |
- )+ #' app <- init( |
|
151 | +57 |
- }+ #' data = teal_data( |
|
152 | +58 |
-
+ #' dataset("new_iris", new_iris), |
|
153 | -6x | +||
59 | +
- validate(+ #' dataset("new_mtcars", new_mtcars), |
||
154 | -6x | +||
60 | +
- need(+ #' code = " |
||
155 | -6x | +||
61 | +
- inherits(data, "teal_data"),+ #' new_iris <- transform(iris, id = seq_len(nrow(iris))) |
||
156 | -6x | +||
62 | +
- paste(+ #' new_mtcars <- transform(mtcars, id = seq_len(nrow(mtcars))) |
||
157 | -6x | +||
63 | +
- "Error: `teal_data_module` did not return `teal_data` object",+ #' " |
||
158 | -6x | +||
64 | +
- "\n Check your inputs or contact app developer if error persists"+ #' ), |
||
159 | +65 |
- )+ #' modules = modules( |
|
160 | +66 |
- )+ #' module( |
|
161 | +67 |
- )+ #' label = "data source", |
|
162 | +68 |
-
+ #' server = function(input, output, session, data) {}, |
|
163 | -5x | +||
69 | +
- validate(need(teal.data::datanames(data), "Data has no datanames. Contact app developer."))+ #' ui = function(id, ...) div(p("information about data source")), |
||
164 | +70 |
-
+ #' datanames = "all" |
|
165 | +71 |
-
+ #' ), |
|
166 | -4x | +||
72 | +
- is_modules_ok <- check_modules_datanames(modules, teal.data::datanames(data))+ #' example_module(label = "example teal module"), |
||
167 | -4x | +||
73 | +
- validate(need(isTRUE(is_modules_ok), is_modules_ok))+ #' module( |
||
168 | +74 |
-
+ #' "Iris Sepal.Length histogram", |
|
169 | -3x | +||
75 | +
- is_filter_ok <- check_filter_datanames(filter, teal.data::datanames(data))+ #' server = function(input, output, session, data) { |
||
170 | -3x | +||
76 | +
- if (!isTRUE(is_filter_ok)) {+ #' output$hist <- renderPlot( |
||
171 | -1x | +||
77 | +
- showNotification(+ #' hist(data[["new_iris"]]()$Sepal.Length) |
||
172 | -1x | +||
78 | +
- "Some filters were not applied because of incompatibility with data. Contact app developer.",+ #' ) |
||
173 | -1x | +||
79 | +
- type = "warning",+ #' }, |
||
174 | -1x | +||
80 | +
- duration = 10+ #' ui = function(id, ...) { |
||
175 | +81 |
- )+ #' ns <- NS(id) |
|
176 | -1x | +||
82 | +
- logger::log_warn(is_filter_ok)+ #' plotOutput(ns("hist")) |
||
177 | +83 |
- }+ #' }, |
|
178 | +84 |
-
+ #' datanames = "new_iris" |
|
179 | -3x | +||
85 | +
- teal_data_rv()+ #' ) |
||
180 | +86 |
- })+ #' ), |
|
181 | +87 |
-
+ #' title = "App title", |
|
182 | -12x | +||
88 | +
- output$error <- renderUI({+ #' filter = teal_slices( |
||
183 | -1x | +||
89 | +
- teal_data_rv_validate()+ #' teal_slice(dataname = "new_iris", varname = "Species"), |
||
184 | -1x | +||
90 | +
- NULL+ #' teal_slice(dataname = "new_iris", varname = "Sepal.Length"), |
||
185 | +91 |
- })+ #' teal_slice(dataname = "new_mtcars", varname = "cyl"), |
|
186 | +92 |
-
+ #' exclude_varnames = list(new_iris = c("Sepal.Width", "Petal.Width")), |
|
187 | +93 |
-
+ #' mapping = list( |
|
188 | +94 |
-
+ #' `example teal module` = "new_iris Species", |
|
189 | -12x | +||
95 | +
- res <- srv_teal(id = "teal", modules = modules, teal_data_rv = teal_data_rv_validate, filter = filter)+ #' `Iris Sepal.Length histogram` = "new_iris Species", |
||
190 | -12x | +||
96 | +
- logger::log_trace("srv_teal_with_splash initialized module with data.")+ #' global_filters = "new_mtcars cyl" |
||
191 | -12x | +||
97 | +
- return(res)+ #' ) |
||
192 | +98 |
- })+ #' ), |
|
193 | +99 |
- }+ #' header = tags$h1("Sample App"), |
1 | +100 |
- #' @title `TealReportCard`+ #' footer = tags$p("Copyright 2017 - 2023") |
||
2 | +101 |
- #' @description `r lifecycle::badge("experimental")`+ #' ) |
||
3 | +102 |
- #' A child of [`ReportCard`] that is used for teal specific applications.+ #' if (interactive()) { |
||
4 | +103 |
- #' In addition to the parent methods, it supports rendering teal specific elements such as+ #' shinyApp(app$ui, app$server) |
||
5 | +104 |
- #' the source code, the encodings panel content and the filter panel content as part of the+ #' } |
||
6 | +105 |
- #' meta data.+ #' |
||
7 | +106 |
- #' @export+ init <- function(data, |
||
8 | +107 |
- #'+ modules, |
||
9 | +108 |
- TealReportCard <- R6::R6Class( # nolint: object_name_linter.+ title = NULL, |
||
10 | +109 |
- classname = "TealReportCard",+ filter = teal_slices(), |
||
11 | +110 |
- inherit = teal.reporter::ReportCard,+ header = tags$p(), |
||
12 | +111 |
- public = list(+ footer = tags$p(), |
||
13 | +112 |
- #' @description Appends the source code to the `content` meta data of this `TealReportCard`.+ id = character(0)) {+ |
+ ||
113 | +30x | +
+ logger::log_trace("init initializing teal app with: data ({ class(data)[1] }).")+ |
+ ||
114 | +30x | +
+ if (!inherits(data, c("TealData", "teal_data", "teal_data_module"))) {+ |
+ ||
115 | +24x | +
+ data <- teal.data::to_relational_data(data = data) |
||
14 | +116 |
- #'+ } |
||
15 | +117 |
- #' @param src (`character(1)`) code as text.+ + |
+ ||
118 | +25x | +
+ checkmate::assert_multi_class(data, c("TealData", "teal_data", "teal_data_module"))+ |
+ ||
119 | +25x | +
+ checkmate::assert_multi_class(modules, c("teal_module", "list", "teal_modules")) |
||
16 | -+ | |||
120 | +25x |
- #' @param ... any `rmarkdown` R chunk parameter and its value.+ checkmate::assert_string(title, null.ok = TRUE) |
||
17 | -+ | |||
121 | +25x |
- #' But `eval` parameter is always set to `FALSE`.+ checkmate::assert( |
||
18 | -+ | |||
122 | +25x |
- #' @return invisibly self+ checkmate::check_class(filter, "teal_slices"), |
||
19 | -+ | |||
123 | +25x |
- #' @examples+ checkmate::check_list(filter, names = "named") |
||
20 | +124 |
- #' card <- TealReportCard$new()$append_src(+ ) |
||
21 | -+ | |||
125 | +24x |
- #' "plot(iris)"+ checkmate::assert_multi_class(header, c("shiny.tag", "character")) |
||
22 | -+ | |||
126 | +24x |
- #' )+ checkmate::assert_multi_class(footer, c("shiny.tag", "character")) |
||
23 | -+ | |||
127 | +24x |
- #' card$get_content()[[1]]$get_content()+ checkmate::assert_character(id, max.len = 1, any.missing = FALSE) |
||
24 | +128 |
- append_src = function(src, ...) {+ |
||
25 | -4x | +129 | +24x |
- checkmate::assert_character(src, min.len = 0, max.len = 1)+ teal.logger::log_system_info() |
26 | -4x | +|||
130 | +
- params <- list(...)+ |
|||
27 | -4x | +131 | +24x |
- params$eval <- FALSE+ if (inherits(modules, "teal_module")) { |
28 | -4x | +132 | +1x |
- rblock <- RcodeBlock$new(src)+ modules <- list(modules) |
29 | -4x | +|||
133 | +
- rblock$set_params(params)+ } |
|||
30 | -4x | +134 | +24x |
- self$append_content(rblock)+ if (inherits(modules, "list")) { |
31 | +135 | 4x |
- self$append_metadata("SRC", src)+ modules <- do.call(teal::modules, modules) |
|
32 | -4x | +|||
136 | +
- invisible(self)+ } |
|||
33 | +137 |
- },+ |
||
34 | -+ | |||
138 | +24x |
- #' @description Appends the filter state list to the `content` and `metadata` of this `TealReportCard`.+ landing <- extract_module(modules, "teal_module_landing") |
||
35 | -+ | |||
139 | +! |
- #' If the filter state list has an attribute named `formatted`, it appends it to the card otherwise it uses+ if (length(landing) > 1L) stop("Only one `landing_popup_module` can be used.") |
||
36 | -+ | |||
140 | +24x |
- #' the default `yaml::as.yaml` to format the list.+ modules <- drop_module(modules, "teal_module_landing") |
||
37 | +141 |
- #' If the filter state list is empty, nothing is appended to the `content`.+ |
||
38 | +142 |
- #'+ # Calculate app id that will be used to stamp filter state snapshots. |
||
39 | +143 |
- #' @param fs (`teal_slices`) object returned from [teal_slices()] function.+ # App id is a hash of the app's data and modules. |
||
40 | +144 |
- #' @return invisibly self+ # See "transferring snapshots" section in ?snapshot. |
||
41 | -+ | |||
145 | +24x |
- append_fs = function(fs) {+ hashables <- mget(c("data", "modules")) |
||
42 | -5x | +146 | +24x |
- checkmate::assert_class(fs, "teal_slices")+ hashables$data <- if (inherits(hashables$data, "teal_data")) { |
43 | +147 | 4x |
- self$append_text("Filter State", "header3")+ as.list(hashables$data@env) |
|
44 | -4x | +148 | +24x |
- self$append_content(TealSlicesBlock$new(fs))+ } else if (inherits(data, "teal_data_module")) { |
45 | -4x | +149 | +1x |
- invisible(self)+ body(data$server) |
46 | -+ | |||
150 | +24x |
- },+ } else if (hashables$data$is_pulled()) { |
||
47 | -+ | |||
151 | +17x |
- #' @description Appends the encodings list to the `content` and `metadata` of this `TealReportCard`.+ sapply(get_dataname(hashables$data), simplify = FALSE, function(dn) { |
||
48 | -+ | |||
152 | +23x |
- #'+ hashables$data$get_dataset(dn)$get_raw_data() |
||
49 | +153 |
- #' @param encodings (`list`) list of encodings selections of the teal app.+ }) |
||
50 | +154 |
- #' @return invisibly self+ } else { |
||
51 | -+ | |||
155 | +2x |
- #' @examples+ hashables$data$get_code() |
||
52 | +156 |
- #' card <- TealReportCard$new()$append_encodings(list(variable1 = "X"))+ } |
||
53 | +157 |
- #' card$get_content()[[1]]$get_content()+ + |
+ ||
158 | +24x | +
+ attr(filter, "app_id") <- rlang::hash(hashables) |
||
54 | +159 |
- #'+ |
||
55 | +160 |
- append_encodings = function(encodings) {+ # convert teal.slice::teal_slices to teal::teal_slices |
||
56 | -4x | +161 | +24x |
- checkmate::assert_list(encodings)+ filter <- as.teal_slices(as.list(filter)) |
57 | -4x | +|||
162 | +
- self$append_text("Selected Options", "header3")+ |
|||
58 | -4x | +163 | +24x |
- if (requireNamespace("yaml", quietly = TRUE)) {+ if (isTRUE(attr(filter, "module_specific"))) { |
59 | -4x | +|||
164 | +! |
- self$append_text(yaml::as.yaml(encodings, handlers = list(+ module_names <- unlist(c(module_labels(modules), "global_filters")) |
||
60 | -4x | +|||
165 | +! |
- POSIXct = function(x) format(x, "%Y-%m-%d"),+ failed_mod_names <- setdiff(names(attr(filter, "mapping")), module_names) |
||
61 | -4x | +|||
166 | +! |
- POSIXlt = function(x) format(x, "%Y-%m-%d"),+ if (length(failed_mod_names)) { |
||
62 | -4x | +|||
167 | +! |
- Date = function(x) format(x, "%Y-%m-%d")+ stop( |
||
63 | -4x | +|||
168 | +! |
- )), "verbatim")+ sprintf( |
||
64 | -+ | |||
169 | +! |
- } else {+ "Some module names in the mapping arguments don't match module labels.\n %s not in %s", |
||
65 | +170 | ! |
- stop("yaml package is required to format the encodings list")+ toString(failed_mod_names), |
|
66 | -+ | |||
171 | +! |
- }+ toString(unique(module_names)) |
||
67 | -4x | +|||
172 | +
- self$append_metadata("Encodings", encodings)+ ) |
|||
68 | -4x | +|||
173 | +
- invisible(self)+ ) |
|||
69 | +174 |
} |
||
70 | +175 |
- ),+ |
||
71 | -+ | |||
176 | +! |
- private = list()+ if (anyDuplicated(module_names)) { |
||
72 | +177 |
- )+ # In teal we are able to set nested modules with duplicated label. |
||
73 | +178 |
-
+ # Because mapping argument bases on the relationship between module-label and filter-id, |
||
74 | +179 |
- #' @title `RcodeBlock`+ # it is possible that module-label in mapping might refer to multiple teal_module (identified by the same label) |
||
75 | -+ | |||
180 | +! |
- #' @keywords internal+ stop( |
||
76 | -+ | |||
181 | +! |
- TealSlicesBlock <- R6::R6Class( # nolint: object_name_linter.+ sprintf( |
||
77 | -+ | |||
182 | +! |
- classname = "TealSlicesBlock",+ "Module labels should be unique when teal_slices(mapping = TRUE). Duplicated labels:\n%s ", |
||
78 | -+ | |||
183 | +! |
- inherit = teal.reporter:::TextBlock,+ toString(module_names[duplicated(module_names)]) |
||
79 | +184 |
- public = list(+ ) |
||
80 | +185 |
- #' @description Returns a `TealSlicesBlock` object.+ ) |
||
81 | +186 |
- #'+ } |
||
82 | +187 |
- #' @details Returns a `TealSlicesBlock` object with no content and no parameters.+ } |
||
83 | +188 |
- #'+ |
||
84 | -+ | |||
189 | +24x |
- #' @param content (`teal_slices`) object returned from [teal_slices()] function.+ if (inherits(data, "teal_data")) { |
||
85 | -+ | |||
190 | +4x |
- #' @param style (`character(1)`) string specifying style to apply.+ if (length(teal.data::datanames(data)) == 0) { |
||
86 | -+ | |||
191 | +1x |
- #'+ stop("`data` object has no datanames. Specify `datanames(data)` and try again.") |
||
87 | +192 |
- #' @return `TealSlicesBlock`+ } |
||
88 | +193 |
- #' @examples+ |
||
89 | +194 |
- #' block <- teal:::TealSlicesBlock$new()+ # in case of teal_data_module this check is postponed to the srv_teal_with_splash |
||
90 | -+ | |||
195 | +3x |
- #'+ is_modules_ok <- check_modules_datanames(modules, teal.data::datanames(data)) |
||
91 | -+ | |||
196 | +3x |
- initialize = function(content = teal_slices(), style = "verbatim") {+ if (!isTRUE(is_modules_ok)) { |
||
92 | -10x | +197 | +1x |
- self$set_content(content)+ logger::log_error(is_modules_ok) |
93 | -9x | +198 | +1x |
- self$set_style(style)+ checkmate::assert(is_modules_ok, .var.name = "modules") |
94 | -9x | +|||
199 | +
- invisible(self)+ } |
|||
95 | +200 |
- },+ |
||
96 | +201 | |||
97 | -+ | |||
202 | +2x |
- #' @description Sets content of this `TealSlicesBlock`.+ is_filter_ok <- check_filter_datanames(filter, teal.data::datanames(data)) |
||
98 | -+ | |||
203 | +2x |
- #' Sets content as `YAML` text which represents a list generated from `teal_slices`.+ if (!isTRUE(is_filter_ok)) { |
||
99 | -+ | |||
204 | +1x |
- #' The list displays limited number of fields from `teal_slice` objects, but this list is+ logger::log_warn(is_filter_ok) |
||
100 | +205 |
- #' sufficient to conclude which filters were applied.+ # we allow app to continue if applied filters are outside |
||
101 | +206 |
- #' When `selected` field in `teal_slice` object is a range, then it is displayed as a "min"+ # of possible data range |
||
102 | +207 |
- #'+ } |
||
103 | +208 |
- #'+ } |
||
104 | +209 |
- #' @param content (`teal_slices`) object returned from [teal_slices()] function.+ |
||
105 | +210 |
- #' @return invisibly self+ # Note regarding case `id = character(0)`: |
||
106 | +211 |
- set_content = function(content) {+ # rather than using `callModule` and creating a submodule of this module, we directly modify |
||
107 | -11x | +|||
212 | +
- checkmate::assert_class(content, "teal_slices")+ # the `ui` and `server` with `id = character(0)` and calling the server function directly |
|||
108 | -10x | +|||
213 | +
- if (length(content) != 0) {+ # rather than through `callModule` |
|||
109 | -7x | +214 | +22x |
- states_list <- lapply(content, function(x) {+ res <- list( |
110 | -7x | +215 | +22x |
- x_list <- shiny::isolate(as.list(x))+ ui = ui_teal_with_splash(id = id, data = data, title = title, header = header, footer = footer), |
111 | -7x | +216 | +22x |
- if (+ server = function(input, output, session) { |
112 | -7x | +|||
217 | +! |
- inherits(x_list$choices, c("integer", "numeric", "Date", "POSIXct", "POSIXlt")) &&+ if (length(landing) == 1L) { |
||
113 | -7x | +|||
218 | +! |
- length(x_list$choices) == 2 &&+ landing_module <- landing[[1L]] |
||
114 | -7x | +|||
219 | +! |
- length(x_list$selected) == 2+ do.call(landing_module$server, c(list(id = "landing_module_shiny_id"), landing_module$server_args)) |
||
115 | +220 |
- ) {+ } |
||
116 | +221 | ! |
- x_list$range <- paste(x_list$selected, collapse = " - ")+ if (inherits(data, "TealDataAbstract")) {+ |
+ |
222 | ++ |
+ # copy TealData so that load won't be shared between the session |
||
117 | +223 | ! |
- x_list["selected"] <- NULL+ data <- data$copy(deep = TRUE) |
|
118 | +224 |
- }+ } |
||
119 | -7x | +|||
225 | +! |
- if (!is.null(x_list$arg)) {+ filter <- deep_copy_filter(filter) |
||
120 | +226 | ! |
- x_list$arg <- if (x_list$arg == "subset") "Genes" else "Samples"+ srv_teal_with_splash(id = id, data = data, modules = modules, filter = filter) |
|
121 | +227 |
- }+ } |
||
122 | +228 |
-
+ ) |
||
123 | -7x | +229 | +22x |
- x_list <- x_list[+ logger::log_trace("init teal app has been initialized.") |
124 | -7x | +230 | +22x |
- c("dataname", "varname", "experiment", "arg", "expr", "selected", "range", "keep_na", "keep_inf")+ return(res) |
125 | +231 |
- ]- |
- ||
126 | -7x | -
- names(x_list) <- c(- |
- ||
127 | -7x | -
- "Dataset name", "Variable name", "Experiment", "Filtering by", "Applied expression",- |
- ||
128 | -7x | -
- "Selected Values", "Selected range", "Include NA values", "Include Inf values"+ } |
129 | +1 |
- )+ #' Create a UI of nested tabs of `teal_modules` |
|
130 | +2 | - - | -|
131 | -7x | -
- Filter(Negate(is.null), x_list)+ #' |
|
132 | +3 |
- })+ #' @section `ui_nested_tabs`: |
|
133 | +4 | - - | -|
134 | -7x | -
- if (requireNamespace("yaml", quietly = TRUE)) {- |
- |
135 | -7x | -
- super$set_content(yaml::as.yaml(states_list))+ #' Each `teal_modules` is translated to a `tabsetPanel` and each |
|
136 | +5 |
- } else {- |
- |
137 | -! | -
- stop("yaml package is required to format the filter state list")+ #' of its children is another tab-module called recursively. The UI of a |
|
138 | +6 |
- }+ #' `teal_module` is obtained by calling the `ui` function on it. |
|
139 | +7 |
- }+ #' |
- |
140 | -10x | +||
8 | +
- private$teal_slices <- content+ #' The `datasets` argument is required to resolve the teal arguments in an |
||
141 | -10x | +||
9 | +
- invisible(self)+ #' isolated context (with respect to reactivity) |
||
142 | +10 |
- },+ #' |
|
143 | +11 |
- #' @description Create the `RcodeBlock` from a list.+ #' @section `srv_nested_tabs`: |
|
144 | +12 |
- #' @param x `named list` with two fields `c("text", "params")`.+ #' This module calls recursively all elements of the `modules` returns one which |
|
145 | +13 |
- #' Use the `get_available_params` method to get all possible parameters.+ #' is currently active. |
|
146 | +14 |
- #' @return invisibly self+ #' - `teal_module` returns self as a active module. |
|
147 | +15 |
- from_list = function(x) {+ #' - `teal_modules` also returns module active within self which is determined by the `input$active_tab`. |
|
148 | -1x | +||
16 | +
- checkmate::assert_list(x)+ #' |
||
149 | -1x | +||
17 | +
- checkmate::assert_names(names(x), must.include = c("teal_slices"))+ #' @name module_nested_tabs |
||
150 | -1x | +||
18 | +
- self$set_content(x$teal_slices)+ #' |
||
151 | -1x | +||
19 | +
- invisible(self)+ #' @inheritParams module_tabs_with_filters |
||
152 | +20 |
- },+ #' |
|
153 | +21 |
- #' @description Convert the `RcodeBlock` to a list.+ #' @param depth (`integer(1)`)\cr |
|
154 | +22 |
- #' @return `named list` with a text and `params`.+ #' number which helps to determine depth of the modules nesting. |
|
155 | +23 |
-
+ #' @param is_module_specific (`logical(1)`)\cr |
|
156 | +24 |
- to_list = function() {+ #' flag determining if the filter panel is global or module-specific. |
|
157 | -2x | +||
25 | +
- list(teal_slices = private$teal_slices)+ #' When set to `TRUE`, a filter panel is called inside of each module tab. |
||
158 | +26 |
- }+ #' @return depending on class of `modules`, `ui_nested_tabs` returns: |
|
159 | +27 |
- ),+ #' - `teal_module`: instantiated UI of the module |
|
160 | +28 |
- private = list(+ #' - `teal_modules`: `tabsetPanel` with each tab corresponding to recursively |
|
161 | +29 |
- style = "verbatim",+ #' calling this function on it.\cr |
|
162 | +30 |
- teal_slices = NULL # teal_slices+ #' `srv_nested_tabs` returns a reactive which returns the active module that corresponds to the selected tab. |
|
163 | +31 |
- )+ #' |
|
164 | +32 |
- )+ #' @examples |
1 | +33 |
- #' Send input validation messages to output.+ #' mods <- teal:::example_modules() |
||
2 | +34 |
- #'+ #' datasets <- teal:::example_datasets() |
||
3 | +35 |
- #' Captures messages from `InputValidator` objects and collates them+ #' app <- shinyApp( |
||
4 | +36 |
- #' into one message passed to `validate`.+ #' ui = function() { |
||
5 | +37 |
- #'+ #' tagList( |
||
6 | +38 |
- #' `shiny::validate` is used to withhold rendering of an output element until+ #' teal:::include_teal_css_js(), |
||
7 | +39 |
- #' certain conditions are met and to print a validation message in place+ #' textOutput("info"), |
||
8 | +40 |
- #' of the output element.+ #' fluidPage( # needed for nice tabs |
||
9 | +41 |
- #' `shinyvalidate::InputValidator` allows to validate input elements+ #' teal:::ui_nested_tabs("dummy", modules = mods, datasets = datasets) |
||
10 | +42 |
- #' and to display specific messages in their respective input widgets.+ #' ) |
||
11 | +43 |
- #' `validate_inputs` provides a hybrid solution.+ #' ) |
||
12 | +44 |
- #' Given an `InputValidator` object, messages corresponding to inputs that fail validation+ #' }, |
||
13 | +45 |
- #' are extracted and placed in one validation message that is passed to a `validate`/`need` call.+ #' server = function(input, output, session) { |
||
14 | +46 |
- #' This way the input `validator` messages are repeated in the output.+ #' active_module <- teal:::srv_nested_tabs( |
||
15 | +47 |
- #'+ #' "dummy", |
||
16 | +48 |
- #' The `...` argument accepts any number of `InputValidator` objects+ #' datasets = datasets, |
||
17 | +49 |
- #' or a nested list of such objects.+ #' modules = mods |
||
18 | +50 |
- #' If `validators` are passed directly, all their messages are printed together+ #' ) |
||
19 | +51 |
- #' under one (optional) header message specified by `header`. If a list is passed,+ #' output$info <- renderText({ |
||
20 | +52 |
- #' messages are grouped by `validator`. The list's names are used as headers+ #' paste0("The currently active tab name is ", active_module()$label) |
||
21 | +53 |
- #' for their respective message groups.+ #' }) |
||
22 | +54 |
- #' If neither of the nested list elements is named, a header message is taken from `header`.+ #' } |
||
23 | +55 |
- #'+ #' ) |
||
24 | +56 |
- #' @param ... either any number of `InputValidator` objects+ #' if (interactive()) { |
||
25 | +57 |
- #' or an optionally named, possibly nested `list` of `InputValidator`+ #' shinyApp(app$ui, app$server) |
||
26 | +58 |
- #' objects, see `Details`+ #' } |
||
27 | +59 |
- #' @param header `character(1)` generic validation message; set to NULL to omit+ #' @keywords internal |
||
28 | +60 |
- #'+ NULL |
||
29 | +61 |
- #' @return+ |
||
30 | +62 |
- #' Returns NULL if the final validation call passes and a `shiny.silent.error` if it fails.+ #' @rdname module_nested_tabs |
||
31 | +63 |
- #'+ ui_nested_tabs <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) { |
||
32 | -+ | |||
64 | +2x |
- #' @seealso [`shinyvalidate::InputValidator`], [`shiny::validate`]+ checkmate::assert_multi_class(modules, c("teal_modules", "teal_module")) |
||
33 | -+ | |||
65 | +2x |
- #'+ checkmate::assert_count(depth) |
||
34 | -+ | |||
66 | +2x |
- #' @examples+ UseMethod("ui_nested_tabs", modules) |
||
35 | +67 |
- #' library(shiny)+ } |
||
36 | +68 |
- #' library(shinyvalidate)+ |
||
37 | +69 |
- #'+ #' @rdname module_nested_tabs |
||
38 | +70 |
- #' ui <- fluidPage(+ #' @export |
||
39 | +71 |
- #' selectInput("method", "validation method", c("sequential", "combined", "grouped")),+ ui_nested_tabs.default <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) { |
||
40 | -+ | |||
72 | +! |
- #' sidebarLayout(+ stop("Modules class not supported: ", paste(class(modules), collapse = " ")) |
||
41 | +73 |
- #' sidebarPanel(+ } |
||
42 | +74 |
- #' selectInput("letter", "select a letter:", c(letters[1:3], LETTERS[4:6])),+ |
||
43 | +75 |
- #' selectInput("number", "select a number:", 1:6),+ #' @rdname module_nested_tabs |
||
44 | +76 |
- #' br(),+ #' @export |
||
45 | +77 |
- #' selectInput("color", "select a color:",+ ui_nested_tabs.teal_modules <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) { |
||
46 | -+ | |||
78 | +1x |
- #' c("black", "indianred2", "springgreen2", "cornflowerblue"),+ checkmate::assert_list(datasets, types = c("list", "FilteredData")) |
||
47 | -+ | |||
79 | +1x |
- #' multiple = TRUE+ ns <- NS(id) |
||
48 | -+ | |||
80 | +1x |
- #' ),+ do.call( |
||
49 | -+ | |||
81 | +1x |
- #' sliderInput("size", "select point size:",+ tabsetPanel, |
||
50 | -+ | |||
82 | +1x |
- #' min = 0.1, max = 4, value = 0.25+ c( |
||
51 | +83 |
- #' )+ # by giving an id, we can reactively respond to tab changes |
||
52 | -+ | |||
84 | +1x |
- #' ),+ list( |
||
53 | -+ | |||
85 | +1x |
- #' mainPanel(plotOutput("plot"))+ id = ns("active_tab"), |
||
54 | -+ | |||
86 | +1x |
- #' )+ type = if (modules$label == "root") "pills" else "tabs" |
||
55 | +87 |
- #' )+ ), |
||
56 | -+ | |||
88 | +1x |
- #'+ lapply( |
||
57 | -+ | |||
89 | +1x |
- #' server <- function(input, output) {+ names(modules$children), |
||
58 | -+ | |||
90 | +1x |
- #' # set up input validation+ function(module_id) { |
||
59 | -+ | |||
91 | +1x |
- #' iv <- InputValidator$new()+ module_label <- modules$children[[module_id]]$label |
||
60 | -+ | |||
92 | +1x |
- #' iv$add_rule("letter", sv_in_set(LETTERS, "choose a capital letter"))+ tabPanel( |
||
61 | -+ | |||
93 | +1x |
- #' iv$add_rule("number", ~ if (as.integer(.) %% 2L == 1L) "choose an even number")+ title = module_label, |
||
62 | -+ | |||
94 | +1x |
- #' iv$enable()+ value = module_id, # when clicked this tab value changes input$<tabset panel id> |
||
63 | -+ | |||
95 | +1x |
- #' # more input validation+ ui_nested_tabs( |
||
64 | -+ | |||
96 | +1x |
- #' iv_par <- InputValidator$new()+ id = ns(module_id), |
||
65 | -+ | |||
97 | +1x |
- #' iv_par$add_rule("color", sv_required(message = "choose a color"))+ modules = modules$children[[module_id]], |
||
66 | -+ | |||
98 | +1x |
- #' iv_par$add_rule("color", ~ if (length(.) > 1L) "choose only one color")+ datasets = datasets[[module_label]], |
||
67 | -+ | |||
99 | +1x |
- #' iv_par$add_rule(+ depth = depth + 1L, |
||
68 | -+ | |||
100 | +1x |
- #' "size",+ is_module_specific = is_module_specific |
||
69 | +101 |
- #' sv_between(+ ) |
||
70 | +102 |
- #' left = 0.5, right = 3,+ ) |
||
71 | +103 |
- #' message_fmt = "choose a value between {left} and {right}"+ } |
||
72 | +104 |
- #' )+ ) |
||
73 | +105 |
- #' )+ ) |
||
74 | +106 |
- #' iv_par$enable()+ ) |
||
75 | +107 |
- #'+ } |
||
76 | +108 |
- #' output$plot <- renderPlot({+ |
||
77 | +109 |
- #' # validate output+ #' @rdname module_nested_tabs |
||
78 | +110 |
- #' switch(input[["method"]],+ #' @export |
||
79 | +111 |
- #' "sequential" = {+ ui_nested_tabs.teal_module <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) { |
||
80 | -+ | |||
112 | +1x |
- #' validate_inputs(iv)+ checkmate::assert_class(datasets, classes = "FilteredData") |
||
81 | -+ | |||
113 | +1x |
- #' validate_inputs(iv_par, header = "Set proper graphical parameters")+ ns <- NS(id) |
||
82 | +114 |
- #' },+ |
||
83 | -+ | |||
115 | +1x |
- #' "combined" = validate_inputs(iv, iv_par),+ args <- isolate(teal.transform::resolve_delayed(modules$ui_args, datasets)) |
||
84 | -+ | |||
116 | +1x |
- #' "grouped" = validate_inputs(list(+ args <- c(list(id = ns("module")), args) |
||
85 | +117 |
- #' "Some inputs require attention" = iv,+ |
||
86 | -+ | |||
118 | +1x |
- #' "Set proper graphical parameters" = iv_par+ if (is_arg_used(modules$ui, "datasets")) { |
||
87 | -+ | |||
119 | +! |
- #' ))+ args <- c(args, datasets = datasets) |
||
88 | +120 |
- #' )+ } |
||
89 | +121 |
- #'+ |
||
90 | -+ | |||
122 | +1x |
- #' plot(eruptions ~ waiting, faithful,+ if (is_arg_used(modules$ui, "data")) { |
||
91 | -+ | |||
123 | +1x |
- #' las = 1, pch = 16,+ data <- .datasets_to_data(modules, datasets) |
||
92 | -+ | |||
124 | +1x |
- #' col = input[["color"]], cex = input[["size"]]+ args <- c(args, data = list(data)) |
||
93 | +125 |
- #' )+ } |
||
94 | +126 |
- #' })+ |
||
95 | -+ | |||
127 | +1x |
- #' }+ teal_ui <- tags$div( |
||
96 | -+ | |||
128 | +1x |
- #'+ id = id, |
||
97 | -+ | |||
129 | +1x |
- #' if (interactive()) {+ class = "teal_module", |
||
98 | -+ | |||
130 | +1x |
- #' shinyApp(ui, server)+ uiOutput(ns("data_reactive"), inline = TRUE), |
||
99 | -+ | |||
131 | +1x |
- #' }+ tagList( |
||
100 | -+ | |||
132 | +1x |
- #'+ if (depth >= 2L) div(style = "mt-6"),+ |
+ ||
133 | +1x | +
+ do.call(modules$ui, args) |
||
101 | +134 |
- #' @export+ ) |
||
102 | +135 |
- #'+ ) |
||
103 | +136 |
- validate_inputs <- function(..., header = "Some inputs require attention") {+ |
||
104 | -36x | +137 | +1x |
- dots <- list(...)+ if (!is.null(modules$datanames) && is_module_specific) { |
105 | -2x | +|||
138 | +! |
- if (!is_validators(dots)) stop("validate_inputs accepts validators or a list thereof")+ fluidRow( |
||
106 | -+ | |||
139 | +! |
-
+ column(width = 9, teal_ui, class = "teal_primary_col"), |
||
107 | -34x | +|||
140 | +! |
- messages <- extract_validator(dots, header)+ column( |
||
108 | -34x | +|||
141 | +! |
- failings <- if (!any_names(dots)) {+ width = 3, |
||
109 | -29x | +|||
142 | +! |
- add_header(messages, header)+ datasets$ui_filter_panel(ns("module_filter_panel")), |
||
110 | -+ | |||
143 | +! |
- } else {+ class = "teal_secondary_col" |
||
111 | -5x | +|||
144 | +
- unlist(messages)+ ) |
|||
112 | +145 |
- }+ ) |
||
113 | +146 |
-
+ } else { |
||
114 | -34x | +147 | +1x |
- shiny::validate(shiny::need(is.null(failings), failings))+ teal_ui |
115 | +148 |
- }+ } |
||
116 | +149 |
-
+ } |
||
117 | +150 |
- ### internal functions+ |
||
118 | +151 |
-
+ #' @rdname module_nested_tabs |
||
119 | +152 |
- #' @keywords internal+ srv_nested_tabs <- function(id, datasets, modules, is_module_specific = FALSE, |
||
120 | +153 |
- # recursive object type test+ reporter = teal.reporter::Reporter$new()) { |
||
121 | -+ | |||
154 | +54x |
- # returns logical of length 1+ checkmate::assert_multi_class(modules, c("teal_modules", "teal_module")) |
||
122 | -+ | |||
155 | +54x |
- is_validators <- function(x) {+ checkmate::assert_class(reporter, "Reporter") |
||
123 | -118x | +156 | +53x |
- all(if (is.list(x)) unlist(lapply(x, is_validators)) else inherits(x, "InputValidator"))+ UseMethod("srv_nested_tabs", modules) |
124 | +157 |
} |
||
125 | +158 | |||
126 | +159 |
- #' @keywords internal+ #' @rdname module_nested_tabs |
||
127 | +160 |
- # test if an InputValidator object is enabled+ #' @export |
||
128 | +161 |
- # returns logical of length 1+ srv_nested_tabs.default <- function(id, datasets, modules, is_module_specific = FALSE, |
||
129 | +162 |
- # official method requested at https://github.com/rstudio/shinyvalidate/issues/64+ reporter = teal.reporter::Reporter$new()) { |
||
130 | -+ | |||
163 | +! |
- validator_enabled <- function(x) {+ stop("Modules class not supported: ", paste(class(modules), collapse = " ")) |
||
131 | -49x | +|||
164 | +
- x$.__enclos_env__$private$enabled+ } |
|||
132 | +165 |
- }+ |
||
133 | +166 |
-
+ #' @rdname module_nested_tabs |
||
134 | +167 |
- #' @keywords internal+ #' @export |
||
135 | +168 |
- # recursively extract messages from validator list+ srv_nested_tabs.teal_modules <- function(id, datasets, modules, is_module_specific = FALSE, |
||
136 | +169 |
- # returns character vector or a list of character vectors, possibly nested and named+ reporter = teal.reporter::Reporter$new()) {+ |
+ ||
170 | +24x | +
+ checkmate::assert_list(datasets, types = c("list", "FilteredData")) |
||
137 | +171 |
- extract_validator <- function(iv, header) {+ |
||
138 | -113x | +172 | +24x |
- if (inherits(iv, "InputValidator")) {+ moduleServer(id = id, module = function(input, output, session) { |
139 | -49x | +173 | +24x |
- add_header(gather_messages(iv), header)+ logger::log_trace("srv_nested_tabs.teal_modules initializing the module { deparse1(modules$label) }.") |
140 | +174 |
- } else {+ |
||
141 | -58x | +175 | +24x |
- if (is.null(names(iv))) names(iv) <- rep("", length(iv))+ labels <- vapply(modules$children, `[[`, character(1), "label") |
142 | -64x | +176 | +24x |
- mapply(extract_validator, iv = iv, header = names(iv), SIMPLIFY = FALSE)+ modules_reactive <- sapply( |
143 | -+ | |||
177 | +24x |
- }+ names(modules$children), |
||
144 | -+ | |||
178 | +24x |
- }+ function(module_id) { |
||
145 | -+ | |||
179 | +35x |
-
+ srv_nested_tabs( |
||
146 | -+ | |||
180 | +35x |
- #' @keywords internal+ id = module_id, |
||
147 | -+ | |||
181 | +35x |
- # collate failing messages from validator+ datasets = datasets[[labels[module_id]]], |
||
148 | -+ | |||
182 | +35x |
- # returns list+ modules = modules$children[[module_id]],+ |
+ ||
183 | +35x | +
+ is_module_specific = is_module_specific,+ |
+ ||
184 | +35x | +
+ reporter = reporter |
||
149 | +185 |
- gather_messages <- function(iv) {+ ) |
||
150 | -49x | +|||
186 | +
- if (validator_enabled(iv)) {+ }, |
|||
151 | -46x | +187 | +24x |
- status <- iv$validate()+ simplify = FALSE |
152 | -46x | +|||
188 | +
- failing_inputs <- Filter(Negate(is.null), status)+ ) |
|||
153 | -46x | +|||
189 | +
- unique(lapply(failing_inputs, function(x) x[["message"]]))+ |
|||
154 | +190 |
- } else {+ # when not ready input$active_tab would return NULL - this would fail next reactive |
||
155 | -3x | +191 | +24x |
- logger::log_warn("Validator is disabled and will be omitted.")+ input_validated <- eventReactive(input$active_tab, input$active_tab, ignoreNULL = TRUE) |
156 | -3x | +192 | +24x |
- list()+ get_active_module <- reactive({ |
157 | -+ | |||
193 | +13x |
- }+ if (length(modules$children) == 1L) { |
||
158 | +194 |
- }+ # single tab is active by default |
||
159 | -+ | |||
195 | +2x |
-
+ modules_reactive[[1]]() |
||
160 | +196 |
- #' @keywords internal+ } else { |
||
161 | +197 |
- # add optional header to failing messages+ # switch to active tab+ |
+ ||
198 | +11x | +
+ modules_reactive[[input_validated()]]() |
||
162 | +199 |
- add_header <- function(messages, header = "") {+ } |
||
163 | -78x | +|||
200 | +
- ans <- unlist(messages)+ }) |
|||
164 | -78x | +|||
201 | +
- if (length(ans) != 0L && isTRUE(nchar(header) > 0L)) {+ |
|||
165 | -31x | +202 | +24x |
- ans <- c(paste0(header, "\n"), ans, "\n")+ get_active_module |
166 | +203 |
- }- |
- ||
167 | -78x | -
- ans+ }) |
||
168 | +204 |
} |
||
169 | +205 | |||
170 | +206 |
- #' @keywords internal+ #' @rdname module_nested_tabs |
||
171 | +207 |
- # recursively check if the object contains a named list+ #' @export |
||
172 | +208 |
- any_names <- function(x) {+ srv_nested_tabs.teal_module <- function(id, datasets, modules, is_module_specific = TRUE, |
||
173 | -103x | +|||
209 | +
- any(+ reporter = teal.reporter::Reporter$new()) { |
|||
174 | -103x | +210 | +29x |
- if (is.list(x)) {+ checkmate::assert_class(datasets, "FilteredData") |
175 | -58x | +211 | +29x |
- if (!is.null(names(x)) && any(names(x) != "")) TRUE else unlist(lapply(x, any_names))+ logger::log_trace("srv_nested_tabs.teal_module initializing the module: { deparse1(modules$label) }.") |
176 | +212 |
- } else {+ |
||
177 | -40x | +213 | +29x |
- FALSE+ moduleServer(id = id, module = function(input, output, session) {+ |
+
214 | +29x | +
+ modules$server_args <- teal.transform::resolve_delayed(modules$server_args, datasets)+ |
+ ||
215 | +29x | +
+ if (!is.null(modules$datanames) && is_module_specific) {+ |
+ ||
216 | +! | +
+ datasets$srv_filter_panel("module_filter_panel") |
||
178 | +217 |
} |
||
179 | +218 |
- )+ |
||
180 | +219 |
- }+ # Create two triggers to limit reactivity between filter-panel and modules. |
1 | +220 |
- #' Filter settings for teal applications+ # We want to recalculate only visible modules |
||
2 | +221 |
- #'+ # - trigger the data when the tab is selected |
||
3 | +222 |
- #' Specify initial filter states and filtering settings for a `teal` app.+ # - trigger module to be called when the tab is selected for the first time |
||
4 | -+ | |||
223 | +29x |
- #'+ trigger_data <- reactiveVal(1L) |
||
5 | -+ | |||
224 | +29x |
- #' Produces a `teal_slices` object.+ trigger_module <- reactiveVal(NULL) |
||
6 | -+ | |||
225 | +29x |
- #' The `teal_slice` components will specify filter states that will be active when the app starts.+ output$data_reactive <- renderUI({ |
||
7 | -+ | |||
226 | +18x |
- #' Attributes (created with the named arguments) will configure the way the app applies filters.+ lapply(datasets$datanames(), function(x) { |
||
8 | -+ | |||
227 | +22x |
- #' See argument descriptions for details.+ datasets$get_data(x, filtered = TRUE) |
||
9 | +228 |
- #'+ }) |
||
10 | -+ | |||
229 | +18x |
- #' @inheritParams teal.slice::teal_slices+ isolate(trigger_data(trigger_data() + 1))+ |
+ ||
230 | +18x | +
+ isolate(trigger_module(TRUE)) |
||
11 | +231 |
- #'+ + |
+ ||
232 | +18x | +
+ NULL |
||
12 | +233 |
- #' @param module_specific optional (`logical(1)`)\cr+ }) |
||
13 | +234 |
- #' - `FALSE` (default) when one filter panel applied to all modules.+ |
||
14 | +235 |
- #' All filters will be shared by all modules.+ # collect arguments to run teal_module |
||
15 | -+ | |||
236 | +29x |
- #' - `TRUE` when filter panel module-specific.+ args <- c(list(id = "module"), modules$server_args) |
||
16 | -+ | |||
237 | +29x |
- #' Modules can have different set of filters specified - see `mapping` argument.+ if (is_arg_used(modules$server, "reporter")) { |
||
17 | -+ | |||
238 | +! |
- #' @param mapping `r lifecycle::badge("experimental")` _This is a new feature. Do kindly share your opinions.\cr_+ args <- c(args, list(reporter = reporter)) |
||
18 | +239 |
- #' (`named list`)\cr+ } |
||
19 | +240 |
- #' Specifies which filters will be active in which modules on app start.+ |
||
20 | -+ | |||
241 | +29x |
- #' Elements should contain character vector of `teal_slice` `id`s (see [teal.slice::teal_slice()]).+ if (is_arg_used(modules$server, "datasets")) { |
||
21 | -+ | |||
242 | +2x |
- #' Names of the list should correspond to `teal_module` `label` set in [module()] function.+ args <- c(args, datasets = datasets) |
||
22 | +243 |
- #' `id`s listed under `"global_filters` will be active in all modules.+ } |
||
23 | +244 |
- #' If missing, all filters will be applied to all modules.+ |
||
24 | -+ | |||
245 | +29x |
- #' If empty list, all filters will be available to all modules but will start inactive.+ if (is_arg_used(modules$server, "data")) { |
||
25 | -+ | |||
246 | +9x |
- #' If `module_specific` is `FALSE`, only `global_filters` will be active on start.+ data <- .datasets_to_data(modules, datasets, trigger_data) |
||
26 | -+ | |||
247 | +9x |
- #' @param app_id (`character(1)`)\cr+ args <- c(args, data = list(data)) |
||
27 | +248 |
- #' For internal use only, do not set manually.+ } |
||
28 | +249 |
- #' Added by `init` so that a `teal_slices` can be matched to the app in which it was used.+ |
||
29 | -+ | |||
250 | +29x |
- #' Used for verifying snapshots uploaded from file. See `snapshot`.+ if (is_arg_used(modules$server, "filter_panel_api")) { |
||
30 | -+ | |||
251 | +2x |
- #'+ filter_panel_api <- teal.slice::FilterPanelAPI$new(datasets) |
||
31 | -+ | |||
252 | +2x |
- #' @param x (`list`) of lists to convert to `teal_slices`+ args <- c(args, filter_panel_api = filter_panel_api) |
||
32 | +253 |
- #'+ } |
||
33 | +254 |
- #' @return+ |
||
34 | -+ | |||
255 | +29x |
- #' A `teal_slices` object.+ if (is_arg_used(modules$server, "datasets") && is_arg_used(modules$server, "data")) { |
||
35 | -+ | |||
256 | +1x |
- #'+ warning( |
||
36 | -+ | |||
257 | +1x |
- #' @seealso [`teal.slice::teal_slices`], [`teal.slice::teal_slice`], [`slices_store`]+ "Module '", modules$label, "' has `data` and `datasets` arguments in the formals.", |
||
37 | -+ | |||
258 | +1x |
- #'+ "\nIt's recommended to use `data` to work with filtered objects." |
||
38 | +259 |
- #' @examples+ ) |
||
39 | +260 |
- #' filter <- teal_slices(+ } |
||
40 | +261 |
- #' teal.slice::teal_slice(dataname = "iris", varname = "Species", id = "species"),+ |
||
41 | +262 |
- #' teal.slice::teal_slice(dataname = "iris", varname = "Sepal.Length", id = "sepal_length"),+ # observe the trigger_module above to induce the module once the renderUI is triggered |
||
42 | -+ | |||
263 | +29x |
- #' teal.slice::teal_slice(+ observeEvent( |
||
43 | -+ | |||
264 | +29x |
- #' dataname = "iris", id = "long_petals", title = "Long petals", expr = "Petal.Length > 5"+ ignoreNULL = TRUE, |
||
44 | -+ | |||
265 | +29x |
- #' ),+ once = TRUE, |
||
45 | -+ | |||
266 | +29x |
- #' teal.slice::teal_slice(dataname = "mtcars", varname = "mpg", id = "mtcars_mpg"),+ eventExpr = trigger_module(), |
||
46 | -+ | |||
267 | +29x |
- #' mapping = list(+ handlerExpr = { |
||
47 | -+ | |||
268 | +18x |
- #' module1 = c("species", "sepal_length"),+ module_output <- if (is_arg_used(modules$server, "id")) { |
||
48 | -+ | |||
269 | +18x |
- #' module2 = c("mtcars_mpg"),+ do.call(modules$server, args) |
||
49 | +270 |
- #' global_filters = "long_petals"+ } else { |
||
50 | -+ | |||
271 | +! |
- #' )+ do.call(callModule, c(args, list(module = modules$server))) |
||
51 | +272 |
- #' )+ } |
||
52 | +273 |
- #'+ } |
||
53 | +274 |
- #' app <- teal::init(+ ) |
||
54 | +275 |
- #' modules = list(+ |
||
55 | -+ | |||
276 | +29x |
- #' module("module1"),+ reactive(modules) |
||
56 | +277 |
- #' module("module2")+ }) |
||
57 | +278 |
- #' ),+ } |
||
58 | +279 |
- #' data = list(iris, mtcars),+ |
||
59 | +280 |
- #' filter = filter+ #' Convert `FilteredData` to reactive list of datasets of the `tdata` type. |
||
60 | +281 |
- #' )+ #' |
||
61 | +282 |
- #'+ #' Converts `FilteredData` object to `tdata` object containing datasets needed for a specific module. |
||
62 | +283 |
- #' if (interactive()) {+ #' Please note that if module needs dataset which has a parent, then parent will be also returned. |
||
63 | +284 |
- #' shinyApp(app$ui, app$server)+ #' A hash per `dataset` is calculated internally and returned in the code. |
||
64 | +285 |
- #' }+ #' |
||
65 | +286 |
- #'+ #' @param module (`teal_module`) module where needed filters are taken from |
||
66 | +287 |
- #' @export+ #' @param datasets (`FilteredData`) object where needed data are taken from |
||
67 | +288 |
- teal_slices <- function(...,+ #' @param trigger_data (`reactiveVal`) to trigger getting the filtered data |
||
68 | +289 |
- exclude_varnames = NULL,+ #' @return list of reactive datasets with following attributes: |
||
69 | +290 |
- include_varnames = NULL,+ #' - `code` (`character`) containing datasets reproducible code. |
||
70 | +291 |
- count_type = NULL,+ #' - `join_keys` (`join_keys`) containing relationships between datasets. |
||
71 | +292 |
- allow_add = TRUE,+ #' - `metadata` (`list`) containing metadata of datasets. |
||
72 | +293 |
- module_specific = FALSE,+ #' |
||
73 | +294 |
- mapping,+ #' @keywords internal |
||
74 | +295 |
- app_id = NULL) {+ .datasets_to_data <- function(module, datasets, trigger_data = reactiveVal(1L)) { |
||
75 | -90x | +296 | +15x |
- shiny::isolate({+ checkmate::assert_class(module, "teal_module") |
76 | -90x | +297 | +15x |
- checkmate::assert_flag(allow_add)+ checkmate::assert_class(datasets, "FilteredData") |
77 | -90x | +298 | +15x |
- checkmate::assert_flag(module_specific)+ checkmate::assert_class(trigger_data, "reactiveVal")+ |
+
299 | ++ | + | ||
78 | -44x | +300 | +14x |
- if (!missing(mapping)) checkmate::assert_list(mapping, types = c("character", "NULL"), names = "named")+ datanames <- if (is.null(module$datanames) || identical(module$datanames, "all")) { |
79 | -87x | +301 | +5x |
- checkmate::assert_string(app_id, null.ok = TRUE)+ datasets$datanames() |
80 | +302 |
-
+ } else { |
||
81 | -87x | +303 | +9x |
- slices <- list(...)+ unique(module$datanames) # todo: include parents! unique shouldn't be needed here! |
82 | -87x | +|||
304 | +
- all_slice_id <- vapply(slices, `[[`, character(1L), "id")+ } |
|||
83 | +305 | |||
84 | -87x | +|||
306 | +
- if (missing(mapping)) {+ # list of reactive filtered data |
|||
85 | -46x | +307 | +14x |
- mapping <- list(global_filters = all_slice_id)+ data <- sapply( |
86 | -+ | |||
308 | +14x |
- }+ datanames, |
||
87 | -87x | +309 | +14x |
- if (!module_specific) {+ function(x) eventReactive(trigger_data(), datasets$get_data(x, filtered = TRUE)), |
88 | -83x | +310 | +14x |
- mapping[setdiff(names(mapping), "global_filters")] <- NULL+ simplify = FALSE |
89 | +311 |
- }+ ) |
||
90 | +312 | |||
91 | -87x | +313 | +14x |
- failed_slice_id <- setdiff(unlist(mapping), all_slice_id)+ hashes <- calculate_hashes(datanames, datasets) |
92 | -87x | +314 | +14x |
- if (length(failed_slice_id)) {+ metadata <- sapply(datanames, datasets$get_metadata, simplify = FALSE) |
93 | -1x | +|||
315 | +
- stop(sprintf(+ |
|||
94 | -1x | +316 | +14x |
- "Filters in mapping don't match any available filter.\n %s not in %s",+ new_tdata( |
95 | -1x | +317 | +14x |
- toString(failed_slice_id),+ data, |
96 | -1x | -
- toString(all_slice_id)- |
- ||
97 | -- |
- ))- |
- ||
98 | -- |
- }- |
- ||
99 | -+ | 318 | +14x |
-
+ eventReactive( |
100 | -86x | +319 | +14x |
- tss <- teal.slice::teal_slices(+ trigger_data(), |
101 | +320 |
- ...,+ { |
||
102 | -86x | +321 | +14x |
- exclude_varnames = exclude_varnames,+ c( |
103 | -86x | +322 | +14x |
- include_varnames = include_varnames,+ get_rcode_str_install(), |
104 | -86x | +323 | +14x |
- count_type = count_type,+ get_rcode_libraries(), |
105 | -86x | +324 | +14x |
- allow_add = allow_add+ get_datasets_code(datanames, datasets, hashes) |
106 | +325 |
- )- |
- ||
107 | -86x | -
- attr(tss, "mapping") <- mapping+ ) |
||
108 | -86x | +|||
326 | +
- attr(tss, "module_specific") <- module_specific+ } |
|||
109 | -86x | +|||
327 | +
- attr(tss, "app_id") <- app_id+ ), |
|||
110 | -86x | +328 | +14x |
- class(tss) <- c("modules_teal_slices", class(tss))+ datasets$get_join_keys(), |
111 | -86x | +329 | +14x |
- tss+ metadata |
112 | +330 |
- })+ ) |
||
113 | +331 |
} |
||
114 | +332 | |||
115 | +333 |
-
+ #' Get the hash of a dataset |
||
116 | +334 |
- #' @rdname teal_slices+ #' |
||
117 | +335 |
- #' @export+ #' @param datanames (`character`) names of datasets |
||
118 | +336 |
- #' @keywords internal+ #' @param datasets (`FilteredData`) object holding the data |
||
119 | +337 |
#' |
||
120 | +338 |
- as.teal_slices <- function(x) { # nolint+ #' @return A list of hashes per dataset |
||
121 | -25x | +|||
339 | +
- checkmate::assert_list(x)+ #' @keywords internal |
|||
122 | -25x | +|||
340 | +
- lapply(x, checkmate::assert_list, names = "named", .var.name = "list element")+ #' |
|||
123 | +341 |
-
+ calculate_hashes <- function(datanames, datasets) { |
||
124 | -25x | +342 | +17x |
- attrs <- attributes(unclass(x))+ sapply(datanames, function(x) rlang::hash(datasets$get_data(x, filtered = FALSE)), simplify = FALSE) |
125 | -25x | +|||
343 | +
- ans <- lapply(x, function(x) if (is.teal_slice(x)) x else as.teal_slice(x))+ } |
|||
126 | -25x | +
1 | +
- do.call(teal_slices, c(ans, attrs))+ #' Add right filter panel into each of the top-level `teal_modules` UIs. |
||
127 | +2 |
- }+ #' |
|
128 | +3 |
-
+ #' The [ui_nested_tabs] function returns a nested tabbed UI corresponding |
|
129 | +4 |
-
+ #' to the nested modules. |
|
130 | +5 |
- #' @rdname teal_slices+ #' This function adds the right filter panel to each main tab. |
|
131 | +6 |
- #' @export+ #' |
|
132 | +7 |
- #' @keywords internal+ #' The right filter panel's filter choices affect the `datasets` object. Therefore, |
|
133 | +8 | ++ |
+ #' all modules using the same `datasets` share the same filters.+ |
+
9 |
#' |
||
134 | +10 |
- c.teal_slices <- function(...) {+ #' This works with nested modules of depth greater than 2, though the filter |
|
135 | -! | +||
11 | +
- x <- list(...)+ #' panel is inserted at the right of the modules at depth 1 and not at the leaves. |
||
136 | -! | +||
12 | +
- checkmate::assert_true(all(vapply(x, is.teal_slices, logical(1L))), .var.name = "all arguments are teal_slices")+ #' |
||
137 | +13 |
-
+ #' @name module_tabs_with_filters |
|
138 | -! | +||
14 | +
- all_attributes <- lapply(x, attributes)+ #' |
||
139 | -! | +||
15 | +
- all_attributes <- coalesce_r(all_attributes)+ #' @inheritParams module_teal |
||
140 | -! | +||
16 | +
- all_attributes <- all_attributes[names(all_attributes) != "class"]+ #' |
||
141 | +17 |
-
+ #' @param datasets (`named list` of `FilteredData`)\cr |
|
142 | -! | +||
18 | +
- do.call(+ #' object to store filter state and filtered datasets, shared across modules. For more |
||
143 | -! | +||
19 | +
- teal_slices,+ #' details see [`teal.slice::FilteredData`]. Structure of the list must be the same as structure |
||
144 | -! | +||
20 | +
- c(+ #' of the `modules` argument and list names must correspond to the labels in `modules`. |
||
145 | -! | +||
21 | +
- unique(unlist(x, recursive = FALSE)),+ #' When filter is not module-specific then list contains the same object in all elements. |
||
146 | -! | +||
22 | +
- all_attributes+ #' @param reporter (`Reporter`) object from `teal.reporter` |
||
147 | +23 |
- )+ #' |
|
148 | +24 |
- )+ #' @return A `tagList` of The main menu, place holders for filters and |
|
149 | +25 |
- }+ #' place holders for the teal modules |
|
150 | +26 |
-
+ #' |
|
151 | +27 |
-
+ #' |
|
152 | +28 |
- #' Deep copy `teal_slices`+ #' @keywords internal |
|
153 | +29 |
#' |
|
154 | +30 |
- #' it's important to create a new copy of `teal_slices` when+ #' @examples |
|
155 | +31 |
- #' starting a new `shiny` session. Otherwise, object will be shared+ #' |
|
156 | +32 |
- #' by multiple users as it is created in global environment before+ #' mods <- teal:::example_modules() |
|
157 | +33 |
- #' `shiny` session starts.+ #' datasets <- teal:::example_datasets() |
|
158 | +34 |
- #' @param filter (`teal_slices`)+ #' |
|
159 | +35 |
- #' @return `teal_slices`+ #' app <- shinyApp( |
|
160 | +36 |
- #' @keywords internal+ #' ui = function() { |
|
161 | +37 |
- deep_copy_filter <- function(filter) {+ #' tagList( |
|
162 | -1x | +||
38 | +
- checkmate::assert_class(filter, "teal_slices")+ #' teal:::include_teal_css_js(), |
||
163 | -1x | +||
39 | +
- shiny::isolate({+ #' textOutput("info"), |
||
164 | -1x | +||
40 | +
- filter_copy <- lapply(filter, function(slice) {+ #' fluidPage( # needed for nice tabs |
||
165 | -2x | +||
41 | +
- teal.slice::as.teal_slice(as.list(slice))+ #' ui_tabs_with_filters("dummy", modules = mods, datasets = datasets) |
||
166 | +42 |
- })+ #' ) |
|
167 | -1x | +||
43 | +
- attributes(filter_copy) <- attributes(filter)+ #' ) |
||
168 | -1x | +||
44 | +
- filter_copy+ #' }, |
||
169 | +45 |
- })+ #' server = function(input, output, session) { |
|
170 | +46 |
- }+ #' output$info <- renderText({ |
1 | +47 |
- #' Creates a `teal_modules` object.+ #' paste0("The currently active tab name is ", active_module()$label) |
||
2 | +48 |
- #'+ #' }) |
||
3 | +49 |
- #' @description `r lifecycle::badge("stable")`+ #' active_module <- srv_tabs_with_filters(id = "dummy", datasets = datasets, modules = mods) |
||
4 | +50 |
- #' This function collects a list of `teal_modules` and `teal_module` objects and returns a `teal_modules` object+ #' } |
||
5 | +51 |
- #' containing the passed objects.+ #' ) |
||
6 | +52 |
- #'+ #' if (interactive()) { |
||
7 | +53 |
- #' This function dictates what modules are included in a `teal` application. The internal structure of `teal_modules`+ #' shinyApp(app$ui, app$server) |
||
8 | +54 |
- #' shapes the navigation panel of a `teal` application.+ #' } |
||
9 | +55 |
#' |
||
10 | +56 |
- #' @param ... (`teal_module` or `teal_modules`) see [module()] and [modules()] for more details+ NULL |
||
11 | +57 |
- #' @param label (`character(1)`) label of modules collection (default `"root"`).+ |
||
12 | +58 |
- #' If using the `label` argument then it must be explicitly named.+ #' @rdname module_tabs_with_filters |
||
13 | +59 |
- #' For example `modules("lab", ...)` should be converted to `modules(label = "lab", ...)`+ ui_tabs_with_filters <- function(id, modules, datasets, filter = teal_slices()) { |
||
14 | -+ | |||
60 | +1x |
- #'+ checkmate::assert_class(modules, "teal_modules") |
||
15 | -+ | |||
61 | +1x |
- #' @export+ checkmate::assert_list(datasets, types = c("list", "FilteredData")) |
||
16 | -+ | |||
62 | +1x |
- #'+ checkmate::assert_class(filter, "teal_slices") |
||
17 | +63 |
- #' @return object of class \code{teal_modules}. Object contains following fields+ |
||
18 | -+ | |||
64 | +1x |
- #' - `label`: taken from the `label` argument+ ns <- NS(id) |
||
19 | -+ | |||
65 | +1x |
- #' - `children`: a list containing objects passed in `...`. List elements are named after+ is_module_specific <- isTRUE(attr(filter, "module_specific")) |
||
20 | +66 |
- #' their `label` attribute converted to a valid `shiny` id.+ |
||
21 | -+ | |||
67 | +1x |
- #' @examples+ teal_ui <- ui_nested_tabs(ns("root"), modules = modules, datasets, is_module_specific = is_module_specific) |
||
22 | -+ | |||
68 | +1x |
- #' library(shiny)+ filter_panel_btns <- tags$li( |
||
23 | -+ | |||
69 | +1x |
- #'+ class = "flex-grow", |
||
24 | -+ | |||
70 | +1x |
- #' app <- init(+ tags$button(+ |
+ ||
71 | +1x | +
+ class = "btn action-button filter_hamburger", # see sidebar.css for style filter_hamburger |
||
25 | -+ | |||
72 | +1x |
- #' data = teal_data(dataset("iris", iris)),+ href = "javascript:void(0)", |
||
26 | -+ | |||
73 | +1x |
- #' modules = modules(+ onclick = "toggleFilterPanel();", # see sidebar.js |
||
27 | -+ | |||
74 | +1x |
- #' label = "Modules",+ title = "Toggle filter panels", |
||
28 | -+ | |||
75 | +1x |
- #' modules(+ icon("fas fa-bars") |
||
29 | +76 |
- #' label = "Module",+ ), |
||
30 | -+ | |||
77 | +1x |
- #' module(+ filter_manager_modal_ui(ns("filter_manager")) |
||
31 | +78 |
- #' label = "Inner module",+ ) |
||
32 | -+ | |||
79 | +1x |
- #' server = function(id, data) {+ teal_ui$children[[1]] <- tagAppendChild(teal_ui$children[[1]], filter_panel_btns) |
||
33 | +80 |
- #' moduleServer(+ |
||
34 | -+ | |||
81 | +1x |
- #' id,+ if (!is_module_specific) { |
||
35 | +82 |
- #' module = function(input, output, session) {+ # need to rearrange html so that filter panel is within tabset |
||
36 | -+ | |||
83 | +1x |
- #' output$data <- renderDataTable(data[["iris"]]())+ tabset_bar <- teal_ui$children[[1]] |
||
37 | -+ | |||
84 | +1x |
- #' }+ teal_modules <- teal_ui$children[[2]] |
||
38 | -+ | |||
85 | +1x |
- #' )+ filter_ui <- unlist(datasets)[[1]]$ui_filter_panel(ns("filter_panel")) |
||
39 | -+ | |||
86 | +1x |
- #' },+ list( |
||
40 | -+ | |||
87 | +1x |
- #' ui = function(id) {+ tabset_bar, |
||
41 | -+ | |||
88 | +1x |
- #' ns <- NS(id)+ tags$hr(class = "my-2"), |
||
42 | -+ | |||
89 | +1x |
- #' tagList(dataTableOutput(ns("data")))+ fluidRow( |
||
43 | -+ | |||
90 | +1x |
- #' },+ column(width = 9, teal_modules, class = "teal_primary_col"), |
||
44 | -+ | |||
91 | +1x |
- #' datanames = "all"+ column(width = 3, filter_ui, class = "teal_secondary_col") |
||
45 | +92 |
- #' )+ ) |
||
46 | +93 |
- #' ),+ ) |
||
47 | +94 |
- #' module(+ } else { |
||
48 | -+ | |||
95 | +! |
- #' label = "Another module",+ teal_ui |
||
49 | +96 |
- #' server = function(id) {+ } |
||
50 | +97 |
- #' moduleServer(+ } |
||
51 | +98 |
- #' id,+ |
||
52 | +99 |
- #' module = function(input, output, session) {+ #' @rdname module_tabs_with_filters |
||
53 | +100 |
- #' output$text <- renderText("Another module")+ srv_tabs_with_filters <- function(id, |
||
54 | +101 |
- #' }+ datasets, |
||
55 | +102 |
- #' )+ modules, |
||
56 | +103 |
- #' },+ reporter = teal.reporter::Reporter$new(), |
||
57 | +104 |
- #' ui = function(id) {+ filter = teal_slices()) { |
||
58 | -+ | |||
105 | +6x |
- #' ns <- NS(id)+ checkmate::assert_class(modules, "teal_modules") |
||
59 | -+ | |||
106 | +6x |
- #' tagList(textOutput(ns("text")))+ checkmate::assert_list(datasets, types = c("list", "FilteredData")) |
||
60 | -+ | |||
107 | +6x |
- #' },+ checkmate::assert_class(reporter, "Reporter") |
||
61 | -+ | |||
108 | +4x |
- #' datanames = NULL+ checkmate::assert_class(filter, "teal_slices") |
||
62 | +109 |
- #' )+ |
||
63 | -+ | |||
110 | +4x |
- #' )+ moduleServer(id, function(input, output, session) { |
||
64 | -+ | |||
111 | +4x |
- #' )+ logger::log_trace("srv_tabs_with_filters initializing the module.") |
||
65 | +112 |
- #' if (interactive()) {+ |
||
66 | -+ | |||
113 | +4x |
- #' shinyApp(app$ui, app$server)+ is_module_specific <- isTRUE(attr(filter, "module_specific")) |
||
67 | -+ | |||
114 | +4x |
- #' }+ manager_out <- filter_manager_modal_srv("filter_manager", filtered_data_list = datasets, filter = filter) |
||
68 | +115 |
- modules <- function(..., label = "root") {+ |
||
69 | -140x | +116 | +4x |
- checkmate::assert_string(label)+ active_module <- srv_nested_tabs( |
70 | -138x | +117 | +4x |
- submodules <- list(...)+ id = "root", |
71 | -138x | +118 | +4x |
- if (any(vapply(submodules, is.character, FUN.VALUE = logical(1)))) {+ datasets = datasets, |
72 | -2x | +119 | +4x |
- stop(+ modules = modules, |
73 | -2x | +120 | +4x |
- "The only character argument to modules() must be 'label' and it must be named, ",+ reporter = reporter, |
74 | -2x | +121 | +4x |
- "change modules('lab', ...) to modules(label = 'lab', ...)"+ is_module_specific = is_module_specific |
75 | +122 |
) |
||
76 | +123 |
- }+ |
||
77 | -+ | |||
124 | +4x |
-
+ if (!is_module_specific) { |
||
78 | -136x | +125 | +4x |
- checkmate::assert_list(submodules, min.len = 1, any.missing = FALSE, types = c("teal_module", "teal_modules"))+ active_datanames <- reactive({ |
79 | -+ | |||
126 | +7x |
- # name them so we can more easily access the children+ if (identical(active_module()$datanames, "all")) { |
||
80 | -+ | |||
127 | +1x |
- # beware however that the label of the submodules should not be changed as it must be kept synced+ singleton$datanames() |
||
81 | -133x | +|||
128 | +
- labels <- vapply(submodules, function(submodule) submodule$label, character(1))+ } else { |
|||
82 | -133x | +129 | +5x |
- names(submodules) <- make.unique(gsub("[^[:alnum:]]+", "_", labels), sep = "_")+ active_module()$datanames |
83 | -133x | +|||
130 | +
- structure(+ } |
|||
84 | -133x | +|||
131 | +
- list(+ }) |
|||
85 | -133x | +132 | +4x |
- label = label,+ singleton <- unlist(datasets)[[1]] |
86 | -133x | +133 | +4x |
- children = submodules+ singleton$srv_filter_panel("filter_panel", active_datanames = active_datanames) |
87 | +134 |
- ),+ |
||
88 | -133x | +135 | +4x |
- class = "teal_modules"+ observeEvent( |
89 | -+ | |||
136 | +4x |
- )+ eventExpr = active_datanames(), |
||
90 | -+ | |||
137 | +4x |
- }+ handlerExpr = { |
||
91 | -+ | |||
138 | +5x |
-
+ script <- if (length(active_datanames()) == 0 || is.null(active_datanames())) { |
||
92 | +139 |
- #' Append a `teal_module` to `children` of a `teal_modules` object+ # hide the filter panel and disable the burger button |
||
93 | -+ | |||
140 | +! |
- #' @keywords internal+ "handleNoActiveDatasets();" |
||
94 | +141 |
- #' @param modules `teal_modules`+ } else { |
||
95 | +142 |
- #' @param module `teal_module` object to be appended onto the children of `modules`+ # show the filter panel and enable the burger button |
||
96 | -+ | |||
143 | +5x |
- #' @return `teal_modules` object with `module` appended+ "handleActiveDatasetsPresent();" |
||
97 | +144 |
- append_module <- function(modules, module) {- |
- ||
98 | -7x | -
- checkmate::assert_class(modules, "teal_modules")+ } |
||
99 | +145 | 5x |
- checkmate::assert_class(module, "teal_module")- |
- |
100 | -3x | -
- modules$children <- c(modules$children, list(module))+ shinyjs::runjs(script) |
||
101 | -3x | +|||
146 | +
- labels <- vapply(modules$children, function(submodule) submodule$label, character(1))+ }, |
|||
102 | -3x | +147 | +4x |
- names(modules$children) <- make.unique(gsub("[^[:alnum:]]", "_", tolower(labels)), sep = "_")+ ignoreNULL = FALSE |
103 | -3x | +|||
148 | +
- modules+ ) |
|||
104 | +149 |
- }+ } |
||
105 | +150 | |||
106 | -+ | |||
151 | +4x | +
+ showNotification("Data loaded - App fully started up")+ |
+ ||
152 | +4x | +
+ logger::log_trace("srv_tabs_with_filters initialized the module")+ |
+ ||
153 | +4x |
- #' Extract/Remove module(s) of specific class+ return(active_module) |
||
107 | +154 |
- #'+ }) |
||
108 | +155 |
- #' Given a `teal_module` or a `teal_modules`, return the elements of the structure according to `class`.+ } |
109 | +1 |
- #'+ # This file adds a splash screen for delayed data loading on top of teal |
||
110 | +2 |
- #' @param modules `teal_modules`+ |
||
111 | +3 |
- #' @param class The class name of `teal_module` to be extracted or dropped.+ #' UI to show a splash screen in the beginning, then delegate to [srv_teal()] |
||
112 | +4 |
- #' @keywords internal+ #' |
||
113 | +5 |
- #' @return+ #' @description `r lifecycle::badge("stable")` |
||
114 | +6 |
- #' For `extract_module`, a `teal_module` of class `class` or `teal_modules` containing modules of class `class`.+ #' The splash screen could be used to query for a password to fetch the data. |
||
115 | +7 |
- #' For `drop_module`, the opposite, which is all `teal_modules` of class other than `class`.+ #' [init()] is a very thin wrapper around this module useful for end-users which |
||
116 | +8 |
- #' @rdname module_management+ #' assumes that it is a top-level module and cannot be embedded. |
||
117 | +9 |
- extract_module <- function(modules, class) {- |
- ||
118 | -68x | -
- if (inherits(modules, class)) {- |
- ||
119 | -! | -
- modules- |
- ||
120 | -68x | -
- } else if (inherits(modules, "teal_module")) {+ #' This function instead adheres to the Shiny module conventions. |
||
121 | -38x | +|||
10 | +
- NULL+ #' |
|||
122 | -30x | +|||
11 | +
- } else if (inherits(modules, "teal_modules")) {+ #' If data is obtained through delayed loading, its splash screen is used. Otherwise, |
|||
123 | -30x | +|||
12 | +
- Filter(function(x) length(x) > 0L, lapply(modules$children, extract_module, class))+ #' a default splash screen is shown. |
|||
124 | +13 |
- }+ #' |
||
125 | +14 |
- }+ #' Please also refer to the doc of [init()]. |
||
126 | +15 |
-
+ #' |
||
127 | +16 |
- #' @keywords internal+ #' @param id (`character(1)`)\cr |
||
128 | +17 |
- #' @return `teal_modules`+ #' module id |
||
129 | +18 |
- #' @rdname module_management+ #' @inheritParams init |
||
130 | +19 |
- drop_module <- function(modules, class) {+ #' @export |
||
131 | -68x | +|||
20 | +
- if (inherits(modules, class)) {+ ui_teal_with_splash <- function(id, |
|||
132 | -! | +|||
21 | +
- NULL+ data, |
|||
133 | -68x | +|||
22 | +
- } else if (inherits(modules, "teal_module")) {+ title, |
|||
134 | -38x | +|||
23 | +
- modules+ header = tags$p("Add Title Here"), |
|||
135 | -30x | +|||
24 | +
- } else if (inherits(modules, "teal_modules")) {+ footer = tags$p("Add Footer Here")) { |
|||
136 | -30x | +25 | +22x |
- do.call(+ checkmate::assert_multi_class(data, c("TealData", "teal_data", "teal_data_module")) |
137 | -30x | +26 | +22x |
- "modules",+ ns <- NS(id) |
138 | -30x | +|||
27 | +
- c(Filter(function(x) length(x) > 0L, lapply(modules$children, drop_module, class)), label = modules$label)+ |
|||
139 | +28 |
- )+ # Startup splash screen for delayed loading |
||
140 | +29 |
- }+ # We use delayed loading in all cases, even when the data does not need to be fetched. |
||
141 | +30 |
- }+ # This has the benefit that when filtering the data takes a lot of time initially, the |
||
142 | +31 |
-
+ # Shiny app does not time out. |
||
143 | +32 |
- #' Does the object make use of the `arg`+ |
||
144 | -+ | |||
33 | +22x |
- #'+ splash_ui <- if (inherits(data, "teal_data_module")) { |
||
145 | -+ | |||
34 | +1x |
- #' @param modules (`teal_module` or `teal_modules`) object+ data$ui(ns("teal_data_module")) |
||
146 | -+ | |||
35 | +22x |
- #' @param arg (`character(1)`) names of the arguments to be checked against formals of `teal` modules.+ } else if (inherits(data, "teal_data")) { |
||
147 | -+ | |||
36 | +2x |
- #' @return `logical` whether the object makes use of `arg`+ div() |
||
148 | -+ | |||
37 | +22x |
- #' @rdname is_arg_used+ } else if (inherits(data, "TealDataAbstract") && teal.data::is_pulled(data)) { |
||
149 | -+ | |||
38 | +17x |
- #' @keywords internal+ div() |
||
150 | +39 |
- is_arg_used <- function(modules, arg) {+ } else { |
||
151 | -320x | +40 | +2x |
- checkmate::assert_string(arg)+ message("App was initialized with delayed data loading.") |
152 | -317x | +41 | +2x |
- if (inherits(modules, "teal_modules")) {+ data$get_ui(ns("startapp_module")) |
153 | -27x | +|||
42 | +
- any(unlist(lapply(modules$children, is_arg_used, arg)))+ } |
|||
154 | -290x | +43 | +22x |
- } else if (inherits(modules, "teal_module")) {+ ui_teal( |
155 | -41x | +44 | +22x |
- is_arg_used(modules$server, arg) || is_arg_used(modules$ui, arg)+ id = ns("teal"), |
156 | -249x | +45 | +22x |
- } else if (is.function(modules)) {+ splash_ui = div(splash_ui, uiOutput(ns("error"))), |
157 | -247x | +46 | +22x |
- isTRUE(arg %in% names(formals(modules)))+ title = title, |
158 | -+ | |||
47 | +22x |
- } else {+ header = header, |
||
159 | -2x | +48 | +22x |
- stop("is_arg_used function not implemented for this object")+ footer = footer |
160 | +49 |
- }+ ) |
||
161 | +50 |
} |
||
162 | +51 | |||
163 | +52 |
-
+ #' Server function that loads the data through reactive loading and then delegates |
||
164 | +53 |
- #' Creates a `teal_module` object.+ #' to [srv_teal()]. |
||
165 | +54 |
#' |
||
166 | +55 |
#' @description `r lifecycle::badge("stable")` |
||
167 | +56 |
- #' This function embeds a `shiny` module inside a `teal` application. One `teal_module` maps to one `shiny` module.+ #' Please also refer to the doc of [init()]. |
||
168 | +57 |
#' |
||
169 | +58 |
- #' @param label (`character(1)`) Label shown in the navigation item for the module. Any label possible except+ #' @inheritParams init |
||
170 | +59 |
- #' `"global_filters"` - read more in `mapping` argument of [teal::teal_slices].+ #' @param modules `teal_modules` object containing the output modules which |
||
171 | +60 |
- #' @param server (`function`) `shiny` module with following arguments:+ #' will be displayed in the teal application. See [modules()] and [module()] for |
||
172 | +61 |
- #' - `id` - teal will set proper shiny namespace for this module (see [shiny::moduleServer()]).+ #' more details. |
||
173 | +62 |
- #' - `input`, `output`, `session` - (not recommended) then [shiny::callModule()] will be used to call a module.+ #' @inheritParams shiny::moduleServer |
||
174 | +63 |
- #' - `data` (optional) module will receive a `tdata` object, a list of reactive (filtered) data specified in+ #' @return `reactive` containing `teal_data` object when data is loaded. |
||
175 | +64 |
- #' the `filters` argument.+ #' If data is not loaded yet, `reactive` returns `NULL`. |
||
176 | +65 |
- #' - `datasets` (optional) module will receive `FilteredData`. (See `[teal.slice::FilteredData]`).+ #' @export |
||
177 | +66 |
- #' - `reporter` (optional) module will receive `Reporter`. (See [teal.reporter::Reporter]).+ srv_teal_with_splash <- function(id, data, modules, filter = teal_slices()) { |
||
178 | -+ | |||
67 | +17x |
- # - `filter_panel_api` (optional) module will receive `FilterPanelAPI`. (See [teal.slice::FilterPanelAPI]).+ checkmate::check_multi_class(data, c("TealData", "teal_data", "teal_data_module")) |
||
179 | +68 |
- #' - `...` (optional) `server_args` elements will be passed to the module named argument or to the `...`.+ + |
+ ||
69 | +17x | +
+ moduleServer(id, function(input, output, session) {+ |
+ ||
70 | +17x | +
+ logger::log_trace("srv_teal_with_splash initializing module with data.") |
||
180 | +71 |
- #' @param ui (`function`) Shiny `ui` module function with following arguments:+ + |
+ ||
72 | +17x | +
+ if (getOption("teal.show_js_log", default = FALSE)) {+ |
+ ||
73 | +! | +
+ shinyjs::showLog() |
||
181 | +74 |
- #' - `id` - teal will set proper shiny namespace for this module.+ } |
||
182 | +75 |
- #' - `data` (optional) module will receive list of reactive (filtered) data specified in the `filters` argument.+ |
||
183 | +76 |
- #' - `datasets` (optional) module will receive `FilteredData`. (See `[teal.slice::FilteredData]`).+ # teal_data_rv contains teal_data object |
||
184 | +77 |
- #' - `...` (optional) `ui_args` elements will be passed to the module named argument or to the `...`.+ # either passed to teal::init or returned from teal_data_module+ |
+ ||
78 | +17x | +
+ teal_data_rv <- if (inherits(data, "teal_data_module")) {+ |
+ ||
79 | +10x | +
+ data <- data$server(id = "teal_data_module")+ |
+ ||
80 | +10x | +
+ if (!is.reactive(data)) {+ |
+ ||
81 | +1x | +
+ stop("The `teal_data_module` must return a reactive expression.", call. = FALSE) |
||
185 | +82 |
- #' @param filters (`character`) Deprecated. Use `datanames` instead.+ }+ |
+ ||
83 | +9x | +
+ data+ |
+ ||
84 | +17x | +
+ } else if (inherits(data, "teal_data")) {+ |
+ ||
85 | +5x | +
+ reactiveVal(data)+ |
+ ||
86 | +17x | +
+ } else if (inherits(data, "TealDataAbstract") && teal.data::is_pulled(data)) {+ |
+ ||
87 | +! | +
+ new_data <- do.call(+ |
+ ||
88 | +! | +
+ teal.data::teal_data,+ |
+ ||
89 | +! | +
+ c(+ |
+ ||
90 | +! | +
+ lapply(data$get_datasets(), function(x) x$get_raw_data()), |
||
186 | -+ | |||
91 | +! |
- #' @param datanames (`character`) A vector with `datanames` that are relevant for the item. The+ list(code = data$get_code()), |
||
187 | -+ | |||
92 | +! |
- #' filter panel will automatically update the shown filters to include only+ list(join_keys = teal.data::join_keys(data)) |
||
188 | +93 |
- #' filters in the listed datasets. `NULL` will hide the filter panel,+ ) |
||
189 | +94 |
- #' and the keyword `'all'` will show filters of all datasets. `datanames` also determines+ ) |
||
190 | -+ | |||
95 | +! |
- #' a subset of datasets which are appended to the `data` argument in `server` function.+ reactiveVal(new_data) # will trigger by setting it |
||
191 | +96 |
- #' @param server_args (named `list`) with additional arguments passed on to the+ } else { |
||
192 | -+ | |||
97 | +2x |
- #' `server` function.+ raw_data_old <- data$get_server()(id = "startapp_module") |
||
193 | -+ | |||
98 | +2x |
- #' @param ui_args (named `list`) with additional arguments passed on to the+ raw_data <- reactive({ |
||
194 | -+ | |||
99 | +3x |
- #' `ui` function.+ data <- raw_data_old() |
||
195 | -+ | |||
100 | +3x |
- #'+ if (!is.null(data)) { |
||
196 | +101 |
- #' @return object of class `teal_module`.+ # raw_data is a reactive which returns data only when submit button clicked |
||
197 | +102 |
- #' @export+ # otherwise it returns NULL |
||
198 | -+ | |||
103 | +1x |
- #' @examples+ do.call( |
||
199 | -+ | |||
104 | +1x |
- #' library(shiny)+ teal.data::teal_data, |
||
200 | -+ | |||
105 | +1x |
- #'+ c( |
||
201 | -+ | |||
106 | +1x |
- #' app <- init(+ lapply(data$get_datasets(), function(x) x$get_raw_data()), |
||
202 | -+ | |||
107 | +1x |
- #' data = teal_data(dataset("iris", iris)),+ list(code = data$get_code()), |
||
203 | -+ | |||
108 | +1x |
- #' modules = list(+ list(join_keys = teal.data::join_keys(data)) |
||
204 | +109 |
- #' module(+ ) |
||
205 | +110 |
- #' label = "Module",+ ) |
||
206 | +111 |
- #' server = function(id, data) {+ } |
||
207 | +112 |
- #' moduleServer(+ }) |
||
208 | -+ | |||
113 | +2x |
- #' id,+ raw_data |
||
209 | +114 |
- #' module = function(input, output, session) {+ } |
||
210 | +115 |
- #' output$data <- renderDataTable(data[["iris"]]())+ |
||
211 | -+ | |||
116 | +16x |
- #' }+ teal_data_rv_validate <- reactive({ |
||
212 | +117 |
- #' )+ # custom module can return error |
||
213 | -+ | |||
118 | +12x |
- #' },+ data <- tryCatch(teal_data_rv(), error = function(e) e) |
||
214 | +119 |
- #' ui = function(id) {+ |
||
215 | +120 |
- #' ns <- NS(id)+ # there is an empty reactive event on init! |
||
216 | -+ | |||
121 | +12x |
- #' tagList(dataTableOutput(ns("data")))+ if (inherits(data, "shiny.silent.error") && identical(data$message, "")) { |
||
217 | -+ | |||
122 | +! |
- #' }+ return(NULL) |
||
218 | +123 |
- #' )+ } |
||
219 | +124 |
- #' )+ |
||
220 | +125 |
- #' )+ # to handle qenv.error |
||
221 | -+ | |||
126 | +12x |
- #' if (interactive()) {+ if (inherits(data, "qenv.error")) { |
||
222 | -+ | |||
127 | +2x |
- #' shinyApp(app$ui, app$server)+ validate( |
||
223 | -+ | |||
128 | +2x |
- #' }+ need( |
||
224 | -+ | |||
129 | +2x |
- module <- function(label = "module",+ FALSE, |
||
225 | -+ | |||
130 | +2x |
- server = function(id, ...) {+ paste( |
||
226 | -! | +|||
131 | +2x |
- moduleServer(id, function(input, output, session) {}) # nolint+ "Error when executing `teal_data_module`:\n ", |
||
227 | -+ | |||
132 | +2x |
- },+ paste(data$message, collapse = "\n"), |
||
228 | -+ | |||
133 | +2x |
- ui = function(id, ...) {+ "\n Check your inputs or contact app developer if error persists." |
||
229 | -! | +|||
134 | +
- tags$p(paste0("This module has no UI (id: ", id, " )"))+ ) |
|||
230 | +135 |
- },+ ) |
||
231 | +136 |
- filters,+ ) |
||
232 | +137 |
- datanames = "all",+ } |
||
233 | +138 |
- server_args = NULL,+ |
||
234 | +139 |
- ui_args = NULL) {+ # to handle module non-qenv errors |
||
235 | -155x | +140 | +10x |
- checkmate::assert_string(label)+ if (inherits(data, "error")) { |
236 | -152x | +141 | +1x |
- checkmate::assert_function(server)+ validate( |
237 | -152x | +142 | +1x |
- checkmate::assert_function(ui)+ need( |
238 | -152x | +143 | +1x |
- checkmate::assert_character(datanames, min.len = 1, null.ok = TRUE, any.missing = FALSE)+ FALSE, |
239 | -151x | +144 | +1x |
- checkmate::assert_list(server_args, null.ok = TRUE, names = "named")+ paste( |
240 | -149x | -
- checkmate::assert_list(ui_args, null.ok = TRUE, names = "named")- |
- ||
241 | -+ | 145 | +1x |
-
+ "Error when executing `teal_data_module`:\n ", |
242 | -147x | -
- if (!missing(filters)) {- |
- ||
243 | -! | -
- checkmate::assert_character(filters, min.len = 1, null.ok = TRUE, any.missing = FALSE)- |
- ||
244 | -! | +146 | +1x |
- datanames <- filters+ paste(data$message, collpase = "\n"), |
245 | -! | +|||
147 | +1x |
- msg <-+ "\n Check your inputs or contact app developer if error persists." |
||
246 | -! | +|||
148 | +
- "The `filters` argument is deprecated and will be removed in the next release. Please use `datanames` instead."+ ) |
|||
247 | -! | +|||
149 | +
- logger::log_warn(msg)+ ) |
|||
248 | -! | +|||
150 | +
- warning(msg)+ ) |
|||
249 | +151 |
- }+ } |
||
250 | +152 | |||
251 | -147x | +153 | +9x |
- if (label == "global_filters") {+ validate( |
252 | -1x | +154 | +9x |
- stop(+ need( |
253 | -1x | +155 | +9x |
- sprintf("module(label = \"%s\", ...\n ", label),+ inherits(data, "teal_data"), |
254 | -1x | +156 | +9x |
- "Label 'global_filters' is reserved in teal. Please change to something else.",+ paste( |
255 | -1x | +157 | +9x |
- call. = FALSE+ "Error: `teal_data_module` did not return `teal_data` object", |
256 | -+ | |||
158 | +9x |
- )+ "\n Check your inputs or contact app developer if error persists" |
||
257 | +159 |
- }- |
- ||
258 | -146x | -
- if (label == "Report previewer") {+ ) |
||
259 | -! | +|||
160 | +
- stop(+ ) |
|||
260 | -! | +|||
161 | +
- sprintf("module(label = \"%s\", ...\n ", label),+ ) |
|||
261 | -! | +|||
162 | +
- "Label 'Report previewer' is reserved in teal.",+ |
|||
262 | -! | +|||
163 | +6x |
- call. = FALSE+ validate(need(teal.data::datanames(data), "Data has no datanames. Contact app developer.")) |
||
263 | +164 |
- )+ |
||
264 | +165 |
- }- |
- ||
265 | -146x | -
- server_formals <- names(formals(server))- |
- ||
266 | -146x | -
- if (!(+ |
||
267 | -146x | +166 | +5x |
- "id" %in% server_formals ||+ is_modules_ok <- check_modules_datanames(modules, teal.data::datanames(data)) |
268 | -146x | +167 | +5x |
- all(c("input", "output", "session") %in% server_formals)+ validate(need(isTRUE(is_modules_ok), is_modules_ok)) |
269 | +168 |
- )) {+ |
||
270 | -2x | +169 | +4x |
- stop(+ is_filter_ok <- check_filter_datanames(filter, teal.data::datanames(data)) |
271 | -2x | +170 | +4x |
- "\nmodule() `server` argument requires a function with following arguments:",+ if (!isTRUE(is_filter_ok)) { |
272 | -2x | +171 | +1x |
- "\n - id - teal will set proper shiny namespace for this module.",+ showNotification( |
273 | -2x | +172 | +1x |
- "\n - input, output, session (not recommended) - then shiny::callModule will be used to call a module.",+ "Some filters were not applied because of incompatibility with data. Contact app developer.", |
274 | -2x | +173 | +1x |
- "\n\nFollowing arguments can be used optionaly:",+ type = "warning", |
275 | -2x | +174 | +1x |
- "\n - `data` - module will receive list of reactive (filtered) data specified in the `filters` argument",+ duration = 10 |
276 | -2x | +|||
175 | +
- "\n - `datasets` - module will receive `FilteredData`. See `help(teal.slice::FilteredData)`",+ ) |
|||
277 | -2x | +176 | +1x |
- "\n - `reporter` - module will receive `Reporter`. See `help(teal.reporter::Reporter)`",+ logger::log_warn(is_filter_ok) |
278 | -2x | +|||
177 | +
- "\n - `filter_panel_api` - module will receive `FilterPanelAPI`. (See [teal.slice::FilterPanelAPI]).",+ } |
|||
279 | -2x | +|||
178 | +
- "\n - `...` server_args elements will be passed to the module named argument or to the `...`"+ |
|||
280 | -+ | |||
179 | +4x |
- )+ teal_data_rv() |
||
281 | +180 |
- }+ }) |
||
282 | +181 | |||
283 | -144x | +182 | +16x |
- if (!is.element("data", server_formals) && !is.null(datanames)) {+ output$error <- renderUI({ |
284 | -61x | +183 | +1x |
- message(sprintf("module \"%s\" server function takes no data so \"datanames\" will be ignored", label))+ teal_data_rv_validate() |
285 | -61x | +184 | +1x |
- datanames <- NULL+ NULL |
286 | +185 |
- }+ }) |
||
287 | +186 | |||
288 | -144x | -
- srv_extra_args <- setdiff(names(server_args), server_formals)- |
- ||
289 | -144x | +|||
187 | +
- if (length(srv_extra_args) > 0 && !"..." %in% server_formals) {+ |
|||
290 | -1x | +|||
188 | +
- stop(+ |
|||
291 | -1x | +189 | +16x |
- "\nFollowing `server_args` elements have no equivalent in the formals of the `server`:\n",+ res <- srv_teal(id = "teal", modules = modules, teal_data_rv = teal_data_rv_validate, filter = filter) |
292 | -1x | +190 | +16x |
- paste(paste(" -", srv_extra_args), collapse = "\n"),+ logger::log_trace("srv_teal_with_splash initialized module with data.") |
293 | -1x | +191 | +16x |
- "\n\nUpdate the `server` arguments by including above or add `...`"+ return(res) |
294 | +192 |
- )+ }) |
||
295 | +193 |
- }+ } |
296 | +1 | - - | -||
297 | -143x | -
- ui_formals <- names(formals(ui))+ #' Creates a `teal_modules` object. |
||
298 | -143x | +|||
2 | +
- if (!"id" %in% ui_formals) {+ #' |
|||
299 | -1x | +|||
3 | +
- stop(+ #' @description `r lifecycle::badge("stable")` |
|||
300 | -1x | +|||
4 | +
- "\nmodule() `ui` argument requires a function with following arguments:",+ #' This function collects a list of `teal_modules` and `teal_module` objects and returns a `teal_modules` object |
|||
301 | -1x | +|||
5 | +
- "\n - id - teal will set proper shiny namespace for this module.",+ #' containing the passed objects. |
|||
302 | -1x | +|||
6 | +
- "\n\nFollowing arguments can be used optionaly:",+ #' |
|||
303 | -1x | +|||
7 | +
- "\n - `data` - module will receive list of reactive (filtered) data specied in the `filters` argument",+ #' This function dictates what modules are included in a `teal` application. The internal structure of `teal_modules` |
|||
304 | -1x | +|||
8 | +
- "\n - `datasets` - module will receive `FilteredData`. See `help(teal.slice::FilteredData)`",+ #' shapes the navigation panel of a `teal` application. |
|||
305 | -1x | +|||
9 | +
- "\n - `...` ui_args elements will be passed to the module argument of the same name or to the `...`"+ #' |
|||
306 | +10 |
- )+ #' @param ... (`teal_module` or `teal_modules`) see [module()] and [modules()] for more details |
||
307 | +11 |
- }+ #' @param label (`character(1)`) label of modules collection (default `"root"`). |
||
308 | +12 |
-
+ #' If using the `label` argument then it must be explicitly named. |
||
309 | -142x | +|||
13 | +
- ui_extra_args <- setdiff(names(ui_args), ui_formals)+ #' For example `modules("lab", ...)` should be converted to `modules(label = "lab", ...)` |
|||
310 | -142x | +|||
14 | +
- if (length(ui_extra_args) > 0 && !"..." %in% ui_formals) {+ #' |
|||
311 | -1x | +|||
15 | +
- stop(+ #' @export |
|||
312 | -1x | +|||
16 | +
- "\nFollowing `ui_args` elements have no equivalent in the formals of `ui`:\n",+ #' |
|||
313 | -1x | +|||
17 | +
- paste(paste(" -", ui_extra_args), collapse = "\n"),+ #' @return object of class \code{teal_modules}. Object contains following fields |
|||
314 | -1x | +|||
18 | +
- "\n\nUpdate the `ui` arguments by including above or add `...`"+ #' - `label`: taken from the `label` argument |
|||
315 | +19 |
- )+ #' - `children`: a list containing objects passed in `...`. List elements are named after |
||
316 | +20 |
- }+ #' their `label` attribute converted to a valid `shiny` id. |
||
317 | +21 |
-
+ #' @examples |
||
318 | -141x | +|||
22 | +
- structure(+ #' library(shiny) |
|||
319 | -141x | +|||
23 | +
- list(+ #' |
|||
320 | -141x | +|||
24 | +
- label = label,+ #' app <- init( |
|||
321 | -141x | +|||
25 | +
- server = server, ui = ui, datanames = unique(datanames),+ #' data = teal_data(dataset("iris", iris)), |
|||
322 | -141x | +|||
26 | +
- server_args = server_args, ui_args = ui_args+ #' modules = modules( |
|||
323 | +27 |
- ),+ #' label = "Modules", |
||
324 | -141x | +|||
28 | +
- class = "teal_module"+ #' modules( |
|||
325 | +29 |
- )+ #' label = "Module", |
||
326 | +30 |
- }+ #' module( |
||
327 | +31 |
-
+ #' label = "Inner module", |
||
328 | +32 |
-
+ #' server = function(id, data) { |
||
329 | +33 |
- #' Get module depth+ #' moduleServer( |
||
330 | +34 |
- #'+ #' id, |
||
331 | +35 |
- #' Depth starts at 0, so a single `teal.module` has depth 0.+ #' module = function(input, output, session) { |
||
332 | +36 |
- #' Nesting it increases overall depth by 1.+ #' output$data <- renderDataTable(data[["iris"]]()) |
||
333 | +37 |
- #'+ #' } |
||
334 | +38 |
- #' @inheritParams init+ #' ) |
||
335 | +39 |
- #' @param depth optional, integer determining current depth level+ #' }, |
||
336 | +40 |
- #'+ #' ui = function(id) { |
||
337 | +41 |
- #' @return depth level for given module+ #' ns <- NS(id) |
||
338 | +42 |
- #' @keywords internal+ #' tagList(dataTableOutput(ns("data"))) |
||
339 | +43 |
- #'+ #' }, |
||
340 | +44 |
- #' @examples+ #' datanames = "all" |
||
341 | +45 |
- #' mods <- modules(+ #' ) |
||
342 | +46 |
- #' label = "d1",+ #' ), |
||
343 | +47 |
- #' modules(+ #' module( |
||
344 | +48 |
- #' label = "d2",+ #' label = "Another module", |
||
345 | +49 |
- #' modules(+ #' server = function(id) { |
||
346 | +50 |
- #' label = "d3",+ #' moduleServer( |
||
347 | +51 |
- #' module(label = "aaa1"), module(label = "aaa2"), module(label = "aaa3")+ #' id, |
||
348 | +52 |
- #' ),+ #' module = function(input, output, session) { |
||
349 | +53 |
- #' module(label = "bbb")+ #' output$text <- renderText("Another module") |
||
350 | +54 |
- #' ),+ #' } |
||
351 | +55 |
- #' module(label = "ccc")+ #' ) |
||
352 | +56 |
- #' )+ #' }, |
||
353 | +57 |
- #' stopifnot(teal:::modules_depth(mods) == 3L)+ #' ui = function(id) { |
||
354 | +58 |
- #'+ #' ns <- NS(id) |
||
355 | +59 |
- #' mods <- modules(+ #' tagList(textOutput(ns("text"))) |
||
356 | +60 |
- #' label = "a",+ #' }, |
||
357 | +61 |
- #' modules(+ #' datanames = NULL |
||
358 | +62 |
- #' label = "b1", module(label = "c")+ #' ) |
||
359 | +63 |
- #' ),+ #' ) |
||
360 | +64 |
- #' module(label = "b2")+ #' ) |
||
361 | +65 |
- #' )+ #' if (interactive()) { |
||
362 | +66 |
- #' stopifnot(teal:::modules_depth(mods) == 2L)+ #' shinyApp(app$ui, app$server) |
||
363 | +67 |
- modules_depth <- function(modules, depth = 0L) {+ #' } |
||
364 | -12x | +|||
68 | +
- checkmate::assert(+ modules <- function(..., label = "root") { |
|||
365 | -12x | +69 | +144x |
- checkmate::check_class(modules, "teal_module"),+ checkmate::assert_string(label) |
366 | -12x | +70 | +142x |
- checkmate::check_class(modules, "teal_modules")+ submodules <- list(...) |
367 | -+ | |||
71 | +142x |
- )+ if (any(vapply(submodules, is.character, FUN.VALUE = logical(1)))) { |
||
368 | -12x | +72 | +2x |
- checkmate::assert_int(depth, lower = 0)+ stop( |
369 | -11x | +73 | +2x |
- if (inherits(modules, "teal_modules")) {+ "The only character argument to modules() must be 'label' and it must be named, ", |
370 | -4x | +74 | +2x |
- max(vapply(modules$children, modules_depth, integer(1), depth = depth + 1L))+ "change modules('lab', ...) to modules(label = 'lab', ...)" |
371 | +75 |
- } else {- |
- ||
372 | -7x | -
- depth+ ) |
||
373 | +76 |
} |
||
374 | +77 |
- }+ |
||
375 | -+ | |||
78 | +140x |
-
+ checkmate::assert_list(submodules, min.len = 1, any.missing = FALSE, types = c("teal_module", "teal_modules")) |
||
376 | +79 |
-
+ # name them so we can more easily access the children |
||
377 | +80 |
- module_labels <- function(modules) {- |
- ||
378 | -! | -
- if (inherits(modules, "teal_modules")) {+ # beware however that the label of the submodules should not be changed as it must be kept synced |
||
379 | -! | +|||
81 | +137x |
- lapply(modules$children, module_labels)+ labels <- vapply(submodules, function(submodule) submodule$label, character(1)) |
||
380 | -+ | |||
82 | +137x |
- } else {+ names(submodules) <- make.unique(gsub("[^[:alnum:]]+", "_", labels), sep = "_") |
||
381 | -! | +|||
83 | +137x |
- modules$label+ structure( |
||
382 | -+ | |||
84 | +137x |
- }+ list( |
||
383 | -+ | |||
85 | +137x |
- }+ label = label, |
||
384 | -+ | |||
86 | +137x |
-
+ children = submodules |
||
385 | +87 |
- #' Converts `teal_modules` to a string+ ), |
||
386 | -+ | |||
88 | +137x |
- #'+ class = "teal_modules" |
||
387 | +89 |
- #' @param x (`teal_modules`) to print+ ) |
||
388 | +90 |
- #' @param indent (`integer`) indent level;+ } |
||
389 | +91 |
- #' each `submodule` is indented one level more+ |
||
390 | +92 |
- #' @param ... (optional) additional parameters to pass to recursive calls of `toString`+ #' Append a `teal_module` to `children` of a `teal_modules` object |
||
391 | +93 |
- #' @return (`character`)+ #' @keywords internal |
||
392 | +94 |
- #' @export+ #' @param modules `teal_modules` |
||
393 | +95 |
- #' @rdname modules+ #' @param module `teal_module` object to be appended onto the children of `modules` |
||
394 | +96 |
- toString.teal_modules <- function(x, indent = 0, ...) { # nolint+ #' @return `teal_modules` object with `module` appended |
||
395 | +97 |
- # argument must be `x` to be consistent with base method- |
- ||
396 | -! | -
- paste(c(- |
- ||
397 | -! | -
- paste0(rep(" ", indent), "+ ", x$label),- |
- ||
398 | -! | -
- unlist(lapply(x$children, toString, indent = indent + 1, ...))+ append_module <- function(modules, module) { |
||
399 | -! | +|||
98 | +7x |
- ), collapse = "\n")+ checkmate::assert_class(modules, "teal_modules") |
||
400 | -+ | |||
99 | +5x |
- }+ checkmate::assert_class(module, "teal_module") |
||
401 | -+ | |||
100 | +3x |
-
+ modules$children <- c(modules$children, list(module)) |
||
402 | -+ | |||
101 | +3x |
- #' Converts `teal_module` to a string+ labels <- vapply(modules$children, function(submodule) submodule$label, character(1)) |
||
403 | -+ | |||
102 | +3x |
- #'+ names(modules$children) <- make.unique(gsub("[^[:alnum:]]", "_", tolower(labels)), sep = "_") |
||
404 | -+ | |||
103 | +3x |
- #' @inheritParams toString.teal_modules+ modules |
||
405 | +104 |
- #' @param x `teal_module`+ } |
||
406 | +105 |
- #' @param ... ignored+ |
||
407 | +106 |
- #' @export+ #' Extract/Remove module(s) of specific class |
||
408 | +107 |
- #' @rdname module+ #' |
||
409 | +108 |
- toString.teal_module <- function(x, indent = 0, ...) { # nolint+ #' Given a `teal_module` or a `teal_modules`, return the elements of the structure according to `class`. |
||
410 | -! | +|||
109 | +
- paste0(paste(rep(" ", indent), collapse = ""), "+ ", x$label, collapse = "")+ #' |
|||
411 | +110 |
- }+ #' @param modules `teal_modules` |
||
412 | +111 |
-
+ #' @param class The class name of `teal_module` to be extracted or dropped. |
||
413 | +112 |
- #' Prints `teal_modules`+ #' @keywords internal |
||
414 | +113 |
- #' @param x `teal_modules`+ #' @return |
||
415 | +114 |
- #' @param ... parameters passed to `toString`+ #' For `extract_module`, a `teal_module` of class `class` or `teal_modules` containing modules of class `class`. |
||
416 | +115 |
- #' @export+ #' For `drop_module`, the opposite, which is all `teal_modules` of class other than `class`. |
||
417 | +116 |
- #' @rdname modules+ #' @rdname module_management |
||
418 | +117 |
- print.teal_modules <- function(x, ...) {+ extract_module <- function(modules, class) { |
||
419 | -! | +|||
118 | +68x |
- s <- toString(x, ...)+ if (inherits(modules, class)) { |
||
420 | +119 | ! |
- cat(s)+ modules |
|
421 | -! | +|||
120 | +68x |
- return(invisible(s))+ } else if (inherits(modules, "teal_module")) { |
||
422 | -+ | |||
121 | +38x |
- }+ NULL |
||
423 | -+ | |||
122 | +30x |
-
+ } else if (inherits(modules, "teal_modules")) { |
||
424 | -+ | |||
123 | +30x |
- #' Prints `teal_module`+ Filter(function(x) length(x) > 0L, lapply(modules$children, extract_module, class)) |
||
425 | +124 |
- #' @param x `teal_module`+ } |
||
426 | +125 |
- #' @param ... parameters passed to `toString`+ } |
||
427 | +126 |
- #' @export+ |
||
428 | +127 |
- #' @rdname module+ #' @keywords internal |
||
429 | +128 |
- print.teal_module <- print.teal_modules+ #' @return `teal_modules` |
1 | +129 |
- #' Create a `teal` module for previewing a report+ #' @rdname module_management |
||
2 | +130 |
- #'+ drop_module <- function(modules, class) { |
||
3 | -+ | |||
131 | +68x |
- #' @description `r lifecycle::badge("experimental")`+ if (inherits(modules, class)) { |
||
4 | -+ | |||
132 | +! |
- #' This function wraps [teal.reporter::reporter_previewer_ui()] and+ NULL |
||
5 | -+ | |||
133 | +68x |
- #' [teal.reporter::reporter_previewer_srv()] into a `teal_module` to be+ } else if (inherits(modules, "teal_module")) {+ |
+ ||
134 | +38x | +
+ modules+ |
+ ||
135 | +30x | +
+ } else if (inherits(modules, "teal_modules")) {+ |
+ ||
136 | +30x | +
+ do.call(+ |
+ ||
137 | +30x | +
+ "modules",+ |
+ ||
138 | +30x | +
+ c(Filter(function(x) length(x) > 0L, lapply(modules$children, drop_module, class)), label = modules$label) |
||
6 | +139 |
- #' used in `teal` applications.+ ) |
||
7 | +140 |
- #'+ } |
||
8 | +141 |
- #' If you are creating a `teal` application using [teal::init()] then this+ } |
||
9 | +142 |
- #' module will be added to your application automatically if any of your `teal modules`+ |
||
10 | +143 |
- #' support report generation.+ #' Does the object make use of the `arg` |
||
11 | +144 |
#' |
||
12 | +145 |
- #' @inheritParams module+ #' @param modules (`teal_module` or `teal_modules`) object |
||
13 | +146 |
- #' @param server_args (`named list`)\cr+ #' @param arg (`character(1)`) names of the arguments to be checked against formals of `teal` modules. |
||
14 | +147 |
- #' Arguments passed to [teal.reporter::reporter_previewer_srv()].+ #' @return `logical` whether the object makes use of `arg` |
||
15 | +148 |
- #' @return `teal_module` (extended with `teal_module_previewer` class) containing the `teal.reporter` previewer+ #' @rdname is_arg_used |
||
16 | +149 |
- #' functionality.+ #' @keywords internal |
||
17 | +150 |
- #' @export+ is_arg_used <- function(modules, arg) {+ |
+ ||
151 | +336x | +
+ checkmate::assert_string(arg)+ |
+ ||
152 | +333x | +
+ if (inherits(modules, "teal_modules")) {+ |
+ ||
153 | +31x | +
+ any(unlist(lapply(modules$children, is_arg_used, arg))) |
||
18 | -+ | |||
154 | +302x |
- reporter_previewer_module <- function(label = "Report previewer", server_args = list()) {+ } else if (inherits(modules, "teal_module")) { |
||
19 | -4x | +155 | +45x |
- checkmate::assert_string(label)+ is_arg_used(modules$server, arg) || is_arg_used(modules$ui, arg) |
20 | -2x | +156 | +257x |
- checkmate::assert_list(server_args, names = "named")+ } else if (is.function(modules)) { |
21 | -2x | +157 | +255x |
- checkmate::assert_true(all(names(server_args) %in% names(formals(teal.reporter::reporter_previewer_srv))))+ isTRUE(arg %in% names(formals(modules))) |
22 | +158 |
-
+ } else { |
||
23 | +159 | 2x |
- logger::log_info("Initializing reporter_previewer_module")+ stop("is_arg_used function not implemented for this object") |
|
24 | +160 |
-
+ } |
||
25 | -2x | +|||
161 | +
- srv <- function(id, reporter, ...) {+ } |
|||
26 | -! | +|||
162 | +
- teal.reporter::reporter_previewer_srv(id, reporter, ...)+ |
|||
27 | +163 |
- }+ |
||
28 | +164 |
-
+ #' Creates a `teal_module` object. |
||
29 | -2x | +|||
165 | +
- ui <- function(id, ...) {+ #' |
|||
30 | -! | +|||
166 | +
- teal.reporter::reporter_previewer_ui(id, ...)+ #' @description `r lifecycle::badge("stable")` |
|||
31 | +167 |
- }+ #' This function embeds a `shiny` module inside a `teal` application. One `teal_module` maps to one `shiny` module. |
||
32 | +168 |
-
+ #' |
||
33 | -2x | +|||
169 | +
- module <- module(+ #' @param label (`character(1)`) Label shown in the navigation item for the module. Any label possible except |
|||
34 | -2x | +|||
170 | +
- label = "temporary label",+ #' `"global_filters"` - read more in `mapping` argument of [teal::teal_slices]. |
|||
35 | -2x | +|||
171 | +
- server = srv, ui = ui,+ #' @param server (`function`) `shiny` module with following arguments: |
|||
36 | -2x | +|||
172 | +
- server_args = server_args, ui_args = list(), datanames = NULL+ #' - `id` - teal will set proper shiny namespace for this module (see [shiny::moduleServer()]). |
|||
37 | +173 |
- )+ #' - `input`, `output`, `session` - (not recommended) then [shiny::callModule()] will be used to call a module. |
||
38 | +174 |
- # Module is created with a placeholder label and the label is changed later.+ #' - `data` (optional) module will receive a `tdata` object, a list of reactive (filtered) data specified in |
||
39 | +175 |
- # This is to prevent another module being labeled "Report previewer".+ #' the `filters` argument. |
||
40 | -2x | +|||
176 | +
- class(module) <- c("teal_module_previewer", class(module))+ #' - `datasets` (optional) module will receive `FilteredData`. (See `[teal.slice::FilteredData]`). |
|||
41 | -2x | +|||
177 | +
- module$label <- label+ #' - `reporter` (optional) module will receive `Reporter`. (See [teal.reporter::Reporter]). |
|||
42 | -2x | +|||
178 | +
- module+ # - `filter_panel_api` (optional) module will receive `FilterPanelAPI`. (See [teal.slice::FilterPanelAPI]). |
|||
43 | +179 |
- }+ #' - `...` (optional) `server_args` elements will be passed to the module named argument or to the `...`. |
1 | +180 |
- #' Validate that dataset has a minimum number of observations+ #' @param ui (`function`) Shiny `ui` module function with following arguments: |
||
2 | +181 |
- #'+ #' - `id` - teal will set proper shiny namespace for this module. |
||
3 | +182 |
- #' @description `r lifecycle::badge("stable")`+ #' - `data` (optional) module will receive list of reactive (filtered) data specified in the `filters` argument. |
||
4 | +183 |
- #' @param x a data.frame+ #' - `datasets` (optional) module will receive `FilteredData`. (See `[teal.slice::FilteredData]`). |
||
5 | +184 |
- #' @param min_nrow minimum number of rows in \code{x}+ #' - `...` (optional) `ui_args` elements will be passed to the module named argument or to the `...`. |
||
6 | +185 |
- #' @param complete \code{logical} default \code{FALSE} when set to \code{TRUE} then complete cases are checked.+ #' @param filters (`character`) Deprecated. Use `datanames` instead. |
||
7 | +186 |
- #' @param allow_inf \code{logical} default \code{TRUE} when set to \code{FALSE} then error thrown if any values are+ #' @param datanames (`character`) A vector with `datanames` that are relevant for the item. The |
||
8 | +187 |
- #' infinite.+ #' filter panel will automatically update the shown filters to include only |
||
9 | +188 |
- #' @param msg (`character(1)`) additional message to display alongside the default message.+ #' filters in the listed datasets. `NULL` will hide the filter panel, |
||
10 | +189 |
- #'+ #' and the keyword `'all'` will show filters of all datasets. `datanames` also determines |
||
11 | +190 |
- #' @details This function is a wrapper for `shiny::validate`.+ #' a subset of datasets which are appended to the `data` argument in `server` function. |
||
12 | +191 |
- #'+ #' @param server_args (named `list`) with additional arguments passed on to the |
||
13 | +192 |
- #' @export+ #' `server` function. |
||
14 | +193 |
- #'+ #' @param ui_args (named `list`) with additional arguments passed on to the |
||
15 | +194 |
- #' @examples+ #' `ui` function. |
||
16 | +195 |
- #' library(teal)+ #' |
||
17 | +196 |
- #' ui <- fluidPage(+ #' @return object of class `teal_module`. |
||
18 | +197 |
- #' sliderInput("len", "Max Length of Sepal",+ #' @export |
||
19 | +198 |
- #' min = 4.3, max = 7.9, value = 5+ #' @examples |
||
20 | +199 |
- #' ),+ #' library(shiny) |
||
21 | +200 |
- #' plotOutput("plot")+ #' |
||
22 | +201 |
- #' )+ #' app <- init(+ |
+ ||
202 | ++ |
+ #' data = teal_data(dataset("iris", iris)), |
||
23 | +203 |
- #'+ #' modules = list( |
||
24 | +204 |
- #' server <- function(input, output) {+ #' module( |
||
25 | +205 |
- #' output$plot <- renderPlot({+ #' label = "Module", |
||
26 | +206 |
- #' df <- iris[iris$Sepal.Length <= input$len, ]+ #' server = function(id, data) { |
||
27 | +207 |
- #' validate_has_data(+ #' moduleServer( |
||
28 | +208 |
- #' iris_f,+ #' id, |
||
29 | +209 |
- #' min_nrow = 10,+ #' module = function(input, output, session) { |
||
30 | +210 |
- #' complete = FALSE,+ #' output$data <- renderDataTable(data[["iris"]]()) |
||
31 | +211 |
- #' msg = "Please adjust Max Length of Sepal"+ #' } |
||
32 | +212 |
- #' )+ #' ) |
||
33 | +213 |
- #'+ #' }, |
||
34 | +214 |
- #' hist(iris_f$Sepal.Length, breaks = 5)+ #' ui = function(id) { |
||
35 | +215 |
- #' })+ #' ns <- NS(id) |
||
36 | +216 |
- #' }+ #' tagList(dataTableOutput(ns("data"))) |
||
37 | +217 |
- #' if (interactive()) {+ #' } |
||
38 | +218 |
- #' shinyApp(ui, server)+ #' ) |
||
39 | +219 |
- #' }+ #' ) |
||
40 | +220 |
- #'+ #' ) |
||
41 | +221 |
- validate_has_data <- function(x,+ #' if (interactive()) { |
||
42 | +222 |
- min_nrow = NULL,+ #' shinyApp(app$ui, app$server) |
||
43 | +223 |
- complete = FALSE,+ #' } |
||
44 | +224 |
- allow_inf = TRUE,+ module <- function(label = "module", |
||
45 | +225 |
- msg = NULL) {+ server = function(id, ...) { |
||
46 | -17x | +|||
226 | +! |
- checkmate::assert_string(msg, null.ok = TRUE)+ moduleServer(id, function(input, output, session) {}) # nolint |
||
47 | -15x | +|||
227 | +
- checkmate::assert_data_frame(x)+ }, |
|||
48 | -15x | +|||
228 | +
- if (!is.null(min_nrow)) {+ ui = function(id, ...) { |
|||
49 | -15x | +|||
229 | +! |
- if (complete) {+ tags$p(paste0("This module has no UI (id: ", id, " )")) |
||
50 | -5x | +|||
230 | +
- complete_index <- stats::complete.cases(x)+ }, |
|||
51 | -5x | +|||
231 | +
- validate(need(+ filters, |
|||
52 | -5x | +|||
232 | +
- sum(complete_index) > 0 && nrow(x[complete_index, , drop = FALSE]) >= min_nrow,+ datanames = "all", |
|||
53 | -5x | +|||
233 | +
- paste(c(paste("Number of complete cases is less than:", min_nrow), msg), collapse = "\n")+ server_args = NULL, |
|||
54 | +234 |
- ))+ ui_args = NULL) { |
||
55 | -+ | |||
235 | +159x |
- } else {+ checkmate::assert_string(label) |
||
56 | -10x | +236 | +156x |
- validate(need(+ checkmate::assert_function(server) |
57 | -10x | +237 | +156x |
- nrow(x) >= min_nrow,+ checkmate::assert_function(ui) |
58 | -10x | +238 | +156x |
- paste(+ checkmate::assert_character(datanames, min.len = 1, null.ok = TRUE, any.missing = FALSE) |
59 | -10x | +239 | +155x |
- c(paste("Minimum number of records not met: >=", min_nrow, "records required."), msg),+ checkmate::assert_list(server_args, null.ok = TRUE, names = "named") |
60 | -10x | +240 | +153x |
- collapse = "\n"+ checkmate::assert_list(ui_args, null.ok = TRUE, names = "named") |
61 | +241 |
- )+ |
||
62 | -+ | |||
242 | +151x |
- ))+ if (!missing(filters)) { |
||
63 | -+ | |||
243 | +! |
- }+ checkmate::assert_character(filters, min.len = 1, null.ok = TRUE, any.missing = FALSE) |
||
64 | -+ | |||
244 | +! |
-
+ datanames <- filters |
||
65 | -10x | +|||
245 | +! |
- if (!allow_inf) {+ msg <- |
||
66 | -6x | +|||
246 | +! |
- validate(need(+ "The `filters` argument is deprecated and will be removed in the next release. Please use `datanames` instead." |
||
67 | -6x | +|||
247 | +! |
- all(vapply(x, function(col) !is.numeric(col) || !any(is.infinite(col)), logical(1))),+ logger::log_warn(msg) |
||
68 | -6x | +|||
248 | +! |
- "Dataframe contains Inf values which is not allowed."+ warning(msg) |
||
69 | +249 |
- ))+ } |
||
70 | +250 |
- }+ |
||
71 | -+ | |||
251 | +151x |
- }+ if (label == "global_filters") { |
||
72 | -+ | |||
252 | +1x |
- }+ stop( |
||
73 | -+ | |||
253 | +1x |
-
+ sprintf("module(label = \"%s\", ...\n ", label), |
||
74 | -+ | |||
254 | +1x |
- #' Validate that dataset has unique rows for key variables+ "Label 'global_filters' is reserved in teal. Please change to something else.", |
||
75 | -+ | |||
255 | +1x |
- #'+ call. = FALSE |
||
76 | +256 |
- #' @description `r lifecycle::badge("stable")`+ ) |
||
77 | +257 |
- #' @param x a data.frame+ } |
||
78 | -+ | |||
258 | +150x |
- #' @param key a vector of ID variables from \code{x} that identify unique records+ if (label == "Report previewer") { |
||
79 | -+ | |||
259 | +! |
- #'+ stop( |
||
80 | -+ | |||
260 | +! |
- #' @details This function is a wrapper for `shiny::validate`.+ sprintf("module(label = \"%s\", ...\n ", label), |
||
81 | -+ | |||
261 | +! |
- #'+ "Label 'Report previewer' is reserved in teal.", |
||
82 | -+ | |||
262 | +! |
- #' @export+ call. = FALSE |
||
83 | +263 |
- #'+ ) |
||
84 | +264 |
- #' @examples+ } |
||
85 | -+ | |||
265 | +150x |
- #' iris$id <- rep(1:50, times = 3)+ server_formals <- names(formals(server)) |
||
86 | -+ | |||
266 | +150x |
- #' ui <- fluidPage(+ if (!( |
||
87 | -+ | |||
267 | +150x |
- #' selectInput(+ "id" %in% server_formals || |
||
88 | -+ | |||
268 | +150x |
- #' inputId = "species",+ all(c("input", "output", "session") %in% server_formals) |
||
89 | +269 |
- #' label = "Select species",+ )) { |
||
90 | -+ | |||
270 | +2x |
- #' choices = c("setosa", "versicolor", "virginica"),+ stop( |
||
91 | -+ | |||
271 | +2x |
- #' selected = "setosa",+ "\nmodule() `server` argument requires a function with following arguments:", |
||
92 | -+ | |||
272 | +2x |
- #' multiple = TRUE+ "\n - id - teal will set proper shiny namespace for this module.", |
||
93 | -+ | |||
273 | +2x |
- #' ),+ "\n - input, output, session (not recommended) - then shiny::callModule will be used to call a module.", |
||
94 | -+ | |||
274 | +2x |
- #' plotOutput("plot")+ "\n\nFollowing arguments can be used optionaly:", |
||
95 | -+ | |||
275 | +2x |
- #' )+ "\n - `data` - module will receive list of reactive (filtered) data specified in the `filters` argument", |
||
96 | -+ | |||
276 | +2x |
- #' server <- function(input, output) {+ "\n - `datasets` - module will receive `FilteredData`. See `help(teal.slice::FilteredData)`", |
||
97 | -+ | |||
277 | +2x |
- #' output$plot <- renderPlot({+ "\n - `reporter` - module will receive `Reporter`. See `help(teal.reporter::Reporter)`", |
||
98 | -+ | |||
278 | +2x |
- #' iris_f <- iris[iris$Species %in% input$species, ]+ "\n - `filter_panel_api` - module will receive `FilterPanelAPI`. (See [teal.slice::FilterPanelAPI]).", |
||
99 | -+ | |||
279 | +2x |
- #' validate_one_row_per_id(iris_f, key = c("id"))+ "\n - `...` server_args elements will be passed to the module named argument or to the `...`" |
||
100 | +280 |
- #'+ ) |
||
101 | +281 |
- #' hist(iris_f$Sepal.Length, breaks = 5)+ } |
||
102 | +282 |
- #' })+ |
||
103 | -+ | |||
283 | +148x |
- #' }+ if (!is.element("data", server_formals) && !is.null(datanames)) { |
||
104 | -+ | |||
284 | +61x |
- #' if (interactive()) {+ message(sprintf("module \"%s\" server function takes no data so \"datanames\" will be ignored", label)) |
||
105 | -+ | |||
285 | +61x |
- #' shinyApp(ui, server)+ datanames <- NULL |
||
106 | +286 |
- #' }+ } |
||
107 | +287 |
- #'+ |
||
108 | -+ | |||
288 | +148x |
- validate_one_row_per_id <- function(x, key = c("USUBJID", "STUDYID")) {+ srv_extra_args <- setdiff(names(server_args), server_formals) |
||
109 | -! | +|||
289 | +148x |
- validate(need(!any(duplicated(x[key])), paste("Found more than one row per id.")))+ if (length(srv_extra_args) > 0 && !"..." %in% server_formals) { |
||
110 | -+ | |||
290 | +1x |
- }+ stop( |
||
111 | -+ | |||
291 | +1x |
-
+ "\nFollowing `server_args` elements have no equivalent in the formals of the `server`:\n", |
||
112 | -+ | |||
292 | +1x |
- #' Validates that vector includes all expected values+ paste(paste(" -", srv_extra_args), collapse = "\n"), |
||
113 | -+ | |||
293 | +1x |
- #'+ "\n\nUpdate the `server` arguments by including above or add `...`" |
||
114 | +294 |
- #' @description `r lifecycle::badge("stable")`+ ) |
||
115 | +295 |
- #' @param x values to test. All must be in \code{choices}+ } |
||
116 | +296 |
- #' @param choices a vector to test for values of \code{x}+ |
||
117 | -+ | |||
297 | +147x |
- #' @param msg warning message to display+ ui_formals <- names(formals(ui)) |
||
118 | -+ | |||
298 | +147x |
- #'+ if (!"id" %in% ui_formals) { |
||
119 | -+ | |||
299 | +1x |
- #' @details This function is a wrapper for `shiny::validate`.+ stop( |
||
120 | -+ | |||
300 | +1x |
- #'+ "\nmodule() `ui` argument requires a function with following arguments:", |
||
121 | -+ | |||
301 | +1x |
- #' @export+ "\n - id - teal will set proper shiny namespace for this module.", |
||
122 | -+ | |||
302 | +1x |
- #'+ "\n\nFollowing arguments can be used optionaly:", |
||
123 | -+ | |||
303 | +1x |
- #' @examples+ "\n - `data` - module will receive list of reactive (filtered) data specied in the `filters` argument", |
||
124 | -+ | |||
304 | +1x |
- #' ui <- fluidPage(+ "\n - `datasets` - module will receive `FilteredData`. See `help(teal.slice::FilteredData)`", |
||
125 | -+ | |||
305 | +1x |
- #' selectInput(+ "\n - `...` ui_args elements will be passed to the module argument of the same name or to the `...`" |
||
126 | +306 |
- #' "species",+ ) |
||
127 | +307 |
- #' "Select species",+ } |
||
128 | +308 |
- #' choices = c("setosa", "versicolor", "virginica", "unknown species"),+ |
||
129 | -+ | |||
309 | +146x |
- #' selected = "setosa",+ ui_extra_args <- setdiff(names(ui_args), ui_formals) |
||
130 | -+ | |||
310 | +146x |
- #' multiple = FALSE+ if (length(ui_extra_args) > 0 && !"..." %in% ui_formals) { |
||
131 | -+ | |||
311 | +1x |
- #' ),+ stop( |
||
132 | -+ | |||
312 | +1x |
- #' verbatimTextOutput("summary")+ "\nFollowing `ui_args` elements have no equivalent in the formals of `ui`:\n", |
||
133 | -+ | |||
313 | +1x |
- #' )+ paste(paste(" -", ui_extra_args), collapse = "\n"), |
||
134 | -+ | |||
314 | +1x |
- #'+ "\n\nUpdate the `ui` arguments by including above or add `...`" |
||
135 | +315 |
- #' server <- function(input, output) {+ ) |
||
136 | +316 |
- #' output$summary <- renderPrint({+ } |
||
137 | +317 |
- #' validate_in(input$species, iris$Species, "Species does not exist.")+ |
||
138 | -+ | |||
318 | +145x |
- #' nrow(iris[iris$Species == input$species, ])+ structure( |
||
139 | -+ | |||
319 | +145x |
- #' })+ list( |
||
140 | -+ | |||
320 | +145x |
- #' }+ label = label, |
||
141 | -+ | |||
321 | +145x |
- #' if (interactive()) {+ server = server, ui = ui, datanames = unique(datanames), |
||
142 | -+ | |||
322 | +145x |
- #' shinyApp(ui, server)+ server_args = server_args, ui_args = ui_args |
||
143 | +323 |
- #' }+ ), |
||
144 | -+ | |||
324 | +145x |
- #'+ class = "teal_module" |
||
145 | +325 |
- validate_in <- function(x, choices, msg) {+ ) |
||
146 | -! | +|||
326 | +
- validate(need(length(x) > 0 && length(choices) > 0 && all(x %in% choices), msg))+ } |
|||
147 | +327 |
- }+ |
||
148 | +328 | |||
149 | +329 |
- #' Validates that vector has length greater than 0+ #' Get module depth |
||
150 | +330 |
#' |
||
151 | +331 |
- #' @description `r lifecycle::badge("stable")`+ #' Depth starts at 0, so a single `teal.module` has depth 0. |
||
152 | +332 |
- #' @param x vector+ #' Nesting it increases overall depth by 1. |
||
153 | +333 |
- #' @param msg message to display+ #' |
||
154 | +334 |
- #'+ #' @inheritParams init |
||
155 | +335 |
- #' @details This function is a wrapper for `shiny::validate`.+ #' @param depth optional, integer determining current depth level |
||
156 | +336 |
#' |
||
157 | +337 |
- #' @export+ #' @return depth level for given module |
||
158 | +338 |
- #'+ #' @keywords internal |
||
159 | +339 |
- #' @examples+ #' |
||
160 | +340 |
- #' data <- data.frame(+ #' @examples |
||
161 | +341 |
- #' id = c(1:10, 11:20, 1:10),+ #' mods <- modules( |
||
162 | +342 |
- #' strata = rep(c("A", "B"), each = 15)+ #' label = "d1", |
||
163 | +343 |
- #' )+ #' modules( |
||
164 | +344 |
- #' ui <- fluidPage(+ #' label = "d2", |
||
165 | +345 |
- #' selectInput("ref1", "Select strata1 to compare",+ #' modules( |
||
166 | +346 |
- #' choices = c("A", "B", "C"), selected = "A"+ #' label = "d3", |
||
167 | +347 |
- #' ),+ #' module(label = "aaa1"), module(label = "aaa2"), module(label = "aaa3") |
||
168 | +348 |
- #' selectInput("ref2", "Select strata2 to compare",+ #' ), |
||
169 | +349 |
- #' choices = c("A", "B", "C"), selected = "B"+ #' module(label = "bbb") |
||
170 | +350 |
#' ), |
||
171 | +351 |
- #' verbatimTextOutput("arm_summary")+ #' module(label = "ccc") |
||
172 | +352 |
#' ) |
||
173 | +353 |
- #'+ #' stopifnot(teal:::modules_depth(mods) == 3L) |
||
174 | +354 |
- #' server <- function(input, output) {+ #' |
||
175 | +355 |
- #' output$arm_summary <- renderText({+ #' mods <- modules( |
||
176 | +356 |
- #' sample_1 <- data$id[data$strata == input$ref1]+ #' label = "a", |
||
177 | +357 |
- #' sample_2 <- data$id[data$strata == input$ref2]+ #' modules( |
||
178 | +358 |
- #'+ #' label = "b1", module(label = "c") |
||
179 | +359 |
- #' validate_has_elements(sample_1, "No subjects in strata1.")+ #' ), |
||
180 | +360 |
- #' validate_has_elements(sample_2, "No subjects in strata2.")+ #' module(label = "b2") |
||
181 | +361 |
- #'+ #' ) |
||
182 | +362 |
- #' paste0(+ #' stopifnot(teal:::modules_depth(mods) == 2L) |
||
183 | +363 |
- #' "Number of samples in: strata1=", length(sample_1),+ modules_depth <- function(modules, depth = 0L) { |
||
184 | -+ | |||
364 | +12x |
- #' " comparions strata2=", length(sample_2)+ checkmate::assert( |
||
185 | -+ | |||
365 | +12x |
- #' )+ checkmate::check_class(modules, "teal_module"), |
||
186 | -+ | |||
366 | +12x |
- #' })+ checkmate::check_class(modules, "teal_modules") |
||
187 | +367 |
- #' }+ ) |
||
188 | -+ | |||
368 | +12x |
- #' if (interactive()) {+ checkmate::assert_int(depth, lower = 0) |
||
189 | -+ | |||
369 | +11x |
- #' shinyApp(ui, server)+ if (inherits(modules, "teal_modules")) { |
||
190 | -+ | |||
370 | +4x |
- #' }+ max(vapply(modules$children, modules_depth, integer(1), depth = depth + 1L)) |
||
191 | +371 |
- validate_has_elements <- function(x, msg) {+ } else { |
||
192 | -! | +|||
372 | +7x |
- validate(need(length(x) > 0, msg))+ depth |
||
193 | +373 |
- }+ } |
||
194 | +374 |
-
+ } |
||
195 | +375 |
- #' Validates no intersection between two vectors+ |
||
196 | +376 |
- #'+ |
||
197 | +377 |
- #' @description `r lifecycle::badge("stable")`+ module_labels <- function(modules) {+ |
+ ||
378 | +! | +
+ if (inherits(modules, "teal_modules")) {+ |
+ ||
379 | +! | +
+ lapply(modules$children, module_labels) |
||
198 | +380 |
- #' @param x vector+ } else {+ |
+ ||
381 | +! | +
+ modules$label |
||
199 | +382 |
- #' @param y vector+ } |
||
200 | +383 |
- #' @param msg message to display if \code{x} and \code{y} intersect+ } |
||
201 | +384 |
- #'+ |
||
202 | +385 |
- #' @details This function is a wrapper for `shiny::validate`.+ #' Converts `teal_modules` to a string |
||
203 | +386 |
#' |
||
204 | +387 |
- #' @export+ #' @param x (`teal_modules`) to print |
||
205 | +388 |
- #'+ #' @param indent (`integer`) indent level; |
||
206 | +389 |
- #' @examples+ #' each `submodule` is indented one level more |
||
207 | +390 |
- #' data <- data.frame(+ #' @param ... (optional) additional parameters to pass to recursive calls of `toString` |
||
208 | +391 |
- #' id = c(1:10, 11:20, 1:10),+ #' @return (`character`) |
||
209 | +392 |
- #' strata = rep(c("A", "B", "C"), each = 10)+ #' @export |
||
210 | +393 |
- #' )+ #' @rdname modules |
||
211 | +394 |
- #'+ toString.teal_modules <- function(x, indent = 0, ...) { # nolint |
||
212 | +395 |
- #' ui <- fluidPage(+ # argument must be `x` to be consistent with base method |
||
213 | -+ | |||
396 | +! |
- #' selectInput("ref1", "Select strata1 to compare",+ paste(c( |
||
214 | -+ | |||
397 | +! |
- #' choices = c("A", "B", "C"),+ paste0(rep(" ", indent), "+ ", x$label), |
||
215 | -+ | |||
398 | +! |
- #' selected = "A"+ unlist(lapply(x$children, toString, indent = indent + 1, ...)) |
||
216 | -+ | |||
399 | +! |
- #' ),+ ), collapse = "\n") |
||
217 | +400 |
- #' selectInput("ref2", "Select strata2 to compare",+ } |
||
218 | +401 |
- #' choices = c("A", "B", "C"),+ |
||
219 | +402 |
- #' selected = "B"+ #' Converts `teal_module` to a string |
||
220 | +403 |
- #' ),+ #' |
||
221 | +404 |
- #' verbatimTextOutput("summary")+ #' @inheritParams toString.teal_modules |
||
222 | +405 |
- #' )+ #' @param x `teal_module` |
||
223 | +406 |
- #'+ #' @param ... ignored |
||
224 | +407 |
- #' server <- function(input, output) {+ #' @export |
||
225 | +408 |
- #' output$summary <- renderText({+ #' @rdname module |
||
226 | +409 |
- #' sample_1 <- data$id[data$strata == input$ref1]+ toString.teal_module <- function(x, indent = 0, ...) { # nolint |
||
227 | -+ | |||
410 | +! |
- #' sample_2 <- data$id[data$strata == input$ref2]+ paste0(paste(rep(" ", indent), collapse = ""), "+ ", x$label, collapse = "") |
||
228 | +411 |
- #'+ } |
||
229 | +412 |
- #' validate_no_intersection(+ |
||
230 | +413 |
- #' sample_1, sample_2,+ #' Prints `teal_modules` |
||
231 | +414 |
- #' "subjects within strata1 and strata2 cannot overlap"+ #' @param x `teal_modules` |
||
232 | +415 |
- #' )+ #' @param ... parameters passed to `toString` |
||
233 | +416 |
- #' paste0(+ #' @export |
||
234 | +417 |
- #' "Number of subject in: reference treatment=", length(sample_1),+ #' @rdname modules |
||
235 | +418 |
- #' " comparions treatment=", length(sample_2)+ print.teal_modules <- function(x, ...) { |
||
236 | -+ | |||
419 | +! |
- #' )+ s <- toString(x, ...) |
||
237 | -+ | |||
420 | +! |
- #' })+ cat(s)+ |
+ ||
421 | +! | +
+ return(invisible(s)) |
||
238 | +422 |
- #' }+ } |
||
239 | +423 |
- #' if (interactive()) {+ |
||
240 | +424 |
- #' shinyApp(ui, server)+ #' Prints `teal_module` |
||
241 | +425 |
- #' }+ #' @param x `teal_module` |
||
242 | +426 |
- #'+ #' @param ... parameters passed to `toString` |
||
243 | +427 |
- validate_no_intersection <- function(x, y, msg) {+ #' @export |
||
244 | -! | +|||
428 | +
- validate(need(length(intersect(x, y)) == 0, msg))+ #' @rdname module |
|||
245 | +429 |
- }+ print.teal_module <- print.teal_modules |
246 | +1 |
-
+ #' Store teal_slices object to a file |
|
247 | +2 |
-
+ #' |
|
248 | +3 |
- #' Validates that dataset contains specific variable+ #' This function takes a `teal_slices` object and saves it to a file in `JSON` format. |
|
249 | +4 |
- #'+ #' The `teal_slices` object contains information about filter states and can be used to |
|
250 | +5 |
- #' @description `r lifecycle::badge("stable")`+ #' create, modify, and delete filter states. The saved file can be later loaded using |
|
251 | +6 |
- #' @param data a data.frame+ #' the `slices_restore` function. |
|
252 | +7 |
- #' @param varname name of variable in \code{data}+ #' |
|
253 | +8 |
- #' @param msg message to display if \code{data} does not include \code{varname}+ #' @param tss (`teal_slices`) object to be stored. |
|
254 | +9 |
- #'+ #' @param file (`character(1)`) The file path where `teal_slices` object will be saved. |
|
255 | +10 |
- #' @details This function is a wrapper for `shiny::validate`.+ #' The file extension should be `".json"`. |
|
256 | +11 |
#' |
|
257 | +12 |
- #' @export+ #' @details `Date` class is stored in `"ISO8601"` format (`YYYY-MM-DD`). `POSIX*t` classes are converted to a |
|
258 | +13 |
- #'+ #' character by using `format.POSIX*t(usetz = TRUE, tz = "UTC")` (`YYYY-MM-DD {N}{N}:{N}{N}:{N}{N} UTC`, where |
|
259 | +14 |
- #' @examples+ #' `{N} = [0-9]` is a number and `UTC` is `Coordinated Universal Time` timezone short-code). |
|
260 | +15 |
- #' data <- data.frame(+ #' This format is assumed during `slices_restore`. All `POSIX*t` objects in `selected` or `choices` fields of |
|
261 | +16 |
- #' one = rep("a", length.out = 20),+ #' `teal_slice` objects are always printed in `UTC` timezone as well. |
|
262 | +17 |
- #' two = rep(c("a", "b"), length.out = 20)+ #' |
|
263 | +18 |
- #' )+ #' @return `NULL`, invisibly. |
|
264 | +19 |
- #' ui <- fluidPage(+ #' |
|
265 | +20 |
- #' selectInput(+ #' @keywords internal |
|
266 | +21 |
- #' "var",+ #' |
|
267 | +22 |
- #' "Select variable",+ #' @examples |
|
268 | +23 |
- #' choices = c("one", "two", "three", "four"),+ #' # Create a teal_slices object |
|
269 | +24 |
- #' selected = "one"+ #' tss <- teal_slices( |
|
270 | +25 |
- #' ),+ #' teal_slice(dataname = "data", varname = "var"), |
|
271 | +26 |
- #' verbatimTextOutput("summary")+ #' teal_slice(dataname = "data", expr = "x > 0", id = "positive_x", title = "Positive x") |
|
272 | +27 |
#' ) |
|
273 | +28 |
#' |
|
274 | +29 |
- #' server <- function(input, output) {+ #' if (interactive()) { |
|
275 | +30 |
- #' output$summary <- renderText({+ #' # Store the teal_slices object to a file |
|
276 | +31 |
- #' validate_has_variable(data, input$var)+ #' slices_store(tss, "path/to/file.json") |
|
277 | +32 |
- #' paste0("Selected treatment variables: ", paste(input$var, collapse = ", "))+ #' } |
|
278 | +33 |
- #' })+ #' |
|
279 | +34 |
- #' }+ slices_store <- function(tss, file) { |
|
280 | -+ | ||
35 | +6x |
- #' if (interactive()) {+ checkmate::assert_class(tss, "teal_slices")+ |
+ |
36 | +6x | +
+ checkmate::assert_path_for_output(file, overwrite = TRUE, extension = "json") |
|
281 | +37 |
- #' shinyApp(ui, server)+ + |
+ |
38 | +6x | +
+ cat(format(tss, trim_lines = FALSE), "\n", file = file) |
|
282 | +39 |
- #' }+ } |
|
283 | +40 |
- validate_has_variable <- function(data, varname, msg) {+ |
|
284 | -! | +||
41 | +
- if (length(varname) != 0) {+ #' Restore teal_slices object from a file |
||
285 | -! | +||
42 | +
- has_vars <- varname %in% names(data)+ #' |
||
286 | +43 |
-
+ #' This function takes a file path to a `JSON` file containing a `teal_slices` object |
|
287 | -! | +||
44 | +
- if (!all(has_vars)) {+ #' and restores it to its original form. The restored `teal_slices` object can be used |
||
288 | -! | +||
45 | +
- if (missing(msg)) {+ #' to access filter states and their corresponding attributes. |
||
289 | -! | +||
46 | +
- msg <- sprintf(+ #' |
||
290 | -! | +||
47 | +
- "%s does not have the required variables: %s.",+ #' @param file Path to file where `teal_slices` is stored. Must have a `.json` extension and read access. |
||
291 | -! | +||
48 | +
- deparse(substitute(data)),+ #' |
||
292 | -! | +||
49 | +
- toString(varname[!has_vars])+ #' @return A `teal_slices` object restored from the file. |
||
293 | +50 |
- )+ #' |
|
294 | +51 |
- }+ #' @keywords internal |
|
295 | -! | +||
52 | +
- validate(need(FALSE, msg))+ #' |
||
296 | +53 |
- }+ #' @examples |
|
297 | +54 |
- }+ #' if (interactive()) { |
|
298 | +55 |
- }+ #' # Restore a teal_slices object from a file |
|
299 | +56 |
-
+ #' tss_restored <- slices_restore("path/to/file.json") |
|
300 | +57 |
- #' Validate that variables has expected number of levels+ #' } |
|
301 | +58 |
#' |
|
302 | +59 |
- #' @description `r lifecycle::badge("stable")`+ slices_restore <- function(file) { |
|
303 | -+ | ||
60 | +6x |
- #' @param x variable name. If \code{x} is not a factor, the unique values+ checkmate::assert_file_exists(file, access = "r", extension = "json") |
|
304 | +61 |
- #' are treated as levels.+ |
|
305 | -+ | ||
62 | +6x |
- #' @param min_levels cutoff for minimum number of levels of \code{x}+ tss_json <- jsonlite::fromJSON(file, simplifyDataFrame = FALSE) |
|
306 | -+ | ||
63 | +6x |
- #' @param max_levels cutoff for maximum number of levels of \code{x}+ tss_json$slices <- |
|
307 | -+ | ||
64 | +6x |
- #' @param var_name name of variable being validated for use in+ lapply(tss_json$slices, function(slice) { |
|
308 | -+ | ||
65 | +6x |
- #' validation message+ for (field in c("selected", "choices")) { |
|
309 | -+ | ||
66 | +12x |
- #'+ if (!is.null(slice[[field]])) { |
|
310 | -+ | ||
67 | +9x |
- #' @details If the number of levels of \code{x} is less than \code{min_levels}+ date_partial_regex <- "^[0-9]{4}-[0-9]{2}-[0-9]{2}" |
|
311 | -+ | ||
68 | +9x |
- #' or greater than \code{max_levels} the validation will fail.+ time_stamp_regex <- paste0(date_partial_regex, "\\s[0-9]{2}:[0-9]{2}:[0-9]{2}\\sUTC$") |
|
312 | +69 |
- #' This function is a wrapper for `shiny::validate`.+ |
|
313 | -+ | ||
70 | +9x |
- #'+ slice[[field]] <- |
|
314 | -+ | ||
71 | +9x |
- #' @export+ if (all(grepl(paste0(date_partial_regex, "$"), slice[[field]]))) { |
|
315 | -+ | ||
72 | +3x |
- #' @examples+ as.Date(slice[[field]]) |
|
316 | -+ | ||
73 | +9x |
- #' data <- data.frame(+ } else if (all(grepl(time_stamp_regex, slice[[field]]))) { |
|
317 | -+ | ||
74 | +3x |
- #' one = rep("a", length.out = 20),+ as.POSIXct(slice[[field]], tz = "UTC") |
|
318 | +75 |
- #' two = rep(c("a", "b"), length.out = 20),+ } else { |
|
319 | -+ | ||
76 | +3x |
- #' three = rep(c("a", "b", "c"), length.out = 20),+ slice[[field]] |
|
320 | +77 |
- #' four = rep(c("a", "b", "c", "d"), length.out = 20),+ } |
|
321 | +78 |
- #' stringsAsFactors = TRUE+ } |
|
322 | +79 |
- #' )+ } |
|
323 | -+ | ||
80 | +6x |
- #' ui <- fluidPage(+ slice |
|
324 | +81 |
- #' selectInput(+ }) |
|
325 | +82 |
- #' "var",+ |
|
326 | -+ | ||
83 | +6x |
- #' "Select variable",+ tss_elements <- lapply(tss_json$slices, as.teal_slice) |
|
327 | +84 |
- #' choices = c("one", "two", "three", "four"),+ |
|
328 | -+ | ||
85 | +6x |
- #' selected = "one"+ do.call(teal_slices, c(tss_elements, tss_json$attributes)) |
|
329 | +86 |
- #' ),+ } |
330 | +1 |
- #' verbatimTextOutput("summary")+ #' Create a `teal` module for previewing a report |
|
331 | +2 |
- #' )+ #' |
|
332 | +3 |
- #'+ #' @description `r lifecycle::badge("experimental")` |
|
333 | +4 |
- #' server <- function(input, output) {+ #' This function wraps [teal.reporter::reporter_previewer_ui()] and |
|
334 | +5 |
- #' output$summary <- renderText({+ #' [teal.reporter::reporter_previewer_srv()] into a `teal_module` to be |
|
335 | +6 |
- #' validate_n_levels(data[[input$var]], min_levels = 2, max_levels = 15, var_name = input$var)+ #' used in `teal` applications. |
|
336 | +7 |
- #' paste0(+ #' |
|
337 | +8 |
- #' "Levels of selected treatment variable: ",+ #' If you are creating a `teal` application using [teal::init()] then this |
|
338 | +9 |
- #' paste(levels(data[[input$var]]),+ #' module will be added to your application automatically if any of your `teal modules` |
|
339 | +10 |
- #' collapse = ", "+ #' support report generation. |
|
340 | +11 |
- #' )+ #' |
|
341 | +12 |
- #' )+ #' @inheritParams module |
|
342 | +13 |
- #' })+ #' @param server_args (`named list`)\cr |
|
343 | +14 |
- #' }+ #' Arguments passed to [teal.reporter::reporter_previewer_srv()]. |
|
344 | +15 |
- #' if (interactive()) {+ #' @return `teal_module` (extended with `teal_module_previewer` class) containing the `teal.reporter` previewer |
|
345 | +16 |
- #' shinyApp(ui, server)+ #' functionality. |
|
346 | +17 |
- #' }+ #' @export |
|
347 | +18 |
- validate_n_levels <- function(x, min_levels = 1, max_levels = 12, var_name) {+ reporter_previewer_module <- function(label = "Report previewer", server_args = list()) { |
|
348 | -! | +||
19 | +4x |
- x_levels <- if (is.factor(x)) {+ checkmate::assert_string(label) |
|
349 | -! | +||
20 | +2x |
- levels(x)+ checkmate::assert_list(server_args, names = "named") |
|
350 | -+ | ||
21 | +2x |
- } else {+ checkmate::assert_true(all(names(server_args) %in% names(formals(teal.reporter::reporter_previewer_srv)))) |
|
351 | -! | +||
22 | +
- unique(x)+ |
||
352 | -+ | ||
23 | +2x |
- }+ logger::log_info("Initializing reporter_previewer_module") |
|
353 | +24 | ||
354 | -! | +||
25 | +2x |
- if (!is.null(min_levels) && !(is.null(max_levels))) {+ srv <- function(id, reporter, ...) { |
|
355 | +26 | ! |
- validate(need(+ teal.reporter::reporter_previewer_srv(id, reporter, ...) |
356 | -! | +||
27 | +
- length(x_levels) >= min_levels && length(x_levels) <= max_levels,+ } |
||
357 | -! | +||
28 | +
- sprintf(+ |
||
358 | -! | +||
29 | +2x |
- "%s variable needs minimum %s level(s) and maximum %s level(s).",+ ui <- function(id, ...) { |
|
359 | +30 | ! |
- var_name, min_levels, max_levels+ teal.reporter::reporter_previewer_ui(id, ...) |
360 | +31 |
- )+ } |
|
361 | +32 |
- ))+ |
|
362 | -! | +||
33 | +2x |
- } else if (!is.null(min_levels)) {+ module <- module( |
|
363 | -! | +||
34 | +2x |
- validate(need(+ label = "temporary label", |
|
364 | -! | +||
35 | +2x |
- length(x_levels) >= min_levels,+ server = srv, ui = ui, |
|
365 | -! | +||
36 | +2x |
- sprintf("%s variable needs minimum %s levels(s)", var_name, min_levels)+ server_args = server_args, ui_args = list(), datanames = NULL |
|
366 | +37 |
- ))- |
- |
367 | -! | -
- } else if (!is.null(max_levels)) {+ ) |
|
368 | -! | +||
38 | +
- validate(need(+ # Module is created with a placeholder label and the label is changed later. |
||
369 | -! | +||
39 | +
- length(x_levels) <= max_levels,+ # This is to prevent another module being labeled "Report previewer". |
||
370 | -! | +||
40 | +2x |
- sprintf("%s variable needs maximum %s level(s)", var_name, max_levels)+ class(module) <- c("teal_module_previewer", class(module)) |
|
371 | -+ | ||
41 | +2x |
- ))+ module$label <- label |
|
372 | -+ | ||
42 | +2x |
- }+ module |
|
373 | +43 |
}@@ -27997,6 +28731,34 @@ teal coverage - 71.59%RcodeBlock <- getFromNamespace("RcodeBlock", "teal.reporter") # nolint |
|
35 | ++ | + + | +|
36 | ++ |
+ # Use non-exported function(s) from teal.code+ |
+ |
37 | ++ |
+ # This one is here because lang2calls should not be exported from teal.code+ |
+ |
38 | ++ |
+ lang2calls <- getFromNamespace("lang2calls", "teal.code")+ |
+
1 |
- #' Data module for `teal` applications+ #' Show R Code Modal |
||
3 |
- #' Creates `teal_data_module` object - a `shiny` module to supply or modify data in a `teal` application.+ #' @export |
||
4 |
- #'+ #' @description `r lifecycle::badge("stable")` |
||
5 |
- #' This function creates a `shiny` module that allows for running data pre-processing code after the app starts.+ #' Use the [shiny::showModal()] function to show the R code inside. |
||
6 |
- #' The body of the server function will be run in the app rather than in the global environment.+ #' |
||
7 |
- #' This means it will be run every time the app starts, so use sparingly.+ #' @param title (`character(1)`)\cr |
||
8 |
- #'+ #' Title of the modal, displayed in the first comment of the R-code. |
||
9 |
- #' Pass this module instead of a `teal_data` object in a call to [init()].+ #' @param rcode (`character`)\cr |
||
10 |
- #'+ #' vector with R code to show inside the modal. |
||
11 |
- #' See vignette \code{vignette("data-as-shiny-module", package = "teal")} for more details.+ #' @param session (`ShinySession` optional)\cr |
||
12 |
- #'+ #' `shiny` Session object, if missing then [shiny::getDefaultReactiveDomain()] is used. |
||
13 |
- #' @param ui (`function(id)`)\cr+ #' |
||
14 |
- #' `shiny` module `ui` function; must only take `id` argument+ #' @references [shiny::showModal()] |
||
15 |
- #' @param server (`function(id)`)\cr+ show_rcode_modal <- function(title = NULL, rcode, session = getDefaultReactiveDomain()) { |
||
16 | -+ | ! |
- #' `shiny` module `ui` function; must only take `id` argument;+ rcode <- paste(rcode, collapse = "\n") |
17 |
- #' must return reactive expression containing `teal_data` object+ |
||
18 | -+ | ! |
- #'+ ns <- session$ns |
19 | -+ | ! |
- #' @return Object of class `teal_data_module`.+ showModal(modalDialog( |
20 | -+ | ! |
- #'+ tagList( |
21 | -+ | ! |
- #' @examples+ tags$div( |
22 | -+ | ! |
- #' data <- teal_data_module(+ actionButton(ns("copyRCode"), "Copy to Clipboard", `data-clipboard-target` = paste0("#", ns("r_code"))), |
23 | -+ | ! |
- #' ui = function(id) {+ modalButton("Dismiss"), |
24 | -+ | ! |
- #' ns <- NS(id)+ style = "mb-4" |
25 |
- #' actionButton(ns("submit"), label = "Load data")+ ), |
||
26 | -+ | ! |
- #' },+ tags$div(tags$pre(id = ns("r_code"), rcode)), |
27 |
- #' server = function(id) {+ ), |
||
28 | -+ | ! |
- #' moduleServer(id, function(input, output, session) {+ title = title, |
29 | -+ | ! |
- #' eventReactive(input$submit, {+ footer = tagList( |
30 | -+ | ! |
- #' data <- within(+ actionButton(ns("copyRCode"), "Copy to Clipboard", `data-clipboard-target` = paste0("#", ns("r_code"))), |
31 | -+ | ! |
- #' teal_data(),+ modalButton("Dismiss") |
32 |
- #' {+ ), |
||
33 | -+ | ! |
- #' dataset1 <- iris+ size = "l", |
34 | -+ | ! |
- #' dataset2 <- mtcars+ easyClose = TRUE |
35 |
- #' }+ )) |
||
36 |
- #' )+ |
||
37 | -+ | ! |
- #' datanames(data) <- c("iris", "mtcars")+ return(NULL) |
38 |
- #'+ } |
39 | +1 |
- #' data+ # This file contains Shiny modules useful for debugging and developing teal. |
|
40 | +2 |
- #' })+ # We do not export the functions in this file. They are for |
|
41 | +3 |
- #' })+ # developers only and can be accessed via `:::`. |
|
42 | +4 |
- #' }+ |
|
43 | +5 |
- #' )+ #' Dummy module to show the filter calls generated by the right encoding panel |
|
44 | +6 |
- #' @export+ #' |
|
45 | +7 |
- teal_data_module <- function(ui, server) {- |
- |
46 | -10x | -
- checkmate::assert_function(ui, args = "id", nargs = 1)- |
- |
47 | -9x | -
- checkmate::assert_function(server, args = "id", nargs = 1)- |
- |
48 | -8x | -
- structure(- |
- |
49 | -8x | -
- list(ui = ui, server = server),- |
- |
50 | -8x | -
- class = "teal_data_module"+ #' |
|
51 | +8 |
- )+ #' Please do not remove, this is useful for debugging teal without |
|
52 | +9 |
- }+ #' dependencies and simplifies `\link[devtools]{load_all}` which otherwise fails |
1 | +10 |
- #' Show R Code Modal+ #' and avoids session restarts! |
|
2 | +11 |
#' |
|
3 | +12 |
- #' @export+ #' @param label `character` label of module |
|
4 | +13 |
- #' @description `r lifecycle::badge("stable")`+ #' @keywords internal |
|
5 | +14 |
- #' Use the [shiny::showModal()] function to show the R code inside.+ #' |
|
6 | +15 |
- #'+ #' @examples |
|
7 | +16 |
- #' @param title (`character(1)`)\cr+ #' app <- init( |
|
8 | +17 |
- #' Title of the modal, displayed in the first comment of the R-code.+ #' data = list(iris = iris, mtcars = mtcars), |
|
9 | +18 |
- #' @param rcode (`character`)\cr+ #' modules = teal:::filter_calls_module(), |
|
10 | +19 |
- #' vector with R code to show inside the modal.+ #' header = "Simple teal app" |
|
11 | +20 |
- #' @param session (`ShinySession` optional)\cr+ #' ) |
|
12 | +21 |
- #' `shiny` Session object, if missing then [shiny::getDefaultReactiveDomain()] is used.+ #' if (interactive()) { |
|
13 | +22 |
- #'+ #' shinyApp(app$ui, app$server) |
|
14 | +23 |
- #' @references [shiny::showModal()]+ #' } |
|
15 | +24 |
- show_rcode_modal <- function(title = NULL, rcode, session = getDefaultReactiveDomain()) {+ filter_calls_module <- function(label = "Filter Calls Module") { # nolint |
|
16 | +25 | ! |
- rcode <- paste(rcode, collapse = "\n")+ checkmate::assert_string(label) |
17 | +26 | ||
18 | -! | -
- ns <- session$ns- |
- |
19 | +27 | ! |
- showModal(modalDialog(+ module( |
20 | +28 | ! |
- tagList(+ label = label, |
21 | +29 | ! |
- tags$div(+ server = function(input, output, session, data) { |
22 | +30 | ! |
- actionButton(ns("copyRCode"), "Copy to Clipboard", `data-clipboard-target` = paste0("#", ns("r_code"))),+ checkmate::assert_class(data, "tdata") |
23 | -! | +||
31 | +
- modalButton("Dismiss"),+ |
||
24 | +32 | ! |
- style = "mb-4"- |
-
25 | -- |
- ),+ output$filter_calls <- renderText({ |
|
26 | +33 | ! |
- tags$div(tags$pre(id = ns("r_code"), rcode)),+ get_code_tdata(data) |
27 | +34 |
- ),+ }) |
|
28 | -! | +||
35 | +
- title = title,+ }, |
||
29 | +36 | ! |
- footer = tagList(+ ui = function(id, ...) { |
30 | +37 | ! |
- actionButton(ns("copyRCode"), "Copy to Clipboard", `data-clipboard-target` = paste0("#", ns("r_code"))),+ ns <- NS(id) |
31 | +38 | ! |
- modalButton("Dismiss")- |
-
32 | -- |
- ),+ div( |
|
33 | +39 | ! |
- size = "l",+ h2("The following filter calls are generated:"), |
34 | +40 | ! |
- easyClose = TRUE+ verbatimTextOutput(ns("filter_calls")) |
35 | +41 |
- ))+ ) |
|
36 | +42 |
-
+ }, |
|
37 | +43 | ! |
- return(NULL)+ datanames = "all" |
38 | +44 | ++ |
+ )+ |
+
45 |
}@@ -29264,320 +29977,369 @@ teal coverage - 71.59% |
1 |
- # This file contains Shiny modules useful for debugging and developing teal.+ #' Data module for `teal` applications |
||
2 |
- # We do not export the functions in this file. They are for+ #' |
||
3 |
- # developers only and can be accessed via `:::`.+ #' Creates `teal_data_module` object - a `shiny` module to supply or modify data in a `teal` application. |
||
4 |
-
+ #' |
||
5 |
- #' Dummy module to show the filter calls generated by the right encoding panel+ #' This function creates a `shiny` module that allows for running data pre-processing code after the app starts. |
||
6 |
- #'+ #' The body of the server function will be run in the app rather than in the global environment. |
||
7 |
- #'+ #' This means it will be run every time the app starts, so use sparingly. |
||
8 |
- #' Please do not remove, this is useful for debugging teal without+ #' |
||
9 |
- #' dependencies and simplifies `\link[devtools]{load_all}` which otherwise fails+ #' Pass this module instead of a `teal_data` object in a call to [init()]. |
||
10 |
- #' and avoids session restarts!+ #' |
||
11 |
- #'+ #' See vignette \code{vignette("data-as-shiny-module", package = "teal")} for more details. |
||
12 |
- #' @param label `character` label of module+ #' |
||
13 |
- #' @keywords internal+ #' @param ui (`function(id)`)\cr |
||
14 |
- #'+ #' `shiny` module `ui` function; must only take `id` argument |
||
15 |
- #' @examples+ #' @param server (`function(id)`)\cr |
||
16 |
- #' app <- init(+ #' `shiny` module `ui` function; must only take `id` argument; |
||
17 |
- #' data = list(iris = iris, mtcars = mtcars),+ #' must return reactive expression containing `teal_data` object |
||
18 |
- #' modules = teal:::filter_calls_module(),+ #' |
||
19 |
- #' header = "Simple teal app"+ #' @return Object of class `teal_data_module`. |
||
20 |
- #' )+ #' |
||
21 |
- #' if (interactive()) {+ #' @examples |
||
22 |
- #' shinyApp(app$ui, app$server)+ #' data <- teal_data_module( |
||
23 |
- #' }+ #' ui = function(id) { |
||
24 |
- filter_calls_module <- function(label = "Filter Calls Module") { # nolint+ #' ns <- NS(id) |
||
25 | -! | +
- checkmate::assert_string(label)+ #' actionButton(ns("submit"), label = "Load data") |
|
26 |
-
+ #' }, |
||
27 | -! | +
- module(+ #' server = function(id) { |
|
28 | -! | +
- label = label,+ #' moduleServer(id, function(input, output, session) { |
|
29 | -! | +
- server = function(input, output, session, data) {+ #' eventReactive(input$submit, { |
|
30 | -! | +
- checkmate::assert_class(data, "tdata")+ #' data <- within( |
|
31 |
-
+ #' teal_data(), |
||
32 | -! | +
- output$filter_calls <- renderText({+ #' { |
|
33 | -! | +
- get_code_tdata(data)+ #' dataset1 <- iris |
|
34 |
- })+ #' dataset2 <- mtcars |
||
35 |
- },+ #' } |
||
36 | -! | +
- ui = function(id, ...) {+ #' ) |
|
37 | -! | +
- ns <- NS(id)+ #' datanames(data) <- c("dataset1", "dataset2") |
|
38 | -! | +
- div(+ #' |
|
39 | -! | +
- h2("The following filter calls are generated:"),+ #' data |
|
40 | -! | +
- verbatimTextOutput(ns("filter_calls"))+ #' }) |
|
41 |
- )+ #' }) |
||
42 |
- },+ #' } |
||
43 | -! | +
- datanames = "all"+ #' ) |
|
44 |
- )+ #' @export |
||
45 | + |
+ teal_data_module <- function(ui, server) {+ |
+ |
46 | +35x | +
+ checkmate::assert_function(ui, args = "id", nargs = 1)+ |
+ |
47 | +34x | +
+ checkmate::assert_function(server, args = "id", nargs = 1)+ |
+ |
48 | +33x | +
+ structure(+ |
+ |
49 | +33x | +
+ list(ui = ui, server = server),+ |
+ |
50 | +33x | +
+ class = "teal_data_module"+ |
+ |
51 | ++ |
+ )+ |
+ |
52 | +
} |