diff --git a/main/coverage-report/index.html b/main/coverage-report/index.html index 174c07972f..fe41f0a21e 100644 --- a/main/coverage-report/index.html +++ b/main/coverage-report/index.html @@ -94,7 +94,7 @@ font-size: 11px; }
1 |
- #' Store teal_slices object to a file+ #' @title `TealReportCard` |
||
2 |
- #'+ #' @description `r lifecycle::badge("experimental")` |
||
3 |
- #' This function takes a `teal_slices` object and saves it to a file in `JSON` format.+ #' A child of [`ReportCard`] that is used for teal specific applications. |
||
4 |
- #' The `teal_slices` object contains information about filter states and can be used to+ #' In addition to the parent methods, it supports rendering teal specific elements such as |
||
5 |
- #' create, modify, and delete filter states. The saved file can be later loaded using+ #' the source code, the encodings panel content and the filter panel content as part of the |
||
6 |
- #' the `slices_restore` function.+ #' meta data. |
||
7 |
- #'+ #' @export |
||
8 |
- #' @param tss (`teal_slices`) object to be stored.+ #' |
||
9 |
- #' @param file (`character(1)`) The file path where `teal_slices` object will be saved.+ TealReportCard <- R6::R6Class( # nolint: object_name_linter. |
||
10 |
- #' The file extension should be `".json"`.+ classname = "TealReportCard", |
||
11 |
- #'+ inherit = teal.reporter::ReportCard, |
||
12 |
- #' @details `Date` class is stored in `"ISO8601"` format (`YYYY-MM-DD`). `POSIX*t` classes are converted to a+ public = list( |
||
13 |
- #' character by using `format.POSIX*t(usetz = TRUE, tz = "UTC")` (`YYYY-MM-DD {N}{N}:{N}{N}:{N}{N} UTC`, where+ #' @description Appends the source code to the `content` meta data of this `TealReportCard`. |
||
14 |
- #' `{N} = [0-9]` is a number and `UTC` is `Coordinated Universal Time` timezone short-code).+ #' |
||
15 |
- #' This format is assumed during `slices_restore`. All `POSIX*t` objects in `selected` or `choices` fields of+ #' @param src (`character(1)`) code as text. |
||
16 |
- #' `teal_slice` objects are always printed in `UTC` timezone as well.+ #' @param ... any `rmarkdown` R chunk parameter and its value. |
||
17 |
- #'+ #' But `eval` parameter is always set to `FALSE`. |
||
18 |
- #' @return `NULL`, invisibly.+ #' @return invisibly self |
||
19 |
- #'+ #' @examples |
||
20 |
- #' @keywords internal+ #' card <- TealReportCard$new()$append_src( |
||
21 |
- #'+ #' "plot(iris)" |
||
22 |
- #' @examples+ #' ) |
||
23 |
- #' # Create a teal_slices object+ #' card$get_content()[[1]]$get_content() |
||
24 |
- #' tss <- teal_slices(+ append_src = function(src, ...) { |
||
25 | -+ | 4x |
- #' teal_slice(dataname = "data", varname = "var"),+ checkmate::assert_character(src, min.len = 0, max.len = 1) |
26 | -+ | 4x |
- #' teal_slice(dataname = "data", expr = "x > 0", id = "positive_x", title = "Positive x")+ params <- list(...) |
27 | -+ | 4x |
- #' )+ params$eval <- FALSE |
28 | -+ | 4x |
- #'+ rblock <- RcodeBlock$new(src) |
29 | -+ | 4x |
- #' if (interactive()) {+ rblock$set_params(params) |
30 | -+ | 4x |
- #' # Store the teal_slices object to a file+ self$append_content(rblock) |
31 | -+ | 4x |
- #' slices_store(tss, "path/to/file.json")+ self$append_metadata("SRC", src) |
32 | -+ | 4x |
- #' }+ invisible(self) |
33 |
- #'+ }, |
||
34 |
- slices_store <- function(tss, file) {+ #' @description Appends the filter state list to the `content` and `metadata` of this `TealReportCard`. |
||
35 | -9x | +
- checkmate::assert_class(tss, "teal_slices")+ #' If the filter state list has an attribute named `formatted`, it appends it to the card otherwise it uses |
|
36 | -9x | +
- checkmate::assert_path_for_output(file, overwrite = TRUE, extension = "json")+ #' the default `yaml::as.yaml` to format the list. |
|
37 |
-
+ #' If the filter state list is empty, nothing is appended to the `content`. |
||
38 | -9x | +
- cat(format(tss, trim_lines = FALSE), "\n", file = file)+ #' |
|
39 |
- }+ #' @param fs (`teal_slices`) object returned from [teal_slices()] function. |
||
40 |
-
+ #' @return invisibly self |
||
41 |
- #' Restore teal_slices object from a file+ append_fs = function(fs) { |
||
42 | -+ | 5x |
- #'+ checkmate::assert_class(fs, "teal_slices") |
43 | -+ | 4x |
- #' This function takes a file path to a `JSON` file containing a `teal_slices` object+ self$append_text("Filter State", "header3") |
44 | -+ | 4x |
- #' and restores it to its original form. The restored `teal_slices` object can be used+ self$append_content(TealSlicesBlock$new(fs)) |
45 | -+ | 4x |
- #' to access filter states and their corresponding attributes.+ invisible(self) |
46 |
- #'+ }, |
||
47 |
- #' @param file Path to file where `teal_slices` is stored. Must have a `.json` extension and read access.+ #' @description Appends the encodings list to the `content` and `metadata` of this `TealReportCard`. |
||
48 |
- #'+ #' |
||
49 |
- #' @return A `teal_slices` object restored from the file.+ #' @param encodings (`list`) list of encodings selections of the teal app. |
||
50 |
- #'+ #' @return invisibly self |
||
51 |
- #' @keywords internal+ #' @examples |
||
52 |
- #'+ #' card <- TealReportCard$new()$append_encodings(list(variable1 = "X")) |
||
53 |
- #' @examples+ #' card$get_content()[[1]]$get_content() |
||
54 |
- #' if (interactive()) {+ #' |
||
55 |
- #' # Restore a teal_slices object from a file+ append_encodings = function(encodings) { |
||
56 | -+ | 4x |
- #' tss_restored <- slices_restore("path/to/file.json")+ checkmate::assert_list(encodings) |
57 | -+ | 4x |
- #' }+ self$append_text("Selected Options", "header3") |
58 | -+ | 4x |
- #'+ if (requireNamespace("yaml", quietly = TRUE)) { |
59 | -+ | 4x |
- slices_restore <- function(file) {+ self$append_text(yaml::as.yaml(encodings, handlers = list( |
60 | -9x | +4x |
- checkmate::assert_file_exists(file, access = "r", extension = "json")+ POSIXct = function(x) format(x, "%Y-%m-%d"), |
61 | -+ | 4x |
-
+ POSIXlt = function(x) format(x, "%Y-%m-%d"), |
62 | -9x | +4x |
- tss_json <- jsonlite::fromJSON(file, simplifyDataFrame = FALSE)+ Date = function(x) format(x, "%Y-%m-%d") |
63 | -9x | +4x |
- tss_json$slices <-+ )), "verbatim") |
64 | -9x | +
- lapply(tss_json$slices, function(slice) {+ } else { |
|
65 | -9x | +! |
- for (field in c("selected", "choices")) {+ stop("yaml package is required to format the encodings list") |
66 | -18x | +
- if (!is.null(slice[[field]])) {+ } |
|
67 | -12x | +4x |
- if (length(slice[[field]]) > 0) {+ self$append_metadata("Encodings", encodings) |
68 | -9x | +4x |
- date_partial_regex <- "^[0-9]{4}-[0-9]{2}-[0-9]{2}"+ invisible(self) |
69 | -9x | +
- time_stamp_regex <- paste0(date_partial_regex, "\\s[0-9]{2}:[0-9]{2}:[0-9]{2}\\sUTC$")+ } |
|
70 |
-
+ ), |
||
71 | -9x | +
- slice[[field]] <-+ private = list() |
|
72 | -9x | +
- if (all(grepl(paste0(date_partial_regex, "$"), slice[[field]]))) {+ ) |
|
73 | -3x | +
- as.Date(slice[[field]])+ |
|
74 | -9x | +
- } else if (all(grepl(time_stamp_regex, slice[[field]]))) {+ #' @title `RcodeBlock` |
|
75 | -3x | +
- as.POSIXct(slice[[field]], tz = "UTC")+ #' @keywords internal |
|
76 |
- } else {+ TealSlicesBlock <- R6::R6Class( # nolint: object_name_linter. |
||
77 | -3x | +
- slice[[field]]+ classname = "TealSlicesBlock", |
|
78 |
- }+ inherit = teal.reporter:::TextBlock, |
||
79 |
- } else {+ public = list( |
||
80 | -3x | +
- slice[[field]] <- character(0)+ #' @description Returns a `TealSlicesBlock` object. |
|
81 |
- }+ #' |
||
82 |
- }+ #' @details Returns a `TealSlicesBlock` object with no content and no parameters. |
||
83 |
- }+ #' |
||
84 | -9x | +
- slice+ #' @param content (`teal_slices`) object returned from [teal_slices()] function. |
|
85 |
- })+ #' @param style (`character(1)`) string specifying style to apply. |
||
86 |
-
+ #' |
||
87 | -9x | +
- tss_elements <- lapply(tss_json$slices, as.teal_slice)+ #' @return `TealSlicesBlock` |
|
88 |
-
+ #' @examples |
||
89 | -9x | +
- do.call(teal_slices, c(tss_elements, tss_json$attributes))+ #' block <- teal:::TealSlicesBlock$new() |
|
90 |
- }+ #' |
1 | +91 |
- #' Creates a `teal_modules` object.+ initialize = function(content = teal_slices(), style = "verbatim") { |
|
2 | -+ | ||
92 | +10x |
- #'+ self$set_content(content) |
|
3 | -+ | ||
93 | +9x |
- #' @description `r lifecycle::badge("stable")`+ self$set_style(style) |
|
4 | -+ | ||
94 | +9x |
- #' This function collects a list of `teal_modules` and `teal_module` objects and returns a `teal_modules` object+ invisible(self) |
|
5 | +95 |
- #' containing the passed objects.+ }, |
|
6 | +96 |
- #'+ |
|
7 | +97 |
- #' This function dictates what modules are included in a `teal` application. The internal structure of `teal_modules`+ #' @description Sets content of this `TealSlicesBlock`. |
|
8 | +98 |
- #' shapes the navigation panel of a `teal` application.+ #' Sets content as `YAML` text which represents a list generated from `teal_slices`. |
|
9 | +99 |
- #'+ #' The list displays limited number of fields from `teal_slice` objects, but this list is |
|
10 | +100 |
- #' @param ... (`teal_module` or `teal_modules`) see [module()] and [modules()] for more details+ #' sufficient to conclude which filters were applied. |
|
11 | +101 |
- #' @param label (`character(1)`) label of modules collection (default `"root"`).+ #' When `selected` field in `teal_slice` object is a range, then it is displayed as a "min" |
|
12 | +102 |
- #' If using the `label` argument then it must be explicitly named.+ #' |
|
13 | +103 |
- #' For example `modules("lab", ...)` should be converted to `modules(label = "lab", ...)`+ #' |
|
14 | +104 |
- #'+ #' @param content (`teal_slices`) object returned from [teal_slices()] function. |
|
15 | +105 |
- #' @export+ #' @return invisibly self |
|
16 | +106 |
- #'+ set_content = function(content) { |
|
17 | -+ | ||
107 | +11x |
- #' @return object of class \code{teal_modules}. Object contains following fields+ checkmate::assert_class(content, "teal_slices") |
|
18 | -+ | ||
108 | +10x |
- #' - `label`: taken from the `label` argument+ if (length(content) != 0) { |
|
19 | -+ | ||
109 | +7x |
- #' - `children`: a list containing objects passed in `...`. List elements are named after+ states_list <- lapply(content, function(x) { |
|
20 | -+ | ||
110 | +7x |
- #' their `label` attribute converted to a valid `shiny` id.+ x_list <- shiny::isolate(as.list(x)) |
|
21 | -+ | ||
111 | +7x |
- #' @examples+ if ( |
|
22 | -+ | ||
112 | +7x |
- #' library(shiny)+ inherits(x_list$choices, c("integer", "numeric", "Date", "POSIXct", "POSIXlt")) && |
|
23 | -+ | ||
113 | +7x |
- #'+ length(x_list$choices) == 2 && |
|
24 | -+ | ||
114 | +7x |
- #' app <- init(+ length(x_list$selected) == 2 |
|
25 | +115 |
- #' data = teal_data(iris = iris),+ ) { |
|
26 | -+ | ||
116 | +! |
- #' modules = modules(+ x_list$range <- paste(x_list$selected, collapse = " - ") |
|
27 | -+ | ||
117 | +! |
- #' label = "Modules",+ x_list["selected"] <- NULL |
|
28 | +118 |
- #' modules(+ } |
|
29 | -+ | ||
119 | +7x |
- #' label = "Module",+ if (!is.null(x_list$arg)) { |
|
30 | -+ | ||
120 | +! |
- #' module(+ x_list$arg <- if (x_list$arg == "subset") "Genes" else "Samples" |
|
31 | +121 |
- #' label = "Inner module",+ } |
|
32 | +122 |
- #' server = function(id, data) {+ |
|
33 | -+ | ||
123 | +7x |
- #' moduleServer(+ x_list <- x_list[ |
|
34 | -+ | ||
124 | +7x |
- #' id,+ c("dataname", "varname", "experiment", "arg", "expr", "selected", "range", "keep_na", "keep_inf") |
|
35 | +125 |
- #' module = function(input, output, session) {+ ] |
|
36 | -+ | ||
126 | +7x |
- #' output$data <- renderDataTable(data[["iris"]]())+ names(x_list) <- c( |
|
37 | -+ | ||
127 | +7x |
- #' }+ "Dataset name", "Variable name", "Experiment", "Filtering by", "Applied expression", |
|
38 | -+ | ||
128 | +7x |
- #' )+ "Selected Values", "Selected range", "Include NA values", "Include Inf values" |
|
39 | +129 |
- #' },+ ) |
|
40 | +130 |
- #' ui = function(id) {+ |
|
41 | -+ | ||
131 | +7x |
- #' ns <- NS(id)+ Filter(Negate(is.null), x_list) |
|
42 | +132 |
- #' tagList(dataTableOutput(ns("data")))+ }) |
|
43 | +133 |
- #' },+ + |
+ |
134 | +7x | +
+ if (requireNamespace("yaml", quietly = TRUE)) {+ |
+ |
135 | +7x | +
+ super$set_content(yaml::as.yaml(states_list)) |
|
44 | +136 |
- #' datanames = "all"+ } else {+ |
+ |
137 | +! | +
+ stop("yaml package is required to format the filter state list") |
|
45 | +138 |
- #' )+ } |
|
46 | +139 |
- #' ),+ }+ |
+ |
140 | +10x | +
+ private$teal_slices <- content+ |
+ |
141 | +10x | +
+ invisible(self) |
|
47 | +142 |
- #' module(+ }, |
|
48 | +143 |
- #' label = "Another module",+ #' @description Create the `RcodeBlock` from a list. |
|
49 | +144 |
- #' server = function(id) {+ #' @param x `named list` with two fields `c("text", "params")`. |
|
50 | +145 |
- #' moduleServer(+ #' Use the `get_available_params` method to get all possible parameters. |
|
51 | +146 |
- #' id,+ #' @return invisibly self |
|
52 | +147 |
- #' module = function(input, output, session) {+ from_list = function(x) {+ |
+ |
148 | +1x | +
+ checkmate::assert_list(x)+ |
+ |
149 | +1x | +
+ checkmate::assert_names(names(x), must.include = c("teal_slices"))+ |
+ |
150 | +1x | +
+ self$set_content(x$teal_slices)+ |
+ |
151 | +1x | +
+ invisible(self) |
|
53 | +152 |
- #' output$text <- renderText("Another module")+ }, |
|
54 | +153 |
- #' }+ #' @description Convert the `RcodeBlock` to a list. |
|
55 | +154 |
- #' )+ #' @return `named list` with a text and `params`. |
|
56 | +155 |
- #' },+ |
|
57 | +156 |
- #' ui = function(id) {+ to_list = function() {+ |
+ |
157 | +2x | +
+ list(teal_slices = private$teal_slices) |
|
58 | +158 |
- #' ns <- NS(id)+ } |
|
59 | +159 |
- #' tagList(textOutput(ns("text")))+ ), |
|
60 | +160 |
- #' },+ private = list( |
|
61 | +161 |
- #' datanames = NULL+ style = "verbatim", |
|
62 | +162 |
- #' )+ teal_slices = NULL # teal_slices |
|
63 | +163 |
- #' )+ ) |
|
64 | +164 |
- #' )+ ) |
65 | +1 |
- #' if (interactive()) {+ #' Get Client Timezone |
||
66 | +2 |
- #' shinyApp(app$ui, app$server)+ #' |
||
67 | +3 |
- #' }+ #' Local timezone in the browser may differ from the system timezone from the server. |
||
68 | +4 |
- modules <- function(..., label = "root") {+ #' This script can be run to register a shiny input which contains information about |
||
69 | -109x | -
- checkmate::assert_string(label)- |
- ||
70 | -107x | -
- submodules <- list(...)- |
- ||
71 | -107x | -
- if (any(vapply(submodules, is.character, FUN.VALUE = logical(1)))) {- |
- ||
72 | -2x | -
- stop(- |
- ||
73 | -2x | -
- "The only character argument to modules() must be 'label' and it must be named, ",- |
- ||
74 | -2x | +|||
5 | +
- "change modules('lab', ...) to modules(label = 'lab', ...)"+ #' the timezone in the browser. |
|||
75 | +6 |
- )+ #' |
||
76 | +7 |
- }+ #' @param ns (`function`) namespace function passed from the `session` object in the |
||
77 | +8 |
-
+ #' Shiny server. For Shiny modules this will allow for proper name spacing of the |
||
78 | -105x | +|||
9 | +
- checkmate::assert_list(submodules, min.len = 1, any.missing = FALSE, types = c("teal_module", "teal_modules"))+ #' registered input. |
|||
79 | +10 |
- # name them so we can more easily access the children+ #' |
||
80 | +11 |
- # beware however that the label of the submodules should not be changed as it must be kept synced+ #' @return (`Shiny`) input variable accessible with `input$tz` which is a (`character`) |
||
81 | -102x | +|||
12 | +
- labels <- vapply(submodules, function(submodule) submodule$label, character(1))+ #' string containing the timezone of the browser/client. |
|||
82 | -102x | +|||
13 | +
- names(submodules) <- make.unique(gsub("[^[:alnum:]]+", "_", labels), sep = "_")+ #' @keywords internal |
|||
83 | -102x | +|||
14 | +
- structure(+ get_client_timezone <- function(ns) { |
|||
84 | -102x | +15 | +18x |
- list(+ script <- sprintf( |
85 | -102x | +16 | +18x |
- label = label,+ "Shiny.setInputValue(`%s`, Intl.DateTimeFormat().resolvedOptions().timeZone)", |
86 | -102x | +17 | +18x |
- children = submodules+ ns("timezone") |
87 | +18 |
- ),+ ) |
||
88 | -102x | +19 | +18x |
- class = "teal_modules"+ shinyjs::runjs(script) # function does not return anything |
89 | -+ | |||
20 | +18x |
- )+ return(invisible(NULL)) |
||
90 | +21 |
} |
||
91 | +22 | |||
92 | +23 |
- #' Append a `teal_module` to `children` of a `teal_modules` object+ #' Resolve the expected bootstrap theme |
||
93 | +24 |
#' @keywords internal |
||
94 | -- |
- #' @param modules `teal_modules`- |
- ||
95 | -- |
- #' @param module `teal_module` object to be appended onto the children of `modules`- |
- ||
96 | -- |
- #' @return `teal_modules` object with `module` appended- |
- ||
97 | +25 |
- append_module <- function(modules, module) {+ get_teal_bs_theme <- function() { |
||
98 | -7x | +26 | +16x |
- checkmate::assert_class(modules, "teal_modules")+ bs_theme <- getOption("teal.bs_theme") |
99 | -5x | +27 | +16x |
- checkmate::assert_class(module, "teal_module")+ if (is.null(bs_theme)) { |
100 | -3x | +28 | +13x |
- modules$children <- c(modules$children, list(module))+ NULL |
101 | +29 | 3x |
- labels <- vapply(modules$children, function(submodule) submodule$label, character(1))+ } else if (!inherits(bs_theme, "bs_theme")) { |
|
102 | -3x | +30 | +2x |
- names(modules$children) <- make.unique(gsub("[^[:alnum:]]", "_", tolower(labels)), sep = "_")+ warning("teal.bs_theme has to be of a bslib::bs_theme class, the default shiny bootstrap is used.") |
103 | -3x | +31 | +2x |
- modules+ NULL |
104 | +32 |
- }+ } else { |
||
105 | -+ | |||
33 | +1x |
-
+ bs_theme |
||
106 | +34 |
- #' Extract/Remove module(s) of specific class+ } |
||
107 | +35 |
- #'+ } |
||
108 | +36 |
- #' Given a `teal_module` or a `teal_modules`, return the elements of the structure according to `class`.+ |
||
109 | +37 |
- #'+ include_parent_datanames <- function(dataname, join_keys) { |
||
110 | -+ | |||
38 | +3x |
- #' @param modules `teal_modules`+ parents <- character(0) |
||
111 | -+ | |||
39 | +3x |
- #' @param class The class name of `teal_module` to be extracted or dropped.+ for (i in dataname) { |
||
112 | -+ | |||
40 | +6x |
- #' @keywords internal+ while (length(i) > 0) { |
||
113 | -+ | |||
41 | +6x |
- #' @return+ parent_i <- teal.data::parent(join_keys, i) |
||
114 | -+ | |||
42 | +6x |
- #' For `extract_module`, a `teal_module` of class `class` or `teal_modules` containing modules of class `class`.+ parents <- c(parent_i, parents)+ |
+ ||
43 | +6x | +
+ i <- parent_i |
||
115 | +44 |
- #' For `drop_module`, the opposite, which is all `teal_modules` of class other than `class`.+ } |
||
116 | +45 |
- #' @rdname module_management+ } |
||
117 | +46 |
- extract_module <- function(modules, class) {+ |
||
118 | -30x | +47 | +3x |
- if (inherits(modules, class)) {+ return(unique(c(parents, dataname))) |
119 | -! | +|||
48 | +
- modules+ } |
|||
120 | -30x | +|||
49 | +
- } else if (inherits(modules, "teal_module")) {+ |
|||
121 | -16x | +|||
50 | +
- NULL+ |
|||
122 | -14x | +|||
51 | +
- } else if (inherits(modules, "teal_modules")) {+ |
|||
123 | -14x | +|||
52 | +
- Filter(function(x) length(x) > 0L, lapply(modules$children, extract_module, class))+ #' Create a `FilteredData` |
|||
124 | +53 |
- }+ #' |
||
125 | +54 |
- }+ #' Create a `FilteredData` object from a `teal_data` object |
||
126 | +55 |
-
+ #' @param x (`teal_data`) object |
||
127 | +56 |
- #' @keywords internal+ #' @param datanames (`character`) vector of data set names to include; must be subset of `datanames(x)` |
||
128 | +57 |
- #' @return `teal_modules`+ #' @return (`FilteredData`) object |
||
129 | +58 |
- #' @rdname module_management+ #' @keywords internal |
||
130 | +59 |
- drop_module <- function(modules, class) {+ teal_data_to_filtered_data <- function(x, datanames = teal.data::datanames(x)) { |
||
131 | -30x | -
- if (inherits(modules, class)) {- |
- ||
132 | -! | +60 | +12x |
- NULL+ checkmate::assert_class(x, "teal_data") |
133 | -30x | +61 | +12x |
- } else if (inherits(modules, "teal_module")) {+ checkmate::assert_character(datanames, min.len = 1L, any.missing = FALSE) |
134 | -16x | +62 | +12x |
- modules+ checkmate::assert_subset(datanames, teal.data::datanames(x)) |
135 | -14x | +|||
63 | +
- } else if (inherits(modules, "teal_modules")) {+ |
|||
136 | -14x | +64 | +12x |
- do.call(+ ans <- teal.slice::init_filtered_data( |
137 | -14x | +65 | +12x |
- "modules",+ x = sapply(datanames, function(dn) x[[dn]], simplify = FALSE), |
138 | -14x | +66 | +12x |
- c(Filter(function(x) length(x) > 0L, lapply(modules$children, drop_module, class)), label = modules$label)+ join_keys = teal.data::join_keys(x) |
139 | +67 |
- )+ ) |
||
140 | +68 |
- }+ # Piggy-back entire pre-processing code so that filtering code can be appended later.+ |
+ ||
69 | +12x | +
+ attr(ans, "preprocessing_code") <- teal.code::get_code(x)+ |
+ ||
70 | +12x | +
+ ans |
||
141 | +71 |
} |
||
142 | +72 | |||
143 | +73 |
- #' Does the object make use of the `arg`+ #' Template Function for `TealReportCard` Creation and Customization |
||
144 | +74 |
#' |
||
145 | +75 |
- #' @param modules (`teal_module` or `teal_modules`) object+ #' This function generates a report card with a title, |
||
146 | +76 |
- #' @param arg (`character(1)`) names of the arguments to be checked against formals of `teal` modules.+ #' an optional description, and the option to append the filter state list. |
||
147 | +77 |
- #' @return `logical` whether the object makes use of `arg`+ #' |
||
148 | +78 |
- #' @rdname is_arg_used+ #' @param title (`character(1)`) title of the card (unless overwritten by label) |
||
149 | +79 |
- #' @keywords internal+ #' @param label (`character(1)`) label provided by the user when adding the card |
||
150 | +80 |
- is_arg_used <- function(modules, arg) {+ #' @param description (`character(1)`) optional additional description |
||
151 | -286x | +|||
81 | +
- checkmate::assert_string(arg)+ #' @param with_filter (`logical(1)`) flag indicating to add filter state |
|||
152 | -283x | +|||
82 | +
- if (inherits(modules, "teal_modules")) {+ #' @param filter_panel_api (`FilterPanelAPI`) object with API that allows the generation |
|||
153 | -29x | +|||
83 | +
- any(unlist(lapply(modules$children, is_arg_used, arg)))+ #' of the filter state in the report |
|||
154 | -254x | +|||
84 | +
- } else if (inherits(modules, "teal_module")) {+ #' |
|||
155 | -43x | +|||
85 | +
- is_arg_used(modules$server, arg) || is_arg_used(modules$ui, arg)+ #' @return (`TealReportCard`) populated with a title, description and filter state |
|||
156 | -211x | +|||
86 | +
- } else if (is.function(modules)) {+ #' |
|||
157 | -209x | +|||
87 | +
- isTRUE(arg %in% names(formals(modules)))+ #' @export |
|||
158 | +88 |
- } else {+ report_card_template <- function(title, label, description = NULL, with_filter, filter_panel_api) { |
||
159 | +89 | 2x |
- stop("is_arg_used function not implemented for this object")+ checkmate::assert_string(title) |
|
160 | -+ | |||
90 | +2x |
- }+ checkmate::assert_string(label) |
||
161 | -+ | |||
91 | +2x |
- }+ checkmate::assert_string(description, null.ok = TRUE) |
||
162 | -+ | |||
92 | +2x |
-
+ checkmate::assert_flag(with_filter) |
||
163 | -+ | |||
93 | +2x |
-
+ checkmate::assert_class(filter_panel_api, classes = "FilterPanelAPI") |
||
164 | +94 |
- #' Creates a `teal_module` object.+ |
||
165 | -+ | |||
95 | +2x |
- #'+ card <- teal::TealReportCard$new() |
||
166 | -+ | |||
96 | +2x |
- #' @description `r lifecycle::badge("stable")`+ title <- if (label == "") title else label |
||
167 | -+ | |||
97 | +2x |
- #' This function embeds a `shiny` module inside a `teal` application. One `teal_module` maps to one `shiny` module.+ card$set_name(title) |
||
168 | -+ | |||
98 | +2x |
- #'+ card$append_text(title, "header2") |
||
169 | -+ | |||
99 | +1x |
- #' @param label (`character(1)`) Label shown in the navigation item for the module. Any label possible except+ if (!is.null(description)) card$append_text(description, "header3") |
||
170 | -+ | |||
100 | +1x |
- #' `"global_filters"` - read more in `mapping` argument of [teal::teal_slices].+ if (with_filter) card$append_fs(filter_panel_api$get_filter_state()) |
||
171 | -+ | |||
101 | +2x |
- #' @param server (`function`) `shiny` module with following arguments:+ card |
||
172 | +102 |
- #' - `id` - teal will set proper shiny namespace for this module (see [shiny::moduleServer()]).+ } |
||
173 | +103 |
- #' - `input`, `output`, `session` - (not recommended) then [shiny::callModule()] will be used to call a module.+ #' Resolve `datanames` for the modules |
||
174 | +104 |
- #' - `data` (optional) module will receive a `teal_data` object, a list of reactive (filtered) data specified in+ #' |
||
175 | +105 |
- #' the `filters` argument.+ #' Modifies `module$datanames` to include names of the parent dataset (taken from `join_keys`). |
||
176 | +106 |
- #' - `datasets` (optional) module will receive `FilteredData`. (See `[teal.slice::FilteredData]`).+ #' When `datanames` is set to `"all"` it is replaced with all available datasets names. |
||
177 | +107 |
- #' - `reporter` (optional) module will receive `Reporter`. (See [teal.reporter::Reporter]).+ #' @param modules (`teal_modules`) object |
||
178 | +108 |
- # - `filter_panel_api` (optional) module will receive `FilterPanelAPI`. (See [teal.slice::FilterPanelAPI]).+ #' @param datanames (`character`) names of datasets available in the `data` object |
||
179 | +109 |
- #' - `...` (optional) `server_args` elements will be passed to the module named argument or to the `...`.+ #' @param join_keys (`join_keys`) object |
||
180 | +110 |
- #' @param ui (`function`) Shiny `ui` module function with following arguments:+ #' @return `teal_modules` with resolved `datanames` |
||
181 | +111 |
- #' - `id` - teal will set proper shiny namespace for this module.+ #' @keywords internal |
||
182 | +112 |
- #' - `...` (optional) `ui_args` elements will be passed to the module named argument or to the `...`.+ resolve_modules_datanames <- function(modules, datanames, join_keys) { |
||
183 | -+ | |||
113 | +! |
- #' @param filters (`character`) Deprecated. Use `datanames` instead.+ if (inherits(modules, "teal_modules")) { |
||
184 | -+ | |||
114 | +! |
- #' @param datanames (`character`) A vector with `datanames` that are relevant for the item. The+ modules$children <- sapply( |
||
185 | -+ | |||
115 | +! |
- #' filter panel will automatically update the shown filters to include only+ modules$children, |
||
186 | -+ | |||
116 | +! |
- #' filters in the listed datasets. `NULL` will hide the filter panel,+ resolve_modules_datanames, |
||
187 | -+ | |||
117 | +! |
- #' and the keyword `'all'` will show filters of all datasets. `datanames` also determines+ simplify = FALSE, |
||
188 | -+ | |||
118 | +! |
- #' a subset of datasets which are appended to the `data` argument in `server` function.+ datanames = datanames, |
||
189 | -+ | |||
119 | +! |
- #' @param server_args (named `list`) with additional arguments passed on to the+ join_keys = join_keys |
||
190 | +120 |
- #' `server` function.+ ) |
||
191 | -+ | |||
121 | +! |
- #' @param ui_args (named `list`) with additional arguments passed on to the+ modules |
||
192 | +122 |
- #' `ui` function.+ } else { |
||
193 | -+ | |||
123 | +! |
- #'+ modules$datanames <- if (identical(modules$datanames, "all")) { |
||
194 | -+ | |||
124 | +! |
- #' @return object of class `teal_module`.+ datanames |
||
195 | -+ | |||
125 | +! |
- #' @export+ } else if (is.character(modules$datanames)) { |
||
196 | -+ | |||
126 | +! |
- #' @examples+ extra_datanames <- setdiff(modules$datanames, datanames) |
||
197 | -+ | |||
127 | +! |
- #' library(shiny)+ if (length(extra_datanames)) { |
||
198 | -+ | |||
128 | +! |
- #'+ stop( |
||
199 | -+ | |||
129 | +! |
- #' app <- init(+ sprintf( |
||
200 | -+ | |||
130 | +! |
- #' data = teal_data(iris = iris),+ "Module %s has datanames that are not available in a 'data':\n %s not in %s", |
||
201 | -+ | |||
131 | +! |
- #' modules = list(+ modules$label, |
||
202 | -+ | |||
132 | +! |
- #' module(+ toString(extra_datanames), |
||
203 | -+ | |||
133 | +! |
- #' label = "Module",+ toString(datanames) |
||
204 | +134 |
- #' server = function(id, data) {+ ) |
||
205 | +135 |
- #' moduleServer(+ ) |
||
206 | +136 |
- #' id,+ } |
||
207 | -+ | |||
137 | +! |
- #' module = function(input, output, session) {+ datanames_adjusted <- intersect(modules$datanames, datanames) |
||
208 | -+ | |||
138 | +! |
- #' output$data <- renderDataTable(data[["iris"]]())+ include_parent_datanames(dataname = datanames_adjusted, join_keys = join_keys) |
||
209 | +139 |
- #' }+ }+ |
+ ||
140 | +! | +
+ modules |
||
210 | +141 |
- #' )+ } |
||
211 | +142 |
- #' },+ } |
||
212 | +143 |
- #' ui = function(id) {+ |
||
213 | +144 |
- #' ns <- NS(id)+ #' Check `datanames` in modules |
||
214 | +145 |
- #' tagList(dataTableOutput(ns("data")))+ #' |
||
215 | +146 |
- #' }+ #' This function ensures specified `datanames` in modules match those in the data object, |
||
216 | +147 |
- #' )+ #' returning error messages or `TRUE` for successful validation. |
||
217 | +148 |
- #' )+ #' |
||
218 | +149 |
- #' )+ #' @param modules (`teal_modules`) object |
||
219 | +150 |
- #' if (interactive()) {+ #' @param datanames (`character`) names of datasets available in the `data` object |
||
220 | +151 |
- #' shinyApp(app$ui, app$server)+ #' |
||
221 | +152 |
- #' }+ #' @return A `character(1)` containing error message or `TRUE` if validation passes. |
||
222 | +153 |
- module <- function(label = "module",+ #' @keywords internal |
||
223 | +154 |
- server = function(id, ...) {+ check_modules_datanames <- function(modules, datanames) { |
||
224 | -! | +|||
155 | +16x |
- moduleServer(id, function(input, output, session) {}) # nolint+ checkmate::assert_class(modules, "teal_modules") |
||
225 | -+ | |||
156 | +16x |
- },+ checkmate::assert_character(datanames) |
||
226 | +157 |
- ui = function(id, ...) {+ |
||
227 | -! | +|||
158 | +16x |
- tags$p(paste0("This module has no UI (id: ", id, " )"))+ recursive_check_datanames <- function(modules, datanames) { |
||
228 | +159 |
- },+ # check teal_modules against datanames |
||
229 | -+ | |||
160 | +34x |
- filters,+ if (inherits(modules, "teal_modules")) { |
||
230 | -+ | |||
161 | +16x |
- datanames = "all",+ sapply(modules$children, function(module) recursive_check_datanames(module, datanames = datanames)) |
||
231 | +162 |
- server_args = NULL,+ } else { |
||
232 | -+ | |||
163 | +18x |
- ui_args = NULL) {+ extra_datanames <- setdiff(modules$datanames, c("all", datanames)) |
||
233 | -135x | +164 | +18x |
- checkmate::assert_string(label)+ if (length(extra_datanames)) { |
234 | -132x | +165 | +2x |
- checkmate::assert_function(server)+ sprintf( |
235 | -132x | +166 | +2x |
- checkmate::assert_function(ui)+ "- Module '%s' uses datanames not available in 'data': (%s) not in (%s)", |
236 | -132x | +167 | +2x |
- checkmate::assert_character(datanames, min.len = 1, null.ok = TRUE, any.missing = FALSE)+ modules$label, |
237 | -131x | +168 | +2x |
- checkmate::assert_list(server_args, null.ok = TRUE, names = "named")+ toString(dQuote(extra_datanames, q = FALSE)), |
238 | -129x | +169 | +2x |
- checkmate::assert_list(ui_args, null.ok = TRUE, names = "named")+ toString(dQuote(datanames, q = FALSE)) |
239 | +170 |
-
+ ) |
||
240 | -127x | -
- if (!missing(filters)) {- |
- ||
241 | -! | -
- checkmate::assert_character(filters, min.len = 1, null.ok = TRUE, any.missing = FALSE)- |
- ||
242 | -! | +|||
171 | +
- datanames <- filters+ } |
|||
243 | -! | +|||
172 | +
- msg <-+ } |
|||
244 | -! | +|||
173 | +
- "The `filters` argument is deprecated and will be removed in the next release. Please use `datanames` instead."+ } |
|||
245 | -! | +|||
174 | +16x |
- logger::log_warn(msg)+ check_datanames <- unlist(recursive_check_datanames(modules, datanames)) |
||
246 | -! | +|||
175 | +16x |
- warning(msg)+ if (length(check_datanames)) { |
||
247 | -+ | |||
176 | +2x |
- }+ paste(check_datanames, collapse = "\n") |
||
248 | +177 |
-
+ } else { |
||
249 | -127x | +178 | +14x |
- if (label == "global_filters") {+ TRUE |
250 | -1x | +|||
179 | +
- stop(+ } |
|||
251 | -1x | +|||
180 | +
- sprintf("module(label = \"%s\", ...\n ", label),+ } |
|||
252 | -1x | +|||
181 | +
- "Label 'global_filters' is reserved in teal. Please change to something else.",+ |
|||
253 | -1x | +|||
182 | +
- call. = FALSE+ #' Check `datanames` in filters |
|||
254 | +183 |
- )+ #' |
||
255 | +184 |
- }+ #' This function checks whether `datanames` in filters correspond to those in `data`, |
||
256 | -126x | +|||
185 | +
- if (label == "Report previewer") {+ #' returning character vector with error messages or TRUE if all checks pass. |
|||
257 | -! | +|||
186 | +
- stop(+ #' |
|||
258 | -! | +|||
187 | +
- sprintf("module(label = \"%s\", ...\n ", label),+ #' @param filters (`teal_slices`) object |
|||
259 | -! | +|||
188 | +
- "Label 'Report previewer' is reserved in teal.",+ #' @param datanames (`character`) names of datasets available in the `data` object |
|||
260 | -! | +|||
189 | +
- call. = FALSE+ #' |
|||
261 | +190 |
- )+ #' @return A `character(1)` containing error message or TRUE if validation passes. |
||
262 | +191 |
- }+ #' @keywords internal |
||
263 | -126x | +|||
192 | +
- server_formals <- names(formals(server))+ check_filter_datanames <- function(filters, datanames) { |
|||
264 | -126x | +193 | +14x |
- if (!(+ checkmate::assert_class(filters, "teal_slices") |
265 | -126x | +194 | +14x |
- "id" %in% server_formals ||+ checkmate::assert_character(datanames) |
266 | -126x | +|||
195 | +
- all(c("input", "output", "session") %in% server_formals)+ |
|||
267 | +196 |
- )) {+ # check teal_slices against datanames |
||
268 | -2x | +197 | +14x |
- stop(+ out <- unlist(sapply( |
269 | -2x | +198 | +14x |
- "\nmodule() `server` argument requires a function with following arguments:",+ filters, function(filter) { |
270 | -2x | +199 | +3x |
- "\n - id - teal will set proper shiny namespace for this module.",+ dataname <- shiny::isolate(filter$dataname) |
271 | -2x | +200 | +3x |
- "\n - input, output, session (not recommended) - then shiny::callModule will be used to call a module.",+ if (!dataname %in% datanames) { |
272 | +201 | 2x |
- "\n\nFollowing arguments can be used optionaly:",+ sprintf( |
|
273 | +202 | 2x |
- "\n - `data` - module will receive list of reactive (filtered) data specified in the `filters` argument",+ "- Filter '%s' refers to dataname not available in 'data':\n %s not in (%s)", |
|
274 | +203 | 2x |
- "\n - `datasets` - module will receive `FilteredData`. See `help(teal.slice::FilteredData)`",+ shiny::isolate(filter$id), |
|
275 | +204 | 2x |
- "\n - `reporter` - module will receive `Reporter`. See `help(teal.reporter::Reporter)`",+ dQuote(dataname, q = FALSE), |
|
276 | +205 | 2x |
- "\n - `filter_panel_api` - module will receive `FilterPanelAPI`. (See [teal.slice::FilterPanelAPI]).",+ toString(dQuote(datanames, q = FALSE)) |
|
277 | -2x | +|||
206 | +
- "\n - `...` server_args elements will be passed to the module named argument or to the `...`"+ ) |
|||
278 | +207 |
- )+ } |
||
279 | +208 |
- }+ } |
||
280 | +209 |
-
+ )) |
||
281 | -124x | +|||
210 | +
- if (!is.element("data", server_formals) && !is.null(datanames)) {+ + |
+ |||
211 | ++ | + | ||
282 | -46x | +212 | +14x |
- message(sprintf("module \"%s\" server function takes no data so \"datanames\" will be ignored", label))+ if (length(out)) { |
283 | -46x | +213 | +2x |
- datanames <- NULL+ paste(out, collapse = "\n") |
284 | +214 |
- }+ } else { |
||
285 | -124x | +215 | +12x |
- if ("datasets" %in% server_formals) {+ TRUE |
286 | -2x | +|||
216 | +
- warning(+ } |
|||
287 | -2x | +|||
217 | +
- sprintf("Called from module(label = \"%s\", ...)\n ", label),+ } |
|||
288 | -2x | +
1 | +
- "`datasets` argument in the `server` is deprecated and will be removed in the next release. ",+ #' Creates a `teal_modules` object. |
|||
289 | -2x | +|||
2 | +
- "Please use `data` instead.",+ #' |
|||
290 | -2x | +|||
3 | +
- call. = FALSE+ #' @description `r lifecycle::badge("stable")` |
|||
291 | +4 |
- )+ #' This function collects a list of `teal_modules` and `teal_module` objects and returns a `teal_modules` object |
||
292 | +5 |
- }+ #' containing the passed objects. |
||
293 | +6 |
-
+ #' |
||
294 | -124x | +|||
7 | +
- srv_extra_args <- setdiff(names(server_args), server_formals)+ #' This function dictates what modules are included in a `teal` application. The internal structure of `teal_modules` |
|||
295 | -124x | +|||
8 | +
- if (length(srv_extra_args) > 0 && !"..." %in% server_formals) {+ #' shapes the navigation panel of a `teal` application. |
|||
296 | -1x | +|||
9 | +
- stop(+ #' |
|||
297 | -1x | +|||
10 | +
- "\nFollowing `server_args` elements have no equivalent in the formals of the `server`:\n",+ #' @param ... (`teal_module` or `teal_modules`) see [module()] and [modules()] for more details |
|||
298 | -1x | +|||
11 | +
- paste(paste(" -", srv_extra_args), collapse = "\n"),+ #' @param label (`character(1)`) label of modules collection (default `"root"`). |
|||
299 | -1x | +|||
12 | +
- "\n\nUpdate the `server` arguments by including above or add `...`"+ #' If using the `label` argument then it must be explicitly named. |
|||
300 | +13 |
- )+ #' For example `modules("lab", ...)` should be converted to `modules(label = "lab", ...)` |
||
301 | +14 |
- }+ #' |
||
302 | +15 |
-
+ #' @export |
||
303 | -123x | +|||
16 | +
- ui_formals <- names(formals(ui))+ #' |
|||
304 | -123x | +|||
17 | +
- if (!"id" %in% ui_formals) {+ #' @return object of class \code{teal_modules}. Object contains following fields |
|||
305 | -1x | +|||
18 | +
- stop(+ #' - `label`: taken from the `label` argument |
|||
306 | -1x | +|||
19 | +
- "\nmodule() `ui` argument requires a function with following arguments:",+ #' - `children`: a list containing objects passed in `...`. List elements are named after |
|||
307 | -1x | +|||
20 | +
- "\n - id - teal will set proper shiny namespace for this module.",+ #' their `label` attribute converted to a valid `shiny` id. |
|||
308 | -1x | +|||
21 | +
- "\n\nFollowing arguments can be used optionally:",+ #' @examples |
|||
309 | -1x | +|||
22 | +
- "\n - `...` ui_args elements will be passed to the module argument of the same name or to the `...`"+ #' library(shiny) |
|||
310 | +23 |
- )+ #' |
||
311 | +24 |
- }+ #' app <- init( |
||
312 | +25 |
-
+ #' data = teal_data(iris = iris), |
||
313 | -122x | +|||
26 | +
- if (any(c("data", "datasets") %in% ui_formals)) {+ #' modules = modules( |
|||
314 | -2x | +|||
27 | +
- stop(+ #' label = "Modules", |
|||
315 | -2x | +|||
28 | +
- sprintf("Called from module(label = \"%s\", ...)\n ", label),+ #' modules( |
|||
316 | -2x | +|||
29 | +
- "`ui` with `data` or `datasets` argument is no longer accepted.\n ",- |
- |||
317 | -2x | -
- "If some `ui` inputs depend on data, please move the logic to your `server` instead.\n ",- |
- ||
318 | -2x | -
- "Possible solutions are renderUI() or updateXyzInput() functions."+ #' label = "Module", |
||
319 | +30 |
- )+ #' module( |
||
320 | +31 |
- }+ #' label = "Inner module", |
||
321 | +32 | - - | -||
322 | -120x | -
- ui_extra_args <- setdiff(names(ui_args), ui_formals)- |
- ||
323 | -120x | -
- if (length(ui_extra_args) > 0 && !"..." %in% ui_formals) {- |
- ||
324 | -1x | -
- stop(- |
- ||
325 | -1x | -
- "\nFollowing `ui_args` elements have no equivalent in the formals of `ui`:\n",- |
- ||
326 | -1x | -
- paste(paste(" -", ui_extra_args), collapse = "\n"),- |
- ||
327 | -1x | -
- "\n\nUpdate the `ui` arguments by including above or add `...`"+ #' server = function(id, data) { |
||
328 | +33 |
- )+ #' moduleServer( |
||
329 | +34 |
- }+ #' id, |
||
330 | +35 | - - | -||
331 | -119x | -
- structure(- |
- ||
332 | -119x | -
- list(- |
- ||
333 | -119x | -
- label = label,- |
- ||
334 | -119x | -
- server = server, ui = ui, datanames = unique(datanames),- |
- ||
335 | -119x | -
- server_args = server_args, ui_args = ui_args+ #' module = function(input, output, session) { |
||
336 | +36 |
- ),- |
- ||
337 | -119x | -
- class = "teal_module"+ #' output$data <- renderDataTable(data[["iris"]]()) |
||
338 | +37 |
- )+ #' } |
||
339 | +38 |
- }+ #' ) |
||
340 | +39 |
-
+ #' }, |
||
341 | +40 |
-
+ #' ui = function(id) { |
||
342 | +41 |
- #' Get module depth+ #' ns <- NS(id) |
||
343 | +42 |
- #'+ #' tagList(dataTableOutput(ns("data"))) |
||
344 | +43 |
- #' Depth starts at 0, so a single `teal.module` has depth 0.+ #' }, |
||
345 | +44 |
- #' Nesting it increases overall depth by 1.+ #' datanames = "all" |
||
346 | +45 |
- #'+ #' ) |
||
347 | +46 |
- #' @inheritParams init+ #' ), |
||
348 | +47 |
- #' @param depth optional, integer determining current depth level+ #' module( |
||
349 | +48 |
- #'+ #' label = "Another module", |
||
350 | +49 |
- #' @return depth level for given module+ #' server = function(id) { |
||
351 | +50 |
- #' @keywords internal+ #' moduleServer( |
||
352 | +51 |
- #'+ #' id, |
||
353 | +52 |
- #' @examples+ #' module = function(input, output, session) { |
||
354 | +53 |
- #' mods <- modules(+ #' output$text <- renderText("Another module") |
||
355 | +54 |
- #' label = "d1",+ #' } |
||
356 | +55 |
- #' modules(+ #' ) |
||
357 | +56 |
- #' label = "d2",+ #' }, |
||
358 | +57 |
- #' modules(+ #' ui = function(id) { |
||
359 | +58 |
- #' label = "d3",+ #' ns <- NS(id) |
||
360 | +59 |
- #' module(label = "aaa1"), module(label = "aaa2"), module(label = "aaa3")+ #' tagList(textOutput(ns("text"))) |
||
361 | +60 |
- #' ),+ #' }, |
||
362 | +61 |
- #' module(label = "bbb")+ #' datanames = NULL |
||
363 | +62 |
- #' ),+ #' ) |
||
364 | +63 |
- #' module(label = "ccc")+ #' ) |
||
365 | +64 |
#' ) |
||
366 | +65 |
- #' stopifnot(teal:::modules_depth(mods) == 3L)+ #' if (interactive()) { |
||
367 | +66 |
- #'+ #' shinyApp(app$ui, app$server) |
||
368 | +67 |
- #' mods <- modules(+ #' } |
||
369 | +68 |
- #' label = "a",+ modules <- function(..., label = "root") { |
||
370 | -+ | |||
69 | +109x |
- #' modules(+ checkmate::assert_string(label) |
||
371 | -+ | |||
70 | +107x |
- #' label = "b1", module(label = "c")+ submodules <- list(...) |
||
372 | -+ | |||
71 | +107x |
- #' ),+ if (any(vapply(submodules, is.character, FUN.VALUE = logical(1)))) {+ |
+ ||
72 | +2x | +
+ stop(+ |
+ ||
73 | +2x | +
+ "The only character argument to modules() must be 'label' and it must be named, ",+ |
+ ||
74 | +2x | +
+ "change modules('lab', ...) to modules(label = 'lab', ...)" |
||
373 | +75 |
- #' module(label = "b2")+ ) |
||
374 | +76 |
- #' )+ } |
||
375 | +77 |
- #' stopifnot(teal:::modules_depth(mods) == 2L)+ + |
+ ||
78 | +105x | +
+ checkmate::assert_list(submodules, min.len = 1, any.missing = FALSE, types = c("teal_module", "teal_modules")) |
||
376 | +79 |
- modules_depth <- function(modules, depth = 0L) {+ # name them so we can more easily access the children |
||
377 | -12x | +|||
80 | +
- checkmate::assert(+ # beware however that the label of the submodules should not be changed as it must be kept synced |
|||
378 | -12x | +81 | +102x |
- checkmate::check_class(modules, "teal_module"),+ labels <- vapply(submodules, function(submodule) submodule$label, character(1)) |
379 | -12x | +82 | +102x |
- checkmate::check_class(modules, "teal_modules")+ names(submodules) <- make.unique(gsub("[^[:alnum:]]+", "_", labels), sep = "_") |
380 | -+ | |||
83 | +102x |
- )+ structure( |
||
381 | -12x | +84 | +102x |
- checkmate::assert_int(depth, lower = 0)+ list( |
382 | -11x | +85 | +102x |
- if (inherits(modules, "teal_modules")) {+ label = label, |
383 | -4x | +86 | +102x |
- max(vapply(modules$children, modules_depth, integer(1), depth = depth + 1L))+ children = submodules |
384 | +87 |
- } else {+ ), |
||
385 | -7x | +88 | +102x |
- depth+ class = "teal_modules" |
386 | +89 |
- }+ ) |
||
387 | +90 |
} |
||
388 | +91 | |||
389 | +92 |
-
+ #' Append a `teal_module` to `children` of a `teal_modules` object |
||
390 | +93 |
- module_labels <- function(modules) {+ #' @keywords internal |
||
391 | -! | +|||
94 | +
- if (inherits(modules, "teal_modules")) {+ #' @param modules `teal_modules` |
|||
392 | -! | +|||
95 | +
- lapply(modules$children, module_labels)+ #' @param module `teal_module` object to be appended onto the children of `modules` |
|||
393 | +96 |
- } else {+ #' @return `teal_modules` object with `module` appended |
||
394 | -! | +|||
97 | +
- modules$label+ append_module <- function(modules, module) { |
|||
395 | -+ | |||
98 | +7x |
- }+ checkmate::assert_class(modules, "teal_modules")+ |
+ ||
99 | +5x | +
+ checkmate::assert_class(module, "teal_module")+ |
+ ||
100 | +3x | +
+ modules$children <- c(modules$children, list(module))+ |
+ ||
101 | +3x | +
+ labels <- vapply(modules$children, function(submodule) submodule$label, character(1))+ |
+ ||
102 | +3x | +
+ names(modules$children) <- make.unique(gsub("[^[:alnum:]]", "_", tolower(labels)), sep = "_")+ |
+ ||
103 | +3x | +
+ modules |
||
396 | +104 |
} |
||
397 | +105 | |||
398 | +106 |
- #' Converts `teal_modules` to a string+ #' Extract/Remove module(s) of specific class |
||
399 | +107 |
#' |
||
400 | +108 |
- #' @param x (`teal_modules`) to print+ #' Given a `teal_module` or a `teal_modules`, return the elements of the structure according to `class`. |
||
401 | +109 |
- #' @param indent (`integer`) indent level;+ #' |
||
402 | +110 |
- #' each `submodule` is indented one level more+ #' @param modules `teal_modules` |
||
403 | +111 |
- #' @param ... (optional) additional parameters to pass to recursive calls of `toString`+ #' @param class The class name of `teal_module` to be extracted or dropped. |
||
404 | +112 |
- #' @return (`character`)+ #' @keywords internal |
||
405 | +113 |
- #' @export+ #' @return |
||
406 | +114 |
- #' @rdname modules+ #' For `extract_module`, a `teal_module` of class `class` or `teal_modules` containing modules of class `class`. |
||
407 | +115 |
- toString.teal_modules <- function(x, indent = 0, ...) { # nolint+ #' For `drop_module`, the opposite, which is all `teal_modules` of class other than `class`. |
||
408 | +116 |
- # argument must be `x` to be consistent with base method+ #' @rdname module_management |
||
409 | -! | +|||
117 | +
- paste(c(+ extract_module <- function(modules, class) { |
|||
410 | -! | +|||
118 | +30x |
- paste0(rep(" ", indent), "+ ", x$label),+ if (inherits(modules, class)) { |
||
411 | +119 | ! |
- unlist(lapply(x$children, toString, indent = indent + 1, ...))+ modules |
|
412 | -! | +|||
120 | +30x |
- ), collapse = "\n")+ } else if (inherits(modules, "teal_module")) { |
||
413 | -+ | |||
121 | +16x |
- }+ NULL |
||
414 | -+ | |||
122 | +14x |
-
+ } else if (inherits(modules, "teal_modules")) { |
||
415 | -+ | |||
123 | +14x |
- #' Converts `teal_module` to a string+ Filter(function(x) length(x) > 0L, lapply(modules$children, extract_module, class)) |
||
416 | +124 |
- #'+ } |
||
417 | +125 |
- #' @inheritParams toString.teal_modules+ } |
||
418 | +126 |
- #' @param x `teal_module`+ |
||
419 | +127 |
- #' @param ... ignored+ #' @keywords internal |
||
420 | +128 |
- #' @export+ #' @return `teal_modules` |
||
421 | +129 |
- #' @rdname module+ #' @rdname module_management |
||
422 | +130 |
- toString.teal_module <- function(x, indent = 0, ...) { # nolint+ drop_module <- function(modules, class) {+ |
+ ||
131 | +30x | +
+ if (inherits(modules, class)) { |
||
423 | +132 | ! |
- paste0(paste(rep(" ", indent), collapse = ""), "+ ", x$label, collapse = "")+ NULL |
|
424 | -+ | |||
133 | +30x |
- }+ } else if (inherits(modules, "teal_module")) { |
||
425 | -+ | |||
134 | +16x |
-
+ modules |
||
426 | -+ | |||
135 | +14x |
- #' Prints `teal_modules`+ } else if (inherits(modules, "teal_modules")) { |
||
427 | -+ | |||
136 | +14x |
- #' @param x `teal_modules`+ do.call( |
||
428 | -+ | |||
137 | +14x |
- #' @param ... parameters passed to `toString`+ "modules", |
||
429 | -+ | |||
138 | +14x |
- #' @export+ c(Filter(function(x) length(x) > 0L, lapply(modules$children, drop_module, class)), label = modules$label) |
||
430 | +139 |
- #' @rdname modules+ ) |
||
431 | +140 |
- print.teal_modules <- function(x, ...) {- |
- ||
432 | -! | -
- s <- toString(x, ...)- |
- ||
433 | -! | -
- cat(s)- |
- ||
434 | -! | -
- return(invisible(s))+ } |
||
435 | +141 |
} |
||
436 | +142 | |||
437 | +143 |
- #' Prints `teal_module`+ #' Does the object make use of the `arg` |
||
438 | +144 |
- #' @param x `teal_module`+ #' |
||
439 | +145 |
- #' @param ... parameters passed to `toString`+ #' @param modules (`teal_module` or `teal_modules`) object |
||
440 | +146 |
- #' @export+ #' @param arg (`character(1)`) names of the arguments to be checked against formals of `teal` modules. |
||
441 | +147 |
- #' @rdname module+ #' @return `logical` whether the object makes use of `arg` |
||
442 | +148 |
- print.teal_module <- print.teal_modules+ #' @rdname is_arg_used |
1 | +149 |
- #' Filter state snapshot management.+ #' @keywords internal |
|||
2 | +150 |
- #'+ is_arg_used <- function(modules, arg) { |
|||
3 | -+ | ||||
151 | +286x |
- #' Capture and restore snapshots of the global (app) filter state.+ checkmate::assert_string(arg) |
|||
4 | -+ | ||||
152 | +283x |
- #'+ if (inherits(modules, "teal_modules")) { |
|||
5 | -+ | ||||
153 | +29x |
- #' This module introduces snapshots: stored descriptions of the filter state of the entire application.+ any(unlist(lapply(modules$children, is_arg_used, arg))) |
|||
6 | -+ | ||||
154 | +254x |
- #' Snapshots allow the user to save the current filter state of the application for later use in the session,+ } else if (inherits(modules, "teal_module")) { |
|||
7 | -+ | ||||
155 | +43x |
- #' as well as to save it to file in order to share it with an app developer or other users,+ is_arg_used(modules$server, arg) || is_arg_used(modules$ui, arg) |
|||
8 | -+ | ||||
156 | +211x |
- #' who in turn can upload it to their own session.+ } else if (is.function(modules)) { |
|||
9 | -- |
- #'+ | |||
157 | +209x | +
+ isTRUE(arg %in% names(formals(modules))) |
|||
10 | +158 |
- #' The snapshot manager is accessed through the filter manager, with the cog icon in the top right corner.+ } else { |
|||
11 | -+ | ||||
159 | +2x |
- #' At the beginning of a session it presents three icons: a camera, an upload, and an circular arrow.+ stop("is_arg_used function not implemented for this object") |
|||
12 | +160 |
- #' Clicking the camera captures a snapshot, clicking the upload adds a snapshot from a file+ } |
|||
13 | +161 |
- #' and applies the filter states therein, and clicking the arrow resets initial application state.+ } |
|||
14 | +162 |
- #' As snapshots are added, they will show up as rows in a table and each will have a select button and a save button.+ |
|||
15 | +163 |
- #'+ |
|||
16 | +164 |
- #' @section Server logic:+ #' Creates a `teal_module` object. |
|||
17 | +165 |
- #' Snapshots are basically `teal_slices` objects, however, since each module is served by a separate instance+ #' |
|||
18 | +166 |
- #' of `FilteredData` and these objects require shared state, `teal_slice` is a `reactiveVal` so `teal_slices`+ #' @description `r lifecycle::badge("stable")` |
|||
19 | +167 |
- #' cannot be stored as is. Therefore, `teal_slices` are reversibly converted to a list of lists representation+ #' This function embeds a `shiny` module inside a `teal` application. One `teal_module` maps to one `shiny` module. |
|||
20 | +168 |
- #' (attributes are maintained).+ #' |
|||
21 | +169 |
- #'+ #' @param label (`character(1)`) Label shown in the navigation item for the module. Any label possible except |
|||
22 | +170 |
- #' Snapshots are stored in a `reactiveVal` as a named list.+ #' `"global_filters"` - read more in `mapping` argument of [teal::teal_slices]. |
|||
23 | +171 |
- #' The first snapshot is the initial state of the application and the user can add a snapshot whenever they see fit.+ #' @param server (`function`) `shiny` module with following arguments: |
|||
24 | +172 |
- #'+ #' - `id` - teal will set proper shiny namespace for this module (see [shiny::moduleServer()]). |
|||
25 | +173 |
- #' For every snapshot except the initial one, a piece of UI is generated that contains+ #' - `input`, `output`, `session` - (not recommended) then [shiny::callModule()] will be used to call a module. |
|||
26 | +174 |
- #' the snapshot name, a select button to restore that snapshot, and a save button to save it to a file.+ #' - `data` (optional) module will receive a `teal_data` object, a list of reactive (filtered) data specified in |
|||
27 | +175 |
- #' The initial snapshot is restored by a separate "reset" button.+ #' the `filters` argument. |
|||
28 | +176 |
- #' It cannot be saved directly but a user is welcome to capture the initial state as a snapshot and save that.+ #' - `datasets` (optional) module will receive `FilteredData`. (See `[teal.slice::FilteredData]`). |
|||
29 | +177 |
- #'+ #' - `reporter` (optional) module will receive `Reporter`. (See [teal.reporter::Reporter]). |
|||
30 | +178 |
- #' @section Snapshot mechanics:+ # - `filter_panel_api` (optional) module will receive `FilterPanelAPI`. (See [teal.slice::FilterPanelAPI]). |
|||
31 | +179 |
- #' When a snapshot is captured, the user is prompted to name it.+ #' - `...` (optional) `server_args` elements will be passed to the module named argument or to the `...`. |
|||
32 | +180 |
- #' Names are displayed as is but since they are used to create button ids,+ #' @param ui (`function`) Shiny `ui` module function with following arguments: |
|||
33 | +181 |
- #' under the hood they are converted to syntactically valid strings.+ #' - `id` - teal will set proper shiny namespace for this module. |
|||
34 | +182 |
- #' New snapshot names are validated so that their valid versions are unique.+ #' - `...` (optional) `ui_args` elements will be passed to the module named argument or to the `...`. |
|||
35 | +183 |
- #' Leading and trailing white space is trimmed.+ #' @param filters (`character`) Deprecated. Use `datanames` instead. |
|||
36 | +184 |
- #'+ #' @param datanames (`character`) A vector with `datanames` that are relevant for the item. The |
|||
37 | +185 |
- #' The module can read the global state of the application from `slices_global` and `mapping_matrix`.+ #' filter panel will automatically update the shown filters to include only |
|||
38 | +186 |
- #' The former provides a list of all existing `teal_slice`s and the latter says which slice is active in which module.+ #' filters in the listed datasets. `NULL` will hide the filter panel, |
|||
39 | +187 |
- #' Once a name has been accepted, `slices_global` is converted to a list of lists - a snapshot.+ #' and the keyword `'all'` will show filters of all datasets. `datanames` also determines |
|||
40 | +188 |
- #' The snapshot contains the `mapping` attribute of the initial application state+ #' a subset of datasets which are appended to the `data` argument in `server` function. |
|||
41 | +189 |
- #' (or one that has been restored), which may not reflect the current one,+ #' @param server_args (named `list`) with additional arguments passed on to the |
|||
42 | +190 |
- #' so `mapping_matrix` is transformed to obtain the current mapping, i.e. a list that,+ #' `server` function. |
|||
43 | +191 |
- #' when passed to the `mapping` argument of [`teal::teal_slices`], would result in the current mapping.+ #' @param ui_args (named `list`) with additional arguments passed on to the |
|||
44 | +192 |
- #' This is substituted as the snapshot's `mapping` attribute and the snapshot is added to the snapshot list.+ #' `ui` function. |
|||
45 | +193 |
#' |
|||
46 | +194 |
- #' To restore app state, a snapshot is retrieved from storage and rebuilt into a `teal_slices` object.+ #' @return object of class `teal_module`. |
|||
47 | +195 |
- #' Then state of all `FilteredData` objects (provided in `filtered_data_list`) is cleared+ #' @export |
|||
48 | +196 |
- #' and set anew according to the `mapping` attribute of the snapshot.+ #' @examples |
|||
49 | +197 |
- #' The snapshot is then set as the current content of `slices_global`.+ #' library(shiny) |
|||
50 | +198 |
#' |
|||
51 | +199 |
- #' To save a snapshot, the snapshot is retrieved and reassembled just like for restoring,+ #' app <- init( |
|||
52 | +200 |
- #' and then saved to file with [`slices_store`].+ #' data = teal_data(iris = iris), |
|||
53 | +201 |
- #'+ #' modules = list( |
|||
54 | +202 |
- #' When a snapshot is uploaded, it will first be added to storage just like a newly created one,+ #' module( |
|||
55 | +203 |
- #' and then used to restore app state much like a snapshot taken from storage.+ #' label = "Module", |
|||
56 | +204 |
- #' Upon clicking the upload icon the user will be prompted for a file to upload+ #' server = function(id, data) { |
|||
57 | +205 |
- #' and may choose to name the new snapshot. The name defaults to the name of the file (the extension is dropped)+ #' moduleServer( |
|||
58 | +206 |
- #' and normal naming rules apply. Loading the file yields a `teal_slices` object,+ #' id, |
|||
59 | +207 |
- #' which is disassembled for storage and used directly for restoring app state.+ #' module = function(input, output, session) { |
|||
60 | +208 |
- #'+ #' output$data <- renderDataTable(data[["iris"]]()) |
|||
61 | +209 |
- #' @section Transferring snapshots:+ #' } |
|||
62 | +210 |
- #' Snapshots uploaded from disk should only be used in the same application they come from,+ #' ) |
|||
63 | +211 |
- #' _i.e._ an application that uses the same data and the same modules.+ #' }, |
|||
64 | +212 |
- #' To ensure this is the case, `init` stamps `teal_slices` with an app id that is stored in the `app_id` attribute of+ #' ui = function(id) { |
|||
65 | +213 |
- #' a `teal_slices` object. When a snapshot is restored from file, its `app_id` is compared to that+ #' ns <- NS(id) |
|||
66 | +214 |
- #' of the current app state and only if the match is the snapshot admitted to the session.+ #' tagList(dataTableOutput(ns("data"))) |
|||
67 | +215 |
- #'+ #' } |
|||
68 | +216 |
- #' @param id (`character(1)`) `shiny` module id+ #' ) |
|||
69 | +217 |
- #' @param slices_global (`reactiveVal`) that contains a `teal_slices` object+ #' ) |
|||
70 | +218 |
- #' containing all `teal_slice`s existing in the app, both active and inactive+ #' ) |
|||
71 | +219 |
- #' @param mapping_matrix (`reactive`) that contains a `data.frame` representation+ #' if (interactive()) { |
|||
72 | +220 |
- #' of the mapping of filter state ids (rows) to modules labels (columns);+ #' shinyApp(app$ui, app$server) |
|||
73 | +221 |
- #' all columns are `logical` vectors+ #' } |
|||
74 | +222 |
- #' @param filtered_data_list non-nested (`named list`) that contains `FilteredData` objects+ module <- function(label = "module", |
|||
75 | +223 |
- #'+ server = function(id, ...) { |
|||
76 | -+ | ||||
224 | +! |
- #' @return Nothing is returned.+ moduleServer(id, function(input, output, session) {}) # nolint |
|||
77 | +225 |
- #'+ }, |
|||
78 | +226 |
- #' @name snapshot_manager_module+ ui = function(id, ...) { |
|||
79 | -+ | ||||
227 | +! |
- #' @aliases snapshot snapshot_manager+ tags$p(paste0("This module has no UI (id: ", id, " )")) |
|||
80 | +228 |
- #'+ }, |
|||
81 | +229 |
- #' @author Aleksander Chlebowski+ filters, |
|||
82 | +230 |
- #'+ datanames = "all", |
|||
83 | +231 |
- #' @rdname snapshot_manager_module+ server_args = NULL, |
|||
84 | +232 |
- #' @keywords internal+ ui_args = NULL) { |
|||
85 | -+ | ||||
233 | +135x |
- #'+ checkmate::assert_string(label) |
|||
86 | -+ | ||||
234 | +132x |
- snapshot_manager_ui <- function(id) {+ checkmate::assert_function(server) |
|||
87 | -! | +||||
235 | +132x |
- ns <- NS(id)+ checkmate::assert_function(ui) |
|||
88 | -! | +||||
236 | +132x |
- div(+ checkmate::assert_character(datanames, min.len = 1, null.ok = TRUE, any.missing = FALSE) |
|||
89 | -! | +||||
237 | +131x |
- class = "snapshot_manager_content",+ checkmate::assert_list(server_args, null.ok = TRUE, names = "named") |
|||
90 | -! | +||||
238 | +129x |
- div(+ checkmate::assert_list(ui_args, null.ok = TRUE, names = "named") |
|||
91 | -! | +||||
239 | +
- class = "snapshot_table_row",+ |
||||
92 | -! | +||||
240 | +127x |
- span(tags$b("Snapshot manager")),+ if (!missing(filters)) { |
|||
93 | +241 | ! |
- actionLink(ns("snapshot_add"), label = NULL, icon = icon("camera"), title = "add snapshot"),+ checkmate::assert_character(filters, min.len = 1, null.ok = TRUE, any.missing = FALSE) |
||
94 | +242 | ! |
- actionLink(ns("snapshot_load"), label = NULL, icon = icon("upload"), title = "upload snapshot"),+ datanames <- filters |
||
95 | +243 | ! |
- actionLink(ns("snapshot_reset"), label = NULL, icon = icon("undo"), title = "reset initial state"),+ msg <- |
||
96 | +244 | ! |
- NULL+ "The `filters` argument is deprecated and will be removed in the next release. Please use `datanames` instead." |
||
97 | -+ | ||||
245 | +! |
- ),+ logger::log_warn(msg) |
|||
98 | +246 | ! |
- uiOutput(ns("snapshot_list"))+ warning(msg) |
||
99 | +247 |
- )+ } |
|||
100 | +248 |
- }+ |
|||
101 | -+ | ||||
249 | +127x |
-
+ if (label == "global_filters") { |
|||
102 | -+ | ||||
250 | +1x |
- #' @rdname snapshot_manager_module+ stop( |
|||
103 | -+ | ||||
251 | +1x |
- #' @keywords internal+ sprintf("module(label = \"%s\", ...\n ", label),+ |
+ |||
252 | +1x | +
+ "Label 'global_filters' is reserved in teal. Please change to something else.",+ |
+ |||
253 | +1x | +
+ call. = FALSE |
|||
104 | +254 |
- #'+ ) |
|||
105 | +255 |
- snapshot_manager_srv <- function(id, slices_global, mapping_matrix, filtered_data_list) {+ } |
|||
106 | -6x | +256 | +126x |
- checkmate::assert_character(id)+ if (label == "Report previewer") { |
|
107 | -6x | +||||
257 | +! |
- checkmate::assert_true(is.reactive(slices_global))+ stop( |
|||
108 | -6x | +||||
258 | +! |
- checkmate::assert_class(isolate(slices_global()), "teal_slices")+ sprintf("module(label = \"%s\", ...\n ", label), |
|||
109 | -6x | +||||
259 | +! |
- checkmate::assert_true(is.reactive(mapping_matrix))+ "Label 'Report previewer' is reserved in teal.", |
|||
110 | -6x | +||||
260 | +! |
- checkmate::assert_data_frame(isolate(mapping_matrix()), null.ok = TRUE)+ call. = FALSE |
|||
111 | -6x | +||||
261 | +
- checkmate::assert_list(filtered_data_list, types = "FilteredData", any.missing = FALSE, names = "named")+ ) |
||||
112 | +262 |
-
+ } |
|||
113 | -6x | -
- moduleServer(id, function(input, output, session) {+ | 263 | +126x | +
+ server_formals <- names(formals(server)) |
114 | -6x | +264 | +126x |
- ns <- session$ns+ if (!( |
|
115 | -+ | ||||
265 | +126x |
-
+ "id" %in% server_formals ||+ |
+ |||
266 | +126x | +
+ all(c("input", "output", "session") %in% server_formals) |
|||
116 | +267 |
- # Store global filter states ----+ )) { |
|||
117 | -6x | +268 | +2x |
- filter <- isolate(slices_global())+ stop( |
|
118 | -6x | +269 | +2x |
- snapshot_history <- reactiveVal({+ "\nmodule() `server` argument requires a function with following arguments:", |
|
119 | -6x | +270 | +2x |
- list(+ "\n - id - teal will set proper shiny namespace for this module.", |
|
120 | -6x | +271 | +2x |
- "Initial application state" = as.list(filter, recursive = TRUE)+ "\n - input, output, session (not recommended) - then shiny::callModule will be used to call a module.", |
|
121 | -+ | ||||
272 | +2x |
- )+ "\n\nFollowing arguments can be used optionaly:", |
|||
122 | -+ | ||||
273 | +2x |
- })+ "\n - `data` - module will receive list of reactive (filtered) data specified in the `filters` argument",+ |
+ |||
274 | +2x | +
+ "\n - `datasets` - module will receive `FilteredData`. See `help(teal.slice::FilteredData)`",+ |
+ |||
275 | +2x | +
+ "\n - `reporter` - module will receive `Reporter`. See `help(teal.reporter::Reporter)`",+ |
+ |||
276 | +2x | +
+ "\n - `filter_panel_api` - module will receive `FilterPanelAPI`. (See [teal.slice::FilterPanelAPI]).",+ |
+ |||
277 | +2x | +
+ "\n - `...` server_args elements will be passed to the module named argument or to the `...`" |
|||
123 | +278 |
-
+ ) |
|||
124 | +279 |
- # Snapshot current application state ----+ } |
|||
125 | +280 |
- # Name snaphsot.+ |
|||
126 | -6x | +281 | +124x |
- observeEvent(input$snapshot_add, {+ if (!is.element("data", server_formals) && !is.null(datanames)) { |
|
127 | -! | +||||
282 | +46x |
- showModal(+ message(sprintf("module \"%s\" server function takes no data so \"datanames\" will be ignored", label)) |
|||
128 | -! | +||||
283 | +46x |
- modalDialog(+ datanames <- NULL |
|||
129 | -! | +||||
284 | +
- textInput(ns("snapshot_name"), "Name the snapshot", width = "100%", placeholder = "Meaningful, unique name"),+ } |
||||
130 | -! | +||||
285 | +124x |
- footer = tagList(+ if ("datasets" %in% server_formals) { |
|||
131 | -! | +||||
286 | +2x |
- actionButton(ns("snapshot_name_accept"), "Accept", icon = icon("thumbs-up")),+ warning( |
|||
132 | -! | +||||
287 | +2x |
- modalButton(label = "Cancel", icon = icon("thumbs-down"))+ sprintf("Called from module(label = \"%s\", ...)\n ", label), |
|||
133 | -+ | ||||
288 | +2x |
- ),+ "`datasets` argument in the `server` is deprecated and will be removed in the next release. ", |
|||
134 | -! | +||||
289 | +2x |
- size = "s"+ "Please use `data` instead.", |
|||
135 | -+ | ||||
290 | +2x |
- )+ call. = FALSE |
|||
136 | +291 |
- )+ ) |
|||
137 | +292 |
- })+ } |
|||
138 | +293 |
- # Store snaphsot.+ |
|||
139 | -6x | +294 | +124x |
- observeEvent(input$snapshot_name_accept, {+ srv_extra_args <- setdiff(names(server_args), server_formals) |
|
140 | -! | +||||
295 | +124x |
- snapshot_name <- trimws(input$snapshot_name)+ if (length(srv_extra_args) > 0 && !"..." %in% server_formals) { |
|||
141 | -! | +||||
296 | +1x |
- if (identical(snapshot_name, "")) {+ stop( |
|||
142 | -! | +||||
297 | +1x |
- showNotification(+ "\nFollowing `server_args` elements have no equivalent in the formals of the `server`:\n", |
|||
143 | -! | +||||
298 | +1x |
- "Please name the snapshot.",+ paste(paste(" -", srv_extra_args), collapse = "\n"), |
|||
144 | -! | +||||
299 | +1x |
- type = "message"+ "\n\nUpdate the `server` arguments by including above or add `...`" |
|||
145 | +300 |
- )+ ) |
|||
146 | -! | +||||
301 | +
- updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name")+ } |
||||
147 | -! | +||||
302 | +
- } else if (is.element(make.names(snapshot_name), make.names(names(snapshot_history())))) {+ |
||||
148 | -! | +||||
303 | +123x |
- showNotification(+ ui_formals <- names(formals(ui)) |
|||
149 | -! | +||||
304 | +123x |
- "This name is in conflict with other snapshot names. Please choose a different one.",+ if (!"id" %in% ui_formals) { |
|||
150 | -! | +||||
305 | +1x |
- type = "message"+ stop(+ |
+ |||
306 | +1x | +
+ "\nmodule() `ui` argument requires a function with following arguments:",+ |
+ |||
307 | +1x | +
+ "\n - id - teal will set proper shiny namespace for this module.",+ |
+ |||
308 | +1x | +
+ "\n\nFollowing arguments can be used optionally:",+ |
+ |||
309 | +1x | +
+ "\n - `...` ui_args elements will be passed to the module argument of the same name or to the `...`" |
|||
151 | +310 |
- )+ ) |
|||
152 | -! | +||||
311 | +
- updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name")+ } |
||||
153 | +312 |
- } else {+ |
|||
154 | -! | +||||
313 | +122x |
- snapshot <- as.list(slices_global(), recursive = TRUE)+ if (any(c("data", "datasets") %in% ui_formals)) { |
|||
155 | -! | +||||
314 | +2x |
- attr(snapshot, "mapping") <- matrix_to_mapping(mapping_matrix())+ stop( |
|||
156 | -! | +||||
315 | +2x |
- snapshot_update <- c(snapshot_history(), list(snapshot))+ sprintf("Called from module(label = \"%s\", ...)\n ", label), |
|||
157 | -! | +||||
316 | +2x |
- names(snapshot_update)[length(snapshot_update)] <- snapshot_name+ "`ui` with `data` or `datasets` argument is no longer accepted.\n ", |
|||
158 | -! | +||||
317 | +2x |
- snapshot_history(snapshot_update)+ "If some `ui` inputs depend on data, please move the logic to your `server` instead.\n ", |
|||
159 | -! | +||||
318 | +2x |
- removeModal()+ "Possible solutions are renderUI() or updateXyzInput() functions." |
|||
160 | +319 |
- # Reopen filter manager modal by clicking button in the main application.+ ) |
|||
161 | -! | +||||
320 | +
- shinyjs::click(id = "teal-main_ui-filter_manager-show", asis = TRUE)+ } |
||||
162 | +321 |
- }+ |
|||
163 | -+ | ||||
322 | +120x |
- })+ ui_extra_args <- setdiff(names(ui_args), ui_formals)+ |
+ |||
323 | +120x | +
+ if (length(ui_extra_args) > 0 && !"..." %in% ui_formals) {+ |
+ |||
324 | +1x | +
+ stop(+ |
+ |||
325 | +1x | +
+ "\nFollowing `ui_args` elements have no equivalent in the formals of `ui`:\n",+ |
+ |||
326 | +1x | +
+ paste(paste(" -", ui_extra_args), collapse = "\n"),+ |
+ |||
327 | +1x | +
+ "\n\nUpdate the `ui` arguments by including above or add `...`" |
|||
164 | +328 |
-
+ ) |
|||
165 | +329 |
- # Upload a snapshot file ----+ } |
|||
166 | +330 |
- # Select file.+ |
|||
167 | -6x | +331 | +119x |
- observeEvent(input$snapshot_load, {+ structure( |
|
168 | -! | +||||
332 | +119x |
- showModal(+ list( |
|||
169 | -! | +||||
333 | +119x |
- modalDialog(+ label = label, |
|||
170 | -! | +||||
334 | +119x |
- fileInput(ns("snapshot_file"), "Choose snapshot file", accept = ".json", width = "100%"),+ server = server, ui = ui, datanames = unique(datanames), |
|||
171 | -! | +||||
335 | +119x |
- textInput(+ server_args = server_args, ui_args = ui_args |
|||
172 | -! | +||||
336 | +
- ns("snapshot_name"),+ ), |
||||
173 | -! | +||||
337 | +119x |
- "Name the snapshot (optional)",+ class = "teal_module" |
|||
174 | -! | +||||
338 | +
- width = "100%",+ ) |
||||
175 | -! | +||||
339 | +
- placeholder = "Meaningful, unique name"+ } |
||||
176 | +340 |
- ),+ |
|||
177 | -! | +||||
341 | +
- footer = tagList(+ |
||||
178 | -! | +||||
342 | +
- actionButton(ns("snaphot_file_accept"), "Accept", icon = icon("thumbs-up")),+ #' Get module depth |
||||
179 | -! | +||||
343 | +
- modalButton(label = "Cancel", icon = icon("thumbs-down"))+ #' |
||||
180 | +344 |
- )+ #' Depth starts at 0, so a single `teal.module` has depth 0. |
|||
181 | +345 |
- )+ #' Nesting it increases overall depth by 1. |
|||
182 | +346 |
- )+ #' |
|||
183 | +347 |
- })+ #' @inheritParams init |
|||
184 | +348 |
- # Store new snapshot to list and restore filter states.+ #' @param depth optional, integer determining current depth level |
|||
185 | -6x | +||||
349 | +
- observeEvent(input$snaphot_file_accept, {+ #' |
||||
186 | -! | +||||
350 | +
- snapshot_name <- trimws(input$snapshot_name)+ #' @return depth level for given module |
||||
187 | -! | +||||
351 | +
- if (identical(snapshot_name, "")) {+ #' @keywords internal |
||||
188 | -! | +||||
352 | +
- snapshot_name <- tools::file_path_sans_ext(input$snapshot_file$name)+ #' |
||||
189 | +353 |
- }+ #' @examples |
|||
190 | -! | +||||
354 | +
- if (is.element(make.names(snapshot_name), make.names(names(snapshot_history())))) {+ #' mods <- modules( |
||||
191 | -! | +||||
355 | +
- showNotification(+ #' label = "d1", |
||||
192 | -! | +||||
356 | +
- "This name is in conflict with other snapshot names. Please choose a different one.",+ #' modules( |
||||
193 | -! | +||||
357 | +
- type = "message"+ #' label = "d2", |
||||
194 | +358 |
- )+ #' modules( |
|||
195 | -! | +||||
359 | +
- updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name")+ #' label = "d3", |
||||
196 | +360 |
- } else {+ #' module(label = "aaa1"), module(label = "aaa2"), module(label = "aaa3") |
|||
197 | +361 |
- # Restore snapshot and verify app compatibility.+ #' ), |
|||
198 | -! | +||||
362 | +
- snapshot_state <- try(slices_restore(input$snapshot_file$datapath))+ #' module(label = "bbb") |
||||
199 | -! | +||||
363 | +
- if (!inherits(snapshot_state, "modules_teal_slices")) {+ #' ), |
||||
200 | -! | +||||
364 | +
- showNotification(+ #' module(label = "ccc") |
||||
201 | -! | +||||
365 | +
- "File appears to be corrupt.",+ #' ) |
||||
202 | -! | +||||
366 | ++ |
+ #' stopifnot(teal:::modules_depth(mods) == 3L)+ |
+ |||
367 | +
- type = "error"+ #' |
||||
203 | +368 |
- )+ #' mods <- modules( |
|||
204 | -! | +||||
369 | +
- } else if (!identical(attr(snapshot_state, "app_id"), attr(slices_global(), "app_id"))) {+ #' label = "a", |
||||
205 | -! | +||||
370 | +
- showNotification(+ #' modules( |
||||
206 | -! | +||||
371 | +
- "This snapshot file is not compatible with the app and cannot be loaded.",+ #' label = "b1", module(label = "c") |
||||
207 | -! | +||||
372 | +
- type = "warning"+ #' ), |
||||
208 | +373 |
- )+ #' module(label = "b2") |
|||
209 | +374 |
- } else {+ #' ) |
|||
210 | +375 |
- # Add to snapshot history.+ #' stopifnot(teal:::modules_depth(mods) == 2L) |
|||
211 | -! | +||||
376 | +
- snapshot <- as.list(snapshot_state, recursive = TRUE)+ modules_depth <- function(modules, depth = 0L) { |
||||
212 | -! | +||||
377 | +12x |
- snapshot_update <- c(snapshot_history(), list(snapshot))+ checkmate::assert( |
|||
213 | -! | +||||
378 | +12x |
- names(snapshot_update)[length(snapshot_update)] <- snapshot_name+ checkmate::check_class(modules, "teal_module"), |
|||
214 | -! | +||||
379 | +12x |
- snapshot_history(snapshot_update)+ checkmate::check_class(modules, "teal_modules") |
|||
215 | +380 |
- ### Begin simplified restore procedure. ###+ ) |
|||
216 | -! | +||||
381 | +12x |
- mapping_unfolded <- unfold_mapping(attr(snapshot_state, "mapping"), names(filtered_data_list))+ checkmate::assert_int(depth, lower = 0) |
|||
217 | -! | +||||
382 | +11x |
- mapply(+ if (inherits(modules, "teal_modules")) { |
|||
218 | -! | +||||
383 | +4x |
- function(filtered_data, filter_ids) {+ max(vapply(modules$children, modules_depth, integer(1), depth = depth + 1L)) |
|||
219 | -! | +||||
384 | +
- filtered_data$clear_filter_states(force = TRUE)+ } else { |
||||
220 | -! | +||||
385 | +7x |
- slices <- Filter(function(x) x$id %in% filter_ids, snapshot_state)+ depth |
|||
221 | -! | +||||
386 | +
- filtered_data$set_filter_state(slices)+ } |
||||
222 | +387 |
- },+ } |
|||
223 | -! | +||||
388 | +
- filtered_data = filtered_data_list,+ |
||||
224 | -! | +||||
389 | +
- filter_ids = mapping_unfolded+ |
||||
225 | +390 |
- )+ module_labels <- function(modules) { |
|||
226 | +391 | ! |
- slices_global(snapshot_state)+ if (inherits(modules, "teal_modules")) { |
||
227 | +392 | ! |
- removeModal()+ lapply(modules$children, module_labels) |
||
228 | +393 |
- ### End simplified restore procedure. ###+ } else {+ |
+ |||
394 | +! | +
+ modules$label |
|||
229 | +395 |
- }+ } |
|||
230 | +396 |
- }+ } |
|||
231 | +397 |
- })+ |
|||
232 | +398 |
- # Apply newly added snapshot.+ #' Converts `teal_modules` to a string |
|||
233 | +399 |
-
+ #' |
|||
234 | +400 |
- # Restore initial state ----+ #' @param x (`teal_modules`) to print |
|||
235 | -6x | +||||
401 | +
- observeEvent(input$snapshot_reset, {+ #' @param indent (`integer`) indent level; |
||||
236 | -! | +||||
402 | +
- s <- "Initial application state"+ #' each `submodule` is indented one level more |
||||
237 | +403 |
- ### Begin restore procedure. ###+ #' @param ... (optional) additional parameters to pass to recursive calls of `toString` |
|||
238 | -! | +||||
404 | +
- snapshot <- snapshot_history()[[s]]+ #' @return (`character`) |
||||
239 | -! | +||||
405 | +
- snapshot_state <- as.teal_slices(snapshot)+ #' @export |
||||
240 | -! | +||||
406 | +
- mapping_unfolded <- unfold_mapping(attr(snapshot_state, "mapping"), names(filtered_data_list))+ #' @rdname modules |
||||
241 | -! | +||||
407 | ++ |
+ toString.teal_modules <- function(x, indent = 0, ...) { # nolint+ |
+ |||
408 | +
- mapply(+ # argument must be `x` to be consistent with base method |
||||
242 | +409 | ! |
- function(filtered_data, filter_ids) {+ paste(c( |
||
243 | +410 | ! |
- filtered_data$clear_filter_states(force = TRUE)+ paste0(rep(" ", indent), "+ ", x$label), |
||
244 | +411 | ! |
- slices <- Filter(function(x) x$id %in% filter_ids, snapshot_state)+ unlist(lapply(x$children, toString, indent = indent + 1, ...)) |
||
245 | +412 | ! |
- filtered_data$set_filter_state(slices)+ ), collapse = "\n") |
||
246 | +413 |
- },+ } |
|||
247 | -! | +||||
414 | +
- filtered_data = filtered_data_list,+ |
||||
248 | -! | +||||
415 | +
- filter_ids = mapping_unfolded+ #' Converts `teal_module` to a string |
||||
249 | +416 |
- )+ #' |
|||
250 | -! | +||||
417 | +
- slices_global(snapshot_state)+ #' @inheritParams toString.teal_modules |
||||
251 | -! | +||||
418 | +
- removeModal()+ #' @param x `teal_module` |
||||
252 | +419 |
- ### End restore procedure. ###+ #' @param ... ignored |
|||
253 | +420 |
- })+ #' @export |
|||
254 | +421 |
-
+ #' @rdname module |
|||
255 | +422 |
- # Build snapshot table ----+ toString.teal_module <- function(x, indent = 0, ...) { # nolint |
|||
256 | -+ | ||||
423 | +! |
- # Create UI elements and server logic for the snapshot table.+ paste0(paste(rep(" ", indent), collapse = ""), "+ ", x$label, collapse = "") |
|||
257 | +424 |
- # Observers must be tracked to avoid duplication and excess reactivity.+ } |
|||
258 | +425 |
- # Remaining elements are tracked likewise for consistency and a slight speed margin.+ |
|||
259 | -6x | +||||
426 | +
- observers <- reactiveValues()+ #' Prints `teal_modules` |
||||
260 | -6x | +||||
427 | +
- handlers <- reactiveValues()+ #' @param x `teal_modules` |
||||
261 | -6x | +||||
428 | +
- divs <- reactiveValues()+ #' @param ... parameters passed to `toString` |
||||
262 | +429 |
-
+ #' @export |
|||
263 | -6x | +||||
430 | +
- observeEvent(snapshot_history(), {+ #' @rdname modules |
||||
264 | -2x | +||||
431 | +
- lapply(names(snapshot_history())[-1L], function(s) {+ print.teal_modules <- function(x, ...) { |
||||
265 | +432 | ! |
- id_pickme <- sprintf("pickme_%s", make.names(s))+ s <- toString(x, ...) |
||
266 | +433 | ! |
- id_saveme <- sprintf("saveme_%s", make.names(s))+ cat(s) |
||
267 | +434 | ! |
- id_rowme <- sprintf("rowme_%s", make.names(s))+ return(invisible(s)) |
||
268 | +435 |
-
+ } |
|||
269 | +436 |
- # Observer for restoring snapshot.- |
- |||
270 | -! | -
- if (!is.element(id_pickme, names(observers))) {+ |
|||
271 | -! | +||||
437 | +
- observers[[id_pickme]] <- observeEvent(input[[id_pickme]], {+ #' Prints `teal_module` |
||||
272 | +438 |
- ### Begin restore procedure. ###+ #' @param x `teal_module` |
|||
273 | -! | +||||
439 | +
- snapshot <- snapshot_history()[[s]]+ #' @param ... parameters passed to `toString` |
||||
274 | -! | +||||
440 | +
- snapshot_state <- as.teal_slices(snapshot)+ #' @export |
||||
275 | -! | +||||
441 | +
- mapping_unfolded <- unfold_mapping(attr(snapshot_state, "mapping"), names(filtered_data_list))+ #' @rdname module |
||||
276 | -! | +||||
442 | +
- mapply(+ print.teal_module <- print.teal_modules |
||||
277 | -! | +
1 | +
- function(filtered_data, filter_ids) {+ #' Filter settings for teal applications |
||
278 | -! | +||
2 | +
- filtered_data$clear_filter_states(force = TRUE)+ #' |
||
279 | -! | +||
3 | +
- slices <- Filter(function(x) x$id %in% filter_ids, snapshot_state)+ #' Specify initial filter states and filtering settings for a `teal` app. |
||
280 | -! | +||
4 | +
- filtered_data$set_filter_state(slices)+ #' |
||
281 | +5 |
- },+ #' Produces a `teal_slices` object. |
|
282 | -! | +||
6 | +
- filtered_data = filtered_data_list,+ #' The `teal_slice` components will specify filter states that will be active when the app starts. |
||
283 | -! | +||
7 | +
- filter_ids = mapping_unfolded+ #' Attributes (created with the named arguments) will configure the way the app applies filters. |
||
284 | +8 |
- )+ #' See argument descriptions for details. |
|
285 | -! | +||
9 | +
- slices_global(snapshot_state)+ #' |
||
286 | -! | +||
10 | +
- removeModal()+ #' @inheritParams teal.slice::teal_slices |
||
287 | +11 |
- ### End restore procedure. ###+ #' |
|
288 | +12 |
- })+ #' @param module_specific optional (`logical(1)`)\cr |
|
289 | +13 |
- }+ #' - `FALSE` (default) when one filter panel applied to all modules. |
|
290 | +14 |
- # Create handler for downloading snapshot.+ #' All filters will be shared by all modules. |
|
291 | -! | +||
15 | +
- if (!is.element(id_saveme, names(handlers))) {+ #' - `TRUE` when filter panel module-specific. |
||
292 | -! | +||
16 | +
- output[[id_saveme]] <- downloadHandler(+ #' Modules can have different set of filters specified - see `mapping` argument. |
||
293 | -! | +||
17 | +
- filename = function() {+ #' @param mapping `r lifecycle::badge("experimental")` _This is a new feature. Do kindly share your opinions.\cr_ |
||
294 | -! | +||
18 | +
- sprintf("teal_snapshot_%s_%s.json", s, Sys.Date())+ #' (`named list`)\cr |
||
295 | +19 |
- },+ #' Specifies which filters will be active in which modules on app start. |
|
296 | -! | +||
20 | +
- content = function(file) {+ #' Elements should contain character vector of `teal_slice` `id`s (see [teal.slice::teal_slice()]). |
||
297 | -! | +||
21 | +
- snapshot <- snapshot_history()[[s]]+ #' Names of the list should correspond to `teal_module` `label` set in [module()] function. |
||
298 | -! | +||
22 | +
- snapshot_state <- as.teal_slices(snapshot)+ #' `id`s listed under `"global_filters` will be active in all modules. |
||
299 | -! | +||
23 | +
- slices_store(tss = snapshot_state, file = file)+ #' If missing, all filters will be applied to all modules. |
||
300 | +24 |
- }+ #' If empty list, all filters will be available to all modules but will start inactive. |
|
301 | +25 |
- )+ #' If `module_specific` is `FALSE`, only `global_filters` will be active on start. |
|
302 | -! | +||
26 | +
- handlers[[id_saveme]] <- id_saveme+ #' @param app_id (`character(1)`)\cr |
||
303 | +27 |
- }+ #' For internal use only, do not set manually. |
|
304 | +28 |
- # Create a row for the snapshot table.+ #' Added by `init` so that a `teal_slices` can be matched to the app in which it was used. |
|
305 | -! | +||
29 | +
- if (!is.element(id_rowme, names(divs))) {+ #' Used for verifying snapshots uploaded from file. See `snapshot`. |
||
306 | -! | +||
30 | +
- divs[[id_rowme]] <- div(+ #' |
||
307 | -! | +||
31 | +
- class = "snapshot_table_row",+ #' @param x (`list`) of lists to convert to `teal_slices` |
||
308 | -! | +||
32 | +
- span(h5(s)),+ #' |
||
309 | -! | +||
33 | +
- actionLink(inputId = ns(id_pickme), label = icon("circle-check"), title = "select"),+ #' @return |
||
310 | -! | +||
34 | +
- downloadLink(outputId = ns(id_saveme), label = icon("save"), title = "save to file")+ #' A `teal_slices` object. |
||
311 | +35 |
- )+ #' |
|
312 | +36 |
- }+ #' @seealso [`teal.slice::teal_slices`], [`teal.slice::teal_slice`], [`slices_store`] |
|
313 | +37 |
- })+ #' |
|
314 | +38 |
- })+ #' @examples |
|
315 | +39 |
-
+ #' filter <- teal_slices( |
|
316 | +40 |
- # Create table to display list of snapshots and their actions.+ #' teal.slice::teal_slice(dataname = "iris", varname = "Species", id = "species"), |
|
317 | -6x | +||
41 | +
- output$snapshot_list <- renderUI({+ #' teal.slice::teal_slice(dataname = "iris", varname = "Sepal.Length", id = "sepal_length"), |
||
318 | -2x | +||
42 | +
- rows <- lapply(rev(reactiveValuesToList(divs)), function(d) d)+ #' teal.slice::teal_slice( |
||
319 | -2x | +||
43 | +
- if (length(rows) == 0L) {+ #' dataname = "iris", id = "long_petals", title = "Long petals", expr = "Petal.Length > 5" |
||
320 | -2x | +||
44 | +
- div(+ #' ), |
||
321 | -2x | +||
45 | +
- class = "snapshot_manager_placeholder",+ #' teal.slice::teal_slice(dataname = "mtcars", varname = "mpg", id = "mtcars_mpg"), |
||
322 | -2x | +||
46 | +
- "Snapshots will appear here."+ #' mapping = list( |
||
323 | +47 |
- )+ #' module1 = c("species", "sepal_length"), |
|
324 | +48 |
- } else {+ #' module2 = c("mtcars_mpg"), |
|
325 | -! | +||
49 | +
- rows+ #' global_filters = "long_petals" |
||
326 | +50 |
- }+ #' ) |
|
327 | +51 |
- })+ #' ) |
|
328 | +52 |
- })+ #' |
|
329 | +53 |
- }+ #' app <- teal::init( |
|
330 | +54 |
-
+ #' data = list(iris = iris, mtcars = mtcars), |
|
331 | +55 |
-
+ #' modules = list( |
|
332 | +56 |
-
+ #' module("module1"), |
|
333 | +57 |
-
+ #' module("module2") |
|
334 | +58 |
- ### utility functions ----+ #' ), |
|
335 | +59 |
-
+ #' filter = filter |
|
336 | +60 |
- #' Explicitly enumerate global filters.+ #' ) |
|
337 | +61 |
#' |
|
338 | +62 |
- #' Transform module mapping such that global filters are explicitly specified for every module.+ #' if (interactive()) { |
|
339 | +63 |
- #'+ #' shinyApp(app$ui, app$server) |
|
340 | +64 |
- #' @param mapping (`named list`) as stored in mapping parameter of `teal_slices`+ #' } |
|
341 | +65 |
- #' @param module_names (`character`) vector containing names of all modules in the app+ #' |
|
342 | +66 |
- #' @return A `named_list` with one element per module, each element containing all filters applied to that module.+ #' @export |
|
343 | +67 |
- #' @keywords internal+ teal_slices <- function(..., |
|
344 | +68 |
- #'+ exclude_varnames = NULL, |
|
345 | +69 |
- unfold_mapping <- function(mapping, module_names) {+ include_varnames = NULL, |
|
346 | -! | +||
70 | +
- module_names <- structure(module_names, names = module_names)+ count_type = NULL, |
||
347 | -! | +||
71 | +
- lapply(module_names, function(x) c(mapping[[x]], mapping[["global_filters"]]))+ allow_add = TRUE, |
||
348 | +72 |
- }+ module_specific = FALSE, |
|
349 | +73 |
-
+ mapping, |
|
350 | +74 |
- #' Convert mapping matrix to filter mapping specification.+ app_id = NULL) { |
|
351 | -+ | ||
75 | +76x |
- #'+ shiny::isolate({ |
|
352 | -+ | ||
76 | +76x |
- #' Transform a mapping matrix, i.e. a data frame that maps each filter state to each module,+ checkmate::assert_flag(allow_add) |
|
353 | -+ | ||
77 | +76x |
- #' to a list specification like the one used in the `mapping` attribute of `teal_slices`.+ checkmate::assert_flag(module_specific) |
|
354 | -+ | ||
78 | +37x |
- #' Global filters are gathered in one list element.+ if (!missing(mapping)) checkmate::assert_list(mapping, types = c("character", "NULL"), names = "named") |
|
355 | -+ | ||
79 | +73x |
- #' If a module has no active filters but the global ones, it will not be mentioned in the output.+ checkmate::assert_string(app_id, null.ok = TRUE) |
|
356 | +80 |
- #'+ |
|
357 | -+ | ||
81 | +73x |
- #' @param mapping_matrix (`data.frame`) of logical vectors where+ slices <- list(...) |
|
358 | -+ | ||
82 | +73x |
- #' columns represent modules and row represent `teal_slice`s+ all_slice_id <- vapply(slices, `[[`, character(1L), "id") |
|
359 | +83 |
- #' @return `named list` like that in the `mapping` attribute of a `teal_slices` object.+ |
|
360 | -+ | ||
84 | +73x |
- #' @keywords internal+ if (missing(mapping)) {+ |
+ |
85 | +39x | +
+ mapping <- list(global_filters = all_slice_id) |
|
361 | +86 |
- #'+ }+ |
+ |
87 | +73x | +
+ if (!module_specific) {+ |
+ |
88 | +69x | +
+ mapping[setdiff(names(mapping), "global_filters")] <- NULL |
|
362 | +89 |
- matrix_to_mapping <- function(mapping_matrix) {+ } |
|
363 | -! | +||
90 | +
- mapping_matrix[] <- lapply(mapping_matrix, function(x) x | is.na(x))+ |
||
364 | -! | +||
91 | +73x |
- global <- vapply(as.data.frame(t(mapping_matrix)), all, logical(1L))+ failed_slice_id <- setdiff(unlist(mapping), all_slice_id) |
|
365 | -! | +||
92 | +73x |
- global_filters <- names(global[global])+ if (length(failed_slice_id)) { |
|
366 | -! | +||
93 | +1x |
- local_filters <- mapping_matrix[!rownames(mapping_matrix) %in% global_filters, ]+ stop(sprintf( |
|
367 | -+ | ||
94 | +1x |
-
+ "Filters in mapping don't match any available filter.\n %s not in %s", |
|
368 | -! | +||
95 | +1x |
- mapping <- c(lapply(local_filters, function(x) rownames(local_filters)[x]), list(global_filters = global_filters))+ toString(failed_slice_id), |
|
369 | -! | +||
96 | +1x |
- Filter(function(x) length(x) != 0L, mapping)+ toString(all_slice_id) |
|
370 | +97 |
- }+ )) |
1 | +98 |
- # This module is the main teal module that puts everything together.+ } |
||
2 | +99 | |||
3 | -+ | |||
100 | +72x |
- #' teal main app module+ tss <- teal.slice::teal_slices( |
||
4 | +101 |
- #'+ ..., |
||
5 | -+ | |||
102 | +72x |
- #' This is the main teal app that puts everything together.+ exclude_varnames = exclude_varnames, |
||
6 | -+ | |||
103 | +72x |
- #'+ include_varnames = include_varnames, |
||
7 | -+ | |||
104 | +72x |
- #' It displays the splash UI which is used to fetch the data, possibly+ count_type = count_type, |
||
8 | -+ | |||
105 | +72x |
- #' prompting for a password input to fetch the data. Once the data is ready,+ allow_add = allow_add |
||
9 | +106 |
- #' the splash screen is replaced by the actual teal UI that is tabsetted and+ ) |
||
10 | -+ | |||
107 | +72x |
- #' has a filter panel with `datanames` that are relevant for the current tab.+ attr(tss, "mapping") <- mapping |
||
11 | -+ | |||
108 | +72x |
- #' Nested tabs are possible, but we limit it to two nesting levels for reasons+ attr(tss, "module_specific") <- module_specific |
||
12 | -+ | |||
109 | +72x |
- #' of clarity of the UI.+ attr(tss, "app_id") <- app_id |
||
13 | -+ | |||
110 | +72x |
- #'+ class(tss) <- c("modules_teal_slices", class(tss)) |
||
14 | -+ | |||
111 | +72x |
- #' The splash screen functionality can also be used+ tss |
||
15 | +112 |
- #' for non-delayed data which takes time to load into memory, avoiding+ }) |
||
16 | +113 |
- #' Shiny session timeouts.+ } |
||
17 | +114 |
- #'+ |
||
18 | +115 |
- #' Server evaluates the `teal_data_rv` (delayed data mechanism) and creates the+ |
||
19 | +116 |
- #' `datasets` object that is shared across modules.+ #' @rdname teal_slices |
||
20 | +117 |
- #' Once it is ready and non-`NULL`, the splash screen is replaced by the+ #' @export |
||
21 | +118 |
- #' main teal UI that depends on the data.+ #' @keywords internal |
||
22 | +119 |
- #' The currently active tab is tracked and the right filter panel+ #' |
||
23 | +120 |
- #' updates the displayed datasets to filter for according to the active `datanames`+ as.teal_slices <- function(x) { # nolint |
||
24 | -+ | |||
121 | +15x |
- #' of the tab.+ checkmate::assert_list(x) |
||
25 | -+ | |||
122 | +15x |
- #'+ lapply(x, checkmate::assert_list, names = "named", .var.name = "list element") |
||
26 | +123 |
- #' It is written as a Shiny module so it can be added into other apps as well.+ |
||
27 | -+ | |||
124 | +15x |
- #'+ attrs <- attributes(unclass(x)) |
||
28 | -+ | |||
125 | +15x |
- #' @name module_teal+ ans <- lapply(x, function(x) if (is.teal_slice(x)) x else as.teal_slice(x)) |
||
29 | -+ | |||
126 | +15x |
- #'+ do.call(teal_slices, c(ans, attrs)) |
||
30 | +127 |
- #' @inheritParams ui_teal_with_splash+ } |
||
31 | +128 |
- #'+ |
||
32 | +129 |
- #' @param splash_ui (`shiny.tag`)\cr UI to display initially,+ |
||
33 | +130 |
- #' can be a splash screen or a Shiny module UI. For the latter, see+ #' @rdname teal_slices |
||
34 | +131 |
- #' [init()] about how to call the corresponding server function.+ #' @export |
||
35 | +132 |
- #'+ #' @keywords internal |
||
36 | +133 |
- #' @param teal_data_rv (`reactive`)\cr+ #' |
||
37 | +134 |
- #' returns the `teal_data`, only evaluated once, `NULL` value is ignored+ c.teal_slices <- function(...) { |
||
38 | -+ | |||
135 | +! |
- #'+ x <- list(...) |
||
39 | -+ | |||
136 | +! |
- #' @return+ checkmate::assert_true(all(vapply(x, is.teal_slices, logical(1L))), .var.name = "all arguments are teal_slices") |
||
40 | +137 |
- #' `ui_teal` returns `HTML` for Shiny module UI.+ |
||
41 | -+ | |||
138 | +! |
- #' `srv_teal` returns `reactive` which returns the currently active module.+ all_attributes <- lapply(x, attributes) |
||
42 | -+ | |||
139 | +! |
- #'+ all_attributes <- coalesce_r(all_attributes) |
||
43 | -+ | |||
140 | +! |
- #' @keywords internal+ all_attributes <- all_attributes[names(all_attributes) != "class"] |
||
44 | +141 |
- #'+ |
||
45 | -+ | |||
142 | +! |
- #' @examples+ do.call( |
||
46 | -+ | |||
143 | +! |
- #' mods <- teal:::example_modules()+ teal_slices, |
||
47 | -+ | |||
144 | +! |
- #' teal_data_rv <- reactive(teal:::example_cdisc_data())+ c( |
||
48 | -+ | |||
145 | +! |
- #' app <- shinyApp(+ unique(unlist(x, recursive = FALSE)), |
||
49 | -+ | |||
146 | +! |
- #' ui = function() {+ all_attributes |
||
50 | +147 |
- #' teal:::ui_teal("dummy")+ ) |
||
51 | +148 |
- #' },+ ) |
||
52 | +149 |
- #' server = function(input, output, session) {+ } |
||
53 | +150 |
- #' active_module <- teal:::srv_teal(id = "dummy", modules = mods, teal_data_rv = teal_data_rv)+ |
||
54 | +151 |
- #' }+ |
||
55 | +152 |
- #' )+ #' Deep copy `teal_slices` |
||
56 | +153 |
- #' if (interactive()) {+ #' |
||
57 | +154 |
- #' shinyApp(app$ui, app$server)+ #' it's important to create a new copy of `teal_slices` when |
||
58 | +155 |
- #' }+ #' starting a new `shiny` session. Otherwise, object will be shared |
||
59 | +156 |
- NULL+ #' by multiple users as it is created in global environment before |
||
60 | +157 |
-
+ #' `shiny` session starts. |
||
61 | +158 |
- #' @rdname module_teal+ #' @param filter (`teal_slices`) |
||
62 | +159 |
- ui_teal <- function(id,+ #' @return `teal_slices` |
||
63 | +160 |
- splash_ui = tags$h2("Starting the Teal App"),+ #' @keywords internal |
||
64 | +161 |
- title = NULL,+ deep_copy_filter <- function(filter) { |
||
65 | -+ | |||
162 | +1x |
- header = tags$p(""),+ checkmate::assert_class(filter, "teal_slices") |
||
66 | -+ | |||
163 | +1x |
- footer = tags$p("")) {+ shiny::isolate({ |
||
67 | -12x | +164 | +1x |
- if (checkmate::test_string(header)) {+ filter_copy <- lapply(filter, function(slice) { |
68 | -! | +|||
165 | +2x |
- header <- tags$h1(header)+ teal.slice::as.teal_slice(as.list(slice)) |
||
69 | +166 |
- }+ }) |
||
70 | -12x | +167 | +1x |
- if (checkmate::test_string(footer)) {+ attributes(filter_copy) <- attributes(filter) |
71 | -! | +|||
168 | +1x |
- footer <- tags$p(footer)+ filter_copy |
||
72 | +169 |
- }+ }) |
||
73 | -12x | +|||
170 | +
- checkmate::assert(+ } |
|||
74 | -12x | +
1 | +
- checkmate::check_class(splash_ui, "shiny.tag"),+ #' Create a UI of nested tabs of `teal_modules` |
|||
75 | -12x | +|||
2 | +
- checkmate::check_class(splash_ui, "shiny.tag.list"),+ #' |
|||
76 | -12x | +|||
3 | +
- checkmate::check_class(splash_ui, "html")+ #' @section `ui_nested_tabs`: |
|||
77 | +4 |
- )+ #' Each `teal_modules` is translated to a `tabsetPanel` and each |
||
78 | -12x | +|||
5 | +
- checkmate::assert(+ #' of its children is another tab-module called recursively. The UI of a |
|||
79 | -12x | +|||
6 | +
- checkmate::check_class(header, "shiny.tag"),+ #' `teal_module` is obtained by calling the `ui` function on it. |
|||
80 | -12x | +|||
7 | +
- checkmate::check_class(header, "shiny.tag.list"),+ #' |
- |||
81 | -12x | +|||
8 | +
- checkmate::check_class(header, "html")+ #' The `datasets` argument is required to resolve the teal arguments in an |
|||
82 | +9 |
- )+ #' isolated context (with respect to reactivity) |
||
83 | -12x | +|||
10 | +
- checkmate::assert(+ #' |
|||
84 | -12x | +|||
11 | +
- checkmate::check_class(footer, "shiny.tag"),+ #' @section `srv_nested_tabs`: |
|||
85 | -12x | +|||
12 | +
- checkmate::check_class(footer, "shiny.tag.list"),+ #' This module calls recursively all elements of the `modules` returns one which |
|||
86 | -12x | +|||
13 | +
- checkmate::check_class(footer, "html")+ #' is currently active. |
|||
87 | +14 |
- )+ #' - `teal_module` returns self as a active module. |
||
88 | +15 |
-
+ #' - `teal_modules` also returns module active within self which is determined by the `input$active_tab`. |
||
89 | -12x | +|||
16 | +
- ns <- NS(id)+ #' |
|||
90 | +17 |
- # Once the data is loaded, we will remove this element and add the real teal UI instead+ #' @name module_nested_tabs |
||
91 | -12x | +|||
18 | +
- splash_ui <- div(+ #' |
|||
92 | +19 |
- # id so we can remove the splash screen once ready, which is the first child of this container+ #' @inheritParams module_tabs_with_filters |
||
93 | -12x | +|||
20 | +
- id = ns("main_ui_container"),+ #' |
|||
94 | +21 |
- # we put it into a div, so it can easily be removed as a whole, also when it is a tagList (and not+ #' @param depth (`integer(1)`)\cr |
||
95 | +22 |
- # just the first item of the tagList)+ #' number which helps to determine depth of the modules nesting. |
||
96 | -12x | +|||
23 | +
- div(splash_ui)+ #' @param is_module_specific (`logical(1)`)\cr |
|||
97 | +24 |
- )+ #' flag determining if the filter panel is global or module-specific. |
||
98 | +25 |
-
+ #' When set to `TRUE`, a filter panel is called inside of each module tab. |
||
99 | +26 |
- # show busy icon when shiny session is busy computing stuff+ #' @return depending on class of `modules`, `ui_nested_tabs` returns: |
||
100 | +27 |
- # based on https://stackoverflow.com/questions/17325521/r-shiny-display-loading-message-while-function-is-running/22475216#22475216 #nolint+ #' - `teal_module`: instantiated UI of the module |
||
101 | -12x | +|||
28 | +
- shiny_busy_message_panel <- conditionalPanel(+ #' - `teal_modules`: `tabsetPanel` with each tab corresponding to recursively |
|||
102 | -12x | +|||
29 | +
- condition = "(($('html').hasClass('shiny-busy')) && (document.getElementById('shiny-notification-panel') == null))", # nolint+ #' calling this function on it.\cr |
|||
103 | -12x | +|||
30 | +
- div(+ #' `srv_nested_tabs` returns a reactive which returns the active module that corresponds to the selected tab. |
|||
104 | -12x | +|||
31 | +
- icon("arrows-rotate", "spin fa-spin"),+ #' |
|||
105 | -12x | +|||
32 | +
- "Computing ...",+ #' @examples |
|||
106 | +33 |
- # CSS defined in `custom.css`+ #' mods <- teal:::example_modules() |
||
107 | -12x | +|||
34 | +
- class = "shinybusymessage"+ #' datasets <- teal:::example_datasets() |
|||
108 | +35 |
- )+ #' app <- shinyApp( |
||
109 | +36 |
- )+ #' ui = function() { |
||
110 | +37 |
-
+ #' tagList( |
||
111 | -12x | +|||
38 | +
- res <- fluidPage(+ #' teal:::include_teal_css_js(), |
|||
112 | -12x | +|||
39 | +
- title = title,+ #' textOutput("info"), |
|||
113 | -12x | +|||
40 | +
- theme = get_teal_bs_theme(),+ #' fluidPage( # needed for nice tabs |
|||
114 | -12x | +|||
41 | +
- include_teal_css_js(),+ #' teal:::ui_nested_tabs("dummy", modules = mods, datasets = datasets) |
|||
115 | -12x | +|||
42 | +
- tags$header(header),+ #' ) |
|||
116 | -12x | +|||
43 | +
- tags$hr(class = "my-2"),+ #' ) |
|||
117 | -12x | +|||
44 | +
- shiny_busy_message_panel,+ #' }, |
|||
118 | -12x | +|||
45 | +
- splash_ui,+ #' server = function(input, output, session) { |
|||
119 | -12x | +|||
46 | +
- tags$hr(),+ #' active_module <- teal:::srv_nested_tabs( |
|||
120 | -12x | +|||
47 | +
- tags$footer(+ #' "dummy", |
|||
121 | -12x | +|||
48 | +
- div(+ #' datasets = datasets, |
|||
122 | -12x | +|||
49 | +
- footer,+ #' modules = mods |
|||
123 | -12x | +|||
50 | +
- teal.widgets::verbatim_popup_ui(ns("sessionInfo"), "Session Info", type = "link"),+ #' ) |
|||
124 | -12x | +|||
51 | +
- textOutput(ns("identifier"))+ #' output$info <- renderText({ |
|||
125 | +52 |
- )+ #' paste0("The currently active tab name is ", active_module()$label) |
||
126 | +53 |
- )+ #' }) |
||
127 | +54 |
- )+ #' } |
||
128 | -12x | +|||
55 | +
- return(res)+ #' ) |
|||
129 | +56 |
- }+ #' if (interactive()) { |
||
130 | +57 |
-
+ #' shinyApp(app$ui, app$server) |
||
131 | +58 |
-
+ #' } |
||
132 | +59 |
- #' @rdname module_teal+ #' @keywords internal |
||
133 | +60 |
- srv_teal <- function(id, modules, teal_data_rv, filter = teal_slices()) {+ NULL |
||
134 | -19x | +|||
61 | +
- stopifnot(is.reactive(teal_data_rv))+ |
|||
135 | -18x | +|||
62 | +
- moduleServer(id, function(input, output, session) {+ #' @rdname module_nested_tabs |
|||
136 | -18x | +|||
63 | +
- logger::log_trace("srv_teal initializing the module.")+ ui_nested_tabs <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) { |
|||
137 | -+ | |||
64 | +! |
-
+ checkmate::assert_multi_class(modules, c("teal_modules", "teal_module")) |
||
138 | -18x | +|||
65 | +! |
- output$identifier <- renderText(+ checkmate::assert_count(depth) |
||
139 | -18x | +|||
66 | +! |
- paste0("Pid:", Sys.getpid(), " Token:", substr(session$token, 25, 32))+ UseMethod("ui_nested_tabs", modules) |
||
140 | +67 |
- )+ } |
||
141 | +68 | |||
142 | -18x | +|||
69 | +
- teal.widgets::verbatim_popup_srv(+ #' @rdname module_nested_tabs |
|||
143 | -18x | +|||
70 | +
- "sessionInfo",+ #' @export |
|||
144 | -18x | +|||
71 | +
- verbatim_content = utils::capture.output(utils::sessionInfo()),+ ui_nested_tabs.default <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) { |
|||
145 | -18x | +|||
72 | +! |
- title = "SessionInfo"+ stop("Modules class not supported: ", paste(class(modules), collapse = " ")) |
||
146 | +73 |
- )+ } |
||
147 | +74 | |||
148 | +75 |
- # `JavaScript` code+ #' @rdname module_nested_tabs |
||
149 | -18x | +|||
76 | +
- run_js_files(files = "init.js") # `JavaScript` code to make the clipboard accessible+ #' @export |
|||
150 | +77 |
- # set timezone in shiny app+ ui_nested_tabs.teal_modules <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) { |
||
151 | -+ | |||
78 | +! |
- # timezone is set in the early beginning so it will be available also+ checkmate::assert_list(datasets, types = c("list", "FilteredData")) |
||
152 | -+ | |||
79 | +! |
- # for `DDL` and all shiny modules+ ns <- NS(id) |
||
153 | -18x | +|||
80 | +! |
- get_client_timezone(session$ns)+ do.call( |
||
154 | -18x | +|||
81 | +! |
- observeEvent(+ tabsetPanel, |
||
155 | -18x | +|||
82 | +! |
- eventExpr = input$timezone,+ c( |
||
156 | -18x | +|||
83 | +
- once = TRUE,+ # by giving an id, we can reactively respond to tab changes |
|||
157 | -18x | +|||
84 | +! |
- handlerExpr = {+ list( |
||
158 | +85 | ! |
- session$userData$timezone <- input$timezone+ id = ns("active_tab"), |
|
159 | +86 | ! |
- logger::log_trace("srv_teal@1 Timezone set to client's timezone: { input$timezone }.")+ type = if (modules$label == "root") "pills" else "tabs" |
|
160 | +87 |
- }+ ),+ |
+ ||
88 | +! | +
+ lapply( |
||
161 | -+ | |||
89 | +! |
- )+ names(modules$children), |
||
162 | -+ | |||
90 | +! |
-
+ function(module_id) { |
||
163 | -18x | +|||
91 | +! |
- reporter <- teal.reporter::Reporter$new()+ module_label <- modules$children[[module_id]]$label |
||
164 | -18x | +|||
92 | +! |
- if (is_arg_used(modules, "reporter") && length(extract_module(modules, "teal_module_previewer")) == 0) {+ tabPanel( |
||
165 | +93 | ! |
- modules <- append_module(modules, reporter_previewer_module())+ title = module_label, |
|
166 | -+ | |||
94 | +! |
- }+ value = module_id, # when clicked this tab value changes input$<tabset panel id> |
||
167 | -+ | |||
95 | +! |
-
+ ui_nested_tabs( |
||
168 | -18x | +|||
96 | +! |
- env <- environment()+ id = ns(module_id), |
||
169 | -18x | +|||
97 | +! |
- datasets_reactive <- eventReactive(teal_data_rv(), {+ modules = modules$children[[module_id]], |
||
170 | -4x | +|||
98 | +! |
- env$progress <- shiny::Progress$new(session)+ datasets = datasets[[module_label]], |
||
171 | -4x | +|||
99 | +! |
- env$progress$set(0.25, message = "Setting data")+ depth = depth + 1L, |
||
172 | -+ | |||
100 | +! |
-
+ is_module_specific = is_module_specific |
||
173 | +101 |
- # create a list of data following structure of the nested modules list structure.+ ) |
||
174 | +102 |
- # Because it's easier to unpack modules and datasets when they follow the same nested structure.+ ) |
||
175 | -4x | +|||
103 | +
- datasets_singleton <- teal_data_to_filtered_data(teal_data_rv())+ } |
|||
176 | +104 |
-
+ ) |
||
177 | +105 |
- # Singleton starts with only global filters active.+ ) |
||
178 | -4x | +|||
106 | +
- filter_global <- Filter(function(x) x$id %in% attr(filter, "mapping")$global_filters, filter)+ ) |
|||
179 | -4x | +|||
107 | +
- datasets_singleton$set_filter_state(filter_global)+ } |
|||
180 | +108 | |||
181 | -4x | +|||
109 | +
- module_datasets <- function(modules) {+ #' @rdname module_nested_tabs |
|||
182 | -18x | +|||
110 | +
- if (inherits(modules, "teal_modules")) {+ #' @export |
|||
183 | -7x | +|||
111 | +
- datasets <- lapply(modules$children, module_datasets)+ ui_nested_tabs.teal_module <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) { |
|||
184 | -7x | +|||
112 | +! |
- labels <- vapply(modules$children, `[[`, character(1), "label")+ checkmate::assert_class(datasets, classes = "FilteredData") |
||
185 | -7x | +|||
113 | +! |
- names(datasets) <- labels+ ns <- NS(id) |
||
186 | -7x | +|||
114 | +
- datasets+ |
|||
187 | -11x | +|||
115 | +! |
- } else if (isTRUE(attr(filter, "module_specific"))) {+ args <- c(list(id = ns("module")), modules$ui_args) |
||
188 | +116 |
- # we should create FilteredData even if modules$datanames is null+ |
||
189 | -+ | |||
117 | +! |
- # null controls a display of filter panel but data should be still passed+ teal_ui <- tags$div( |
||
190 | -3x | +|||
118 | +! |
- datanames <- if (is.null(modules$datanames) || modules$datanames == "all") {+ id = id, |
||
191 | -3x | +|||
119 | +! |
- include_parent_datanames(+ class = "teal_module", |
||
192 | -3x | +|||
120 | +! |
- teal.data::datanames(teal_data_rv()),+ uiOutput(ns("data_reactive"), inline = TRUE), |
||
193 | -3x | +|||
121 | +! |
- teal_data_rv()@join_keys+ tagList( |
||
194 | -+ | |||
122 | +! |
- )+ if (depth >= 2L) div(style = "mt-6"), |
||
195 | -+ | |||
123 | +! |
- } else {+ do.call(modules$ui, args) |
||
196 | -! | +|||
124 | +
- modules$datanames+ ) |
|||
197 | +125 |
- }+ ) |
||
198 | +126 |
- # todo: subset teal_data to datanames+ |
||
199 | -3x | +|||
127 | +! |
- datasets_module <- teal_data_to_filtered_data(teal_data_rv(), datanames = datanames)+ if (!is.null(modules$datanames) && is_module_specific) { |
||
200 | -+ | |||
128 | +! |
-
+ fluidRow( |
||
201 | -+ | |||
129 | +! |
- # set initial filters+ column(width = 9, teal_ui, class = "teal_primary_col"), |
||
202 | -+ | |||
130 | +! |
- # - filtering filters for this module+ column( |
||
203 | -3x | +|||
131 | +! |
- slices <- Filter(x = filter, f = function(x) {+ width = 3, |
||
204 | +132 | ! |
- x$id %in% unique(unlist(attr(filter, "mapping")[c(modules$label, "global_filters")])) &&+ datasets$ui_filter_panel(ns("module_filter_panel")), |
|
205 | +133 | ! |
- x$dataname %in% datanames+ class = "teal_secondary_col" |
|
206 | +134 |
- })+ ) |
||
207 | -3x | +|||
135 | +
- include_varnames <- attr(slices, "include_varnames")[names(attr(slices, "include_varnames")) %in% datanames]+ ) |
|||
208 | -3x | +|||
136 | +
- exclude_varnames <- attr(slices, "exclude_varnames")[names(attr(slices, "exclude_varnames")) %in% datanames]+ } else { |
|||
209 | -3x | +|||
137 | +! |
- slices$include_varnames <- include_varnames+ teal_ui |
||
210 | -3x | +|||
138 | +
- slices$exclude_varnames <- exclude_varnames+ } |
|||
211 | -3x | +|||
139 | +
- datasets_module$set_filter_state(slices)+ } |
|||
212 | -3x | +|||
140 | +
- datasets_module+ |
|||
213 | +141 |
- } else {+ #' @rdname module_nested_tabs |
||
214 | -8x | +|||
142 | +
- datasets_singleton+ srv_nested_tabs <- function(id, datasets, modules, is_module_specific = FALSE, |
|||
215 | +143 |
- }+ reporter = teal.reporter::Reporter$new()) { |
||
216 | -+ | |||
144 | +50x |
- }+ checkmate::assert_multi_class(modules, c("teal_modules", "teal_module")) |
||
217 | -4x | +145 | +50x |
- module_datasets(modules)+ checkmate::assert_class(reporter, "Reporter")+ |
+
146 | +49x | +
+ UseMethod("srv_nested_tabs", modules) |
||
218 | +147 |
- })+ } |
||
219 | +148 | |||
220 | +149 |
- # Replace splash / welcome screen once data is loaded ----+ #' @rdname module_nested_tabs |
||
221 | +150 |
- # ignoreNULL to not trigger at the beginning when data is NULL+ #' @export |
||
222 | +151 |
- # just handle it once because data obtained through delayed loading should+ srv_nested_tabs.default <- function(id, datasets, modules, is_module_specific = FALSE, |
||
223 | +152 |
- # usually not change afterwards+ reporter = teal.reporter::Reporter$new()) {+ |
+ ||
153 | +! | +
+ stop("Modules class not supported: ", paste(class(modules), collapse = " ")) |
||
224 | +154 |
- # if restored from bookmarked state, `filter` is ignored+ } |
||
225 | +155 | |||
226 | -18x | +|||
156 | +
- observeEvent(datasets_reactive(), once = TRUE, {+ #' @rdname module_nested_tabs |
|||
227 | -! | +|||
157 | +
- logger::log_trace("srv_teal@5 setting main ui after data was pulled")+ #' @export |
|||
228 | -! | +|||
158 | +
- on.exit(env$progress$close())+ srv_nested_tabs.teal_modules <- function(id, datasets, modules, is_module_specific = FALSE, |
|||
229 | -! | +|||
159 | +
- env$progress$set(0.5, message = "Setting up main UI")+ reporter = teal.reporter::Reporter$new()) { |
|||
230 | -! | +|||
160 | +22x |
- datasets <- datasets_reactive()+ checkmate::assert_list(datasets, types = c("list", "FilteredData")) |
||
231 | +161 | |||
232 | -+ | |||
162 | +22x |
- # main_ui_container contains splash screen first and we remove it and replace it by the real UI+ moduleServer(id = id, module = function(input, output, session) { |
||
233 | -! | +|||
163 | +22x |
- removeUI(sprintf("#%s > div:nth-child(1)", session$ns("main_ui_container")))+ logger::log_trace("srv_nested_tabs.teal_modules initializing the module { deparse1(modules$label) }.") |
||
234 | -! | +|||
164 | +
- insertUI(+ |
|||
235 | -! | +|||
165 | +22x |
- selector = paste0("#", session$ns("main_ui_container")),+ labels <- vapply(modules$children, `[[`, character(1), "label") |
||
236 | -! | +|||
166 | +22x |
- where = "beforeEnd",+ modules_reactive <- sapply( |
||
237 | -+ | |||
167 | +22x |
- # we put it into a div, so it can easily be removed as a whole, also when it is a tagList (and not+ names(modules$children), |
||
238 | -+ | |||
168 | +22x |
- # just the first item of the tagList)+ function(module_id) { |
||
239 | -! | +|||
169 | +33x |
- ui = div(ui_tabs_with_filters(+ srv_nested_tabs( |
||
240 | -! | +|||
170 | +33x |
- session$ns("main_ui"),+ id = module_id, |
||
241 | -! | +|||
171 | +33x |
- modules = modules,+ datasets = datasets[[labels[module_id]]], |
||
242 | -! | +|||
172 | +33x |
- datasets = datasets,+ modules = modules$children[[module_id]], |
||
243 | -! | +|||
173 | +33x |
- filter = filter+ is_module_specific = is_module_specific, |
||
244 | -+ | |||
174 | +33x |
- )),+ reporter = reporter |
||
245 | +175 |
- # needed so that the UI inputs are available and can be immediately updated, otherwise, updating may not+ ) |
||
246 | +176 |
- # have any effect as they are ignored when not present+ }, |
||
247 | -! | +|||
177 | +22x |
- immediate = TRUE+ simplify = FALSE |
||
248 | +178 |
- )+ ) |
||
249 | +179 | |||
250 | +180 |
- # must make sure that this is only executed once as modules assume their observers are only+ # when not ready input$active_tab would return NULL - this would fail next reactive |
||
251 | -+ | |||
181 | +22x |
- # registered once (calling server functions twice would trigger observers twice each time)+ input_validated <- eventReactive(input$active_tab, input$active_tab, ignoreNULL = TRUE) |
||
252 | -! | +|||
182 | +22x |
- active_module <- srv_tabs_with_filters(+ get_active_module <- reactive({ |
||
253 | -! | +|||
183 | +12x |
- id = "main_ui",+ if (length(modules$children) == 1L) { |
||
254 | -! | +|||
184 | +
- datasets = datasets,+ # single tab is active by default |
|||
255 | -! | +|||
185 | +1x |
- modules = modules,+ modules_reactive[[1]]() |
||
256 | -! | +|||
186 | +
- reporter = reporter,+ } else { |
|||
257 | -! | +|||
187 | +
- filter = filter+ # switch to active tab+ |
+ |||
188 | +11x | +
+ modules_reactive[[input_validated()]]() |
||
258 | +189 |
- )+ } |
||
259 | -! | +|||
190 | +
- return(active_module)+ }) |
|||
260 | +191 |
- })+ + |
+ ||
192 | +22x | +
+ get_active_module |
||
261 | +193 |
}) |
||
262 | +194 |
} |
1 | +195 |
- #' Create a `tdata` Object+ |
||
2 | +196 |
- #'+ #' @rdname module_nested_tabs |
||
3 | +197 |
- #' @description `r lifecycle::badge("deprecated")`+ #' @export |
||
4 | +198 |
- #' Create a new object called `tdata` which contains `data`, a `reactive` list of data.frames+ srv_nested_tabs.teal_module <- function(id, datasets, modules, is_module_specific = TRUE, |
||
5 | +199 |
- #' (or `MultiAssayExperiment`), with attributes:+ reporter = teal.reporter::Reporter$new()) { |
||
6 | -+ | |||
200 | +27x |
- #' \itemize{+ checkmate::assert_class(datasets, "FilteredData")+ |
+ ||
201 | +27x | +
+ logger::log_trace("srv_nested_tabs.teal_module initializing the module: { deparse1(modules$label) }.") |
||
7 | +202 |
- #' \item{`code` (`reactive`) containing code used to generate the data}+ + |
+ ||
203 | +27x | +
+ moduleServer(id = id, module = function(input, output, session) {+ |
+ ||
204 | +27x | +
+ if (!is.null(modules$datanames) && is_module_specific) {+ |
+ ||
205 | +! | +
+ datasets$srv_filter_panel("module_filter_panel") |
||
8 | +206 |
- #' \item{join_keys (`join_keys`) containing the relationships between the data}+ } |
||
9 | +207 |
- #' \item{metadata (`named list`) containing any metadata associated with the data frames}+ |
||
10 | +208 |
- #' }+ # Create two triggers to limit reactivity between filter-panel and modules. |
||
11 | +209 |
- #' @name tdata+ # We want to recalculate only visible modules |
||
12 | +210 |
- #' @param data A `named list` of `data.frames` (or `MultiAssayExperiment`)+ # - trigger the data when the tab is selected |
||
13 | +211 |
- #' which optionally can be `reactive`.+ # - trigger module to be called when the tab is selected for the first time |
||
14 | -+ | |||
212 | +27x |
- #' Inside this object all of these items will be made `reactive`.+ trigger_data <- reactiveVal(1L) |
||
15 | -+ | |||
213 | +27x |
- #' @param code A `character` (or `reactive` which evaluates to a `character`) containing+ trigger_module <- reactiveVal(NULL) |
||
16 | -+ | |||
214 | +27x |
- #' the code used to generate the data. This should be `reactive` if the code is changing+ output$data_reactive <- renderUI({ |
||
17 | -+ | |||
215 | +17x |
- #' during a reactive context (e.g. if filtering changes the code). Inside this+ lapply(datasets$datanames(), function(x) { |
||
18 | -+ | |||
216 | +21x |
- #' object `code` will be made reactive+ datasets$get_data(x, filtered = TRUE) |
||
19 | +217 |
- #' @param join_keys A `teal.data::join_keys` object containing relationships between the+ }) |
||
20 | -+ | |||
218 | +17x |
- #' datasets.+ isolate(trigger_data(trigger_data() + 1)) |
||
21 | -+ | |||
219 | +17x |
- #' @param metadata A `named list` each element contains a list of metadata about the named data.frame+ isolate(trigger_module(TRUE)) |
||
22 | +220 |
- #' Each element of these list should be atomic and length one.+ |
||
23 | -+ | |||
221 | +17x |
- #' @return A `tdata` object+ NULL |
||
24 | +222 |
- #'+ }) |
||
25 | +223 |
- #' @seealso `as_tdata`+ |
||
26 | +224 |
- #'+ # collect arguments to run teal_module |
||
27 | -+ | |||
225 | +27x |
- #' @examples+ args <- c(list(id = "module"), modules$server_args) |
||
28 | -+ | |||
226 | +27x |
- #'+ if (is_arg_used(modules$server, "reporter")) { |
||
29 | -+ | |||
227 | +! |
- #' data <- new_tdata(+ args <- c(args, list(reporter = reporter)) |
||
30 | +228 |
- #' data = list(iris = iris, mtcars = reactive(mtcars), dd = data.frame(x = 1:10)),+ } |
||
31 | +229 |
- #' code = "iris <- iris+ |
||
32 | -+ | |||
230 | +27x |
- #' mtcars <- mtcars+ if (is_arg_used(modules$server, "datasets")) { |
||
33 | -+ | |||
231 | +1x |
- #' dd <- data.frame(x = 1:10)",+ args <- c(args, datasets = datasets) |
||
34 | +232 |
- #' metadata = list(dd = list(author = "NEST"), iris = list(version = 1))+ } |
||
35 | +233 |
- #' )+ |
||
36 | -+ | |||
234 | +27x |
- #'+ if (is_arg_used(modules$server, "data")) { |
||
37 | -+ | |||
235 | +7x |
- #' # Extract a data.frame+ data <- eventReactive(trigger_data(), .datasets_to_data(modules, datasets)) |
||
38 | -+ | |||
236 | +7x |
- #' isolate(data[["iris"]]())+ args <- c(args, data = list(data)) |
||
39 | +237 |
- #'+ } |
||
40 | +238 |
- #' # Get code+ |
||
41 | -+ | |||
239 | +27x |
- #' isolate(get_code_tdata(data))+ if (is_arg_used(modules$server, "filter_panel_api")) { |
||
42 | -+ | |||
240 | +2x |
- #'+ filter_panel_api <- teal.slice::FilterPanelAPI$new(datasets) |
||
43 | -+ | |||
241 | +2x |
- #' # Get metadata+ args <- c(args, filter_panel_api = filter_panel_api) |
||
44 | +242 |
- #' get_metadata(data, "iris")+ } |
||
45 | +243 |
- #'+ |
||
46 | +244 |
- #' @export+ # observe the trigger_module above to induce the module once the renderUI is triggered |
||
47 | -+ | |||
245 | +27x |
- new_tdata <- function(data, code = "", join_keys = NULL, metadata = NULL) {+ observeEvent( |
||
48 | -35x | +246 | +27x |
- lifecycle::deprecate_soft(+ ignoreNULL = TRUE, |
49 | -35x | +247 | +27x |
- when = "0.99.0",+ once = TRUE, |
50 | -35x | +248 | +27x |
- what = "tdata()",+ eventExpr = trigger_module(), |
51 | -35x | +249 | +27x |
- details = paste(+ handlerExpr = { |
52 | -35x | +250 | +17x |
- "tdata is deprecated and will be removed in the next release. Use `teal_data` instead.\n",+ module_output <- if (is_arg_used(modules$server, "id")) { |
53 | -35x | +251 | +17x |
- "Please follow migration instructions https://github.com/insightsengineering/teal/discussions/987."+ do.call(modules$server, args) |
54 | +252 |
- )+ } else {+ |
+ ||
253 | +! | +
+ do.call(callModule, c(args, list(module = modules$server))) |
||
55 | +254 |
- )+ } |
||
56 | -35x | +|||
255 | +
- checkmate::assert_list(+ } |
|||
57 | -35x | +|||
256 | +
- data,+ ) |
|||
58 | -35x | +|||
257 | +
- any.missing = FALSE, names = "unique",+ |
|||
59 | -35x | +258 | +27x |
- types = c("data.frame", "reactive", "MultiAssayExperiment")+ reactive(modules) |
60 | +259 |
- )- |
- ||
61 | -31x | -
- checkmate::assert_class(join_keys, "join_keys", null.ok = TRUE)+ }) |
||
62 | -30x | +|||
260 | +
- checkmate::assert_multi_class(code, c("character", "reactive"))+ } |
|||
63 | +261 | |||
64 | -29x | +|||
262 | +
- checkmate::assert_list(metadata, names = "unique", null.ok = TRUE)+ #' Convert `FilteredData` to reactive list of datasets of the `teal_data` type. |
|||
65 | -27x | +|||
263 | +
- checkmate::assert_subset(names(metadata), names(data))+ #' |
|||
66 | +264 |
-
+ #' Converts `FilteredData` object to `teal_data` object containing datasets needed for a specific module. |
||
67 | -26x | +|||
265 | +
- if (is.reactive(code)) {+ #' Please note that if module needs dataset which has a parent, then parent will be also returned. |
|||
68 | -9x | +|||
266 | +
- isolate(checkmate::assert_class(code(), "character", .var.name = "code"))+ #' A hash per `dataset` is calculated internally and returned in the code. |
|||
69 | +267 |
- }+ #' |
||
70 | +268 |
-
+ #' @param module (`teal_module`) module where needed filters are taken from |
||
71 | +269 |
- # create reactive data.frames+ #' @param datasets (`FilteredData`) object where needed data are taken from |
||
72 | -25x | +|||
270 | +
- for (x in names(data)) {+ #' @return A `teal_data` object. |
|||
73 | -48x | +|||
271 | +
- if (!is.reactive(data[[x]])) {+ #' |
|||
74 | -31x | +|||
272 | +
- data[[x]] <- do.call(reactive, list(as.name(x)), envir = list2env(data[x]))+ #' @keywords internal |
|||
75 | +273 |
- } else {+ .datasets_to_data <- function(module, datasets) { |
||
76 | -17x | +274 | +4x |
- isolate(+ checkmate::assert_class(module, "teal_module") |
77 | -17x | +275 | +4x |
- checkmate::assert_multi_class(+ checkmate::assert_class(datasets, "FilteredData") |
78 | -17x | +|||
276 | +
- data[[x]](), c("data.frame", "MultiAssayExperiment"),+ |
|||
79 | -17x | +277 | +4x |
- .var.name = "data"+ datanames <- if (is.null(module$datanames) || identical(module$datanames, "all")) { |
80 | -+ | |||
278 | +1x |
- )+ datasets$datanames() |
||
81 | +279 |
- )+ } else { |
||
82 | -+ | |||
280 | +3x |
- }+ unique(module$datanames) # todo: include parents! unique shouldn't be needed here! |
||
83 | +281 |
} |
||
84 | +282 | |||
85 | +283 |
- # set attributes+ # list of reactive filtered data |
||
86 | -24x | +284 | +4x |
- attr(data, "code") <- if (is.reactive(code)) code else reactive(code)+ data <- sapply(datanames, function(x) datasets$get_data(x, filtered = TRUE), simplify = FALSE) |
87 | -24x | +|||
285 | +
- attr(data, "join_keys") <- join_keys+ |
|||
88 | -24x | +286 | +4x |
- attr(data, "metadata") <- metadata+ hashes <- calculate_hashes(datanames, datasets) |
89 | +287 | |||
90 | -+ | |||
288 | +4x |
- # set class+ code <- c( |
||
91 | -24x | +289 | +4x |
- class(data) <- c("tdata", class(data))+ get_rcode_str_install(),+ |
+
290 | +4x | +
+ get_rcode_libraries(), |
||
92 | -24x | +291 | +4x |
- data+ get_datasets_code(datanames, datasets, hashes) |
93 | +292 |
- }+ ) |
||
94 | +293 | |||
95 | -- |
- #' Function to convert a `tdata` object to an `environment`- |
- ||
96 | -+ | |||
294 | +4x |
- #' Any `reactives` inside `tdata` are first evaluated+ do.call( |
||
97 | -+ | |||
295 | +4x |
- #' @param data a `tdata` object+ teal.data::teal_data, |
||
98 | -+ | |||
296 | +4x |
- #' @return an `environment`+ args = c(data, code = list(code), join_keys = list(datasets$get_join_keys()[datanames])) |
||
99 | +297 |
- #' @examples+ ) |
||
100 | +298 |
- #'+ } |
||
101 | +299 |
- #' data <- new_tdata(+ |
||
102 | +300 |
- #' data = list(iris = iris, mtcars = reactive(mtcars)),+ #' Get the hash of a dataset |
||
103 | +301 |
- #' code = "iris <- iris+ #' |
||
104 | +302 |
- #' mtcars = mtcars"+ #' @param datanames (`character`) names of datasets |
||
105 | +303 |
- #' )+ #' @param datasets (`FilteredData`) object holding the data |
||
106 | +304 |
#' |
||
107 | +305 |
- #' my_env <- isolate(tdata2env(data))+ #' @return A list of hashes per dataset |
||
108 | +306 |
- #'+ #' @keywords internal |
||
109 | +307 |
- #' @export+ #' |
||
110 | +308 |
- tdata2env <- function(data) { # nolint- |
- ||
111 | -2x | -
- checkmate::assert_class(data, "tdata")+ calculate_hashes <- function(datanames, datasets) { |
||
112 | -1x | +309 | +7x |
- list2env(lapply(data, function(x) if (is.reactive(x)) x() else x))+ sapply(datanames, function(x) rlang::hash(datasets$get_data(x, filtered = FALSE)), simplify = FALSE) |
113 | +310 |
} |
114 | +1 |
-
+ # This module is the main teal module that puts everything together. |
||
115 | +2 | |||
116 | -- |
- #' Wrapper for `get_code.tdata`- |
- ||
117 | +3 |
- #' This wrapper is to be used by downstream packages to extract the code of a `tdata` object+ #' teal main app module |
||
118 | +4 |
#' |
||
119 | +5 |
- #' @param data (`tdata`) object+ #' This is the main teal app that puts everything together. |
||
120 | +6 |
#' |
||
121 | -- |
- #' @return (`character`) code used in the `tdata` object.- |
- ||
122 | -- |
- #' @export- |
- ||
123 | +7 |
- get_code_tdata <- function(data) {- |
- ||
124 | -7x | -
- checkmate::assert_class(data, "tdata")- |
- ||
125 | -5x | -
- attr(data, "code")()+ #' It displays the splash UI which is used to fetch the data, possibly |
||
126 | +8 |
- }+ #' prompting for a password input to fetch the data. Once the data is ready, |
||
127 | +9 |
-
+ #' the splash screen is replaced by the actual teal UI that is tabsetted and |
||
128 | +10 |
- #' Extract `join_keys` from `tdata`+ #' has a filter panel with `datanames` that are relevant for the current tab. |
||
129 | +11 |
- #' @param data A `tdata` object+ #' Nested tabs are possible, but we limit it to two nesting levels for reasons |
||
130 | +12 |
- #' @param ... Additional arguments (not used)+ #' of clarity of the UI. |
||
131 | +13 |
- #' @export+ #' |
||
132 | +14 |
- join_keys.tdata <- function(data, ...) {- |
- ||
133 | -2x | -
- attr(data, "join_keys")+ #' The splash screen functionality can also be used |
||
134 | +15 |
- }+ #' for non-delayed data which takes time to load into memory, avoiding |
||
135 | +16 |
-
+ #' Shiny session timeouts. |
||
136 | +17 |
-
+ #' |
||
137 | +18 |
- #' Function to get metadata from a `tdata` object+ #' Server evaluates the `teal_data_rv` (delayed data mechanism) and creates the |
||
138 | +19 |
- #' @param data `tdata` - object to extract the data from+ #' `datasets` object that is shared across modules. |
||
139 | +20 |
- #' @param dataname `character(1)` the dataset name whose metadata is requested+ #' Once it is ready and non-`NULL`, the splash screen is replaced by the |
||
140 | +21 |
- #' @return Either list of metadata or NULL if no metadata+ #' main teal UI that depends on the data. |
||
141 | +22 |
- #' @export+ #' The currently active tab is tracked and the right filter panel |
||
142 | +23 |
- get_metadata <- function(data, dataname) {- |
- ||
143 | -4x | -
- checkmate::assert_string(dataname)- |
- ||
144 | -4x | -
- UseMethod("get_metadata", data)+ #' updates the displayed datasets to filter for according to the active `datanames` |
||
145 | +24 |
- }+ #' of the tab. |
||
146 | +25 |
-
+ #' |
||
147 | +26 |
- #' @rdname get_metadata+ #' It is written as a Shiny module so it can be added into other apps as well. |
||
148 | +27 |
- #' @export+ #' |
||
149 | +28 |
- get_metadata.tdata <- function(data, dataname) {+ #' @name module_teal |
||
150 | -4x | +|||
29 | +
- metadata <- attr(data, "metadata")+ #' |
|||
151 | -4x | +|||
30 | +
- if (is.null(metadata)) {+ #' @inheritParams ui_teal_with_splash |
|||
152 | -1x | +|||
31 | +
- return(NULL)+ #' |
|||
153 | +32 |
- }+ #' @param splash_ui (`shiny.tag`)\cr UI to display initially, |
||
154 | -3x | +|||
33 | +
- metadata[[dataname]]+ #' can be a splash screen or a Shiny module UI. For the latter, see |
|||
155 | +34 |
- }+ #' [init()] about how to call the corresponding server function. |
||
156 | +35 |
-
+ #' |
||
157 | +36 |
- #' @rdname get_metadata+ #' @param teal_data_rv (`reactive`)\cr |
||
158 | +37 |
- #' @export+ #' returns the `teal_data`, only evaluated once, `NULL` value is ignored |
||
159 | +38 |
- get_metadata.default <- function(data, dataname) {+ #' |
||
160 | -! | +|||
39 | +
- stop("get_metadata function not implemented for this object")+ #' @return |
|||
161 | +40 |
- }+ #' `ui_teal` returns `HTML` for Shiny module UI. |
||
162 | +41 |
-
+ #' `srv_teal` returns `reactive` which returns the currently active module. |
||
163 | +42 |
-
+ #' |
||
164 | +43 |
- #' Downgrade `teal_data` objects in modules for compatibility.+ #' @keywords internal |
||
165 | +44 |
#' |
||
166 | +45 |
- #' Convert `teal_data` to `tdata` in `teal` modules.+ #' @examples |
||
167 | +46 |
- #'+ #' mods <- teal:::example_modules() |
||
168 | +47 |
- #' Recent changes in `teal` cause modules to fail because modules expect a `tdata` object+ #' teal_data_rv <- reactive(teal:::example_cdisc_data()) |
||
169 | +48 |
- #' to be passed to the `data` argument but instead they receive a `teal_data` object,+ #' app <- shinyApp( |
||
170 | +49 |
- #' which is additionally wrapped in a reactive expression in the server functions.+ #' ui = function() { |
||
171 | +50 |
- #' In order to easily adapt such modules without a proper refactor,+ #' teal:::ui_teal("dummy") |
||
172 | +51 |
- #' use this function to downgrade the `data` argument.+ #' }, |
||
173 | +52 |
- #'+ #' server = function(input, output, session) { |
||
174 | +53 |
- #' @param x data object, either `tdata` or `teal_data`, the latter possibly in a reactive expression+ #' active_module <- teal:::srv_teal(id = "dummy", modules = mods, teal_data_rv = teal_data_rv) |
||
175 | +54 |
- #'+ #' } |
||
176 | +55 |
- #' @return Object of class `tdata`.+ #' ) |
||
177 | +56 |
- #'+ #' if (interactive()) { |
||
178 | +57 |
- #' @examples+ #' shinyApp(app$ui, app$server) |
||
179 | +58 |
- #' td <- teal_data()+ #' } |
||
180 | +59 |
- #' td <- within(td, iris <- iris) %>% within(mtcars <- mtcars)+ NULL |
||
181 | +60 |
- #' td+ |
||
182 | +61 |
- #' as_tdata(td)+ #' @rdname module_teal |
||
183 | +62 |
- #' as_tdata(reactive(td))+ ui_teal <- function(id, |
||
184 | +63 |
- #'+ splash_ui = tags$h2("Starting the Teal App"), |
||
185 | +64 |
- #' @export+ title = NULL, |
||
186 | +65 |
- #' @rdname tdata_deprecation+ header = tags$p(""), |
||
187 | +66 |
- #'+ footer = tags$p("")) {+ |
+ ||
67 | +12x | +
+ if (checkmate::test_string(header)) {+ |
+ ||
68 | +! | +
+ header <- tags$h1(header) |
||
188 | +69 |
- as_tdata <- function(x) {+ } |
||
189 | -8x | +70 | +12x |
- if (inherits(x, "tdata")) {+ if (checkmate::test_string(footer)) { |
190 | -2x | +|||
71 | +! |
- return(x)+ footer <- tags$p(footer) |
||
191 | +72 |
} |
||
192 | -6x | +73 | +12x |
- if (is.reactive(x)) {+ checkmate::assert( |
193 | -1x | +74 | +12x |
- checkmate::assert_class(isolate(x()), "teal_data")+ checkmate::check_class(splash_ui, "shiny.tag"), |
194 | -1x | +75 | +12x | +
+ checkmate::check_class(splash_ui, "shiny.tag.list"),+ |
+
76 | +12x | +
+ checkmate::check_class(splash_ui, "html")+ |
+ ||
77 | ++ |
+ )+ |
+ ||
78 | +12x |
- datanames <- isolate(teal.data::datanames(x()))+ checkmate::assert( |
||
195 | -1x | +79 | +12x |
- datasets <- sapply(datanames, function(dataname) reactive(x()[[dataname]]), simplify = FALSE)+ checkmate::check_class(header, "shiny.tag"), |
196 | -1x | +80 | +12x |
- code <- reactive(teal.code::get_code(x()))+ checkmate::check_class(header, "shiny.tag.list"), |
197 | -1x | +81 | +12x |
- join_keys <- isolate(teal.data::join_keys(x()))+ checkmate::check_class(header, "html") |
198 | -5x | +|||
82 | +
- } else if (inherits(x, "teal_data")) {+ ) |
|||
199 | -5x | +83 | +12x |
- datanames <- teal.data::datanames(x)+ checkmate::assert( |
200 | -5x | +84 | +12x |
- datasets <- sapply(datanames, function(dataname) reactive(x[[dataname]]), simplify = FALSE)+ checkmate::check_class(footer, "shiny.tag"), |
201 | -5x | +85 | +12x |
- code <- reactive(teal.code::get_code(x))+ checkmate::check_class(footer, "shiny.tag.list"), |
202 | -5x | +86 | +12x |
- join_keys <- isolate(teal.data::join_keys(x))+ checkmate::check_class(footer, "html") |
203 | +87 |
- }+ ) |
||
204 | +88 | |||
205 | -6x | +89 | +12x |
- new_tdata(data = datasets, code = code, join_keys = join_keys)+ ns <- NS(id) |
206 | +90 |
- }+ # Once the data is loaded, we will remove this element and add the real teal UI instead |
1 | -+ | |||
91 | +12x |
- # This is the main function from teal to be used by the end-users. Although it delegates+ splash_ui <- div( |
||
2 | +92 |
- # directly to `module_teal_with_splash.R`, we keep it in a separate file because its doc is quite large+ # id so we can remove the splash screen once ready, which is the first child of this container |
||
3 | -+ | |||
93 | +12x |
- # and it is very end-user oriented. It may also perform more argument checking with more informative+ id = ns("main_ui_container"), |
||
4 | +94 |
- # error messages.+ # we put it into a div, so it can easily be removed as a whole, also when it is a tagList (and not |
||
5 | +95 |
-
+ # just the first item of the tagList) |
||
6 | -+ | |||
96 | +12x |
-
+ div(splash_ui) |
||
7 | +97 |
- #' Create the Server and UI Function For the Shiny App+ ) |
||
8 | +98 |
- #'+ |
||
9 | +99 |
- #' @description `r lifecycle::badge("stable")`+ # show busy icon when shiny session is busy computing stuff |
||
10 | +100 |
- #' End-users: This is the most important function for you to start a+ # based on https://stackoverflow.com/questions/17325521/r-shiny-display-loading-message-while-function-is-running/22475216#22475216 #nolint |
||
11 | -+ | |||
101 | +12x |
- #' teal app that is composed out of teal modules.+ shiny_busy_message_panel <- conditionalPanel( |
||
12 | -+ | |||
102 | +12x |
- #'+ condition = "(($('html').hasClass('shiny-busy')) && (document.getElementById('shiny-notification-panel') == null))", # nolint |
||
13 | -+ | |||
103 | +12x |
- #' @param data (`teal_data`, `teal_data_module`, `named list`)\cr+ div( |
||
14 | -+ | |||
104 | +12x |
- #' `teal_data` object as returned by [teal.data::teal_data()] or+ icon("arrows-rotate", "spin fa-spin"), |
||
15 | -+ | |||
105 | +12x |
- #' `teal_data_modules` or simply a list of a named list of objects+ "Computing ...", |
||
16 | +106 |
- #' (`data.frame` or `MultiAssayExperiment`).+ # CSS defined in `custom.css` |
||
17 | -+ | |||
107 | +12x |
- #' @param modules (`list`, `teal_modules` or `teal_module`)\cr+ class = "shinybusymessage" |
||
18 | +108 |
- #' nested list of `teal_modules` or `teal_module` objects or a single+ ) |
||
19 | +109 |
- #' `teal_modules` or `teal_module` object. These are the specific output modules which+ ) |
||
20 | +110 |
- #' will be displayed in the teal application. See [modules()] and [module()] for+ |
||
21 | -+ | |||
111 | +12x |
- #' more details.+ res <- fluidPage( |
||
22 | -+ | |||
112 | +12x |
- #' @param title (`NULL` or `character`)\cr+ title = title, |
||
23 | -+ | |||
113 | +12x |
- #' The browser window title (defaults to the host URL of the page).+ theme = get_teal_bs_theme(), |
||
24 | -+ | |||
114 | +12x |
- #' @param filter (`teal_slices`)\cr+ include_teal_css_js(), |
||
25 | -+ | |||
115 | +12x |
- #' Specification of initial filter. Filters can be specified using [teal::teal_slices()].+ tags$header(header), |
||
26 | -+ | |||
116 | +12x |
- #' Old way of specifying filters through a list is deprecated and will be removed in the+ tags$hr(class = "my-2"), |
||
27 | -+ | |||
117 | +12x | +
+ shiny_busy_message_panel,+ |
+ ||
118 | +12x | +
+ splash_ui,+ |
+ ||
119 | +12x |
- #' next release. Please fix your applications to use [teal::teal_slices()].+ tags$hr(), |
||
28 | -+ | |||
120 | +12x |
- #' @param header (`shiny.tag` or `character`) \cr+ tags$footer( |
||
29 | -+ | |||
121 | +12x |
- #' the header of the app. Note shiny code placed here (and in the footer+ div( |
||
30 | -+ | |||
122 | +12x |
- #' argument) will be placed in the app's `ui` function so code which needs to be placed in the `ui` function+ footer, |
||
31 | -+ | |||
123 | +12x |
- #' (such as loading `CSS` via [htmltools::htmlDependency()]) should be included here.+ teal.widgets::verbatim_popup_ui(ns("sessionInfo"), "Session Info", type = "link"), |
||
32 | -+ | |||
124 | +12x |
- #' @param footer (`shiny.tag` or `character`)\cr+ textOutput(ns("identifier")) |
||
33 | +125 |
- #' the footer of the app+ ) |
||
34 | +126 |
- #' @param id (`character`)\cr+ ) |
||
35 | +127 |
- #' module id to embed it, if provided,+ ) |
||
36 | -+ | |||
128 | +12x |
- #' the server function must be called with [shiny::moduleServer()];+ return(res) |
||
37 | +129 |
- #' See the vignette for an example. However, [ui_teal_with_splash()]+ } |
||
38 | +130 |
- #' is then preferred to this function.+ |
||
39 | +131 |
- #'+ |
||
40 | +132 |
- #' @return named list with `server` and `ui` function+ #' @rdname module_teal |
||
41 | +133 |
- #'+ srv_teal <- function(id, modules, teal_data_rv, filter = teal_slices()) { |
||
42 | -+ | |||
134 | +19x |
- #' @export+ stopifnot(is.reactive(teal_data_rv)) |
||
43 | -+ | |||
135 | +18x |
- #'+ moduleServer(id, function(input, output, session) { |
||
44 | -+ | |||
136 | +18x |
- #' @include modules.R+ logger::log_trace("srv_teal initializing the module.") |
||
45 | +137 |
- #'+ |
||
46 | -+ | |||
138 | +18x |
- #' @examples+ output$identifier <- renderText( |
||
47 | -+ | |||
139 | +18x |
- #' app <- init(+ paste0("Pid:", Sys.getpid(), " Token:", substr(session$token, 25, 32)) |
||
48 | +140 |
- #' data = teal_data(+ ) |
||
49 | +141 |
- #' new_iris = transform(iris, id = seq_len(nrow(iris))),+ |
||
50 | -+ | |||
142 | +18x |
- #' new_mtcars = transform(mtcars, id = seq_len(nrow(mtcars))),+ teal.widgets::verbatim_popup_srv( |
||
51 | -+ | |||
143 | +18x |
- #' code = "+ "sessionInfo", |
||
52 | -+ | |||
144 | +18x |
- #' new_iris <- transform(iris, id = seq_len(nrow(iris)))+ verbatim_content = utils::capture.output(utils::sessionInfo()), |
||
53 | -+ | |||
145 | +18x |
- #' new_mtcars <- transform(mtcars, id = seq_len(nrow(mtcars)))+ title = "SessionInfo" |
||
54 | +146 |
- #' "+ ) |
||
55 | +147 |
- #' ),+ |
||
56 | +148 |
- #' modules = modules(+ # `JavaScript` code |
||
57 | -+ | |||
149 | +18x |
- #' module(+ run_js_files(files = "init.js") # `JavaScript` code to make the clipboard accessible |
||
58 | +150 |
- #' label = "data source",+ # set timezone in shiny app |
||
59 | +151 |
- #' server = function(input, output, session, data) {},+ # timezone is set in the early beginning so it will be available also |
||
60 | +152 |
- #' ui = function(id, ...) div(p("information about data source")),+ # for `DDL` and all shiny modules |
||
61 | -+ | |||
153 | +18x |
- #' datanames = "all"+ get_client_timezone(session$ns) |
||
62 | -+ | |||
154 | +18x |
- #' ),+ observeEvent( |
||
63 | -+ | |||
155 | +18x |
- #' example_module(label = "example teal module"),+ eventExpr = input$timezone, |
||
64 | -+ | |||
156 | +18x |
- #' module(+ once = TRUE, |
||
65 | -+ | |||
157 | +18x |
- #' "Iris Sepal.Length histogram",+ handlerExpr = { |
||
66 | -+ | |||
158 | +! |
- #' server = function(input, output, session, data) {+ session$userData$timezone <- input$timezone |
||
67 | -+ | |||
159 | +! |
- #' output$hist <- renderPlot(+ logger::log_trace("srv_teal@1 Timezone set to client's timezone: { input$timezone }.") |
||
68 | +160 |
- #' hist(data()[["new_iris"]]$Sepal.Length)+ } |
||
69 | +161 |
- #' )+ ) |
||
70 | +162 |
- #' },+ |
||
71 | -+ | |||
163 | +18x |
- #' ui = function(id, ...) {+ reporter <- teal.reporter::Reporter$new() |
||
72 | -+ | |||
164 | +18x |
- #' ns <- NS(id)+ if (is_arg_used(modules, "reporter") && length(extract_module(modules, "teal_module_previewer")) == 0) { |
||
73 | -+ | |||
165 | +! |
- #' plotOutput(ns("hist"))+ modules <- append_module(modules, reporter_previewer_module()) |
||
74 | +166 |
- #' },+ } |
||
75 | +167 |
- #' datanames = "new_iris"+ |
||
76 | -+ | |||
168 | +18x |
- #' )+ env <- environment() |
||
77 | -+ | |||
169 | +18x |
- #' ),+ datasets_reactive <- eventReactive(teal_data_rv(), { |
||
78 | -+ | |||
170 | +4x |
- #' title = "App title",+ env$progress <- shiny::Progress$new(session) |
||
79 | -+ | |||
171 | +4x |
- #' filter = teal_slices(+ env$progress$set(0.25, message = "Setting data") |
||
80 | +172 |
- #' teal_slice(dataname = "new_iris", varname = "Species"),+ |
||
81 | +173 |
- #' teal_slice(dataname = "new_iris", varname = "Sepal.Length"),+ # create a list of data following structure of the nested modules list structure. |
||
82 | +174 |
- #' teal_slice(dataname = "new_mtcars", varname = "cyl"),+ # Because it's easier to unpack modules and datasets when they follow the same nested structure. |
||
83 | -+ | |||
175 | +4x |
- #' exclude_varnames = list(new_iris = c("Sepal.Width", "Petal.Width")),+ datasets_singleton <- teal_data_to_filtered_data(teal_data_rv()) |
||
84 | +176 |
- #' mapping = list(+ |
||
85 | +177 |
- #' `example teal module` = "new_iris Species",+ # Singleton starts with only global filters active. |
||
86 | -+ | |||
178 | +4x |
- #' `Iris Sepal.Length histogram` = "new_iris Species",+ filter_global <- Filter(function(x) x$id %in% attr(filter, "mapping")$global_filters, filter) |
||
87 | -+ | |||
179 | +4x |
- #' global_filters = "new_mtcars cyl"+ datasets_singleton$set_filter_state(filter_global) |
||
88 | +180 |
- #' )+ |
||
89 | -+ | |||
181 | +4x |
- #' ),+ module_datasets <- function(modules) { |
||
90 | -+ | |||
182 | +18x |
- #' header = tags$h1("Sample App"),+ if (inherits(modules, "teal_modules")) { |
||
91 | -+ | |||
183 | +7x |
- #' footer = tags$p("Copyright 2017 - 2023")+ datasets <- lapply(modules$children, module_datasets) |
||
92 | -+ | |||
184 | +7x |
- #' )+ labels <- vapply(modules$children, `[[`, character(1), "label") |
||
93 | -+ | |||
185 | +7x |
- #' if (interactive()) {+ names(datasets) <- labels |
||
94 | -+ | |||
186 | +7x |
- #' shinyApp(app$ui, app$server)+ datasets |
||
95 | -+ | |||
187 | +11x |
- #' }+ } else if (isTRUE(attr(filter, "module_specific"))) { |
||
96 | +188 |
- #'+ # we should create FilteredData even if modules$datanames is null |
||
97 | +189 |
- init <- function(data,+ # null controls a display of filter panel but data should be still passed |
||
98 | -+ | |||
190 | +3x |
- modules,+ datanames <- if (is.null(modules$datanames) || modules$datanames == "all") { |
||
99 | -+ | |||
191 | +3x |
- title = NULL,+ include_parent_datanames( |
||
100 | -+ | |||
192 | +3x |
- filter = teal_slices(),+ teal.data::datanames(teal_data_rv()), |
||
101 | -+ | |||
193 | +3x |
- header = tags$p(),+ teal_data_rv()@join_keys |
||
102 | +194 |
- footer = tags$p(),+ ) |
||
103 | +195 |
- id = character(0)) {- |
- ||
104 | -15x | -
- logger::log_trace("init initializing teal app with: data ({ class(data)[1] }).")- |
- ||
105 | -15x | -
- if (is.list(data) && !inherits(data, "teal_data_module")) {+ } else { |
||
106 | -10x | +|||
196 | +! |
- checkmate::assert_list(data, names = "named")+ modules$datanames |
- ||
107 | -10x | +|||
197 | +
- data <- do.call(teal.data::teal_data, data)+ } |
|||
108 | +198 |
- }+ # todo: subset teal_data to datanames |
||
109 | -15x | +199 | +3x |
- if (inherits(data, "TealData")) {+ datasets_module <- teal_data_to_filtered_data(teal_data_rv(), datanames = datanames) |
110 | -! | +|||
200 | +
- lifecycle::deprecate_stop(+ |
|||
111 | -! | +|||
201 | +
- when = "0.99.0",+ # set initial filters |
|||
112 | -! | +|||
202 | +
- what = "init(data)",+ # - filtering filters for this module |
|||
113 | -! | +|||
203 | +3x |
- paste(+ slices <- Filter(x = filter, f = function(x) { |
||
114 | +204 | ! |
- "TealData is no longer supported. Use teal_data() instead.",+ x$id %in% unique(unlist(attr(filter, "mapping")[c(modules$label, "global_filters")])) && |
|
115 | +205 | ! |
- "Please follow migration instructions https://github.com/insightsengineering/teal/discussions/988."+ x$dataname %in% datanames |
|
116 | +206 |
- )+ }) |
||
117 | -+ | |||
207 | +3x |
- )+ include_varnames <- attr(slices, "include_varnames")[names(attr(slices, "include_varnames")) %in% datanames] |
||
118 | -+ | |||
208 | +3x |
- }+ exclude_varnames <- attr(slices, "exclude_varnames")[names(attr(slices, "exclude_varnames")) %in% datanames] |
||
119 | -+ | |||
209 | +3x |
-
+ slices$include_varnames <- include_varnames |
||
120 | -15x | +210 | +3x |
- checkmate::assert_multi_class(data, c("teal_data", "teal_data_module"))+ slices$exclude_varnames <- exclude_varnames |
121 | -15x | +211 | +3x |
- checkmate::assert_multi_class(modules, c("teal_module", "list", "teal_modules"))+ datasets_module$set_filter_state(slices) |
122 | -15x | +212 | +3x |
- checkmate::assert_string(title, null.ok = TRUE)+ datasets_module |
123 | -15x | +|||
213 | +
- checkmate::assert(+ } else { |
|||
124 | -15x | +214 | +8x |
- checkmate::check_class(filter, "teal_slices"),+ datasets_singleton |
125 | -15x | +|||
215 | +
- checkmate::check_list(filter, names = "named")+ } |
|||
126 | +216 |
- )+ } |
||
127 | -14x | +217 | +4x |
- checkmate::assert_multi_class(header, c("shiny.tag", "character"))+ module_datasets(modules) |
128 | -14x | +|||
218 | +
- checkmate::assert_multi_class(footer, c("shiny.tag", "character"))+ }) |
|||
129 | -14x | +|||
219 | +
- checkmate::assert_character(id, max.len = 1, any.missing = FALSE)+ |
|||
130 | +220 |
-
+ # Replace splash / welcome screen once data is loaded ---- |
||
131 | -14x | +|||
221 | +
- teal.logger::log_system_info()+ # ignoreNULL to not trigger at the beginning when data is NULL |
|||
132 | +222 |
-
+ # just handle it once because data obtained through delayed loading should |
||
133 | -14x | +|||
223 | +
- if (inherits(modules, "teal_module")) {+ # usually not change afterwards |
|||
134 | -1x | +|||
224 | +
- modules <- list(modules)+ # if restored from bookmarked state, `filter` is ignored |
|||
135 | +225 |
- }+ |
||
136 | -14x | +226 | +18x |
- if (inherits(modules, "list")) {+ observeEvent(datasets_reactive(), once = TRUE, { |
137 | -4x | +|||
227 | +! |
- modules <- do.call(teal::modules, modules)+ logger::log_trace("srv_teal@5 setting main ui after data was pulled")+ |
+ ||
228 | +! | +
+ on.exit(env$progress$close())+ |
+ ||
229 | +! | +
+ env$progress$set(0.5, message = "Setting up main UI")+ |
+ ||
230 | +! | +
+ datasets <- datasets_reactive() |
||
138 | +231 |
- }+ |
||
139 | +232 |
-
+ # main_ui_container contains splash screen first and we remove it and replace it by the real UI |
||
140 | -14x | +|||
233 | +! |
- landing <- extract_module(modules, "teal_module_landing")+ removeUI(sprintf("#%s > div:nth-child(1)", session$ns("main_ui_container"))) |
||
141 | +234 | ! |
- if (length(landing) > 1L) stop("Only one `landing_popup_module` can be used.")+ insertUI( |
|
142 | -14x | +|||
235 | +! |
- modules <- drop_module(modules, "teal_module_landing")+ selector = paste0("#", session$ns("main_ui_container")), |
||
143 | -+ | |||
236 | +! |
-
+ where = "beforeEnd", |
||
144 | +237 |
- # Calculate app id that will be used to stamp filter state snapshots.+ # we put it into a div, so it can easily be removed as a whole, also when it is a tagList (and not |
||
145 | +238 |
- # App id is a hash of the app's data and modules.+ # just the first item of the tagList) |
||
146 | -+ | |||
239 | +! |
- # See "transferring snapshots" section in ?snapshot.+ ui = div(ui_tabs_with_filters( |
||
147 | -14x | +|||
240 | +! |
- hashables <- mget(c("data", "modules"))+ session$ns("main_ui"), |
||
148 | -14x | +|||
241 | +! |
- hashables$data <- if (inherits(hashables$data, "teal_data")) {+ modules = modules, |
||
149 | -13x | +|||
242 | +! |
- as.list(hashables$data@env)+ datasets = datasets, |
||
150 | -14x | +|||
243 | +! |
- } else if (inherits(data, "teal_data_module")) {+ filter = filter |
||
151 | -1x | +|||
244 | +
- body(data$server)+ )), |
|||
152 | +245 |
- }+ # needed so that the UI inputs are available and can be immediately updated, otherwise, updating may not |
||
153 | +246 |
-
+ # have any effect as they are ignored when not present |
||
154 | -14x | +|||
247 | +! |
- attr(filter, "app_id") <- rlang::hash(hashables)+ immediate = TRUE |
||
155 | +248 |
-
+ ) |
||
156 | +249 |
- # convert teal.slice::teal_slices to teal::teal_slices- |
- ||
157 | -14x | -
- filter <- as.teal_slices(as.list(filter))+ |
||
158 | +250 |
-
+ # must make sure that this is only executed once as modules assume their observers are only |
||
159 | -14x | +|||
251 | +
- if (isTRUE(attr(filter, "module_specific"))) {+ # registered once (calling server functions twice would trigger observers twice each time) |
|||
160 | +252 | ! |
- module_names <- unlist(c(module_labels(modules), "global_filters"))+ active_module <- srv_tabs_with_filters( |
|
161 | +253 | ! |
- failed_mod_names <- setdiff(names(attr(filter, "mapping")), module_names)+ id = "main_ui", |
|
162 | +254 | ! |
- if (length(failed_mod_names)) {+ datasets = datasets, |
|
163 | +255 | ! |
- stop(+ modules = modules, |
|
164 | +256 | ! |
- sprintf(+ reporter = reporter, |
|
165 | +257 | ! |
- "Some module names in the mapping arguments don't match module labels.\n %s not in %s",+ filter = filter |
|
166 | -! | +|||
258 | +
- toString(failed_mod_names),+ ) |
|||
167 | +259 | ! |
- toString(unique(module_names))+ return(active_module) |
|
168 | +260 |
- )+ }) |
||
169 | +261 |
- )+ }) |
||
170 | +262 |
- }+ } |
171 | +1 | - - | -|
172 | -! | -
- if (anyDuplicated(module_names)) {+ # This is the main function from teal to be used by the end-users. Although it delegates |
|
173 | +2 |
- # In teal we are able to set nested modules with duplicated label.+ # directly to `module_teal_with_splash.R`, we keep it in a separate file because its doc is quite large |
|
174 | +3 |
- # Because mapping argument bases on the relationship between module-label and filter-id,+ # and it is very end-user oriented. It may also perform more argument checking with more informative |
|
175 | +4 |
- # it is possible that module-label in mapping might refer to multiple teal_module (identified by the same label)- |
- |
176 | -! | -
- stop(- |
- |
177 | -! | -
- sprintf(- |
- |
178 | -! | -
- "Module labels should be unique when teal_slices(mapping = TRUE). Duplicated labels:\n%s ",- |
- |
179 | -! | -
- toString(module_names[duplicated(module_names)])+ # error messages. |
|
180 | +5 |
- )+ |
|
181 | +6 |
- )+ |
|
182 | +7 |
- }+ #' Create the Server and UI Function For the Shiny App |
|
183 | +8 |
- }+ #' |
|
184 | +9 | - - | -|
185 | -14x | -
- if (inherits(data, "teal_data")) {- |
- |
186 | -13x | -
- if (length(teal.data::datanames(data)) == 0) {- |
- |
187 | -1x | -
- stop("`data` object has no datanames. Specify `datanames(data)` and try again.")+ #' @description `r lifecycle::badge("stable")` |
|
188 | +10 |
- }+ #' End-users: This is the most important function for you to start a |
|
189 | +11 |
-
+ #' teal app that is composed out of teal modules. |
|
190 | +12 |
- # in case of teal_data_module this check is postponed to the srv_teal_with_splash+ #' |
|
191 | -12x | +||
13 | +
- is_modules_ok <- check_modules_datanames(modules, teal.data::datanames(data))+ #' @param data (`teal_data`, `teal_data_module`, `named list`)\cr |
||
192 | -12x | +||
14 | +
- if (!isTRUE(is_modules_ok)) {+ #' `teal_data` object as returned by [teal.data::teal_data()] or |
||
193 | -1x | +||
15 | +
- logger::log_error(is_modules_ok)+ #' `teal_data_modules` or simply a list of a named list of objects |
||
194 | -1x | +||
16 | +
- checkmate::assert(is_modules_ok, .var.name = "modules")+ #' (`data.frame` or `MultiAssayExperiment`). |
||
195 | +17 |
- }+ #' @param modules (`list`, `teal_modules` or `teal_module`)\cr |
|
196 | +18 |
-
+ #' nested list of `teal_modules` or `teal_module` objects or a single |
|
197 | +19 |
-
+ #' `teal_modules` or `teal_module` object. These are the specific output modules which |
|
198 | -11x | +||
20 | +
- is_filter_ok <- check_filter_datanames(filter, teal.data::datanames(data))+ #' will be displayed in the teal application. See [modules()] and [module()] for |
||
199 | -11x | +||
21 | +
- if (!isTRUE(is_filter_ok)) {+ #' more details. |
||
200 | -1x | +||
22 | +
- logger::log_warn(is_filter_ok)+ #' @param title (`NULL` or `character`)\cr |
||
201 | +23 |
- # we allow app to continue if applied filters are outside+ #' The browser window title (defaults to the host URL of the page). |
|
202 | +24 |
- # of possible data range+ #' @param filter (`teal_slices`)\cr |
|
203 | +25 |
- }+ #' Specification of initial filter. Filters can be specified using [teal::teal_slices()]. |
|
204 | +26 |
- }+ #' Old way of specifying filters through a list is deprecated and will be removed in the |
|
205 | +27 |
-
+ #' next release. Please fix your applications to use [teal::teal_slices()]. |
|
206 | +28 |
- # Note regarding case `id = character(0)`:+ #' @param header (`shiny.tag` or `character`) \cr |
|
207 | +29 |
- # rather than using `callModule` and creating a submodule of this module, we directly modify+ #' the header of the app. Note shiny code placed here (and in the footer |
|
208 | +30 |
- # the `ui` and `server` with `id = character(0)` and calling the server function directly+ #' argument) will be placed in the app's `ui` function so code which needs to be placed in the `ui` function |
|
209 | +31 |
- # rather than through `callModule`+ #' (such as loading `CSS` via [htmltools::htmlDependency()]) should be included here. |
|
210 | -12x | +||
32 | +
- res <- list(+ #' @param footer (`shiny.tag` or `character`)\cr |
||
211 | -12x | +||
33 | +
- ui = ui_teal_with_splash(id = id, data = data, title = title, header = header, footer = footer),+ #' the footer of the app |
||
212 | -12x | +||
34 | +
- server = function(input, output, session) {+ #' @param id (`character`)\cr |
||
213 | -! | +||
35 | +
- if (length(landing) == 1L) {+ #' module id to embed it, if provided, |
||
214 | -! | +||
36 | +
- landing_module <- landing[[1L]]+ #' the server function must be called with [shiny::moduleServer()]; |
||
215 | -! | +||
37 | +
- do.call(landing_module$server, c(list(id = "landing_module_shiny_id"), landing_module$server_args))+ #' See the vignette for an example. However, [ui_teal_with_splash()] |
||
216 | +38 |
- }+ #' is then preferred to this function. |
|
217 | -! | +||
39 | +
- filter <- deep_copy_filter(filter)+ #' |
||
218 | -! | +||
40 | +
- srv_teal_with_splash(id = id, data = data, modules = modules, filter = filter)+ #' @return named list with `server` and `ui` function |
||
219 | +41 |
- }+ #' |
|
220 | +42 |
- )+ #' @export |
|
221 | -12x | +||
43 | +
- logger::log_trace("init teal app has been initialized.")+ #' |
||
222 | -12x | +||
44 | +
- return(res)+ #' @include modules.R |
||
223 | +45 |
- }+ #' |
1 | +46 |
- #' Filter settings for teal applications+ #' @examples |
||
2 | +47 |
- #'+ #' app <- init( |
||
3 | +48 |
- #' Specify initial filter states and filtering settings for a `teal` app.+ #' data = teal_data( |
||
4 | +49 |
- #'+ #' new_iris = transform(iris, id = seq_len(nrow(iris))), |
||
5 | +50 |
- #' Produces a `teal_slices` object.+ #' new_mtcars = transform(mtcars, id = seq_len(nrow(mtcars))), |
||
6 | +51 |
- #' The `teal_slice` components will specify filter states that will be active when the app starts.+ #' code = " |
||
7 | +52 |
- #' Attributes (created with the named arguments) will configure the way the app applies filters.+ #' new_iris <- transform(iris, id = seq_len(nrow(iris))) |
||
8 | +53 |
- #' See argument descriptions for details.+ #' new_mtcars <- transform(mtcars, id = seq_len(nrow(mtcars))) |
||
9 | +54 |
- #'+ #' " |
||
10 | +55 |
- #' @inheritParams teal.slice::teal_slices+ #' ), |
||
11 | +56 |
- #'+ #' modules = modules( |
||
12 | +57 |
- #' @param module_specific optional (`logical(1)`)\cr+ #' module( |
||
13 | +58 |
- #' - `FALSE` (default) when one filter panel applied to all modules.+ #' label = "data source", |
||
14 | +59 |
- #' All filters will be shared by all modules.+ #' server = function(input, output, session, data) {}, |
||
15 | +60 |
- #' - `TRUE` when filter panel module-specific.+ #' ui = function(id, ...) div(p("information about data source")), |
||
16 | +61 |
- #' Modules can have different set of filters specified - see `mapping` argument.+ #' datanames = "all" |
||
17 | +62 |
- #' @param mapping `r lifecycle::badge("experimental")` _This is a new feature. Do kindly share your opinions.\cr_+ #' ), |
||
18 | +63 |
- #' (`named list`)\cr+ #' example_module(label = "example teal module"), |
||
19 | +64 |
- #' Specifies which filters will be active in which modules on app start.+ #' module( |
||
20 | +65 |
- #' Elements should contain character vector of `teal_slice` `id`s (see [teal.slice::teal_slice()]).+ #' "Iris Sepal.Length histogram", |
||
21 | +66 |
- #' Names of the list should correspond to `teal_module` `label` set in [module()] function.+ #' server = function(input, output, session, data) { |
||
22 | +67 |
- #' `id`s listed under `"global_filters` will be active in all modules.+ #' output$hist <- renderPlot( |
||
23 | +68 |
- #' If missing, all filters will be applied to all modules.+ #' hist(data()[["new_iris"]]$Sepal.Length) |
||
24 | +69 |
- #' If empty list, all filters will be available to all modules but will start inactive.+ #' ) |
||
25 | +70 |
- #' If `module_specific` is `FALSE`, only `global_filters` will be active on start.+ #' }, |
||
26 | +71 |
- #' @param app_id (`character(1)`)\cr+ #' ui = function(id, ...) { |
||
27 | +72 |
- #' For internal use only, do not set manually.+ #' ns <- NS(id) |
||
28 | +73 |
- #' Added by `init` so that a `teal_slices` can be matched to the app in which it was used.+ #' plotOutput(ns("hist")) |
||
29 | +74 |
- #' Used for verifying snapshots uploaded from file. See `snapshot`.+ #' }, |
||
30 | +75 |
- #'+ #' datanames = "new_iris" |
||
31 | +76 |
- #' @param x (`list`) of lists to convert to `teal_slices`+ #' ) |
||
32 | +77 |
- #'+ #' ), |
||
33 | +78 |
- #' @return+ #' title = "App title", |
||
34 | +79 |
- #' A `teal_slices` object.+ #' filter = teal_slices( |
||
35 | +80 |
- #'+ #' teal_slice(dataname = "new_iris", varname = "Species"), |
||
36 | +81 |
- #' @seealso [`teal.slice::teal_slices`], [`teal.slice::teal_slice`], [`slices_store`]+ #' teal_slice(dataname = "new_iris", varname = "Sepal.Length"), |
||
37 | +82 |
- #'+ #' teal_slice(dataname = "new_mtcars", varname = "cyl"), |
||
38 | +83 |
- #' @examples+ #' exclude_varnames = list(new_iris = c("Sepal.Width", "Petal.Width")), |
||
39 | +84 |
- #' filter <- teal_slices(+ #' mapping = list( |
||
40 | +85 |
- #' teal.slice::teal_slice(dataname = "iris", varname = "Species", id = "species"),+ #' `example teal module` = "new_iris Species", |
||
41 | +86 |
- #' teal.slice::teal_slice(dataname = "iris", varname = "Sepal.Length", id = "sepal_length"),+ #' `Iris Sepal.Length histogram` = "new_iris Species", |
||
42 | +87 |
- #' teal.slice::teal_slice(+ #' global_filters = "new_mtcars cyl" |
||
43 | +88 |
- #' dataname = "iris", id = "long_petals", title = "Long petals", expr = "Petal.Length > 5"+ #' ) |
||
44 | +89 |
#' ), |
||
45 | +90 |
- #' teal.slice::teal_slice(dataname = "mtcars", varname = "mpg", id = "mtcars_mpg"),+ #' header = tags$h1("Sample App"), |
||
46 | +91 |
- #' mapping = list(+ #' footer = tags$p("Copyright 2017 - 2023") |
||
47 | +92 |
- #' module1 = c("species", "sepal_length"),+ #' ) |
||
48 | +93 |
- #' module2 = c("mtcars_mpg"),+ #' if (interactive()) { |
||
49 | +94 |
- #' global_filters = "long_petals"+ #' shinyApp(app$ui, app$server) |
||
50 | +95 |
- #' )+ #' } |
||
51 | +96 |
- #' )+ #' |
||
52 | +97 |
- #'+ init <- function(data, |
||
53 | +98 |
- #' app <- teal::init(+ modules, |
||
54 | +99 |
- #' data = list(iris = iris, mtcars = mtcars),+ title = NULL, |
||
55 | +100 |
- #' modules = list(+ filter = teal_slices(), |
||
56 | +101 |
- #' module("module1"),+ header = tags$p(), |
||
57 | +102 |
- #' module("module2")+ footer = tags$p(), |
||
58 | +103 |
- #' ),+ id = character(0)) { |
||
59 | -+ | |||
104 | +15x |
- #' filter = filter+ logger::log_trace("init initializing teal app with: data ({ class(data)[1] }).") |
||
60 | -+ | |||
105 | +15x |
- #' )+ if (is.list(data) && !inherits(data, "teal_data_module")) { |
||
61 | -+ | |||
106 | +10x |
- #'+ checkmate::assert_list(data, names = "named") |
||
62 | -+ | |||
107 | +10x |
- #' if (interactive()) {+ data <- do.call(teal.data::teal_data, data) |
||
63 | +108 |
- #' shinyApp(app$ui, app$server)+ } |
||
64 | -+ | |||
109 | +15x |
- #' }+ if (inherits(data, "TealData")) { |
||
65 | -+ | |||
110 | +! |
- #'+ lifecycle::deprecate_stop( |
||
66 | -+ | |||
111 | +! |
- #' @export+ when = "0.99.0", |
||
67 | -+ | |||
112 | +! |
- teal_slices <- function(...,+ what = "init(data)", |
||
68 | -+ | |||
113 | +! |
- exclude_varnames = NULL,+ paste( |
||
69 | -+ | |||
114 | +! |
- include_varnames = NULL,+ "TealData is no longer supported. Use teal_data() instead.", |
||
70 | -+ | |||
115 | +! |
- count_type = NULL,+ "Please follow migration instructions https://github.com/insightsengineering/teal/discussions/988." |
||
71 | +116 |
- allow_add = TRUE,+ ) |
||
72 | +117 |
- module_specific = FALSE,+ ) |
||
73 | +118 |
- mapping,+ } |
||
74 | +119 |
- app_id = NULL) {- |
- ||
75 | -76x | -
- shiny::isolate({+ |
||
76 | -76x | +120 | +15x |
- checkmate::assert_flag(allow_add)+ checkmate::assert_multi_class(data, c("teal_data", "teal_data_module")) |
77 | -76x | +121 | +15x |
- checkmate::assert_flag(module_specific)+ checkmate::assert_multi_class(modules, c("teal_module", "list", "teal_modules")) |
78 | -37x | +122 | +15x |
- if (!missing(mapping)) checkmate::assert_list(mapping, types = c("character", "NULL"), names = "named")+ checkmate::assert_string(title, null.ok = TRUE) |
79 | -73x | -
- checkmate::assert_string(app_id, null.ok = TRUE)- |
- ||
80 | -+ | 123 | +15x |
-
+ checkmate::assert( |
81 | -73x | +124 | +15x |
- slices <- list(...)+ checkmate::check_class(filter, "teal_slices"), |
82 | -73x | +125 | +15x |
- all_slice_id <- vapply(slices, `[[`, character(1L), "id")+ checkmate::check_list(filter, names = "named") |
83 | +126 | - - | -||
84 | -73x | -
- if (missing(mapping)) {+ ) |
||
85 | -39x | -
- mapping <- list(global_filters = all_slice_id)- |
- ||
86 | -+ | 127 | +14x |
- }+ checkmate::assert_multi_class(header, c("shiny.tag", "character")) |
87 | -73x | +128 | +14x |
- if (!module_specific) {+ checkmate::assert_multi_class(footer, c("shiny.tag", "character")) |
88 | -69x | -
- mapping[setdiff(names(mapping), "global_filters")] <- NULL- |
- ||
89 | -+ | 129 | +14x |
- }+ checkmate::assert_character(id, max.len = 1, any.missing = FALSE) |
90 | +130 | |||
91 | -73x | +131 | +14x |
- failed_slice_id <- setdiff(unlist(mapping), all_slice_id)+ teal.logger::log_system_info() |
92 | -73x | +|||
132 | +
- if (length(failed_slice_id)) {+ |
|||
93 | -1x | +133 | +14x |
- stop(sprintf(+ if (inherits(modules, "teal_module")) { |
94 | +134 | 1x |
- "Filters in mapping don't match any available filter.\n %s not in %s",+ modules <- list(modules) |
|
95 | -1x | +|||
135 | +
- toString(failed_slice_id),+ } |
|||
96 | -1x | +136 | +14x |
- toString(all_slice_id)+ if (inherits(modules, "list")) { |
97 | -+ | |||
137 | +4x |
- ))+ modules <- do.call(teal::modules, modules) |
||
98 | +138 |
- }+ } |
||
99 | +139 | |||
100 | -72x | +140 | +14x |
- tss <- teal.slice::teal_slices(+ landing <- extract_module(modules, "teal_module_landing") |
101 | -+ | |||
141 | +! |
- ...,+ if (length(landing) > 1L) stop("Only one `landing_popup_module` can be used.") |
||
102 | -72x | +142 | +14x |
- exclude_varnames = exclude_varnames,+ modules <- drop_module(modules, "teal_module_landing") |
103 | -72x | +|||
143 | +
- include_varnames = include_varnames,+ |
|||
104 | -72x | +|||
144 | +
- count_type = count_type,+ # Calculate app id that will be used to stamp filter state snapshots. |
|||
105 | -72x | +|||
145 | +
- allow_add = allow_add+ # App id is a hash of the app's data and modules. |
|||
106 | +146 |
- )+ # See "transferring snapshots" section in ?snapshot. |
||
107 | -72x | +147 | +14x |
- attr(tss, "mapping") <- mapping+ hashables <- mget(c("data", "modules")) |
108 | -72x | +148 | +14x |
- attr(tss, "module_specific") <- module_specific+ hashables$data <- if (inherits(hashables$data, "teal_data")) { |
109 | -72x | +149 | +13x |
- attr(tss, "app_id") <- app_id+ as.list(hashables$data@env) |
110 | -72x | +150 | +14x |
- class(tss) <- c("modules_teal_slices", class(tss))+ } else if (inherits(data, "teal_data_module")) { |
111 | -72x | +151 | +1x |
- tss+ body(data$server) |
112 | +152 |
- })+ } |
||
113 | +153 |
- }+ |
||
114 | -+ | |||
154 | +14x |
-
+ attr(filter, "app_id") <- rlang::hash(hashables) |
||
115 | +155 | |||
116 | +156 |
- #' @rdname teal_slices+ # convert teal.slice::teal_slices to teal::teal_slices |
||
117 | -+ | |||
157 | +14x |
- #' @export+ filter <- as.teal_slices(as.list(filter)) |
||
118 | +158 |
- #' @keywords internal+ |
||
119 | -+ | |||
159 | +14x |
- #'+ if (isTRUE(attr(filter, "module_specific"))) { |
||
120 | -+ | |||
160 | +! |
- as.teal_slices <- function(x) { # nolint+ module_names <- unlist(c(module_labels(modules), "global_filters")) |
||
121 | -15x | +|||
161 | +! |
- checkmate::assert_list(x)+ failed_mod_names <- setdiff(names(attr(filter, "mapping")), module_names) |
||
122 | -15x | +|||
162 | +! |
- lapply(x, checkmate::assert_list, names = "named", .var.name = "list element")+ if (length(failed_mod_names)) { |
||
123 | -+ | |||
163 | +! |
-
+ stop( |
||
124 | -15x | +|||
164 | +! |
- attrs <- attributes(unclass(x))+ sprintf( |
||
125 | -15x | +|||
165 | +! |
- ans <- lapply(x, function(x) if (is.teal_slice(x)) x else as.teal_slice(x))+ "Some module names in the mapping arguments don't match module labels.\n %s not in %s", |
||
126 | -15x | +|||
166 | +! |
- do.call(teal_slices, c(ans, attrs))+ toString(failed_mod_names), |
||
127 | -+ | |||
167 | +! |
- }+ toString(unique(module_names)) |
||
128 | +168 |
-
+ ) |
||
129 | +169 |
-
+ ) |
||
130 | +170 |
- #' @rdname teal_slices+ } |
||
131 | +171 |
- #' @export+ + |
+ ||
172 | +! | +
+ if (anyDuplicated(module_names)) { |
||
132 | +173 |
- #' @keywords internal+ # In teal we are able to set nested modules with duplicated label. |
||
133 | +174 |
- #'+ # Because mapping argument bases on the relationship between module-label and filter-id, |
||
134 | +175 |
- c.teal_slices <- function(...) {+ # it is possible that module-label in mapping might refer to multiple teal_module (identified by the same label) |
||
135 | +176 | ! |
- x <- list(...)+ stop( |
|
136 | +177 | ! |
- checkmate::assert_true(all(vapply(x, is.teal_slices, logical(1L))), .var.name = "all arguments are teal_slices")+ sprintf( |
|
137 | -+ | |||
178 | +! |
-
+ "Module labels should be unique when teal_slices(mapping = TRUE). Duplicated labels:\n%s ", |
||
138 | +179 | ! |
- all_attributes <- lapply(x, attributes)+ toString(module_names[duplicated(module_names)]) |
|
139 | -! | +|||
180 | +
- all_attributes <- coalesce_r(all_attributes)+ ) |
|||
140 | -! | +|||
181 | +
- all_attributes <- all_attributes[names(all_attributes) != "class"]+ ) |
|||
141 | +182 | ++ |
+ }+ |
+ |
183 | ++ |
+ }+ |
+ ||
184 | ||||
142 | -! | +|||
185 | +14x |
- do.call(+ if (inherits(data, "teal_data")) { |
||
143 | -! | +|||
186 | +13x |
- teal_slices,+ if (length(teal.data::datanames(data)) == 0) { |
||
144 | -! | +|||
187 | +1x |
- c(+ stop("`data` object has no datanames. Specify `datanames(data)` and try again.") |
||
145 | -! | +|||
188 | +
- unique(unlist(x, recursive = FALSE)),+ }+ |
+ |||
189 | ++ | + + | +||
190 | ++ |
+ # in case of teal_data_module this check is postponed to the srv_teal_with_splash+ |
+ ||
191 | +12x | +
+ is_modules_ok <- check_modules_datanames(modules, teal.data::datanames(data))+ |
+ ||
192 | +12x | +
+ if (!isTRUE(is_modules_ok)) { |
||
146 | -! | +|||
193 | +1x |
- all_attributes+ logger::log_error(is_modules_ok) |
||
147 | -+ | |||
194 | +1x |
- )+ checkmate::assert(is_modules_ok, .var.name = "modules") |
||
148 | +195 |
- )+ } |
||
149 | +196 |
- }+ |
||
150 | +197 | |||
151 | -+ | |||
198 | +11x |
-
+ is_filter_ok <- check_filter_datanames(filter, teal.data::datanames(data)) |
||
152 | -+ | |||
199 | +11x |
- #' Deep copy `teal_slices`+ if (!isTRUE(is_filter_ok)) {+ |
+ ||
200 | +1x | +
+ logger::log_warn(is_filter_ok) |
||
153 | +201 |
- #'+ # we allow app to continue if applied filters are outside |
||
154 | +202 |
- #' it's important to create a new copy of `teal_slices` when+ # of possible data range |
||
155 | +203 |
- #' starting a new `shiny` session. Otherwise, object will be shared+ } |
||
156 | +204 |
- #' by multiple users as it is created in global environment before+ } |
||
157 | +205 |
- #' `shiny` session starts.+ |
||
158 | +206 |
- #' @param filter (`teal_slices`)+ # Note regarding case `id = character(0)`: |
||
159 | +207 |
- #' @return `teal_slices`+ # rather than using `callModule` and creating a submodule of this module, we directly modify |
||
160 | +208 |
- #' @keywords internal+ # the `ui` and `server` with `id = character(0)` and calling the server function directly |
||
161 | +209 |
- deep_copy_filter <- function(filter) {+ # rather than through `callModule` |
||
162 | -1x | +210 | +12x |
- checkmate::assert_class(filter, "teal_slices")+ res <- list( |
163 | -1x | +211 | +12x |
- shiny::isolate({+ ui = ui_teal_with_splash(id = id, data = data, title = title, header = header, footer = footer), |
164 | -1x | +212 | +12x |
- filter_copy <- lapply(filter, function(slice) {+ server = function(input, output, session) { |
165 | -2x | +|||
213 | +! |
- teal.slice::as.teal_slice(as.list(slice))+ if (length(landing) == 1L) {+ |
+ ||
214 | +! | +
+ landing_module <- landing[[1L]]+ |
+ ||
215 | +! | +
+ do.call(landing_module$server, c(list(id = "landing_module_shiny_id"), landing_module$server_args)) |
||
166 | +216 |
- })+ } |
||
167 | -1x | +|||
217 | +! |
- attributes(filter_copy) <- attributes(filter)+ filter <- deep_copy_filter(filter) |
||
168 | -1x | +|||
218 | +! |
- filter_copy+ srv_teal_with_splash(id = id, data = data, modules = modules, filter = filter) |
||
169 | +219 |
- })+ } |
||
170 | +220 | ++ |
+ )+ |
+ |
221 | +12x | +
+ logger::log_trace("init teal app has been initialized.")+ |
+ ||
222 | +12x | +
+ return(res)+ |
+ ||
223 |
}@@ -12494,14 +12669,14 @@ teal coverage - 63.30% |
1 |
- #' Include `CSS` files from `/inst/css/` package directory to application header+ #' Filter state snapshot management. |
||
3 |
- #' `system.file` should not be used to access files in other packages, it does+ #' Capture and restore snapshots of the global (app) filter state. |
||
4 |
- #' not work with `devtools`. Therefore, we redefine this method in each package+ #' |
||
5 |
- #' as needed. Thus, we do not export this method+ #' This module introduces snapshots: stored descriptions of the filter state of the entire application. |
||
6 |
- #'+ #' Snapshots allow the user to save the current filter state of the application for later use in the session, |
||
7 |
- #' @param pattern (`character`) pattern of files to be included+ #' as well as to save it to file in order to share it with an app developer or other users, |
||
8 |
- #'+ #' who in turn can upload it to their own session. |
||
9 |
- #' @return HTML code that includes `CSS` files+ #' |
||
10 |
- #' @keywords internal+ #' The snapshot manager is accessed through the filter manager, with the cog icon in the top right corner. |
||
11 |
- include_css_files <- function(pattern = "*") {+ #' At the beginning of a session it presents three icons: a camera, an upload, and an circular arrow. |
||
12 | -12x | +
- css_files <- list.files(+ #' Clicking the camera captures a snapshot, clicking the upload adds a snapshot from a file |
|
13 | -12x | +
- system.file("css", package = "teal", mustWork = TRUE),+ #' and applies the filter states therein, and clicking the arrow resets initial application state. |
|
14 | -12x | +
- pattern = pattern, full.names = TRUE+ #' As snapshots are added, they will show up as rows in a table and each will have a select button and a save button. |
|
15 |
- )+ #' |
||
16 | -12x | +
- return(+ #' @section Server logic: |
|
17 | -12x | +
- shiny::singleton(+ #' Snapshots are basically `teal_slices` objects, however, since each module is served by a separate instance |
|
18 | -12x | +
- shiny::tags$head(lapply(css_files, shiny::includeCSS))+ #' of `FilteredData` and these objects require shared state, `teal_slice` is a `reactiveVal` so `teal_slices` |
|
19 |
- )+ #' cannot be stored as is. Therefore, `teal_slices` are reversibly converted to a list of lists representation |
||
20 |
- )+ #' (attributes are maintained). |
||
21 |
- }+ #' |
||
22 |
-
+ #' Snapshots are stored in a `reactiveVal` as a named list. |
||
23 |
- #' Include `JS` files from `/inst/js/` package directory to application header+ #' The first snapshot is the initial state of the application and the user can add a snapshot whenever they see fit. |
||
25 |
- #' `system.file` should not be used to access files in other packages, it does+ #' For every snapshot except the initial one, a piece of UI is generated that contains |
||
26 |
- #' not work with `devtools`. Therefore, we redefine this method in each package+ #' the snapshot name, a select button to restore that snapshot, and a save button to save it to a file. |
||
27 |
- #' as needed. Thus, we do not export this method+ #' The initial snapshot is restored by a separate "reset" button. |
||
28 |
- #'+ #' It cannot be saved directly but a user is welcome to capture the initial state as a snapshot and save that. |
||
29 |
- #' @param pattern (`character`) pattern of files to be included, passed to `system.file`+ #' |
||
30 |
- #' @param except (`character`) vector of basename filenames to be excluded+ #' @section Snapshot mechanics: |
||
31 |
- #'+ #' When a snapshot is captured, the user is prompted to name it. |
||
32 |
- #' @return HTML code that includes `JS` files+ #' Names are displayed as is but since they are used to create button ids, |
||
33 |
- #' @keywords internal+ #' under the hood they are converted to syntactically valid strings. |
||
34 |
- include_js_files <- function(pattern = NULL, except = NULL) {+ #' New snapshot names are validated so that their valid versions are unique. |
||
35 | -12x | +
- checkmate::assert_character(except, min.len = 1, any.missing = FALSE, null.ok = TRUE)+ #' Leading and trailing white space is trimmed. |
|
36 | -12x | +
- js_files <- list.files(system.file("js", package = "teal", mustWork = TRUE), pattern = pattern, full.names = TRUE)+ #' |
|
37 | -12x | +
- js_files <- js_files[!(basename(js_files) %in% except)] # no-op if except is NULL+ #' The module can read the global state of the application from `slices_global` and `mapping_matrix`. |
|
38 |
-
+ #' The former provides a list of all existing `teal_slice`s and the latter says which slice is active in which module. |
||
39 | -12x | +
- return(singleton(lapply(js_files, includeScript)))+ #' 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 |
-
+ #' (or one that has been restored), which may not reflect the current one, |
||
42 |
- #' Run `JS` file from `/inst/js/` package directory+ #' so `mapping_matrix` is transformed to obtain the current mapping, i.e. a list that, |
||
43 |
- #'+ #' when passed to the `mapping` argument of [`teal::teal_slices`], would result in the current mapping. |
||
44 |
- #' This is triggered from the server to execute on the client+ #' This is substituted as the snapshot's `mapping` attribute and the snapshot is added to the snapshot list. |
||
45 |
- #' rather than triggered directly on the client.+ #' |
||
46 |
- #' Unlike `include_js_files` which includes `JavaScript` functions,+ #' To restore app state, a snapshot is retrieved from storage and rebuilt into a `teal_slices` object. |
||
47 |
- #' the `run_js` actually executes `JavaScript` functions.+ #' 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 |
- #' `system.file` should not be used to access files in other packages, it does+ #' The snapshot is then set as the current content of `slices_global`. |
||
50 |
- #' not work with `devtools`. Therefore, we redefine this method in each package+ #' |
||
51 |
- #' as needed. Thus, we do not export this method+ #' To save a snapshot, the snapshot is retrieved and reassembled just like for restoring, |
||
52 |
- #'+ #' and then saved to file with [`slices_store`]. |
||
53 |
- #' @param files (`character`) vector of filenames+ #' |
||
54 |
- #' @keywords internal+ #' When a snapshot is uploaded, it will first be added to storage just like a newly created one, |
||
55 |
- run_js_files <- function(files) {+ #' and then used to restore app state much like a snapshot taken from storage. |
||
56 | -18x | +
- checkmate::assert_character(files, min.len = 1, any.missing = FALSE)+ #' Upon clicking the upload icon the user will be prompted for a file to upload |
|
57 | -18x | +
- lapply(files, function(file) {+ #' and may choose to name the new snapshot. The name defaults to the name of the file (the extension is dropped) |
|
58 | -18x | +
- shinyjs::runjs(paste0(readLines(system.file("js", file, package = "teal", mustWork = TRUE)), collapse = "\n"))+ #' and normal naming rules apply. Loading the file yields a `teal_slices` object, |
|
59 |
- })+ #' which is disassembled for storage and used directly for restoring app state. |
||
60 | -18x | +
- return(invisible(NULL))+ #' |
|
61 |
- }+ #' @section Transferring snapshots: |
||
62 |
-
+ #' Snapshots uploaded from disk should only be used in the same application they come from, |
||
63 |
- #' Code to include teal `CSS` and `JavaScript` files+ #' _i.e._ an application that uses the same data and the same modules. |
||
64 |
- #'+ #' To ensure this is the case, `init` stamps `teal_slices` with an app id that is stored in the `app_id` attribute of |
||
65 |
- #' This is useful when you want to use the same `JavaScript` and `CSS` files that are+ #' a `teal_slices` object. When a snapshot is restored from file, its `app_id` is compared to that |
||
66 |
- #' used with the teal application.+ #' of the current app state and only if the match is the snapshot admitted to the session. |
||
67 |
- #' This is also useful for running standalone modules in teal with the correct+ #' |
||
68 |
- #' styles.+ #' @param id (`character(1)`) `shiny` module id |
||
69 |
- #' Also initializes `shinyjs` so you can use it.+ #' @param slices_global (`reactiveVal`) that contains a `teal_slices` object |
||
70 |
- #'+ #' containing all `teal_slice`s existing in the app, both active and inactive |
||
71 |
- #' @return HTML code to include+ #' @param mapping_matrix (`reactive`) that contains a `data.frame` representation |
||
72 |
- #' @examples+ #' of the mapping of filter state ids (rows) to modules labels (columns); |
||
73 |
- #' shiny_ui <- tagList(+ #' all columns are `logical` vectors |
||
74 |
- #' teal:::include_teal_css_js(),+ #' @param filtered_data_list non-nested (`named list`) that contains `FilteredData` objects |
||
75 |
- #' p("Hello")+ #' |
||
76 |
- #' )+ #' @return Nothing is returned. |
||
77 |
- #' @keywords internal+ #' |
||
78 |
- include_teal_css_js <- function() {- |
- ||
79 | -12x | -
- tagList(- |
- |
80 | -12x | -
- shinyjs::useShinyjs(),- |
- |
81 | -12x | -
- include_css_files(),+ #' @name snapshot_manager_module |
|
82 | +79 |
- # init.js is executed from the server- |
- |
83 | -12x | -
- include_js_files(except = "init.js"),- |
- |
84 | -12x | -
- shinyjs::hidden(icon("gear")), # add hidden icon to load font-awesome css for icons+ #' @aliases snapshot snapshot_manager |
|
85 | +80 |
- )+ #' |
|
86 | +81 |
- }+ #' @author Aleksander Chlebowski |
1 | +82 |
- # This file adds a splash screen for delayed data loading on top of teal+ #' |
||
2 | +83 |
-
+ #' @rdname snapshot_manager_module |
||
3 | +84 |
- #' UI to show a splash screen in the beginning, then delegate to [srv_teal()]+ #' @keywords internal |
||
4 | +85 |
#' |
||
5 | +86 |
- #' @description `r lifecycle::badge("stable")`+ snapshot_manager_ui <- function(id) { |
||
6 | -+ | |||
87 | +! |
- #' The splash screen could be used to query for a password to fetch the data.+ ns <- NS(id) |
||
7 | -+ | |||
88 | +! |
- #' [init()] is a very thin wrapper around this module useful for end-users which+ div( |
||
8 | -+ | |||
89 | +! |
- #' assumes that it is a top-level module and cannot be embedded.+ class = "snapshot_manager_content", |
||
9 | -+ | |||
90 | +! |
- #' This function instead adheres to the Shiny module conventions.+ div( |
||
10 | -+ | |||
91 | +! |
- #'+ class = "snapshot_table_row", |
||
11 | -+ | |||
92 | +! |
- #' If data is obtained through delayed loading, its splash screen is used. Otherwise,+ span(tags$b("Snapshot manager")), |
||
12 | -+ | |||
93 | +! |
- #' a default splash screen is shown.+ actionLink(ns("snapshot_add"), label = NULL, icon = icon("camera"), title = "add snapshot"), |
||
13 | -+ | |||
94 | +! |
- #'+ actionLink(ns("snapshot_load"), label = NULL, icon = icon("upload"), title = "upload snapshot"), |
||
14 | -+ | |||
95 | +! |
- #' Please also refer to the doc of [init()].+ actionLink(ns("snapshot_reset"), label = NULL, icon = icon("undo"), title = "reset initial state"), |
||
15 | -+ | |||
96 | +! |
- #'+ NULL |
||
16 | +97 |
- #' @param id (`character(1)`)\cr+ ), |
||
17 | -+ | |||
98 | +! |
- #' module id+ uiOutput(ns("snapshot_list")) |
||
18 | +99 |
- #' @inheritParams init+ ) |
||
19 | +100 |
- #' @export+ } |
||
20 | +101 |
- ui_teal_with_splash <- function(id,+ |
||
21 | +102 |
- data,+ #' @rdname snapshot_manager_module |
||
22 | +103 |
- title,+ #' @keywords internal |
||
23 | +104 |
- header = tags$p("Add Title Here"),+ #' |
||
24 | +105 |
- footer = tags$p("Add Footer Here")) {+ snapshot_manager_srv <- function(id, slices_global, mapping_matrix, filtered_data_list) { |
||
25 | -12x | +106 | +6x |
- checkmate::assert_multi_class(data, c("teal_data", "teal_data_module"))+ checkmate::assert_character(id) |
26 | -12x | +107 | +6x |
- ns <- NS(id)+ checkmate::assert_true(is.reactive(slices_global)) |
27 | -+ | |||
108 | +6x |
-
+ checkmate::assert_class(isolate(slices_global()), "teal_slices") |
||
28 | -+ | |||
109 | +6x |
- # Startup splash screen for delayed loading+ checkmate::assert_true(is.reactive(mapping_matrix)) |
||
29 | -+ | |||
110 | +6x |
- # We use delayed loading in all cases, even when the data does not need to be fetched.+ checkmate::assert_data_frame(isolate(mapping_matrix()), null.ok = TRUE) |
||
30 | -+ | |||
111 | +6x |
- # This has the benefit that when filtering the data takes a lot of time initially, the+ checkmate::assert_list(filtered_data_list, types = "FilteredData", any.missing = FALSE, names = "named") |
||
31 | +112 |
- # Shiny app does not time out.- |
- ||
32 | -12x | -
- splash_ui <- if (inherits(data, "teal_data_module")) {- |
- ||
33 | -1x | -
- data$ui(ns("teal_data_module"))+ |
||
34 | -12x | +113 | +6x |
- } else if (inherits(data, "teal_data")) {+ moduleServer(id, function(input, output, session) { |
35 | -11x | +114 | +6x |
- div()+ ns <- session$ns |
36 | +115 |
- }- |
- ||
37 | -12x | -
- ui_teal(+ |
||
38 | -12x | +|||
116 | +
- id = ns("teal"),+ # Store global filter states ---- |
|||
39 | -12x | +117 | +6x |
- splash_ui = div(splash_ui, uiOutput(ns("error"))),+ filter <- isolate(slices_global()) |
40 | -12x | +118 | +6x |
- title = title,+ snapshot_history <- reactiveVal({ |
41 | -12x | +119 | +6x |
- header = header,+ list( |
42 | -12x | +120 | +6x |
- footer = footer+ "Initial application state" = as.list(filter, recursive = TRUE) |
43 | +121 |
- )+ ) |
||
44 | +122 |
- }+ }) |
||
45 | +123 | |||
46 | +124 |
- #' Server function that loads the data through reactive loading and then delegates+ # Snapshot current application state ---- |
||
47 | +125 |
- #' to [srv_teal()].+ # Name snaphsot. |
||
48 | -+ | |||
126 | +6x |
- #'+ observeEvent(input$snapshot_add, { |
||
49 | -+ | |||
127 | +! |
- #' @description `r lifecycle::badge("stable")`+ showModal( |
||
50 | -+ | |||
128 | +! |
- #' Please also refer to the doc of [init()].+ modalDialog( |
||
51 | -+ | |||
129 | +! |
- #'+ textInput(ns("snapshot_name"), "Name the snapshot", width = "100%", placeholder = "Meaningful, unique name"), |
||
52 | -+ | |||
130 | +! |
- #' @inheritParams init+ footer = tagList( |
||
53 | -+ | |||
131 | +! |
- #' @param modules `teal_modules` object containing the output modules which+ actionButton(ns("snapshot_name_accept"), "Accept", icon = icon("thumbs-up")),+ |
+ ||
132 | +! | +
+ modalButton(label = "Cancel", icon = icon("thumbs-down")) |
||
54 | +133 |
- #' will be displayed in the teal application. See [modules()] and [module()] for+ ),+ |
+ ||
134 | +! | +
+ size = "s" |
||
55 | +135 |
- #' more details.+ ) |
||
56 | +136 |
- #' @inheritParams shiny::moduleServer+ ) |
||
57 | +137 |
- #' @return `reactive` containing `teal_data` object when data is loaded.+ }) |
||
58 | +138 |
- #' If data is not loaded yet, `reactive` returns `NULL`.+ # Store snaphsot. |
||
59 | -+ | |||
139 | +6x |
- #' @export+ observeEvent(input$snapshot_name_accept, { |
||
60 | -+ | |||
140 | +! |
- srv_teal_with_splash <- function(id, data, modules, filter = teal_slices()) {+ snapshot_name <- trimws(input$snapshot_name) |
||
61 | -15x | +|||
141 | +! |
- checkmate::check_multi_class(data, c("teal_data", "teal_data_module"))+ if (identical(snapshot_name, "")) { |
||
62 | -+ | |||
142 | +! |
-
+ showNotification( |
||
63 | -15x | +|||
143 | +! |
- moduleServer(id, function(input, output, session) {+ "Please name the snapshot.", |
||
64 | -15x | +|||
144 | +! |
- logger::log_trace("srv_teal_with_splash initializing module with data.")+ type = "message" |
||
65 | +145 |
-
+ ) |
||
66 | -15x | +|||
146 | +! |
- if (getOption("teal.show_js_log", default = FALSE)) {+ updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name") |
||
67 | +147 | ! |
- shinyjs::showLog()+ } else if (is.element(make.names(snapshot_name), make.names(names(snapshot_history())))) { |
|
68 | -+ | |||
148 | +! |
- }+ showNotification( |
||
69 | -+ | |||
149 | +! |
-
+ "This name is in conflict with other snapshot names. Please choose a different one.", |
||
70 | -+ | |||
150 | +! |
- # teal_data_rv contains teal_data object+ type = "message" |
||
71 | +151 |
- # either passed to teal::init or returned from teal_data_module+ ) |
||
72 | -15x | +|||
152 | +! |
- teal_data_rv <- if (inherits(data, "teal_data_module")) {+ updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name") |
||
73 | -10x | +|||
153 | +
- data <- data$server(id = "teal_data_module")+ } else { |
|||
74 | -10x | +|||
154 | +! |
- if (!is.reactive(data)) {+ snapshot <- as.list(slices_global(), recursive = TRUE) |
||
75 | -1x | +|||
155 | +! |
- stop("The `teal_data_module` must return a reactive expression.", call. = FALSE)+ attr(snapshot, "mapping") <- matrix_to_mapping(mapping_matrix()) |
||
76 | -+ | |||
156 | +! |
- }+ snapshot_update <- c(snapshot_history(), list(snapshot)) |
||
77 | -9x | +|||
157 | +! |
- data+ names(snapshot_update)[length(snapshot_update)] <- snapshot_name |
||
78 | -15x | +|||
158 | +! |
- } else if (inherits(data, "teal_data")) {+ snapshot_history(snapshot_update) |
||
79 | -5x | +|||
159 | +! |
- reactiveVal(data)+ removeModal() |
||
80 | +160 |
- }+ # Reopen filter manager modal by clicking button in the main application. |
||
81 | -+ | |||
161 | +! |
-
+ shinyjs::click(id = "teal-main_ui-filter_manager-show", asis = TRUE) |
||
82 | -14x | +|||
162 | +
- teal_data_rv_validate <- reactive({+ } |
|||
83 | +163 |
- # custom module can return error+ }) |
||
84 | -11x | +|||
164 | +
- data <- tryCatch(teal_data_rv(), error = function(e) e)+ |
|||
85 | +165 |
-
+ # Upload a snapshot file ---- |
||
86 | +166 |
- # there is an empty reactive cycle on init!+ # Select file. |
||
87 | -11x | +167 | +6x |
- if (inherits(data, "shiny.silent.error") && identical(data$message, "")) {+ observeEvent(input$snapshot_load, { |
88 | +168 | ! |
- return(NULL)- |
- |
89 | -- |
- }+ showModal( |
||
90 | -+ | |||
169 | +! |
-
+ modalDialog( |
||
91 | -+ | |||
170 | +! |
- # to handle qenv.error+ fileInput(ns("snapshot_file"), "Choose snapshot file", accept = ".json", width = "100%"), |
||
92 | -11x | +|||
171 | +! |
- if (inherits(data, "qenv.error")) {+ textInput( |
||
93 | -2x | +|||
172 | +! |
- validate(+ ns("snapshot_name"), |
||
94 | -2x | +|||
173 | +! |
- need(+ "Name the snapshot (optional)", |
||
95 | -2x | +|||
174 | +! |
- FALSE,+ width = "100%", |
||
96 | -2x | +|||
175 | +! |
- paste(+ placeholder = "Meaningful, unique name" |
||
97 | -2x | +|||
176 | +
- "Error when executing `teal_data_module`:\n ",+ ), |
|||
98 | -2x | +|||
177 | +! |
- paste(data$message, collapse = "\n"),+ footer = tagList( |
||
99 | -2x | +|||
178 | +! |
- "\n Check your inputs or contact app developer if error persists."+ actionButton(ns("snaphot_file_accept"), "Accept", icon = icon("thumbs-up")), |
||
100 | -+ | |||
179 | +! |
- )+ modalButton(label = "Cancel", icon = icon("thumbs-down")) |
||
101 | +180 |
) |
||
102 | +181 |
) |
||
103 | +182 |
- }+ ) |
||
104 | +183 |
-
+ }) |
||
105 | +184 |
- # to handle module non-qenv errors+ # Store new snapshot to list and restore filter states. |
||
106 | -9x | +185 | +6x |
- if (inherits(data, "error")) {+ observeEvent(input$snaphot_file_accept, { |
107 | -1x | +|||
186 | +! |
- validate(+ snapshot_name <- trimws(input$snapshot_name) |
||
108 | -1x | +|||
187 | +! |
- need(+ if (identical(snapshot_name, "")) { |
||
109 | -1x | +|||
188 | +! |
- FALSE,+ snapshot_name <- tools::file_path_sans_ext(input$snapshot_file$name) |
||
110 | -1x | +|||
189 | +
- paste(+ } |
|||
111 | -1x | +|||
190 | +! |
- "Error when executing `teal_data_module`:\n ",+ if (is.element(make.names(snapshot_name), make.names(names(snapshot_history())))) { |
||
112 | -1x | +|||
191 | +! |
- paste(data$message, collpase = "\n"),+ showNotification( |
||
113 | -1x | +|||
192 | +! |
- "\n Check your inputs or contact app developer if error persists."+ "This name is in conflict with other snapshot names. Please choose a different one.", |
||
114 | -+ | |||
193 | +! |
- )+ type = "message" |
||
115 | +194 |
- )+ ) |
||
116 | -+ | |||
195 | +! |
- )+ updateTextInput(inputId = "snapshot_name", value = "", placeholder = "Meaningful, unique name") |
||
117 | +196 |
- }+ } else { |
||
118 | +197 | - - | -||
119 | -8x | -
- validate(+ # Restore snapshot and verify app compatibility. |
||
120 | -8x | +|||
198 | +! |
- need(+ snapshot_state <- try(slices_restore(input$snapshot_file$datapath)) |
||
121 | -8x | +|||
199 | +! |
- inherits(data, "teal_data"),+ if (!inherits(snapshot_state, "modules_teal_slices")) { |
||
122 | -8x | +|||
200 | +! |
- paste(+ showNotification( |
||
123 | -8x | +|||
201 | +! |
- "Error: `teal_data_module` did not return `teal_data` object",+ "File appears to be corrupt.", |
||
124 | -8x | +|||
202 | +! |
- "\n Check your inputs or contact app developer if error persists"+ type = "error" |
||
125 | +203 |
) |
||
126 | -- |
- )- |
- ||
127 | -- |
- )- |
- ||
128 | -- | - - | -||
129 | -5x | -
- validate(need(teal.data::datanames(data), "Data has no datanames. Contact app developer."))- |
- ||
130 | -+ | |||
204 | +! |
-
+ } else if (!identical(attr(snapshot_state, "app_id"), attr(slices_global(), "app_id"))) { |
||
131 | -+ | |||
205 | +! |
-
+ showNotification( |
||
132 | -4x | +|||
206 | +! |
- is_modules_ok <- check_modules_datanames(modules, teal.data::datanames(data))+ "This snapshot file is not compatible with the app and cannot be loaded.", |
||
133 | -4x | +|||
207 | +! |
- validate(need(isTRUE(is_modules_ok), is_modules_ok))+ type = "warning" |
||
134 | +208 |
-
+ ) |
||
135 | -3x | +|||
209 | +
- is_filter_ok <- check_filter_datanames(filter, teal.data::datanames(data))+ } else { |
|||
136 | -3x | +|||
210 | +
- if (!isTRUE(is_filter_ok)) {+ # Add to snapshot history. |
|||
137 | -1x | +|||
211 | +! |
- showNotification(+ snapshot <- as.list(snapshot_state, recursive = TRUE) |
||
138 | -1x | +|||
212 | +! |
- "Some filters were not applied because of incompatibility with data. Contact app developer.",+ snapshot_update <- c(snapshot_history(), list(snapshot)) |
||
139 | -1x | +|||
213 | +! |
- type = "warning",+ names(snapshot_update)[length(snapshot_update)] <- snapshot_name |
||
140 | -1x | +|||
214 | +! |
- duration = 10+ snapshot_history(snapshot_update) |
||
141 | +215 |
- )+ ### Begin simplified restore procedure. ### |
||
142 | -1x | +|||
216 | +! |
- logger::log_warn(is_filter_ok)+ mapping_unfolded <- unfold_mapping(attr(snapshot_state, "mapping"), names(filtered_data_list)) |
||
143 | -+ | |||
217 | +! |
- }+ mapply( |
||
144 | -+ | |||
218 | +! |
-
+ function(filtered_data, filter_ids) { |
||
145 | -3x | +|||
219 | +! |
- teal_data_rv()+ filtered_data$clear_filter_states(force = TRUE) |
||
146 | -+ | |||
220 | +! |
- })+ slices <- Filter(function(x) x$id %in% filter_ids, snapshot_state) |
||
147 | -+ | |||
221 | +! |
-
+ filtered_data$set_filter_state(slices) |
||
148 | -14x | +|||
222 | +
- output$error <- renderUI({+ }, |
|||
149 | +223 | ! |
- teal_data_rv_validate()+ filtered_data = filtered_data_list, |
|
150 | +224 | ! |
- NULL- |
- |
151 | -- |
- })+ filter_ids = mapping_unfolded |
||
152 | +225 |
-
+ ) |
||
153 | -+ | |||
226 | +! |
-
+ slices_global(snapshot_state) |
||
154 | -14x | +|||
227 | +! |
- res <- srv_teal(id = "teal", modules = modules, teal_data_rv = teal_data_rv_validate, filter = filter)+ removeModal() |
||
155 | -14x | +|||
228 | +
- logger::log_trace("srv_teal_with_splash initialized module with data.")+ ### End simplified restore procedure. ### |
|||
156 | -14x | +|||
229 | +
- return(res)+ } |
|||
157 | +230 |
- })+ } |
||
158 | +231 |
- }+ }) |
1 | +232 |
- setOldClass("teal_data_module")+ # Apply newly added snapshot. |
||
2 | +233 | |||
3 | +234 |
- #' Evaluate the code in the qenv environment+ # Restore initial state ---- |
||
4 | -+ | |||
235 | +6x |
- #' @name eval_code+ observeEvent(input$snapshot_reset, { |
||
5 | -+ | |||
236 | +! |
- #' @description+ s <- "Initial application state" |
||
6 | +237 |
- #' Given code is evaluated in the `qenv` environment of `teal_data` reactive defined in `teal_data_module`.+ ### Begin restore procedure. ### |
||
7 | -+ | |||
238 | +! |
- #' @param object (`teal_data_module`)+ snapshot <- snapshot_history()[[s]] |
||
8 | -+ | |||
239 | +! |
- #' @inheritParams teal.code::eval_code+ snapshot_state <- as.teal_slices(snapshot) |
||
9 | -+ | |||
240 | +! |
- #' @return Returns a `teal_data_module` object.+ mapping_unfolded <- unfold_mapping(attr(snapshot_state, "mapping"), names(filtered_data_list)) |
||
10 | -+ | |||
241 | +! |
- #' @importMethodsFrom teal.code eval_code+ mapply( |
||
11 | -+ | |||
242 | +! |
- #' @importFrom methods setMethod+ function(filtered_data, filter_ids) { |
||
12 | -+ | |||
243 | +! |
- NULL+ filtered_data$clear_filter_states(force = TRUE) |
||
13 | -+ | |||
244 | +! |
-
+ slices <- Filter(function(x) x$id %in% filter_ids, snapshot_state) |
||
14 | -+ | |||
245 | +! |
- #' @rdname eval_code+ filtered_data$set_filter_state(slices) |
||
15 | +246 |
- #' @export+ }, |
||
16 | -+ | |||
247 | +! |
- #'+ filtered_data = filtered_data_list, |
||
17 | -+ | |||
248 | +! |
- #' @examples+ filter_ids = mapping_unfolded |
||
18 | +249 |
- #' tdm <- teal_data_module(+ ) |
||
19 | -+ | |||
250 | +! |
- #' ui = function(id) div(id = shiny::NS(id)("div_id")),+ slices_global(snapshot_state) |
||
20 | -+ | |||
251 | +! |
- #' server = function(id) {+ removeModal() |
||
21 | +252 |
- #' shiny::moduleServer(id, function(input, output, session) {+ ### End restore procedure. ### |
||
22 | +253 |
- #' shiny::reactive(teal_data(IRIS = iris))+ }) |
||
23 | +254 |
- #' })+ |
||
24 | +255 |
- #' }+ # Build snapshot table ---- |
||
25 | +256 |
- #' )+ # Create UI elements and server logic for the snapshot table. |
||
26 | +257 |
- #' eval_code(tdm, "IRIS <- subset(IRIS, Species == 'virginica')")+ # Observers must be tracked to avoid duplication and excess reactivity. |
||
27 | +258 |
- setMethod("eval_code", signature = c("teal_data_module", "character"), function(object, code) {- |
- ||
28 | -13x | -
- teal_data_module(+ # Remaining elements are tracked likewise for consistency and a slight speed margin. |
||
29 | -13x | +259 | +6x |
- ui = function(id) {+ observers <- reactiveValues() |
30 | -1x | +260 | +6x |
- ns <- NS(id)+ handlers <- reactiveValues() |
31 | -1x | +261 | +6x |
- object$ui(ns("mutate_inner"))+ divs <- reactiveValues() |
32 | +262 |
- },+ |
||
33 | -13x | +263 | +6x |
- server = function(id) {+ observeEvent(snapshot_history(), { |
34 | -11x | +264 | +2x |
- moduleServer(id, function(input, output, session) {+ lapply(names(snapshot_history())[-1L], function(s) { |
35 | -11x | +|||
265 | +! |
- teal_data_rv <- object$server("mutate_inner")+ id_pickme <- sprintf("pickme_%s", make.names(s)) |
||
36 | -+ | |||
266 | +! |
-
+ id_saveme <- sprintf("saveme_%s", make.names(s)) |
||
37 | -11x | +|||
267 | +! |
- if (!is.reactive(teal_data_rv)) {+ id_rowme <- sprintf("rowme_%s", make.names(s)) |
||
38 | -1x | +|||
268 | +
- stop("The `teal_data_module` must return a reactive expression.", call. = FALSE)+ |
|||
39 | +269 |
- }+ # Observer for restoring snapshot. |
||
40 | -+ | |||
270 | +! |
-
+ if (!is.element(id_pickme, names(observers))) { |
||
41 | -10x | +|||
271 | +! |
- td <- eventReactive(teal_data_rv(),+ observers[[id_pickme]] <- observeEvent(input[[id_pickme]], { |
||
42 | +272 |
- {+ ### Begin restore procedure. ### |
||
43 | -10x | +|||
273 | +! |
- if (inherits(teal_data_rv(), c("teal_data", "qenv.error"))) {+ snapshot <- snapshot_history()[[s]] |
||
44 | -6x | +|||
274 | +! |
- eval_code(teal_data_rv(), code)+ snapshot_state <- as.teal_slices(snapshot) |
||
45 | -+ | |||
275 | +! |
- } else {+ mapping_unfolded <- unfold_mapping(attr(snapshot_state, "mapping"), names(filtered_data_list)) |
||
46 | -4x | +|||
276 | +! |
- teal_data_rv()+ mapply( |
||
47 | -+ | |||
277 | +! |
- }+ function(filtered_data, filter_ids) { |
||
48 | -+ | |||
278 | +! | +
+ filtered_data$clear_filter_states(force = TRUE)+ |
+ ||
279 | +! |
- },+ slices <- Filter(function(x) x$id %in% filter_ids, snapshot_state) |
||
49 | -10x | +|||
280 | +! |
- ignoreNULL = FALSE+ filtered_data$set_filter_state(slices) |
||
50 | +281 |
- )+ }, |
||
51 | -10x | +|||
282 | +! |
- td+ filtered_data = filtered_data_list, |
||
52 | -+ | |||
283 | +! |
- })+ filter_ids = mapping_unfolded |
||
53 | +284 |
- }+ ) |
||
54 | -+ | |||
285 | +! |
- )+ slices_global(snapshot_state) |
||
55 | -+ | |||
286 | +! |
- })+ removeModal() |
||
56 | +287 |
-
+ ### End restore procedure. ### |
||
57 | +288 |
- #' @rdname eval_code+ }) |
||
58 | +289 |
- #' @export+ } |
||
59 | +290 |
- setMethod("eval_code", signature = c("teal_data_module", "language"), function(object, code) {+ # Create handler for downloading snapshot. |
||
60 | -1x | +|||
291 | +! |
- eval_code(object, code = paste(lang2calls(code), collapse = "\n"))+ if (!is.element(id_saveme, names(handlers))) { |
||
61 | -+ | |||
292 | +! |
- })+ output[[id_saveme]] <- downloadHandler( |
||
62 | -+ | |||
293 | +! |
-
+ filename = function() { |
||
63 | -+ | |||
294 | +! |
- #' @rdname eval_code+ sprintf("teal_snapshot_%s_%s.json", s, Sys.Date()) |
||
64 | +295 |
- #' @export+ }, |
||
65 | -+ | |||
296 | +! |
- setMethod("eval_code", signature = c("teal_data_module", "expression"), function(object, code) {+ content = function(file) { |
||
66 | -6x | +|||
297 | +! |
- eval_code(object, code = paste(lang2calls(code), collapse = "\n"))+ snapshot <- snapshot_history()[[s]] |
||
67 | -+ | |||
298 | +! |
- })+ snapshot_state <- as.teal_slices(snapshot) |
||
68 | -+ | |||
299 | +! |
-
+ slices_store(tss = snapshot_state, file = file) |
||
69 | +300 |
- #' @inherit teal.code::within.qenv params title details+ } |
||
70 | +301 |
- #' @description+ ) |
||
71 | -+ | |||
302 | +! |
- #' Convenience function for evaluating inline code inside the environment of a+ handlers[[id_saveme]] <- id_saveme |
||
72 | +303 |
- #' `teal_data_module`+ } |
||
73 | +304 |
- #'+ # Create a row for the snapshot table. |
||
74 | -+ | |||
305 | +! |
- #' @param data (`teal_data_module`) object+ if (!is.element(id_rowme, names(divs))) { |
||
75 | -+ | |||
306 | +! |
- #' @return Returns a `teal_data_module` object with a delayed evaluation of `expr`+ divs[[id_rowme]] <- div( |
||
76 | -+ | |||
307 | +! |
- #' when module.+ class = "snapshot_table_row", |
||
77 | -+ | |||
308 | +! |
- #' @export+ span(h5(s)), |
||
78 | -+ | |||
309 | +! |
- #' @seealso [base::within()], [teal_data_module()]+ actionLink(inputId = ns(id_pickme), label = icon("circle-check"), title = "select"), |
||
79 | -+ | |||
310 | +! |
- #' @examples+ downloadLink(outputId = ns(id_saveme), label = icon("save"), title = "save to file") |
||
80 | +311 |
- #' tdm <- teal_data_module(+ ) |
||
81 | +312 |
- #' ui = function(id) div(id = shiny::NS(id)("div_id")),+ } |
||
82 | +313 |
- #' server = function(id) {+ }) |
||
83 | +314 |
- #' shiny::moduleServer(id, function(input, output, session) {+ }) |
||
84 | +315 |
- #' shiny::reactive(teal_data(IRIS = iris))+ |
||
85 | +316 |
- #' })+ # Create table to display list of snapshots and their actions. |
||
86 | -+ | |||
317 | +6x |
- #' }+ output$snapshot_list <- renderUI({ |
||
87 | -+ | |||
318 | +2x |
- #' )+ rows <- lapply(rev(reactiveValuesToList(divs)), function(d) d) |
||
88 | -+ | |||
319 | +2x |
- #' within(tdm, IRIS <- subset(IRIS, Species == "virginica"))+ if (length(rows) == 0L) { |
||
89 | -+ | |||
320 | +2x |
- within.teal_data_module <- function(data, expr, ...) {+ div( |
||
90 | -6x | +321 | +2x |
- expr <- substitute(expr)+ class = "snapshot_manager_placeholder", |
91 | -6x | +322 | +2x |
- extras <- list(...)+ "Snapshots will appear here." |
92 | +323 |
-
+ ) |
||
93 | +324 |
- # Add braces for consistency.+ } else { |
||
94 | -6x | +|||
325 | +! |
- if (!identical(as.list(expr)[[1L]], as.symbol("{"))) {+ rows |
||
95 | -6x | +|||
326 | +
- expr <- call("{", expr)+ } |
|||
96 | +327 |
- }+ }) |
||
97 | +328 |
-
+ }) |
||
98 | -6x | +|||
329 | +
- calls <- as.list(expr)[-1]+ } |
|||
99 | +330 | |||
100 | +331 |
- # Inject extra values into expressions.+ |
||
101 | -6x | +|||
332 | +
- calls <- lapply(calls, function(x) do.call(substitute, list(x, env = extras)))+ |
|||
102 | +333 | |||
103 | -6x | +|||
334 | +
- eval_code(object = data, code = as.expression(calls))+ ### utility functions ---- |
|||
104 | +335 |
- }+ |
1 | +336 |
- #' Get Client Timezone+ #' Explicitly enumerate global filters. |
|
2 | +337 |
#' |
|
3 | +338 |
- #' Local timezone in the browser may differ from the system timezone from the server.+ #' Transform module mapping such that global filters are explicitly specified for every module. |
|
4 | +339 |
- #' This script can be run to register a shiny input which contains information about+ #' |
|
5 | +340 |
- #' the timezone in the browser.+ #' @param mapping (`named list`) as stored in mapping parameter of `teal_slices` |
|
6 | +341 |
- #'+ #' @param module_names (`character`) vector containing names of all modules in the app |
|
7 | +342 |
- #' @param ns (`function`) namespace function passed from the `session` object in the+ #' @return A `named_list` with one element per module, each element containing all filters applied to that module. |
|
8 | +343 |
- #' Shiny server. For Shiny modules this will allow for proper name spacing of the+ #' @keywords internal |
|
9 | +344 |
- #' registered input.+ #' |
|
10 | +345 |
- #'+ unfold_mapping <- function(mapping, module_names) {+ |
+ |
346 | +! | +
+ module_names <- structure(module_names, names = module_names)+ |
+ |
347 | +! | +
+ lapply(module_names, function(x) c(mapping[[x]], mapping[["global_filters"]])) |
|
11 | +348 |
- #' @return (`Shiny`) input variable accessible with `input$tz` which is a (`character`)+ } |
|
12 | +349 |
- #' string containing the timezone of the browser/client.+ |
|
13 | +350 |
- #' @keywords internal+ #' Convert mapping matrix to filter mapping specification. |
|
14 | +351 |
- get_client_timezone <- function(ns) {+ #' |
|
15 | -18x | +||
352 | +
- script <- sprintf(+ #' Transform a mapping matrix, i.e. a data frame that maps each filter state to each module, |
||
16 | -18x | +||
353 | +
- "Shiny.setInputValue(`%s`, Intl.DateTimeFormat().resolvedOptions().timeZone)",+ #' to a list specification like the one used in the `mapping` attribute of `teal_slices`. |
||
17 | -18x | +||
354 | +
- ns("timezone")+ #' Global filters are gathered in one list element. |
||
18 | +355 |
- )+ #' If a module has no active filters but the global ones, it will not be mentioned in the output. |
|
19 | -18x | +||
356 | +
- shinyjs::runjs(script) # function does not return anything+ #' |
||
20 | -18x | +||
357 | +
- return(invisible(NULL))+ #' @param mapping_matrix (`data.frame`) of logical vectors where |
||
21 | +358 |
- }+ #' columns represent modules and row represent `teal_slice`s |
|
22 | +359 |
-
+ #' @return `named list` like that in the `mapping` attribute of a `teal_slices` object. |
|
23 | +360 |
- #' Resolve the expected bootstrap theme+ #' @keywords internal |
|
24 | +361 |
- #' @keywords internal+ #' |
|
25 | +362 |
- get_teal_bs_theme <- function() {+ matrix_to_mapping <- function(mapping_matrix) { |
|
26 | -16x | +||
363 | +! | +
+ mapping_matrix[] <- lapply(mapping_matrix, function(x) x | is.na(x))+ |
+ |
364 | +! | +
+ global <- vapply(as.data.frame(t(mapping_matrix)), all, logical(1L))+ |
+ |
365 | +! | +
+ global_filters <- names(global[global])+ |
+ |
366 | +! | +
+ local_filters <- mapping_matrix[!rownames(mapping_matrix) %in% global_filters, ]+ |
+ |
367 | +
- bs_theme <- getOption("teal.bs_theme")+ |
||
27 | -16x | +||
368 | +! |
- if (is.null(bs_theme)) {+ mapping <- c(lapply(local_filters, function(x) rownames(local_filters)[x]), list(global_filters = global_filters)) |
|
28 | -13x | +||
369 | +! |
- NULL+ Filter(function(x) length(x) != 0L, mapping) |
|
29 | -3x | +||
370 | +
- } else if (!inherits(bs_theme, "bs_theme")) {+ } |
||
30 | -2x | +
1 | +
- warning("teal.bs_theme has to be of a bslib::bs_theme class, the default shiny bootstrap is used.")+ #' Include `CSS` files from `/inst/css/` package directory to application header |
|||
31 | -2x | +|||
2 | +
- NULL+ #' |
|||
32 | +3 |
- } else {+ #' `system.file` should not be used to access files in other packages, it does |
||
33 | -1x | +|||
4 | +
- bs_theme+ #' not work with `devtools`. Therefore, we redefine this method in each package |
|||
34 | +5 |
- }+ #' as needed. Thus, we do not export this method |
||
35 | +6 |
- }+ #' |
||
36 | +7 |
-
+ #' @param pattern (`character`) pattern of files to be included |
||
37 | +8 |
- include_parent_datanames <- function(dataname, join_keys) {+ #' |
||
38 | -3x | +|||
9 | +
- parents <- character(0)+ #' @return HTML code that includes `CSS` files |
|||
39 | -3x | +|||
10 | +
- for (i in dataname) {+ #' @keywords internal |
|||
40 | -6x | +|||
11 | +
- while (length(i) > 0) {+ include_css_files <- function(pattern = "*") { |
|||
41 | -6x | +12 | +12x |
- parent_i <- teal.data::parent(join_keys, i)+ css_files <- list.files( |
42 | -6x | +13 | +12x |
- parents <- c(parent_i, parents)+ system.file("css", package = "teal", mustWork = TRUE), |
43 | -6x | +14 | +12x |
- i <- parent_i+ pattern = pattern, full.names = TRUE |
44 | +15 |
- }+ ) |
||
45 | -+ | |||
16 | +12x |
- }+ return( |
||
46 | -+ | |||
17 | +12x |
-
+ shiny::singleton( |
||
47 | -3x | +18 | +12x |
- return(unique(c(parents, dataname)))+ shiny::tags$head(lapply(css_files, shiny::includeCSS)) |
48 | +19 |
- }+ ) |
||
49 | +20 |
-
+ ) |
||
50 | +21 |
-
+ } |
||
51 | +22 | |||
52 | +23 |
- #' Create a `FilteredData`+ #' Include `JS` files from `/inst/js/` package directory to application header |
||
53 | +24 |
#' |
||
54 | +25 |
- #' Create a `FilteredData` object from a `teal_data` object+ #' `system.file` should not be used to access files in other packages, it does |
||
55 | +26 |
- #' @param x (`teal_data`) object+ #' not work with `devtools`. Therefore, we redefine this method in each package |
||
56 | +27 |
- #' @param datanames (`character`) vector of data set names to include; must be subset of `datanames(x)`+ #' as needed. Thus, we do not export this method |
||
57 | +28 |
- #' @return (`FilteredData`) object+ #' |
||
58 | +29 |
- #' @keywords internal+ #' @param pattern (`character`) pattern of files to be included, passed to `system.file` |
||
59 | +30 |
- teal_data_to_filtered_data <- function(x, datanames = teal.data::datanames(x)) {- |
- ||
60 | -12x | -
- checkmate::assert_class(x, "teal_data")- |
- ||
61 | -12x | -
- checkmate::assert_character(datanames, min.len = 1L, any.missing = FALSE)- |
- ||
62 | -12x | -
- checkmate::assert_subset(datanames, teal.data::datanames(x))+ #' @param except (`character`) vector of basename filenames to be excluded |
||
63 | +31 | - - | -||
64 | -12x | -
- ans <- teal.slice::init_filtered_data(- |
- ||
65 | -12x | -
- x = sapply(datanames, function(dn) x[[dn]], simplify = FALSE),+ #' |
||
66 | -12x | +|||
32 | +
- join_keys = teal.data::join_keys(x)+ #' @return HTML code that includes `JS` files |
|||
67 | +33 |
- )+ #' @keywords internal |
||
68 | +34 |
- # Piggy-back entire pre-processing code so that filtering code can be appended later.+ include_js_files <- function(pattern = NULL, except = NULL) { |
||
69 | +35 | 12x |
- attr(ans, "preprocessing_code") <- teal.code::get_code(x)+ checkmate::assert_character(except, min.len = 1, any.missing = FALSE, null.ok = TRUE) |
|
70 | +36 | 12x |
- ans+ js_files <- list.files(system.file("js", package = "teal", mustWork = TRUE), pattern = pattern, full.names = TRUE) |
|
71 | -+ | |||
37 | +12x |
- }+ js_files <- js_files[!(basename(js_files) %in% except)] # no-op if except is NULL |
||
72 | +38 | |||
73 | -+ | |||
39 | +12x |
- #' Template Function for `TealReportCard` Creation and Customization+ return(singleton(lapply(js_files, includeScript))) |
||
74 | +40 |
- #'+ } |
||
75 | +41 |
- #' This function generates a report card with a title,+ |
||
76 | +42 |
- #' an optional description, and the option to append the filter state list.+ #' Run `JS` file from `/inst/js/` package directory |
||
77 | +43 |
#' |
||
78 | +44 |
- #' @param title (`character(1)`) title of the card (unless overwritten by label)+ #' This is triggered from the server to execute on the client |
||
79 | +45 |
- #' @param label (`character(1)`) label provided by the user when adding the card+ #' rather than triggered directly on the client. |
||
80 | +46 |
- #' @param description (`character(1)`) optional additional description+ #' Unlike `include_js_files` which includes `JavaScript` functions, |
||
81 | +47 |
- #' @param with_filter (`logical(1)`) flag indicating to add filter state+ #' the `run_js` actually executes `JavaScript` functions. |
||
82 | +48 |
- #' @param filter_panel_api (`FilterPanelAPI`) object with API that allows the generation+ #' |
||
83 | +49 |
- #' of the filter state in the report+ #' `system.file` should not be used to access files in other packages, it does |
||
84 | +50 |
- #'+ #' not work with `devtools`. Therefore, we redefine this method in each package |
||
85 | +51 |
- #' @return (`TealReportCard`) populated with a title, description and filter state+ #' as needed. Thus, we do not export this method |
||
86 | +52 |
#' |
||
87 | +53 |
- #' @export+ #' @param files (`character`) vector of filenames |
||
88 | +54 |
- report_card_template <- function(title, label, description = NULL, with_filter, filter_panel_api) {- |
- ||
89 | -2x | -
- checkmate::assert_string(title)- |
- ||
90 | -2x | -
- checkmate::assert_string(label)- |
- ||
91 | -2x | -
- checkmate::assert_string(description, null.ok = TRUE)- |
- ||
92 | -2x | -
- checkmate::assert_flag(with_filter)- |
- ||
93 | -2x | -
- checkmate::assert_class(filter_panel_api, classes = "FilterPanelAPI")+ #' @keywords internal |
||
94 | +55 | - - | -||
95 | -2x | -
- card <- teal::TealReportCard$new()- |
- ||
96 | -2x | -
- title <- if (label == "") title else label+ run_js_files <- function(files) { |
||
97 | -2x | +56 | +18x |
- card$set_name(title)+ checkmate::assert_character(files, min.len = 1, any.missing = FALSE) |
98 | -2x | +57 | +18x |
- card$append_text(title, "header2")+ lapply(files, function(file) { |
99 | -1x | +58 | +18x |
- if (!is.null(description)) card$append_text(description, "header3")+ shinyjs::runjs(paste0(readLines(system.file("js", file, package = "teal", mustWork = TRUE)), collapse = "\n")) |
100 | -1x | +|||
59 | +
- if (with_filter) card$append_fs(filter_panel_api$get_filter_state())+ }) |
|||
101 | -2x | +60 | +18x |
- card+ return(invisible(NULL)) |
102 | +61 |
} |
||
103 | -- |
- #' Resolve `datanames` for the modules- |
- ||
104 | +62 |
- #'+ |
||
105 | +63 |
- #' Modifies `module$datanames` to include names of the parent dataset (taken from `join_keys`).+ #' Code to include teal `CSS` and `JavaScript` files |
||
106 | +64 |
- #' When `datanames` is set to `"all"` it is replaced with all available datasets names.+ #' |
||
107 | +65 |
- #' @param modules (`teal_modules`) object+ #' This is useful when you want to use the same `JavaScript` and `CSS` files that are |
||
108 | +66 |
- #' @param datanames (`character`) names of datasets available in the `data` object+ #' used with the teal application. |
||
109 | +67 |
- #' @param join_keys (`join_keys`) object+ #' This is also useful for running standalone modules in teal with the correct |
||
110 | +68 |
- #' @return `teal_modules` with resolved `datanames`+ #' styles. |
||
111 | +69 |
- #' @keywords internal+ #' Also initializes `shinyjs` so you can use it. |
||
112 | +70 |
- resolve_modules_datanames <- function(modules, datanames, join_keys) {- |
- ||
113 | -! | -
- if (inherits(modules, "teal_modules")) {- |
- ||
114 | -! | -
- modules$children <- sapply(- |
- ||
115 | -! | -
- modules$children,- |
- ||
116 | -! | -
- resolve_modules_datanames,- |
- ||
117 | -! | -
- simplify = FALSE,- |
- ||
118 | -! | -
- datanames = datanames,- |
- ||
119 | -! | -
- join_keys = join_keys+ #' |
||
120 | +71 |
- )+ #' @return HTML code to include |
||
121 | -! | +|||
72 | +
- modules+ #' @examples |
|||
122 | +73 |
- } else {+ #' shiny_ui <- tagList( |
||
123 | -! | +|||
74 | +
- modules$datanames <- if (identical(modules$datanames, "all")) {+ #' teal:::include_teal_css_js(), |
|||
124 | -! | +|||
75 | +
- datanames+ #' p("Hello") |
|||
125 | -! | +|||
76 | +
- } else if (is.character(modules$datanames)) {+ #' ) |
|||
126 | -! | +|||
77 | +
- extra_datanames <- setdiff(modules$datanames, datanames)+ #' @keywords internal |
|||
127 | -! | +|||
78 | +
- if (length(extra_datanames)) {+ include_teal_css_js <- function() { |
|||
128 | -! | +|||
79 | +12x |
- stop(+ tagList( |
||
129 | -! | +|||
80 | +12x |
- sprintf(+ shinyjs::useShinyjs(), |
||
130 | -! | +|||
81 | +12x |
- "Module %s has datanames that are not available in a 'data':\n %s not in %s",+ include_css_files(), |
||
131 | -! | +|||
82 | +
- modules$label,+ # init.js is executed from the server |
|||
132 | -! | +|||
83 | +12x |
- toString(extra_datanames),+ include_js_files(except = "init.js"), |
||
133 | -! | +|||
84 | +12x |
- toString(datanames)+ shinyjs::hidden(icon("gear")), # add hidden icon to load font-awesome css for icons |
||
134 | +85 |
- )+ ) |
||
135 | +86 |
- )+ } |
136 | +1 |
- }+ #' Create a `teal` module for previewing a report |
||
137 | -! | +|||
2 | +
- datanames_adjusted <- intersect(modules$datanames, datanames)+ #' |
|||
138 | -! | +|||
3 | +
- include_parent_datanames(dataname = datanames_adjusted, join_keys = join_keys)+ #' @description `r lifecycle::badge("experimental")` |
|||
139 | +4 |
- }+ #' This function wraps [teal.reporter::reporter_previewer_ui()] and |
||
140 | -! | +|||
5 | +
- modules+ #' [teal.reporter::reporter_previewer_srv()] into a `teal_module` to be |
|||
141 | +6 |
- }+ #' used in `teal` applications. |
||
142 | +7 |
- }+ #' |
||
143 | +8 |
-
+ #' If you are creating a `teal` application using [teal::init()] then this |
||
144 | +9 |
- #' Check `datanames` in modules+ #' module will be added to your application automatically if any of your `teal modules` |
||
145 | +10 |
- #'+ #' support report generation. |
||
146 | +11 |
- #' This function ensures specified `datanames` in modules match those in the data object,+ #' |
||
147 | +12 |
- #' returning error messages or `TRUE` for successful validation.+ #' @inheritParams module |
||
148 | +13 |
- #'+ #' @param server_args (`named list`)\cr |
||
149 | +14 |
- #' @param modules (`teal_modules`) object+ #' Arguments passed to [teal.reporter::reporter_previewer_srv()]. |
||
150 | +15 |
- #' @param datanames (`character`) names of datasets available in the `data` object+ #' @return `teal_module` (extended with `teal_module_previewer` class) containing the `teal.reporter` previewer |
||
151 | +16 |
- #'+ #' functionality. |
||
152 | +17 |
- #' @return A `character(1)` containing error message or `TRUE` if validation passes.+ #' @export |
||
153 | +18 |
- #' @keywords internal+ reporter_previewer_module <- function(label = "Report previewer", server_args = list()) { |
||
154 | -+ | |||
19 | +4x |
- check_modules_datanames <- function(modules, datanames) {+ checkmate::assert_string(label) |
||
155 | -16x | +20 | +2x |
- checkmate::assert_class(modules, "teal_modules")+ checkmate::assert_list(server_args, names = "named") |
156 | -16x | +21 | +2x |
- checkmate::assert_character(datanames)+ checkmate::assert_true(all(names(server_args) %in% names(formals(teal.reporter::reporter_previewer_srv)))) |
157 | +22 | |||
158 | -16x | +23 | +2x |
- recursive_check_datanames <- function(modules, datanames) {+ logger::log_info("Initializing reporter_previewer_module") |
159 | +24 |
- # check teal_modules against datanames+ |
||
160 | -34x | +25 | +2x |
- if (inherits(modules, "teal_modules")) {+ srv <- function(id, reporter, ...) { |
161 | -16x | +|||
26 | +! |
- sapply(modules$children, function(module) recursive_check_datanames(module, datanames = datanames))+ teal.reporter::reporter_previewer_srv(id, reporter, ...) |
||
162 | +27 |
- } else {+ }+ |
+ ||
28 | ++ | + | ||
163 | -18x | +29 | +2x |
- extra_datanames <- setdiff(modules$datanames, c("all", datanames))+ ui <- function(id, ...) { |
164 | -18x | +|||
30 | +! |
- if (length(extra_datanames)) {+ teal.reporter::reporter_previewer_ui(id, ...) |
||
165 | -2x | +|||
31 | +
- sprintf(+ } |
|||
166 | -2x | +|||
32 | +
- "- Module '%s' uses datanames not available in 'data': (%s) not in (%s)",+ |
|||
167 | +33 | 2x |
- modules$label,+ module <- module( |
|
168 | +34 | 2x |
- toString(dQuote(extra_datanames, q = FALSE)),+ label = "temporary label", |
|
169 | +35 | 2x |
- toString(dQuote(datanames, q = FALSE))+ server = srv, ui = ui, |
|
170 | -+ | |||
36 | +2x |
- )+ server_args = server_args, ui_args = list(), datanames = NULL |
||
171 | +37 |
- }+ ) |
||
172 | +38 |
- }+ # Module is created with a placeholder label and the label is changed later. |
||
173 | +39 |
- }+ # This is to prevent another module being labeled "Report previewer". |
||
174 | -16x | +40 | +2x |
- check_datanames <- unlist(recursive_check_datanames(modules, datanames))+ class(module) <- c("teal_module_previewer", class(module)) |
175 | -16x | +41 | +2x |
- if (length(check_datanames)) {+ module$label <- label |
176 | +42 | 2x |
- paste(check_datanames, collapse = "\n")+ module |
|
177 | +43 |
- } else {+ } |
||
178 | -14x | +
1 | +
- TRUE+ #' Store teal_slices object to a file |
|||
179 | +2 |
- }+ #' |
||
180 | +3 |
- }+ #' This function takes a `teal_slices` object and saves it to a file in `JSON` format. |
||
181 | +4 |
-
+ #' The `teal_slices` object contains information about filter states and can be used to |
||
182 | +5 |
- #' Check `datanames` in filters+ #' create, modify, and delete filter states. The saved file can be later loaded using |
||
183 | +6 |
- #'+ #' the `slices_restore` function. |
||
184 | +7 |
- #' This function checks whether `datanames` in filters correspond to those in `data`,+ #' |
||
185 | +8 |
- #' returning character vector with error messages or TRUE if all checks pass.+ #' @param tss (`teal_slices`) object to be stored. |
||
186 | +9 |
- #'+ #' @param file (`character(1)`) The file path where `teal_slices` object will be saved. |
||
187 | +10 |
- #' @param filters (`teal_slices`) object+ #' The file extension should be `".json"`. |
||
188 | +11 |
- #' @param datanames (`character`) names of datasets available in the `data` object+ #' |
||
189 | +12 |
- #'+ #' @details `Date` class is stored in `"ISO8601"` format (`YYYY-MM-DD`). `POSIX*t` classes are converted to a |
||
190 | +13 |
- #' @return A `character(1)` containing error message or TRUE if validation passes.+ #' character by using `format.POSIX*t(usetz = TRUE, tz = "UTC")` (`YYYY-MM-DD {N}{N}:{N}{N}:{N}{N} UTC`, where |
||
191 | +14 |
- #' @keywords internal+ #' `{N} = [0-9]` is a number and `UTC` is `Coordinated Universal Time` timezone short-code). |
||
192 | +15 |
- check_filter_datanames <- function(filters, datanames) {+ #' This format is assumed during `slices_restore`. All `POSIX*t` objects in `selected` or `choices` fields of |
||
193 | -14x | +|||
16 | +
- checkmate::assert_class(filters, "teal_slices")+ #' `teal_slice` objects are always printed in `UTC` timezone as well. |
|||
194 | -14x | +|||
17 | +
- checkmate::assert_character(datanames)+ #' |
|||
195 | +18 |
-
+ #' @return `NULL`, invisibly. |
||
196 | +19 |
- # check teal_slices against datanames+ #' |
||
197 | -14x | +|||
20 | +
- out <- unlist(sapply(+ #' @keywords internal |
|||
198 | -14x | +|||
21 | +
- filters, function(filter) {+ #' |
|||
199 | -3x | +|||
22 | +
- dataname <- shiny::isolate(filter$dataname)+ #' @examples |
|||
200 | -3x | +|||
23 | +
- if (!dataname %in% datanames) {+ #' # Create a teal_slices object |
|||
201 | -2x | +|||
24 | +
- sprintf(+ #' tss <- teal_slices( |
|||
202 | -2x | +|||
25 | +
- "- Filter '%s' refers to dataname not available in 'data':\n %s not in (%s)",+ #' teal_slice(dataname = "data", varname = "var"), |
|||
203 | -2x | +|||
26 | +
- shiny::isolate(filter$id),+ #' teal_slice(dataname = "data", expr = "x > 0", id = "positive_x", title = "Positive x") |
|||
204 | -2x | +|||
27 | +
- dQuote(dataname, q = FALSE),+ #' ) |
|||
205 | -2x | +|||
28 | +
- toString(dQuote(datanames, q = FALSE))+ #' |
|||
206 | +29 |
- )+ #' if (interactive()) { |
||
207 | +30 |
- }+ #' # Store the teal_slices object to a file |
||
208 | +31 |
- }+ #' slices_store(tss, "path/to/file.json") |
||
209 | +32 |
- ))+ #' } |
||
210 | +33 |
-
+ #' |
||
211 | +34 |
-
+ slices_store <- function(tss, file) { |
||
212 | -14x | +35 | +9x |
- if (length(out)) {+ checkmate::assert_class(tss, "teal_slices") |
213 | -2x | +36 | +9x |
- paste(out, collapse = "\n")+ checkmate::assert_path_for_output(file, overwrite = TRUE, extension = "json") |
214 | +37 |
- } else {+ |
||
215 | -12x | +38 | +9x |
- TRUE+ cat(format(tss, trim_lines = FALSE), "\n", file = file) |
216 | +39 |
- }+ } |
||
217 | +40 |
- }+ |
1 | +41 |
- #' @title `TealReportCard`+ #' Restore teal_slices object from a file |
||
2 | +42 |
- #' @description `r lifecycle::badge("experimental")`+ #' |
||
3 | +43 |
- #' A child of [`ReportCard`] that is used for teal specific applications.+ #' This function takes a file path to a `JSON` file containing a `teal_slices` object |
||
4 | +44 |
- #' In addition to the parent methods, it supports rendering teal specific elements such as+ #' and restores it to its original form. The restored `teal_slices` object can be used |
||
5 | +45 |
- #' the source code, the encodings panel content and the filter panel content as part of the+ #' to access filter states and their corresponding attributes. |
||
6 | +46 |
- #' meta data.+ #' |
||
7 | +47 |
- #' @export+ #' @param file Path to file where `teal_slices` is stored. Must have a `.json` extension and read access. |
||
8 | +48 |
#' |
||
9 | +49 |
- TealReportCard <- R6::R6Class( # nolint: object_name_linter.+ #' @return A `teal_slices` object restored from the file. |
||
10 | +50 |
- classname = "TealReportCard",+ #' |
||
11 | +51 |
- inherit = teal.reporter::ReportCard,+ #' @keywords internal |
||
12 | +52 |
- public = list(+ #' |
||
13 | +53 |
- #' @description Appends the source code to the `content` meta data of this `TealReportCard`.+ #' @examples |
||
14 | +54 |
- #'+ #' if (interactive()) { |
||
15 | +55 |
- #' @param src (`character(1)`) code as text.+ #' # Restore a teal_slices object from a file |
||
16 | +56 |
- #' @param ... any `rmarkdown` R chunk parameter and its value.+ #' tss_restored <- slices_restore("path/to/file.json") |
||
17 | +57 |
- #' But `eval` parameter is always set to `FALSE`.+ #' } |
||
18 | +58 |
- #' @return invisibly self+ #' |
||
19 | +59 |
- #' @examples+ slices_restore <- function(file) { |
||
20 | -+ | |||
60 | +9x |
- #' card <- TealReportCard$new()$append_src(+ checkmate::assert_file_exists(file, access = "r", extension = "json") |
||
21 | +61 |
- #' "plot(iris)"+ |
||
22 | -+ | |||
62 | +9x |
- #' )+ tss_json <- jsonlite::fromJSON(file, simplifyDataFrame = FALSE) |
||
23 | -+ | |||
63 | +9x |
- #' card$get_content()[[1]]$get_content()+ tss_json$slices <- |
||
24 | -+ | |||
64 | +9x |
- append_src = function(src, ...) {+ lapply(tss_json$slices, function(slice) { |
||
25 | -4x | +65 | +9x |
- checkmate::assert_character(src, min.len = 0, max.len = 1)+ for (field in c("selected", "choices")) { |
26 | -4x | +66 | +18x |
- params <- list(...)+ if (!is.null(slice[[field]])) { |
27 | -4x | +67 | +12x |
- params$eval <- FALSE+ if (length(slice[[field]]) > 0) { |
28 | -4x | +68 | +9x |
- rblock <- RcodeBlock$new(src)+ date_partial_regex <- "^[0-9]{4}-[0-9]{2}-[0-9]{2}" |
29 | -4x | +69 | +9x |
- rblock$set_params(params)+ time_stamp_regex <- paste0(date_partial_regex, "\\s[0-9]{2}:[0-9]{2}:[0-9]{2}\\sUTC$")+ |
+
70 | ++ | + | ||
30 | -4x | +71 | +9x |
- self$append_content(rblock)+ slice[[field]] <- |
31 | -4x | +72 | +9x |
- self$append_metadata("SRC", src)+ if (all(grepl(paste0(date_partial_regex, "$"), slice[[field]]))) { |
32 | -4x | +73 | +3x |
- invisible(self)+ as.Date(slice[[field]]) |
33 | -+ | |||
74 | +9x |
- },+ } else if (all(grepl(time_stamp_regex, slice[[field]]))) {+ |
+ ||
75 | +3x | +
+ as.POSIXct(slice[[field]], tz = "UTC") |
||
34 | +76 |
- #' @description Appends the filter state list to the `content` and `metadata` of this `TealReportCard`.+ } else {+ |
+ ||
77 | +3x | +
+ slice[[field]] |
||
35 | +78 |
- #' If the filter state list has an attribute named `formatted`, it appends it to the card otherwise it uses+ } |
||
36 | +79 |
- #' the default `yaml::as.yaml` to format the list.+ } else {+ |
+ ||
80 | +3x | +
+ slice[[field]] <- character(0) |
||
37 | +81 |
- #' If the filter state list is empty, nothing is appended to the `content`.+ } |
||
38 | +82 |
- #'+ } |
||
39 | +83 |
- #' @param fs (`teal_slices`) object returned from [teal_slices()] function.+ } |
||
40 | -+ | |||
84 | +9x |
- #' @return invisibly self+ slice |
||
41 | +85 |
- append_fs = function(fs) {+ }) |
||
42 | -5x | +|||
86 | +
- checkmate::assert_class(fs, "teal_slices")+ |
|||
43 | -4x | +87 | +9x |
- self$append_text("Filter State", "header3")+ tss_elements <- lapply(tss_json$slices, as.teal_slice) |
44 | -4x | +|||
88 | +
- self$append_content(TealSlicesBlock$new(fs))+ |
|||
45 | -4x | +89 | +9x |
- invisible(self)+ do.call(teal_slices, c(tss_elements, tss_json$attributes)) |
46 | +90 |
- },+ } |
47 | +1 |
- #' @description Appends the encodings list to the `content` and `metadata` of this `TealReportCard`.+ #' Add right filter panel into each of the top-level `teal_modules` UIs. |
||
48 | +2 |
- #'+ #' |
||
49 | +3 |
- #' @param encodings (`list`) list of encodings selections of the teal app.+ #' The [ui_nested_tabs] function returns a nested tabbed UI corresponding |
||
50 | +4 |
- #' @return invisibly self+ #' to the nested modules. |
||
51 | +5 |
- #' @examples+ #' This function adds the right filter panel to each main tab. |
||
52 | +6 |
- #' card <- TealReportCard$new()$append_encodings(list(variable1 = "X"))+ #' |
||
53 | +7 |
- #' card$get_content()[[1]]$get_content()+ #' The right filter panel's filter choices affect the `datasets` object. Therefore, |
||
54 | +8 |
- #'+ #' all modules using the same `datasets` share the same filters. |
||
55 | +9 |
- append_encodings = function(encodings) {- |
- ||
56 | -4x | -
- checkmate::assert_list(encodings)- |
- ||
57 | -4x | -
- self$append_text("Selected Options", "header3")- |
- ||
58 | -4x | -
- if (requireNamespace("yaml", quietly = TRUE)) {+ #' |
||
59 | -4x | +|||
10 | +
- self$append_text(yaml::as.yaml(encodings, handlers = list(+ #' This works with nested modules of depth greater than 2, though the filter |
|||
60 | -4x | +|||
11 | +
- POSIXct = function(x) format(x, "%Y-%m-%d"),+ #' panel is inserted at the right of the modules at depth 1 and not at the leaves. |
|||
61 | -4x | +|||
12 | +
- POSIXlt = function(x) format(x, "%Y-%m-%d"),+ #' |
|||
62 | -4x | +|||
13 | +
- Date = function(x) format(x, "%Y-%m-%d")+ #' @name module_tabs_with_filters |
|||
63 | -4x | +|||
14 | +
- )), "verbatim")+ #' |
|||
64 | +15 |
- } else {+ #' @inheritParams module_teal |
||
65 | -! | +|||
16 | +
- stop("yaml package is required to format the encodings list")+ #' |
|||
66 | +17 |
- }+ #' @param datasets (`named list` of `FilteredData`)\cr |
||
67 | -4x | +|||
18 | +
- self$append_metadata("Encodings", encodings)+ #' object to store filter state and filtered datasets, shared across modules. For more |
|||
68 | -4x | +|||
19 | +
- invisible(self)+ #' details see [`teal.slice::FilteredData`]. Structure of the list must be the same as structure |
|||
69 | +20 |
- }+ #' of the `modules` argument and list names must correspond to the labels in `modules`. |
||
70 | +21 |
- ),+ #' When filter is not module-specific then list contains the same object in all elements. |
||
71 | +22 |
- private = list()+ #' @param reporter (`Reporter`) object from `teal.reporter` |
||
72 | +23 |
- )+ #' |
||
73 | +24 |
-
+ #' @return A `tagList` of The main menu, place holders for filters and |
||
74 | +25 |
- #' @title `RcodeBlock`+ #' place holders for the teal modules |
||
75 | +26 |
- #' @keywords internal+ #' |
||
76 | +27 |
- TealSlicesBlock <- R6::R6Class( # nolint: object_name_linter.+ #' |
||
77 | +28 |
- classname = "TealSlicesBlock",+ #' @keywords internal |
||
78 | +29 |
- inherit = teal.reporter:::TextBlock,+ #' |
||
79 | +30 |
- public = list(+ #' @examples |
||
80 | +31 |
- #' @description Returns a `TealSlicesBlock` object.+ #' |
||
81 | +32 |
- #'+ #' mods <- teal:::example_modules() |
||
82 | +33 |
- #' @details Returns a `TealSlicesBlock` object with no content and no parameters.+ #' datasets <- teal:::example_datasets() |
||
83 | +34 |
- #'+ #' |
||
84 | +35 |
- #' @param content (`teal_slices`) object returned from [teal_slices()] function.+ #' app <- shinyApp( |
||
85 | +36 |
- #' @param style (`character(1)`) string specifying style to apply.+ #' ui = function() { |
||
86 | +37 |
- #'+ #' tagList( |
||
87 | +38 |
- #' @return `TealSlicesBlock`+ #' teal:::include_teal_css_js(), |
||
88 | +39 |
- #' @examples+ #' textOutput("info"), |
||
89 | +40 |
- #' block <- teal:::TealSlicesBlock$new()+ #' fluidPage( # needed for nice tabs |
||
90 | +41 |
- #'+ #' ui_tabs_with_filters("dummy", modules = mods, datasets = datasets) |
||
91 | +42 |
- initialize = function(content = teal_slices(), style = "verbatim") {+ #' ) |
||
92 | -10x | +|||
43 | +
- self$set_content(content)+ #' ) |
|||
93 | -9x | +|||
44 | +
- self$set_style(style)+ #' }, |
|||
94 | -9x | +|||
45 | +
- invisible(self)+ #' server = function(input, output, session) { |
|||
95 | +46 |
- },+ #' output$info <- renderText({ |
||
96 | +47 |
-
+ #' paste0("The currently active tab name is ", active_module()$label) |
||
97 | +48 |
- #' @description Sets content of this `TealSlicesBlock`.+ #' }) |
||
98 | +49 |
- #' Sets content as `YAML` text which represents a list generated from `teal_slices`.+ #' active_module <- srv_tabs_with_filters(id = "dummy", datasets = datasets, modules = mods) |
||
99 | +50 |
- #' The list displays limited number of fields from `teal_slice` objects, but this list is+ #' } |
||
100 | +51 |
- #' sufficient to conclude which filters were applied.+ #' ) |
||
101 | +52 |
- #' When `selected` field in `teal_slice` object is a range, then it is displayed as a "min"+ #' if (interactive()) { |
||
102 | +53 |
- #'+ #' shinyApp(app$ui, app$server) |
||
103 | +54 |
- #'+ #' } |
||
104 | +55 |
- #' @param content (`teal_slices`) object returned from [teal_slices()] function.+ #' |
||
105 | +56 |
- #' @return invisibly self+ NULL |
||
106 | +57 |
- set_content = function(content) {+ |
||
107 | -11x | +|||
58 | +
- checkmate::assert_class(content, "teal_slices")+ #' @rdname module_tabs_with_filters |
|||
108 | -10x | +|||
59 | +
- if (length(content) != 0) {+ ui_tabs_with_filters <- function(id, modules, datasets, filter = teal_slices()) { |
|||
109 | -7x | +|||
60 | +! |
- states_list <- lapply(content, function(x) {+ checkmate::assert_class(modules, "teal_modules") |
||
110 | -7x | +|||
61 | +! |
- x_list <- shiny::isolate(as.list(x))+ checkmate::assert_list(datasets, types = c("list", "FilteredData")) |
||
111 | -7x | +|||
62 | +! |
- if (+ checkmate::assert_class(filter, "teal_slices") |
||
112 | -7x | +|||
63 | +
- inherits(x_list$choices, c("integer", "numeric", "Date", "POSIXct", "POSIXlt")) &&+ |
|||
113 | -7x | +|||
64 | +! |
- length(x_list$choices) == 2 &&+ ns <- NS(id) |
||
114 | -7x | +|||
65 | +! |
- length(x_list$selected) == 2+ is_module_specific <- isTRUE(attr(filter, "module_specific")) |
||
115 | +66 |
- ) {+ |
||
116 | +67 | ! |
- x_list$range <- paste(x_list$selected, collapse = " - ")+ teal_ui <- ui_nested_tabs(ns("root"), modules = modules, datasets, is_module_specific = is_module_specific) |
|
117 | +68 | ! |
- x_list["selected"] <- NULL+ filter_panel_btns <- tags$li( |
|
118 | -+ | |||
69 | +! |
- }+ class = "flex-grow", |
||
119 | -7x | +|||
70 | +! |
- if (!is.null(x_list$arg)) {+ tags$button( |
||
120 | +71 | ! |
- x_list$arg <- if (x_list$arg == "subset") "Genes" else "Samples"+ class = "btn action-button filter_hamburger", # see sidebar.css for style filter_hamburger |
|
121 | -+ | |||
72 | +! |
- }+ href = "javascript:void(0)", |
||
122 | -+ | |||
73 | +! |
-
+ onclick = "toggleFilterPanel();", # see sidebar.js |
||
123 | -7x | +|||
74 | +! |
- x_list <- x_list[+ title = "Toggle filter panels", |
||
124 | -7x | +|||
75 | +! |
- c("dataname", "varname", "experiment", "arg", "expr", "selected", "range", "keep_na", "keep_inf")+ icon("fas fa-bars") |
||
125 | +76 |
- ]- |
- ||
126 | -7x | -
- names(x_list) <- c(+ ), |
||
127 | -7x | +|||
77 | +! |
- "Dataset name", "Variable name", "Experiment", "Filtering by", "Applied expression",+ filter_manager_modal_ui(ns("filter_manager")) |
||
128 | -7x | +|||
78 | +
- "Selected Values", "Selected range", "Include NA values", "Include Inf values"+ ) |
|||
129 | -+ | |||
79 | +! |
- )+ teal_ui$children[[1]] <- tagAppendChild(teal_ui$children[[1]], filter_panel_btns) |
||
130 | +80 | |||
131 | -7x | +|||
81 | +! |
- Filter(Negate(is.null), x_list)+ if (!is_module_specific) { |
||
132 | +82 |
- })+ # need to rearrange html so that filter panel is within tabset |
||
133 | -+ | |||
83 | +! |
-
+ tabset_bar <- teal_ui$children[[1]] |
||
134 | -7x | +|||
84 | +! |
- if (requireNamespace("yaml", quietly = TRUE)) {+ teal_modules <- teal_ui$children[[2]] |
||
135 | -7x | +|||
85 | +! |
- super$set_content(yaml::as.yaml(states_list))+ filter_ui <- unlist(datasets)[[1]]$ui_filter_panel(ns("filter_panel")) |
||
136 | -+ | |||
86 | +! |
- } else {+ list( |
||
137 | +87 | ! |
- stop("yaml package is required to format the filter state list")+ tabset_bar, |
|
138 | -+ | |||
88 | +! |
- }+ tags$hr(class = "my-2"), |
||
139 | -+ | |||
89 | +! |
- }+ fluidRow( |
||
140 | -10x | +|||
90 | +! |
- private$teal_slices <- content+ column(width = 9, teal_modules, class = "teal_primary_col"), |
||
141 | -10x | +|||
91 | +! |
- invisible(self)+ column(width = 3, filter_ui, class = "teal_secondary_col") |
||
142 | +92 |
- },+ ) |
||
143 | +93 |
- #' @description Create the `RcodeBlock` from a list.+ ) |
||
144 | +94 |
- #' @param x `named list` with two fields `c("text", "params")`.+ } else { |
||
145 | -+ | |||
95 | +! |
- #' Use the `get_available_params` method to get all possible parameters.+ teal_ui |
||
146 | +96 |
- #' @return invisibly self+ } |
||
147 | +97 |
- from_list = function(x) {+ } |
||
148 | -1x | +|||
98 | +
- checkmate::assert_list(x)+ |
|||
149 | -1x | +|||
99 | +
- checkmate::assert_names(names(x), must.include = c("teal_slices"))+ #' @rdname module_tabs_with_filters |
|||
150 | -1x | +|||
100 | +
- self$set_content(x$teal_slices)+ srv_tabs_with_filters <- function(id, |
|||
151 | -1x | +|||
101 | +
- invisible(self)+ datasets, |
|||
152 | +102 |
- },+ modules, |
||
153 | +103 |
- #' @description Convert the `RcodeBlock` to a list.+ reporter = teal.reporter::Reporter$new(), |
||
154 | +104 |
- #' @return `named list` with a text and `params`.+ filter = teal_slices()) { |
||
155 | -+ | |||
105 | +5x |
-
+ checkmate::assert_class(modules, "teal_modules") |
||
156 | -+ | |||
106 | +5x |
- to_list = function() {+ checkmate::assert_list(datasets, types = c("list", "FilteredData")) |
||
157 | -2x | +107 | +5x |
- list(teal_slices = private$teal_slices)+ checkmate::assert_class(reporter, "Reporter") |
158 | -+ | |||
108 | +3x |
- }+ checkmate::assert_class(filter, "teal_slices") |
||
159 | +109 |
- ),+ |
||
160 | -+ | |||
110 | +3x |
- private = list(+ moduleServer(id, function(input, output, session) { |
||
161 | -+ | |||
111 | +3x |
- style = "verbatim",+ logger::log_trace("srv_tabs_with_filters initializing the module.") |
||
162 | +112 |
- teal_slices = NULL # teal_slices+ |
||
163 | -+ | |||
113 | +3x |
- )+ is_module_specific <- isTRUE(attr(filter, "module_specific")) |
||
164 | -+ | |||
114 | +3x |
- )+ manager_out <- filter_manager_modal_srv("filter_manager", filtered_data_list = datasets, filter = filter) |
1 | +115 |
- #' Filter manager modal+ |
|
2 | -+ | ||
116 | +3x |
- #'+ active_module <- srv_nested_tabs( |
|
3 | -+ | ||
117 | +3x |
- #' Opens modal containing the filter manager UI.+ id = "root", |
|
4 | -+ | ||
118 | +3x |
- #'+ datasets = datasets, |
|
5 | -+ | ||
119 | +3x |
- #' @name module_filter_manager_modal+ modules = modules, |
|
6 | -+ | ||
120 | +3x |
- #' @inheritParams filter_manager_srv+ reporter = reporter, |
|
7 | -+ | ||
121 | +3x |
- #' @examples+ is_module_specific = is_module_specific |
|
8 | +122 |
- #' fd1 <- teal.slice::init_filtered_data(list(iris = list(dataset = iris)))+ ) |
|
9 | +123 |
- #' fd2 <- teal.slice::init_filtered_data(+ |
|
10 | -+ | ||
124 | +3x |
- #' list(iris = list(dataset = iris), mtcars = list(dataset = mtcars))+ if (!is_module_specific) { |
|
11 | -+ | ||
125 | +3x |
- #' )+ active_datanames <- reactive({ |
|
12 | -+ | ||
126 | +6x |
- #' fd3 <- teal.slice::init_filtered_data(+ if (identical(active_module()$datanames, "all")) { |
|
13 | -+ | ||
127 | +! |
- #' list(iris = list(dataset = iris), women = list(dataset = women))+ singleton$datanames() |
|
14 | +128 |
- #' )+ } else { |
|
15 | -+ | ||
129 | +5x |
- #' filter <- teal_slices(+ active_module()$datanames |
|
16 | +130 |
- #' teal.slice::teal_slice(dataname = "iris", varname = "Sepal.Length"),+ } |
|
17 | +131 |
- #' teal.slice::teal_slice(dataname = "iris", varname = "Species"),+ }) |
|
18 | -+ | ||
132 | +3x |
- #' teal.slice::teal_slice(dataname = "mtcars", varname = "mpg"),+ singleton <- unlist(datasets)[[1]] |
|
19 | -+ | ||
133 | +3x |
- #' teal.slice::teal_slice(dataname = "women", varname = "height"),+ singleton$srv_filter_panel("filter_panel", active_datanames = active_datanames) |
|
20 | +134 |
- #' mapping = list(+ |
|
21 | -+ | ||
135 | +3x |
- #' module2 = c("mtcars mpg"),+ observeEvent( |
|
22 | -+ | ||
136 | +3x |
- #' module3 = c("women height"),+ eventExpr = active_datanames(), |
|
23 | -+ | ||
137 | +3x |
- #' global_filters = "iris Species"+ handlerExpr = { |
|
24 | -+ | ||
138 | +4x |
- #' )+ script <- if (length(active_datanames()) == 0 || is.null(active_datanames())) { |
|
25 | +139 |
- #' )+ # hide the filter panel and disable the burger button |
|
26 | -+ | ||
140 | +! |
- #'+ "handleNoActiveDatasets();" |
|
27 | +141 |
- #' app <- shinyApp(+ } else { |
|
28 | +142 |
- #' ui = fluidPage(+ # show the filter panel and enable the burger button |
|
29 | -+ | ||
143 | +4x |
- #' teal:::filter_manager_modal_ui("manager")+ "handleActiveDatasetsPresent();" |
|
30 | +144 |
- #' ),+ } |
|
31 | -+ | ||
145 | +4x |
- #' server = function(input, output, session) {+ shinyjs::runjs(script) |
|
32 | +146 |
- #' teal:::filter_manager_modal_srv(+ }, |
|
33 | -+ | ||
147 | +3x |
- #' "manager",+ ignoreNULL = FALSE |
|
34 | +148 |
- #' filtered_data_list = list(module1 = fd1, module2 = fd2, module3 = fd3),+ ) |
|
35 | +149 |
- #' filter = filter+ } |
|
36 | +150 |
- #' )+ |
|
37 | -+ | ||
151 | +3x |
- #' }+ showNotification("Data loaded - App fully started up") |
|
38 | -+ | ||
152 | +3x |
- #' )+ logger::log_trace("srv_tabs_with_filters initialized the module") |
|
39 | -+ | ||
153 | +3x |
- #' if (interactive()) {+ return(active_module) |
|
40 | +154 |
- #' shinyApp(app$ui, app$server)+ }) |
|
41 | +155 |
- #' }+ } |
42 | +1 |
- #'+ # This file adds a splash screen for delayed data loading on top of teal |
||
43 | +2 |
- #' @keywords internal+ |
||
44 | +3 |
- #'+ #' UI to show a splash screen in the beginning, then delegate to [srv_teal()] |
||
45 | +4 |
- NULL+ #' |
||
46 | +5 |
-
+ #' @description `r lifecycle::badge("stable")` |
||
47 | +6 |
- #' @rdname module_filter_manager_modal+ #' The splash screen could be used to query for a password to fetch the data. |
||
48 | +7 |
- filter_manager_modal_ui <- function(id) {+ #' [init()] is a very thin wrapper around this module useful for end-users which |
||
49 | -! | +|||
8 | +
- ns <- NS(id)+ #' assumes that it is a top-level module and cannot be embedded. |
|||
50 | -! | +|||
9 | +
- tags$button(+ #' This function instead adheres to the Shiny module conventions. |
|||
51 | -! | +|||
10 | +
- id = ns("show"),+ #' |
|||
52 | -! | +|||
11 | +
- class = "btn action-button filter_manager_button",+ #' If data is obtained through delayed loading, its splash screen is used. Otherwise, |
|||
53 | -! | +|||
12 | +
- title = "Show filters manager modal",+ #' a default splash screen is shown. |
|||
54 | -! | +|||
13 | +
- icon("gear")+ #' |
|||
55 | +14 |
- )+ #' Please also refer to the doc of [init()]. |
||
56 | +15 |
- }+ #' |
||
57 | +16 |
-
+ #' @param id (`character(1)`)\cr |
||
58 | +17 |
- #' @rdname module_filter_manager_modal+ #' module id |
||
59 | +18 |
- filter_manager_modal_srv <- function(id, filtered_data_list, filter) {+ #' @inheritParams init |
||
60 | -3x | +|||
19 | +
- moduleServer(id, function(input, output, session) {+ #' @export |
|||
61 | -3x | +|||
20 | +
- observeEvent(input$show, {+ ui_teal_with_splash <- function(id, |
|||
62 | -! | +|||
21 | +
- logger::log_trace("filter_manager_modal_srv@1 show button has been clicked.")+ data, |
|||
63 | -! | +|||
22 | +
- showModal(+ title, |
|||
64 | -! | +|||
23 | +
- modalDialog(+ header = tags$p("Add Title Here"), |
|||
65 | -! | +|||
24 | +
- filter_manager_ui(session$ns("filter_manager")),+ footer = tags$p("Add Footer Here")) { |
|||
66 | -! | +|||
25 | +12x |
- size = "l",+ checkmate::assert_multi_class(data, c("teal_data", "teal_data_module")) |
||
67 | -! | +|||
26 | +12x |
- footer = NULL,+ ns <- NS(id) |
||
68 | -! | +|||
27 | +
- easyClose = TRUE+ |
|||
69 | +28 |
- )+ # Startup splash screen for delayed loading |
||
70 | +29 |
- )+ # We use delayed loading in all cases, even when the data does not need to be fetched. |
||
71 | +30 |
- })+ # This has the benefit that when filtering the data takes a lot of time initially, the |
||
72 | +31 |
-
+ # Shiny app does not time out. |
||
73 | -3x | +32 | +12x |
- filter_manager_srv("filter_manager", filtered_data_list, filter)+ splash_ui <- if (inherits(data, "teal_data_module")) { |
74 | -+ | |||
33 | +1x |
- })+ data$ui(ns("teal_data_module")) |
||
75 | -+ | |||
34 | +12x |
- }+ } else if (inherits(data, "teal_data")) { |
||
76 | -+ | |||
35 | +11x |
-
+ div() |
||
77 | +36 |
- #' @rdname module_filter_manager+ } |
||
78 | -+ | |||
37 | +12x |
- filter_manager_ui <- function(id) {+ ui_teal( |
||
79 | -! | +|||
38 | +12x |
- ns <- NS(id)+ id = ns("teal"), |
||
80 | -! | +|||
39 | +12x |
- div(+ splash_ui = div(splash_ui, uiOutput(ns("error"))), |
||
81 | -! | +|||
40 | +12x |
- class = "filter_manager_content",+ title = title, |
||
82 | -! | +|||
41 | +12x |
- tableOutput(ns("slices_table")),+ header = header, |
||
83 | -! | +|||
42 | +12x |
- snapshot_manager_ui(ns("snapshot_manager"))+ footer = footer |
||
84 | +43 |
) |
||
85 | +44 |
} |
||
86 | +45 | |||
87 | +46 |
- #' Manage multiple `FilteredData` objects+ #' Server function that loads the data through reactive loading and then delegates |
||
88 | +47 |
- #'+ #' to [srv_teal()]. |
||
89 | +48 |
- #' Oversee filter states in the whole application.+ #' |
||
90 | +49 |
- #'+ #' @description `r lifecycle::badge("stable")` |
||
91 | +50 |
- #' @rdname module_filter_manager+ #' Please also refer to the doc of [init()]. |
||
92 | +51 |
- #' @details+ #' |
||
93 | +52 |
- #' This module observes the changes of the filters in each `FilteredData` object+ #' @inheritParams init |
||
94 | +53 |
- #' and keeps track of all filters used. A mapping of filters to modules+ #' @param modules `teal_modules` object containing the output modules which |
||
95 | +54 |
- #' is kept in the `mapping_matrix` object (which is actually a `data.frame`)+ #' will be displayed in the teal application. See [modules()] and [module()] for |
||
96 | +55 |
- #' that tracks which filters (rows) are active in which modules (columns).+ #' more details. |
||
97 | +56 |
- #'+ #' @inheritParams shiny::moduleServer |
||
98 | +57 |
- #' @param id (`character(1)`)\cr+ #' @return `reactive` containing `teal_data` object when data is loaded. |
||
99 | +58 |
- #' `shiny` module id.+ #' If data is not loaded yet, `reactive` returns `NULL`. |
||
100 | +59 |
- #' @param filtered_data_list (`named list`)\cr+ #' @export |
||
101 | +60 |
- #' A list, possibly nested, of `FilteredData` objects.+ srv_teal_with_splash <- function(id, data, modules, filter = teal_slices()) { |
||
102 | -+ | |||
61 | +15x |
- #' Each `FilteredData` will be served to one module in the `teal` application.+ checkmate::check_multi_class(data, c("teal_data", "teal_data_module")) |
||
103 | +62 |
- #' The structure of the list must reflect the nesting of modules in tabs+ |
||
104 | -+ | |||
63 | +15x |
- #' and names of the list must be the same as labels of their respective modules.+ moduleServer(id, function(input, output, session) {+ |
+ ||
64 | +15x | +
+ logger::log_trace("srv_teal_with_splash initializing module with data.") |
||
105 | +65 |
- #' @inheritParams init+ + |
+ ||
66 | +15x | +
+ if (getOption("teal.show_js_log", default = FALSE)) {+ |
+ ||
67 | +! | +
+ shinyjs::showLog() |
||
106 | +68 |
- #' @return A list of `reactive`s, each holding a `teal_slices`, as returned by `filter_manager_module_srv`.+ } |
||
107 | +69 |
- #' @keywords internal+ |
||
108 | +70 |
- #'+ # teal_data_rv contains teal_data object |
||
109 | +71 |
- filter_manager_srv <- function(id, filtered_data_list, filter) {+ # either passed to teal::init or returned from teal_data_module |
||
110 | -5x | +72 | +15x |
- moduleServer(id, function(input, output, session) {+ teal_data_rv <- if (inherits(data, "teal_data_module")) { |
111 | -5x | +73 | +10x |
- logger::log_trace("filter_manager_srv initializing for: { paste(names(filtered_data_list), collapse = ', ')}.")+ data <- data$server(id = "teal_data_module") |
112 | -+ | |||
74 | +10x |
-
+ if (!is.reactive(data)) { |
||
113 | -5x | +75 | +1x |
- is_module_specific <- isTRUE(attr(filter, "module_specific"))+ stop("The `teal_data_module` must return a reactive expression.", call. = FALSE) |
114 | +76 |
-
+ } |
||
115 | -+ | |||
77 | +9x |
- # Create global list of slices.+ data+ |
+ ||
78 | +15x | +
+ } else if (inherits(data, "teal_data")) {+ |
+ ||
79 | +5x | +
+ reactiveVal(data) |
||
116 | +80 |
- # Contains all available teal_slice objects available to all modules.+ } |
||
117 | +81 |
- # Passed whole to instances of FilteredData used for individual modules.+ + |
+ ||
82 | +14x | +
+ teal_data_rv_validate <- reactive({ |
||
118 | +83 |
- # Down there a subset that pertains to the data sets used in that module is applied and displayed.+ # custom module can return error |
||
119 | -5x | +84 | +11x |
- slices_global <- reactiveVal(filter)+ data <- tryCatch(teal_data_rv(), error = function(e) e) |
120 | +85 | |||
121 | -5x | +|||
86 | +
- filtered_data_list <-+ # there is an empty reactive cycle on init! |
|||
122 | -5x | +87 | +11x |
- if (!is_module_specific) {+ if (inherits(data, "shiny.silent.error") && identical(data$message, "")) { |
123 | -+ | |||
88 | +! |
- # Retrieve the first FilteredData from potentially nested list.+ return(NULL) |
||
124 | +89 |
- # List of length one is named "global_filters" because that name is forbidden for a module label.+ } |
||
125 | -4x | +|||
90 | +
- list(global_filters = unlist(filtered_data_list)[[1]])+ |
|||
126 | +91 |
- } else {+ # to handle qenv.error |
||
127 | -+ | |||
92 | +11x | +
+ if (inherits(data, "qenv.error")) {+ |
+ ||
93 | +2x |
- # Flatten potentially nested list of FilteredData objects while maintaining useful names.+ validate( |
||
128 | -+ | |||
94 | +2x |
- # Simply using `unlist` would result in concatenated names.+ need( |
||
129 | -1x | +95 | +2x |
- flatten_nested <- function(x, name = NULL) {+ FALSE, |
130 | -5x | +96 | +2x |
- if (inherits(x, "FilteredData")) {+ paste( |
131 | -3x | +97 | +2x |
- setNames(list(x), name)+ "Error when executing `teal_data_module`:\n ", |
132 | -+ | |||
98 | +2x |
- } else {+ paste(data$message, collapse = "\n"), |
||
133 | +99 | 2x |
- unlist(lapply(names(x), function(name) flatten_nested(x[[name]], name)))+ "\n Check your inputs or contact app developer if error persists." |
|
134 | +100 |
- }+ ) |
||
135 | +101 |
- }+ ) |
||
136 | -1x | +|||
102 | +
- flatten_nested(filtered_data_list)+ ) |
|||
137 | +103 |
} |
||
138 | +104 | |||
139 | +105 |
- # Create mapping fo filters to modules in matrix form (presented as data.frame).+ # to handle module non-qenv errors |
||
140 | -+ | |||
106 | +9x |
- # Modules get NAs for filters that cannot be set for them.+ if (inherits(data, "error")) { |
||
141 | -5x | +107 | +1x |
- mapping_matrix <- reactive({+ validate( |
142 | -5x | +108 | +1x |
- state_ids_global <- vapply(slices_global(), `[[`, character(1L), "id")+ need( |
143 | -5x | +109 | +1x |
- mapping_smooth <- lapply(filtered_data_list, function(x) {+ FALSE, |
144 | -7x | +110 | +1x |
- state_ids_local <- vapply(x$get_filter_state(), `[[`, character(1L), "id")+ paste( |
145 | -7x | +111 | +1x |
- state_ids_allowed <- vapply(x$get_available_teal_slices()(), `[[`, character(1L), "id")+ "Error when executing `teal_data_module`:\n ", |
146 | -7x | +112 | +1x |
- states_active <- state_ids_global %in% state_ids_local+ paste(data$message, collpase = "\n"), |
147 | -7x | +113 | +1x |
- ifelse(state_ids_global %in% state_ids_allowed, states_active, NA)+ "\n Check your inputs or contact app developer if error persists." |
148 | +114 |
- })+ ) |
||
149 | +115 |
-
+ ) |
||
150 | -5x | +|||
116 | +
- as.data.frame(mapping_smooth, row.names = state_ids_global, check.names = FALSE)+ ) |
|||
151 | +117 |
- })+ } |
||
152 | +118 | |||
153 | -5x | +119 | +8x |
- output$slices_table <- renderTable(+ validate( |
154 | -5x | -
- expr = {- |
- ||
155 | -+ | 120 | +8x |
- # Display logical values as UTF characters.+ need( |
156 | -2x | +121 | +8x |
- mm <- mapping_matrix()+ inherits(data, "teal_data"), |
157 | -2x | +122 | +8x |
- mm[] <- lapply(mm, ifelse, yes = intToUtf8(9989), no = intToUtf8(10060))+ paste( |
158 | -2x | +123 | +8x |
- mm[] <- lapply(mm, function(x) ifelse(is.na(x), intToUtf8(128306), x))+ "Error: `teal_data_module` did not return `teal_data` object", |
159 | -2x | +124 | +8x |
- if (!is_module_specific) colnames(mm) <- "Global Filters"+ "\n Check your inputs or contact app developer if error persists" |
160 | +125 |
-
+ ) |
||
161 | +126 |
- # Display placeholder if no filters defined.- |
- ||
162 | -2x | -
- if (nrow(mm) == 0L) {- |
- ||
163 | -2x | -
- mm <- data.frame(`Filter manager` = "No filters specified.", check.names = FALSE)- |
- ||
164 | -2x | -
- rownames(mm) <- ""+ ) |
||
165 | +127 |
- }+ ) |
||
166 | +128 | |||
167 | -- |
- # Report Previewer will not be displayed.- |
- ||
168 | -2x | +129 | +5x |
- mm[names(mm) != "Report previewer"]+ validate(need(teal.data::datanames(data), "Data has no datanames. Contact app developer.")) |
169 | +130 |
- },+ |
||
170 | -5x | +|||
131 | +
- align = paste(c("l", rep("c", sum(names(filtered_data_list) != "Report previewer"))), collapse = ""),+ |
|||
171 | -5x | +132 | +4x |
- rownames = TRUE+ is_modules_ok <- check_modules_datanames(modules, teal.data::datanames(data)) |
172 | -+ | |||
133 | +4x |
- )+ validate(need(isTRUE(is_modules_ok), is_modules_ok)) |
||
173 | +134 | |||
174 | -+ | |||
135 | +3x |
- # Create list of module calls.+ is_filter_ok <- check_filter_datanames(filter, teal.data::datanames(data)) |
||
175 | -5x | +136 | +3x |
- modules_out <- lapply(names(filtered_data_list), function(module_name) {+ if (!isTRUE(is_filter_ok)) { |
176 | -7x | +137 | +1x |
- filter_manager_module_srv(+ showNotification( |
177 | -7x | +138 | +1x |
- id = module_name,+ "Some filters were not applied because of incompatibility with data. Contact app developer.", |
178 | -7x | +139 | +1x |
- module_fd = filtered_data_list[[module_name]],+ type = "warning", |
179 | -7x | +140 | +1x |
- slices_global = slices_global+ duration = 10 |
180 | +141 |
- )+ ) |
||
181 | -+ | |||
142 | +1x |
- })+ logger::log_warn(is_filter_ok) |
||
182 | +143 |
-
+ } |
||
183 | +144 |
- # Call snapshot manager.+ |
||
184 | -5x | +145 | +3x |
- snapshot_manager_srv("snapshot_manager", slices_global, mapping_matrix, filtered_data_list)+ teal_data_rv() |
185 | +146 | - - | -||
186 | -5x | -
- modules_out # returned for testing purpose+ }) |
||
187 | +147 |
- })+ |
||
188 | -+ | |||
148 | +14x |
- }+ output$error <- renderUI({ |
||
189 | -+ | |||
149 | +! |
-
+ teal_data_rv_validate() |
||
190 | -+ | |||
150 | +! |
- #' Module specific filter manager+ NULL |
||
191 | +151 |
- #'+ }) |
||
192 | +152 |
- #' Track filter states in single module.+ |
||
193 | +153 |
- #'+ |
||
194 | -+ | |||
154 | +14x |
- #' This module tracks the state of a single `FilteredData` object and global `teal_slices`+ res <- srv_teal(id = "teal", modules = modules, teal_data_rv = teal_data_rv_validate, filter = filter) |
||
195 | -+ | |||
155 | +14x |
- #' and updates both objects as necessary. Filter states added in different modules+ logger::log_trace("srv_teal_with_splash initialized module with data.") |
||
196 | -+ | |||
156 | +14x |
- #' Filter states added any individual module are added to global `teal_slices`+ return(res) |
||
197 | +157 |
- #' and from there become available in other modules+ }) |
||
198 | +158 |
- #' by setting `private$available_teal_slices` in each `FilteredData`.+ } |
199 | +1 |
- #'+ #' Create a `tdata` Object |
||
200 | +2 |
- #' @param id (`character(1)`)\cr+ #' |
||
201 | +3 |
- #' `shiny` module id.+ #' @description `r lifecycle::badge("deprecated")` |
||
202 | +4 |
- #' @param module_fd (`FilteredData`)\cr+ #' Create a new object called `tdata` which contains `data`, a `reactive` list of data.frames |
||
203 | +5 |
- #' object to filter data in the teal-module+ #' (or `MultiAssayExperiment`), with attributes: |
||
204 | +6 |
- #' @param slices_global (`reactiveVal`)\cr+ #' \itemize{ |
||
205 | +7 |
- #' stores `teal_slices` with all available filters; allows the following actions:+ #' \item{`code` (`reactive`) containing code used to generate the data} |
||
206 | +8 |
- #' - to disable/enable a specific filter in a module+ #' \item{join_keys (`join_keys`) containing the relationships between the data} |
||
207 | +9 |
- #' - to restore saved filter settings+ #' \item{metadata (`named list`) containing any metadata associated with the data frames} |
||
208 | +10 |
- #' - to save current filter panel settings+ #' } |
||
209 | +11 |
- #' @return A `reactive` expression containing the slices active in this module.+ #' @name tdata |
||
210 | +12 |
- #' @keywords internal+ #' @param data A `named list` of `data.frames` (or `MultiAssayExperiment`) |
||
211 | +13 |
- #'+ #' which optionally can be `reactive`. |
||
212 | +14 |
- filter_manager_module_srv <- function(id, module_fd, slices_global) {- |
- ||
213 | -7x | -
- moduleServer(id, function(input, output, session) {+ #' Inside this object all of these items will be made `reactive`. |
||
214 | +15 |
- # Only operate on slices that refer to data sets present in this module.- |
- ||
215 | -7x | -
- module_fd$set_available_teal_slices(reactive(slices_global()))+ #' @param code A `character` (or `reactive` which evaluates to a `character`) containing |
||
216 | +16 |
-
+ #' the code used to generate the data. This should be `reactive` if the code is changing |
||
217 | +17 |
- # Track filter state of this module.- |
- ||
218 | -7x | -
- slices_module <- reactive(module_fd$get_filter_state())+ #' during a reactive context (e.g. if filtering changes the code). Inside this |
||
219 | +18 |
-
+ #' object `code` will be made reactive |
||
220 | +19 |
- # Reactive values for comparing states.+ #' @param join_keys A `teal.data::join_keys` object containing relationships between the |
||
221 | -7x | +|||
20 | +
- previous_slices <- reactiveVal(isolate(slices_module()))+ #' datasets. |
|||
222 | -7x | +|||
21 | +
- slices_added <- reactiveVal(NULL)+ #' @param metadata A `named list` each element contains a list of metadata about the named data.frame |
|||
223 | +22 |
-
+ #' Each element of these list should be atomic and length one. |
||
224 | +23 |
- # Observe changes in module filter state and trigger appropriate actions.+ #' @return A `tdata` object |
||
225 | -7x | +|||
24 | +
- observeEvent(slices_module(), ignoreNULL = FALSE, {+ #' |
|||
226 | -2x | +|||
25 | +
- logger::log_trace("filter_manager_srv@1 detecting states deltas in module: { id }.")+ #' @seealso `as_tdata` |
|||
227 | -2x | +|||
26 | +
- added <- setdiff_teal_slices(slices_module(), slices_global())+ #' |
|||
228 | -! | +|||
27 | +
- if (length(added)) slices_added(added)+ #' @examples |
|||
229 | -2x | +|||
28 | +
- previous_slices(slices_module())+ #' |
|||
230 | +29 |
- })+ #' data <- new_tdata( |
||
231 | +30 |
-
+ #' data = list(iris = iris, mtcars = reactive(mtcars), dd = data.frame(x = 1:10)), |
||
232 | -7x | +|||
31 | +
- observeEvent(slices_added(), ignoreNULL = TRUE, {+ #' code = "iris <- iris |
|||
233 | -! | +|||
32 | +
- logger::log_trace("filter_manager_srv@2 added filter in module: { id }.")+ #' mtcars <- mtcars |
|||
234 | +33 |
- # In case the new state has the same id as an existing state, add a suffix to it.+ #' dd <- data.frame(x = 1:10)", |
||
235 | -! | +|||
34 | +
- global_ids <- vapply(slices_global(), `[[`, character(1L), "id")+ #' metadata = list(dd = list(author = "NEST"), iris = list(version = 1)) |
|||
236 | -! | +|||
35 | +
- lapply(+ #' ) |
|||
237 | -! | +|||
36 | +
- slices_added(),+ #' |
|||
238 | -! | +|||
37 | +
- function(slice) {+ #' # Extract a data.frame |
|||
239 | -! | +|||
38 | +
- if (slice$id %in% global_ids) {+ #' isolate(data[["iris"]]()) |
|||
240 | -! | +|||
39 | +
- slice$id <- utils::tail(make.unique(c(global_ids, slice$id), sep = "_"), 1)+ #' |
|||
241 | +40 |
- }+ #' # Get code |
||
242 | +41 |
- }+ #' isolate(get_code_tdata(data)) |
||
243 | +42 |
- )+ #' |
||
244 | -! | +|||
43 | +
- slices_global_new <- c(slices_global(), slices_added())+ #' # Get metadata |
|||
245 | -! | +|||
44 | +
- slices_global(slices_global_new)+ #' get_metadata(data, "iris") |
|||
246 | -! | +|||
45 | +
- slices_added(NULL)+ #' |
|||
247 | +46 |
- })+ #' @export |
||
248 | +47 |
-
+ new_tdata <- function(data, code = "", join_keys = NULL, metadata = NULL) { |
||
249 | -7x | +48 | +35x |
- slices_module # returned for testing purpose+ lifecycle::deprecate_soft( |
250 | -+ | |||
49 | +35x |
- })+ when = "0.99.0", |
||
251 | -+ | |||
50 | +35x |
- }+ what = "tdata()", |
1 | -+ | |||
51 | +35x |
- #' Add right filter panel into each of the top-level `teal_modules` UIs.+ details = paste( |
||
2 | -+ | |||
52 | +35x |
- #'+ "tdata is deprecated and will be removed in the next release. Use `teal_data` instead.\n", |
||
3 | -+ | |||
53 | +35x |
- #' The [ui_nested_tabs] function returns a nested tabbed UI corresponding+ "Please follow migration instructions https://github.com/insightsengineering/teal/discussions/987." |
||
4 | +54 |
- #' to the nested modules.+ ) |
||
5 | +55 |
- #' This function adds the right filter panel to each main tab.+ ) |
||
6 | -+ | |||
56 | +35x |
- #'+ checkmate::assert_list( |
||
7 | -+ | |||
57 | +35x | +
+ data,+ |
+ ||
58 | +35x |
- #' The right filter panel's filter choices affect the `datasets` object. Therefore,+ any.missing = FALSE, names = "unique", |
||
8 | -+ | |||
59 | +35x |
- #' all modules using the same `datasets` share the same filters.+ types = c("data.frame", "reactive", "MultiAssayExperiment") |
||
9 | +60 |
- #'+ ) |
||
10 | -+ | |||
61 | +31x |
- #' This works with nested modules of depth greater than 2, though the filter+ checkmate::assert_class(join_keys, "join_keys", null.ok = TRUE) |
||
11 | -+ | |||
62 | +30x |
- #' panel is inserted at the right of the modules at depth 1 and not at the leaves.+ checkmate::assert_multi_class(code, c("character", "reactive")) |
||
12 | +63 |
- #'+ |
||
13 | -+ | |||
64 | +29x |
- #' @name module_tabs_with_filters+ checkmate::assert_list(metadata, names = "unique", null.ok = TRUE) |
||
14 | -+ | |||
65 | +27x |
- #'+ checkmate::assert_subset(names(metadata), names(data)) |
||
15 | +66 |
- #' @inheritParams module_teal+ |
||
16 | -+ | |||
67 | +26x |
- #'+ if (is.reactive(code)) { |
||
17 | -+ | |||
68 | +9x |
- #' @param datasets (`named list` of `FilteredData`)\cr+ isolate(checkmate::assert_class(code(), "character", .var.name = "code")) |
||
18 | +69 |
- #' object to store filter state and filtered datasets, shared across modules. For more+ } |
||
19 | +70 |
- #' details see [`teal.slice::FilteredData`]. Structure of the list must be the same as structure+ |
||
20 | +71 |
- #' of the `modules` argument and list names must correspond to the labels in `modules`.+ # create reactive data.frames |
||
21 | -+ | |||
72 | +25x |
- #' When filter is not module-specific then list contains the same object in all elements.+ for (x in names(data)) { |
||
22 | -+ | |||
73 | +48x |
- #' @param reporter (`Reporter`) object from `teal.reporter`+ if (!is.reactive(data[[x]])) { |
||
23 | -+ | |||
74 | +31x |
- #'+ data[[x]] <- do.call(reactive, list(as.name(x)), envir = list2env(data[x])) |
||
24 | +75 |
- #' @return A `tagList` of The main menu, place holders for filters and+ } else { |
||
25 | -+ | |||
76 | +17x |
- #' place holders for the teal modules+ isolate( |
||
26 | -+ | |||
77 | +17x |
- #'+ checkmate::assert_multi_class( |
||
27 | -+ | |||
78 | +17x |
- #'+ data[[x]](), c("data.frame", "MultiAssayExperiment"), |
||
28 | -+ | |||
79 | +17x |
- #' @keywords internal+ .var.name = "data" |
||
29 | +80 |
- #'+ ) |
||
30 | +81 |
- #' @examples+ ) |
||
31 | +82 |
- #'+ } |
||
32 | +83 |
- #' mods <- teal:::example_modules()+ } |
||
33 | +84 |
- #' datasets <- teal:::example_datasets()+ |
||
34 | +85 |
- #'+ # set attributes |
||
35 | -+ | |||
86 | +24x |
- #' app <- shinyApp(+ attr(data, "code") <- if (is.reactive(code)) code else reactive(code) |
||
36 | -+ | |||
87 | +24x |
- #' ui = function() {+ attr(data, "join_keys") <- join_keys |
||
37 | -+ | |||
88 | +24x |
- #' tagList(+ attr(data, "metadata") <- metadata |
||
38 | +89 |
- #' teal:::include_teal_css_js(),+ |
||
39 | +90 |
- #' textOutput("info"),+ # set class |
||
40 | -+ | |||
91 | +24x |
- #' fluidPage( # needed for nice tabs+ class(data) <- c("tdata", class(data)) |
||
41 | -+ | |||
92 | +24x |
- #' ui_tabs_with_filters("dummy", modules = mods, datasets = datasets)+ data |
||
42 | +93 |
- #' )+ } |
||
43 | +94 |
- #' )+ |
||
44 | +95 |
- #' },+ #' Function to convert a `tdata` object to an `environment` |
||
45 | +96 |
- #' server = function(input, output, session) {+ #' Any `reactives` inside `tdata` are first evaluated |
||
46 | +97 |
- #' output$info <- renderText({+ #' @param data a `tdata` object |
||
47 | +98 |
- #' paste0("The currently active tab name is ", active_module()$label)+ #' @return an `environment` |
||
48 | +99 |
- #' })+ #' @examples |
||
49 | +100 |
- #' active_module <- srv_tabs_with_filters(id = "dummy", datasets = datasets, modules = mods)+ #' |
||
50 | +101 |
- #' }+ #' data <- new_tdata( |
||
51 | +102 |
- #' )+ #' data = list(iris = iris, mtcars = reactive(mtcars)), |
||
52 | +103 |
- #' if (interactive()) {+ #' code = "iris <- iris |
||
53 | +104 |
- #' shinyApp(app$ui, app$server)+ #' mtcars = mtcars" |
||
54 | +105 |
- #' }+ #' ) |
||
55 | +106 |
#' |
||
56 | +107 |
- NULL+ #' my_env <- isolate(tdata2env(data)) |
||
57 | +108 |
-
+ #' |
||
58 | +109 |
- #' @rdname module_tabs_with_filters+ #' @export |
||
59 | +110 |
- ui_tabs_with_filters <- function(id, modules, datasets, filter = teal_slices()) {+ tdata2env <- function(data) { # nolint |
||
60 | -! | +|||
111 | +2x |
- checkmate::assert_class(modules, "teal_modules")+ checkmate::assert_class(data, "tdata") |
||
61 | -! | +|||
112 | +1x |
- checkmate::assert_list(datasets, types = c("list", "FilteredData"))+ list2env(lapply(data, function(x) if (is.reactive(x)) x() else x)) |
||
62 | -! | +|||
113 | +
- checkmate::assert_class(filter, "teal_slices")+ } |
|||
63 | +114 | |||
64 | -! | +|||
115 | +
- ns <- NS(id)+ |
|||
65 | -! | +|||
116 | +
- is_module_specific <- isTRUE(attr(filter, "module_specific"))+ #' Wrapper for `get_code.tdata` |
|||
66 | +117 |
-
+ #' This wrapper is to be used by downstream packages to extract the code of a `tdata` object |
||
67 | -! | +|||
118 | +
- teal_ui <- ui_nested_tabs(ns("root"), modules = modules, datasets, is_module_specific = is_module_specific)+ #' |
|||
68 | -! | +|||
119 | +
- filter_panel_btns <- tags$li(+ #' @param data (`tdata`) object |
|||
69 | -! | +|||
120 | +
- class = "flex-grow",+ #' |
|||
70 | -! | +|||
121 | +
- tags$button(+ #' @return (`character`) code used in the `tdata` object. |
|||
71 | -! | +|||
122 | +
- class = "btn action-button filter_hamburger", # see sidebar.css for style filter_hamburger+ #' @export |
|||
72 | -! | +|||
123 | +
- href = "javascript:void(0)",+ get_code_tdata <- function(data) { |
|||
73 | -! | +|||
124 | +7x |
- onclick = "toggleFilterPanel();", # see sidebar.js+ checkmate::assert_class(data, "tdata") |
||
74 | -! | +|||
125 | +5x |
- title = "Toggle filter panels",+ attr(data, "code")() |
||
75 | -! | +|||
126 | +
- icon("fas fa-bars")+ } |
|||
76 | +127 |
- ),+ |
||
77 | -! | +|||
128 | +
- filter_manager_modal_ui(ns("filter_manager"))+ #' Extract `join_keys` from `tdata` |
|||
78 | +129 |
- )+ #' @param data A `tdata` object |
||
79 | -! | +|||
130 | +
- teal_ui$children[[1]] <- tagAppendChild(teal_ui$children[[1]], filter_panel_btns)+ #' @param ... Additional arguments (not used) |
|||
80 | +131 |
-
+ #' @export |
||
81 | -! | +|||
132 | +
- if (!is_module_specific) {+ join_keys.tdata <- function(data, ...) {+ |
+ |||
133 | +2x | +
+ attr(data, "join_keys") |
||
82 | +134 |
- # need to rearrange html so that filter panel is within tabset+ } |
||
83 | -! | +|||
135 | +
- tabset_bar <- teal_ui$children[[1]]+ |
|||
84 | -! | +|||
136 | +
- teal_modules <- teal_ui$children[[2]]+ |
|||
85 | -! | +|||
137 | +
- filter_ui <- unlist(datasets)[[1]]$ui_filter_panel(ns("filter_panel"))+ #' Function to get metadata from a `tdata` object |
|||
86 | -! | +|||
138 | +
- list(+ #' @param data `tdata` - object to extract the data from |
|||
87 | -! | +|||
139 | +
- tabset_bar,+ #' @param dataname `character(1)` the dataset name whose metadata is requested |
|||
88 | -! | +|||
140 | +
- tags$hr(class = "my-2"),+ #' @return Either list of metadata or NULL if no metadata |
|||
89 | -! | +|||
141 | ++ |
+ #' @export+ |
+ ||
142 | +
- fluidRow(+ get_metadata <- function(data, dataname) { |
|||
90 | -! | +|||
143 | +4x |
- column(width = 9, teal_modules, class = "teal_primary_col"),+ checkmate::assert_string(dataname) |
||
91 | -! | +|||
144 | +4x |
- column(width = 3, filter_ui, class = "teal_secondary_col")+ UseMethod("get_metadata", data) |
||
92 | +145 |
- )+ } |
||
93 | +146 |
- )+ |
||
94 | +147 |
- } else {+ #' @rdname get_metadata |
||
95 | -! | +|||
148 | +
- teal_ui+ #' @export |
|||
96 | +149 |
- }+ get_metadata.tdata <- function(data, dataname) { |
||
97 | -+ | |||
150 | +4x |
- }+ metadata <- attr(data, "metadata") |
||
98 | -+ | |||
151 | +4x |
-
+ if (is.null(metadata)) { |
||
99 | -+ | |||
152 | +1x |
- #' @rdname module_tabs_with_filters+ return(NULL) |
||
100 | +153 |
- srv_tabs_with_filters <- function(id,+ }+ |
+ ||
154 | +3x | +
+ metadata[[dataname]] |
||
101 | +155 |
- datasets,+ } |
||
102 | +156 |
- modules,+ |
||
103 | +157 |
- reporter = teal.reporter::Reporter$new(),+ #' @rdname get_metadata |
||
104 | +158 |
- filter = teal_slices()) {+ #' @export |
||
105 | -5x | +|||
159 | +
- checkmate::assert_class(modules, "teal_modules")+ get_metadata.default <- function(data, dataname) { |
|||
106 | -5x | +|||
160 | +! |
- checkmate::assert_list(datasets, types = c("list", "FilteredData"))+ stop("get_metadata function not implemented for this object") |
||
107 | -5x | +|||
161 | +
- checkmate::assert_class(reporter, "Reporter")+ } |
|||
108 | -3x | +|||
162 | +
- checkmate::assert_class(filter, "teal_slices")+ |
|||
109 | +163 | |||
110 | -3x | +|||
164 | +
- moduleServer(id, function(input, output, session) {+ #' Downgrade `teal_data` objects in modules for compatibility. |
|||
111 | -3x | +|||
165 | +
- logger::log_trace("srv_tabs_with_filters initializing the module.")+ #' |
|||
112 | +166 |
-
+ #' Convert `teal_data` to `tdata` in `teal` modules. |
||
113 | -3x | +|||
167 | +
- is_module_specific <- isTRUE(attr(filter, "module_specific"))+ #' |
|||
114 | -3x | +|||
168 | +
- manager_out <- filter_manager_modal_srv("filter_manager", filtered_data_list = datasets, filter = filter)+ #' Recent changes in `teal` cause modules to fail because modules expect a `tdata` object |
|||
115 | +169 |
-
+ #' to be passed to the `data` argument but instead they receive a `teal_data` object, |
||
116 | -3x | +|||
170 | +
- active_module <- srv_nested_tabs(+ #' which is additionally wrapped in a reactive expression in the server functions. |
|||
117 | -3x | +|||
171 | +
- id = "root",+ #' In order to easily adapt such modules without a proper refactor, |
|||
118 | -3x | +|||
172 | +
- datasets = datasets,+ #' use this function to downgrade the `data` argument. |
|||
119 | -3x | +|||
173 | +
- modules = modules,+ #' |
|||
120 | -3x | +|||
174 | +
- reporter = reporter,+ #' @param x data object, either `tdata` or `teal_data`, the latter possibly in a reactive expression |
|||
121 | -3x | +|||
175 | +
- is_module_specific = is_module_specific+ #' |
|||
122 | +176 |
- )+ #' @return Object of class `tdata`. |
||
123 | +177 |
-
+ #' |
||
124 | -3x | +|||
178 | +
- if (!is_module_specific) {+ #' @examples |
|||
125 | -3x | +|||
179 | +
- active_datanames <- reactive({+ #' td <- teal_data() |
|||
126 | -6x | +|||
180 | +
- if (identical(active_module()$datanames, "all")) {+ #' td <- within(td, iris <- iris) %>% within(mtcars <- mtcars) |
|||
127 | -! | +|||
181 | +
- singleton$datanames()+ #' td |
|||
128 | +182 |
- } else {+ #' as_tdata(td) |
||
129 | -5x | +|||
183 | +
- active_module()$datanames+ #' as_tdata(reactive(td)) |
|||
130 | +184 |
- }+ #' |
||
131 | +185 |
- })+ #' @export |
||
132 | -3x | +|||
186 | +
- singleton <- unlist(datasets)[[1]]+ #' @rdname tdata_deprecation |
|||
133 | -3x | +|||
187 | +
- singleton$srv_filter_panel("filter_panel", active_datanames = active_datanames)+ #' |
|||
134 | +188 |
-
+ as_tdata <- function(x) { |
||
135 | -3x | +189 | +8x |
- observeEvent(+ if (inherits(x, "tdata")) { |
136 | -3x | +190 | +2x |
- eventExpr = active_datanames(),+ return(x) |
137 | -3x | +|||
191 | +
- handlerExpr = {+ } |
|||
138 | -4x | +192 | +6x |
- script <- if (length(active_datanames()) == 0 || is.null(active_datanames())) {+ if (is.reactive(x)) { |
139 | -+ | |||
193 | +1x |
- # hide the filter panel and disable the burger button+ checkmate::assert_class(isolate(x()), "teal_data") |
||
140 | -! | +|||
194 | +1x |
- "handleNoActiveDatasets();"+ datanames <- isolate(teal.data::datanames(x())) |
||
141 | -+ | |||
195 | +1x |
- } else {+ datasets <- sapply(datanames, function(dataname) reactive(x()[[dataname]]), simplify = FALSE) |
||
142 | -+ | |||
196 | +1x |
- # show the filter panel and enable the burger button+ code <- reactive(teal.code::get_code(x())) |
||
143 | -4x | +197 | +1x |
- "handleActiveDatasetsPresent();"+ join_keys <- isolate(teal.data::join_keys(x())) |
144 | -+ | |||
198 | +5x |
- }+ } else if (inherits(x, "teal_data")) { |
||
145 | -4x | +199 | +5x |
- shinyjs::runjs(script)+ datanames <- teal.data::datanames(x) |
146 | -+ | |||
200 | +5x |
- },+ datasets <- sapply(datanames, function(dataname) reactive(x[[dataname]]), simplify = FALSE) |
||
147 | -3x | +201 | +5x |
- ignoreNULL = FALSE+ code <- reactive(teal.code::get_code(x)) |
148 | -+ | |||
202 | +5x |
- )+ join_keys <- isolate(teal.data::join_keys(x)) |
||
149 | +203 |
- }+ } |
||
150 | +204 | |||
151 | -3x | -
- showNotification("Data loaded - App fully started up")- |
- ||
152 | -3x | -
- logger::log_trace("srv_tabs_with_filters initialized the module")- |
- ||
153 | -3x | -
- return(active_module)- |
- ||
154 | -+ | 205 | +6x |
- })+ new_tdata(data = datasets, code = code, join_keys = join_keys) |
155 | +206 |
}@@ -20481,14 +20467,14 @@ teal coverage - 63.30% |
1 |
- #' Create a UI of nested tabs of `teal_modules`+ #' Filter manager modal |
||
3 |
- #' @section `ui_nested_tabs`:+ #' Opens modal containing the filter manager UI. |
||
4 |
- #' Each `teal_modules` is translated to a `tabsetPanel` and each+ #' |
||
5 |
- #' of its children is another tab-module called recursively. The UI of a+ #' @name module_filter_manager_modal |
||
6 |
- #' `teal_module` is obtained by calling the `ui` function on it.+ #' @inheritParams filter_manager_srv |
||
7 |
- #'+ #' @examples |
||
8 |
- #' The `datasets` argument is required to resolve the teal arguments in an+ #' fd1 <- teal.slice::init_filtered_data(list(iris = list(dataset = iris))) |
||
9 |
- #' isolated context (with respect to reactivity)+ #' fd2 <- teal.slice::init_filtered_data( |
||
10 |
- #'+ #' list(iris = list(dataset = iris), mtcars = list(dataset = mtcars)) |
||
11 |
- #' @section `srv_nested_tabs`:+ #' ) |
||
12 |
- #' This module calls recursively all elements of the `modules` returns one which+ #' fd3 <- teal.slice::init_filtered_data( |
||
13 |
- #' is currently active.+ #' list(iris = list(dataset = iris), women = list(dataset = women)) |
||
14 |
- #' - `teal_module` returns self as a active module.+ #' ) |
||
15 |
- #' - `teal_modules` also returns module active within self which is determined by the `input$active_tab`.+ #' filter <- teal_slices( |
||
16 |
- #'+ #' teal.slice::teal_slice(dataname = "iris", varname = "Sepal.Length"), |
||
17 |
- #' @name module_nested_tabs+ #' teal.slice::teal_slice(dataname = "iris", varname = "Species"), |
||
18 |
- #'+ #' teal.slice::teal_slice(dataname = "mtcars", varname = "mpg"), |
||
19 |
- #' @inheritParams module_tabs_with_filters+ #' teal.slice::teal_slice(dataname = "women", varname = "height"), |
||
20 |
- #'+ #' mapping = list( |
||
21 |
- #' @param depth (`integer(1)`)\cr+ #' module2 = c("mtcars mpg"), |
||
22 |
- #' number which helps to determine depth of the modules nesting.+ #' module3 = c("women height"), |
||
23 |
- #' @param is_module_specific (`logical(1)`)\cr+ #' global_filters = "iris Species" |
||
24 |
- #' flag determining if the filter panel is global or module-specific.+ #' ) |
||
25 |
- #' When set to `TRUE`, a filter panel is called inside of each module tab.+ #' ) |
||
26 |
- #' @return depending on class of `modules`, `ui_nested_tabs` returns:+ #' |
||
27 |
- #' - `teal_module`: instantiated UI of the module+ #' app <- shinyApp( |
||
28 |
- #' - `teal_modules`: `tabsetPanel` with each tab corresponding to recursively+ #' ui = fluidPage( |
||
29 |
- #' calling this function on it.\cr+ #' teal:::filter_manager_modal_ui("manager") |
||
30 |
- #' `srv_nested_tabs` returns a reactive which returns the active module that corresponds to the selected tab.+ #' ), |
||
31 |
- #'+ #' server = function(input, output, session) { |
||
32 |
- #' @examples+ #' teal:::filter_manager_modal_srv( |
||
33 |
- #' mods <- teal:::example_modules()+ #' "manager", |
||
34 |
- #' datasets <- teal:::example_datasets()+ #' filtered_data_list = list(module1 = fd1, module2 = fd2, module3 = fd3), |
||
35 |
- #' app <- shinyApp(+ #' filter = filter |
||
36 |
- #' ui = function() {+ #' ) |
||
37 |
- #' tagList(+ #' } |
||
38 |
- #' teal:::include_teal_css_js(),+ #' ) |
||
39 |
- #' textOutput("info"),+ #' if (interactive()) { |
||
40 |
- #' fluidPage( # needed for nice tabs+ #' shinyApp(app$ui, app$server) |
||
41 |
- #' teal:::ui_nested_tabs("dummy", modules = mods, datasets = datasets)+ #' } |
||
42 |
- #' )+ #' |
||
43 |
- #' )+ #' @keywords internal |
||
44 |
- #' },+ #' |
||
45 |
- #' server = function(input, output, session) {+ NULL |
||
46 |
- #' active_module <- teal:::srv_nested_tabs(+ |
||
47 |
- #' "dummy",+ #' @rdname module_filter_manager_modal |
||
48 |
- #' datasets = datasets,+ filter_manager_modal_ui <- function(id) { |
||
49 | -+ | ! |
- #' modules = mods+ ns <- NS(id) |
50 | -+ | ! |
- #' )+ tags$button( |
51 | -+ | ! |
- #' output$info <- renderText({+ id = ns("show"), |
52 | -+ | ! |
- #' paste0("The currently active tab name is ", active_module()$label)+ class = "btn action-button filter_manager_button", |
53 | -+ | ! |
- #' })+ title = "Show filters manager modal", |
54 | -+ | ! |
- #' }+ icon("gear") |
55 |
- #' )+ ) |
||
56 |
- #' if (interactive()) {+ } |
||
57 |
- #' shinyApp(app$ui, app$server)+ |
||
58 |
- #' }+ #' @rdname module_filter_manager_modal |
||
59 |
- #' @keywords internal+ filter_manager_modal_srv <- function(id, filtered_data_list, filter) { |
||
60 | -+ | 3x |
- NULL+ moduleServer(id, function(input, output, session) { |
61 | -+ | 3x |
-
+ observeEvent(input$show, { |
62 | -+ | ! |
- #' @rdname module_nested_tabs+ logger::log_trace("filter_manager_modal_srv@1 show button has been clicked.") |
63 | -+ | ! |
- ui_nested_tabs <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) {+ showModal( |
64 | ! |
- checkmate::assert_multi_class(modules, c("teal_modules", "teal_module"))+ modalDialog( |
|
65 | ! |
- checkmate::assert_count(depth)+ filter_manager_ui(session$ns("filter_manager")), |
|
66 | ! |
- UseMethod("ui_nested_tabs", modules)+ size = "l", |
|
67 | -+ | ! |
- }+ footer = NULL, |
68 | -+ | ! |
-
+ easyClose = TRUE |
69 |
- #' @rdname module_nested_tabs+ ) |
||
70 |
- #' @export+ ) |
||
71 |
- ui_nested_tabs.default <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) {+ }) |
||
72 | -! | +
- stop("Modules class not supported: ", paste(class(modules), collapse = " "))+ |
|
73 | -+ | 3x |
- }+ filter_manager_srv("filter_manager", filtered_data_list, filter) |
74 |
-
+ }) |
||
75 |
- #' @rdname module_nested_tabs+ } |
||
76 |
- #' @export+ |
||
77 |
- ui_nested_tabs.teal_modules <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) {+ #' @rdname module_filter_manager |
||
78 | -! | +
- checkmate::assert_list(datasets, types = c("list", "FilteredData"))+ filter_manager_ui <- function(id) { |
|
80 | ! |
- do.call(+ div( |
|
81 | ! |
- tabsetPanel,+ class = "filter_manager_content", |
|
82 | ! |
- c(+ tableOutput(ns("slices_table")), |
|
83 | -+ | ! |
- # by giving an id, we can reactively respond to tab changes+ snapshot_manager_ui(ns("snapshot_manager")) |
84 | -! | +
- list(+ ) |
|
85 | -! | +
- id = ns("active_tab"),+ } |
|
86 | -! | +
- type = if (modules$label == "root") "pills" else "tabs"+ |
|
87 |
- ),+ #' Manage multiple `FilteredData` objects |
||
88 | -! | +
- lapply(+ #' |
|
89 | -! | +
- names(modules$children),+ #' Oversee filter states in the whole application. |
|
90 | -! | +
- function(module_id) {+ #' |
|
91 | -! | +
- module_label <- modules$children[[module_id]]$label+ #' @rdname module_filter_manager |
|
92 | -! | +
- tabPanel(+ #' @details |
|
93 | -! | +
- title = module_label,+ #' This module observes the changes of the filters in each `FilteredData` object |
|
94 | -! | +
- value = module_id, # when clicked this tab value changes input$<tabset panel id>+ #' and keeps track of all filters used. A mapping of filters to modules |
|
95 | -! | +
- ui_nested_tabs(+ #' is kept in the `mapping_matrix` object (which is actually a `data.frame`) |
|
96 | -! | +
- id = ns(module_id),+ #' that tracks which filters (rows) are active in which modules (columns). |
|
97 | -! | +
- modules = modules$children[[module_id]],+ #' |
|
98 | -! | +
- datasets = datasets[[module_label]],+ #' @param id (`character(1)`)\cr |
|
99 | -! | +
- depth = depth + 1L,+ #' `shiny` module id. |
|
100 | -! | +
- is_module_specific = is_module_specific+ #' @param filtered_data_list (`named list`)\cr |
|
101 |
- )+ #' A list, possibly nested, of `FilteredData` objects. |
||
102 |
- )+ #' Each `FilteredData` will be served to one module in the `teal` application. |
||
103 |
- }+ #' The structure of the list must reflect the nesting of modules in tabs |
||
104 |
- )+ #' and names of the list must be the same as labels of their respective modules. |
||
105 |
- )+ #' @inheritParams init |
||
106 |
- )+ #' @return A list of `reactive`s, each holding a `teal_slices`, as returned by `filter_manager_module_srv`. |
||
107 |
- }+ #' @keywords internal |
||
108 |
-
+ #' |
||
109 |
- #' @rdname module_nested_tabs+ filter_manager_srv <- function(id, filtered_data_list, filter) { |
||
110 | -+ | 5x |
- #' @export+ moduleServer(id, function(input, output, session) { |
111 | -+ | 5x |
- ui_nested_tabs.teal_module <- function(id, modules, datasets, depth = 0L, is_module_specific = FALSE) {+ logger::log_trace("filter_manager_srv initializing for: { paste(names(filtered_data_list), collapse = ', ')}.") |
112 | -! | +
- checkmate::assert_class(datasets, classes = "FilteredData")+ |
|
113 | -! | +5x |
- ns <- NS(id)+ is_module_specific <- isTRUE(attr(filter, "module_specific")) |
115 | -! | +
- args <- isolate(teal.transform::resolve_delayed(modules$ui_args, datasets))+ # Create global list of slices. |
|
116 | -! | +
- args <- c(list(id = ns("module")), args)+ # Contains all available teal_slice objects available to all modules. |
|
117 |
-
+ # Passed whole to instances of FilteredData used for individual modules. |
||
118 | -! | +
- teal_ui <- tags$div(+ # Down there a subset that pertains to the data sets used in that module is applied and displayed. |
|
119 | -! | +5x |
- id = id,+ slices_global <- reactiveVal(filter) |
120 | -! | +
- class = "teal_module",+ |
|
121 | -! | +5x |
- uiOutput(ns("data_reactive"), inline = TRUE),+ filtered_data_list <- |
122 | -! | +5x |
- tagList(+ if (!is_module_specific) { |
123 | -! | +
- if (depth >= 2L) div(style = "mt-6"),+ # Retrieve the first FilteredData from potentially nested list. |
|
124 | -! | +
- do.call(modules$ui, args)+ # List of length one is named "global_filters" because that name is forbidden for a module label. |
|
125 | -+ | 4x |
- )+ list(global_filters = unlist(filtered_data_list)[[1]]) |
126 |
- )+ } else { |
||
127 |
-
+ # Flatten potentially nested list of FilteredData objects while maintaining useful names. |
||
128 | -! | +
- if (!is.null(modules$datanames) && is_module_specific) {+ # Simply using `unlist` would result in concatenated names. |
|
129 | -! | +1x |
- fluidRow(+ flatten_nested <- function(x, name = NULL) { |
130 | -! | +5x |
- column(width = 9, teal_ui, class = "teal_primary_col"),+ if (inherits(x, "FilteredData")) { |
131 | -! | +3x |
- column(+ setNames(list(x), name) |
132 | -! | +
- width = 3,+ } else { |
|
133 | -! | +2x |
- datasets$ui_filter_panel(ns("module_filter_panel")),+ unlist(lapply(names(x), function(name) flatten_nested(x[[name]], name))) |
134 | -! | +
- class = "teal_secondary_col"+ } |
|
135 |
- )+ } |
||
136 | -+ | 1x |
- )+ flatten_nested(filtered_data_list) |
137 |
- } else {+ } |
||
138 | -! | +
- teal_ui+ |
|
139 |
- }+ # Create mapping fo filters to modules in matrix form (presented as data.frame). |
||
140 |
- }+ # Modules get NAs for filters that cannot be set for them. |
||
141 | -+ | 5x |
-
+ mapping_matrix <- reactive({ |
142 | -+ | 5x |
- #' @rdname module_nested_tabs+ state_ids_global <- vapply(slices_global(), `[[`, character(1L), "id") |
143 | -+ | 5x |
- srv_nested_tabs <- function(id, datasets, modules, is_module_specific = FALSE,+ mapping_smooth <- lapply(filtered_data_list, function(x) { |
144 | -+ | 7x |
- reporter = teal.reporter::Reporter$new()) {+ state_ids_local <- vapply(x$get_filter_state(), `[[`, character(1L), "id") |
145 | -50x | +7x |
- checkmate::assert_multi_class(modules, c("teal_modules", "teal_module"))+ state_ids_allowed <- vapply(x$get_available_teal_slices()(), `[[`, character(1L), "id") |
146 | -50x | +7x |
- checkmate::assert_class(reporter, "Reporter")+ states_active <- state_ids_global %in% state_ids_local |
147 | -49x | +7x |
- UseMethod("srv_nested_tabs", modules)+ ifelse(state_ids_global %in% state_ids_allowed, states_active, NA) |
148 |
- }+ }) |
||
150 | -+ | 5x |
- #' @rdname module_nested_tabs+ as.data.frame(mapping_smooth, row.names = state_ids_global, check.names = FALSE) |
151 |
- #' @export+ }) |
||
152 |
- srv_nested_tabs.default <- function(id, datasets, modules, is_module_specific = FALSE,+ |
||
153 | -+ | 5x |
- reporter = teal.reporter::Reporter$new()) {+ output$slices_table <- renderTable( |
154 | -! | +5x |
- stop("Modules class not supported: ", paste(class(modules), collapse = " "))+ expr = { |
155 |
- }+ # Display logical values as UTF characters. |
||
156 | -+ | 2x |
-
+ mm <- mapping_matrix() |
157 | -+ | 2x |
- #' @rdname module_nested_tabs+ mm[] <- lapply(mm, ifelse, yes = intToUtf8(9989), no = intToUtf8(10060)) |
158 | -+ | 2x |
- #' @export+ mm[] <- lapply(mm, function(x) ifelse(is.na(x), intToUtf8(128306), x)) |
159 | -+ | 2x |
- srv_nested_tabs.teal_modules <- function(id, datasets, modules, is_module_specific = FALSE,+ if (!is_module_specific) colnames(mm) <- "Global Filters" |
160 |
- reporter = teal.reporter::Reporter$new()) {+ |
||
161 | -22x | +
- checkmate::assert_list(datasets, types = c("list", "FilteredData"))+ # Display placeholder if no filters defined. |
|
162 | -+ | 2x |
-
+ if (nrow(mm) == 0L) { |
163 | -22x | +2x |
- moduleServer(id = id, module = function(input, output, session) {+ mm <- data.frame(`Filter manager` = "No filters specified.", check.names = FALSE) |
164 | -22x | +2x |
- logger::log_trace("srv_nested_tabs.teal_modules initializing the module { deparse1(modules$label) }.")+ rownames(mm) <- "" |
165 |
-
+ } |
||
166 | -22x | +
- labels <- vapply(modules$children, `[[`, character(1), "label")+ |
|
167 | -22x | +
- modules_reactive <- sapply(+ # Report Previewer will not be displayed. |
|
168 | -22x | +2x |
- names(modules$children),+ mm[names(mm) != "Report previewer"] |
169 | -22x | +
- function(module_id) {+ }, |
|
170 | -33x | +5x |
- srv_nested_tabs(+ align = paste(c("l", rep("c", sum(names(filtered_data_list) != "Report previewer"))), collapse = ""), |
171 | -33x | +5x |
- id = module_id,+ rownames = TRUE |
172 | -33x | +
- datasets = datasets[[labels[module_id]]],+ ) |
|
173 | -33x | +
- modules = modules$children[[module_id]],+ |
|
174 | -33x | +
- is_module_specific = is_module_specific,+ # Create list of module calls. |
|
175 | -33x | +5x |
- reporter = reporter+ modules_out <- lapply(names(filtered_data_list), function(module_name) { |
176 | -+ | 7x |
- )+ filter_manager_module_srv( |
177 | -+ | 7x |
- },+ id = module_name, |
178 | -22x | +7x |
- simplify = FALSE+ module_fd = filtered_data_list[[module_name]], |
179 | -+ | 7x |
- )+ slices_global = slices_global |
180 |
-
+ ) |
||
181 |
- # when not ready input$active_tab would return NULL - this would fail next reactive+ }) |
||
182 | -22x | +
- input_validated <- eventReactive(input$active_tab, input$active_tab, ignoreNULL = TRUE)+ |
|
183 | -22x | +
- get_active_module <- reactive({+ # Call snapshot manager. |
|
184 | -12x | +5x |
- if (length(modules$children) == 1L) {+ snapshot_manager_srv("snapshot_manager", slices_global, mapping_matrix, filtered_data_list) |
185 |
- # single tab is active by default+ |
||
186 | -1x | +5x |
- modules_reactive[[1]]()+ modules_out # returned for testing purpose |
187 |
- } else {+ }) |
||
188 |
- # switch to active tab+ } |
||
189 | -11x | +
- modules_reactive[[input_validated()]]()+ |
|
190 |
- }+ #' Module specific filter manager |
||
191 |
- })+ #' |
||
192 |
-
+ #' Track filter states in single module. |
||
193 | -22x | +
- get_active_module+ #' |
|
194 |
- })+ #' This module tracks the state of a single `FilteredData` object and global `teal_slices` |
||
195 |
- }+ #' and updates both objects as necessary. Filter states added in different modules |
||
196 |
-
+ #' Filter states added any individual module are added to global `teal_slices` |
||
197 |
- #' @rdname module_nested_tabs+ #' and from there become available in other modules |
||
198 |
- #' @export+ #' by setting `private$available_teal_slices` in each `FilteredData`. |
||
199 |
- srv_nested_tabs.teal_module <- function(id, datasets, modules, is_module_specific = TRUE,+ #' |
||
200 |
- reporter = teal.reporter::Reporter$new()) {+ #' @param id (`character(1)`)\cr |
||
201 | -27x | +
- checkmate::assert_class(datasets, "FilteredData")+ #' `shiny` module id. |
|
202 | -27x | +
- logger::log_trace("srv_nested_tabs.teal_module initializing the module: { deparse1(modules$label) }.")+ #' @param module_fd (`FilteredData`)\cr |
|
203 |
-
+ #' object to filter data in the teal-module |
||
204 | -27x | +
- moduleServer(id = id, module = function(input, output, session) {+ #' @param slices_global (`reactiveVal`)\cr |
|
205 | -27x | +
- modules$server_args <- teal.transform::resolve_delayed(modules$server_args, datasets)+ #' stores `teal_slices` with all available filters; allows the following actions: |
|
206 | -27x | +
- if (!is.null(modules$datanames) && is_module_specific) {+ #' - to disable/enable a specific filter in a module |
|
207 | -! | +
- datasets$srv_filter_panel("module_filter_panel")+ #' - to restore saved filter settings |
|
208 |
- }+ #' - to save current filter panel settings |
||
209 |
-
+ #' @return A `reactive` expression containing the slices active in this module. |
||
210 |
- # Create two triggers to limit reactivity between filter-panel and modules.+ #' @keywords internal |
||
211 |
- # We want to recalculate only visible modules+ #' |
||
212 |
- # - trigger the data when the tab is selected+ filter_manager_module_srv <- function(id, module_fd, slices_global) { |
||
213 | -+ | 7x |
- # - trigger module to be called when the tab is selected for the first time+ moduleServer(id, function(input, output, session) { |
214 | -27x | +
- trigger_data <- reactiveVal(1L)+ # Only operate on slices that refer to data sets present in this module. |
|
215 | -27x | +7x |
- trigger_module <- reactiveVal(NULL)+ module_fd$set_available_teal_slices(reactive(slices_global())) |
216 | -27x | +
- output$data_reactive <- renderUI({+ |
|
217 | -17x | +
- lapply(datasets$datanames(), function(x) {+ # Track filter state of this module. |
|
218 | -21x | +7x |
- datasets$get_data(x, filtered = TRUE)+ slices_module <- reactive(module_fd$get_filter_state()) |
219 |
- })+ |
||
220 | -17x | +
- isolate(trigger_data(trigger_data() + 1))+ # Reactive values for comparing states. |
|
221 | -17x | +7x |
- isolate(trigger_module(TRUE))+ previous_slices <- reactiveVal(isolate(slices_module())) |
222 | -+ | 7x |
-
+ slices_added <- reactiveVal(NULL) |
223 | -17x | +
- NULL+ |
|
224 |
- })+ # Observe changes in module filter state and trigger appropriate actions. |
||
225 | -+ | 7x |
-
+ observeEvent(slices_module(), ignoreNULL = FALSE, { |
226 | -+ | 2x |
- # collect arguments to run teal_module+ logger::log_trace("filter_manager_srv@1 detecting states deltas in module: { id }.") |
227 | -27x | +2x |
- args <- c(list(id = "module"), modules$server_args)+ added <- setdiff_teal_slices(slices_module(), slices_global()) |
228 | -27x | +! |
- if (is_arg_used(modules$server, "reporter")) {+ if (length(added)) slices_added(added) |
229 | -! | +2x |
- args <- c(args, list(reporter = reporter))+ previous_slices(slices_module()) |
230 |
- }+ }) |
||
232 | -27x | +7x |
- if (is_arg_used(modules$server, "datasets")) {+ observeEvent(slices_added(), ignoreNULL = TRUE, { |
233 | -1x | +! |
- args <- c(args, datasets = datasets)+ logger::log_trace("filter_manager_srv@2 added filter in module: { id }.") |
234 |
- }+ # In case the new state has the same id as an existing state, add a suffix to it. |
||
235 | -+ | ! |
-
+ global_ids <- vapply(slices_global(), `[[`, character(1L), "id") |
236 | -27x | +! |
- if (is_arg_used(modules$server, "data")) {+ lapply( |
237 | -7x | +! |
- data <- eventReactive(trigger_data(), .datasets_to_data(modules, datasets))+ slices_added(), |
238 | -7x | +! |
- args <- c(args, data = list(data))+ function(slice) { |
239 | -+ | ! |
- }+ if (slice$id %in% global_ids) { |
240 | -+ | ! |
-
+ slice$id <- utils::tail(make.unique(c(global_ids, slice$id), sep = "_"), 1) |
241 | -27x | +
- if (is_arg_used(modules$server, "filter_panel_api")) {+ } |
|
242 | -2x | +
- filter_panel_api <- teal.slice::FilterPanelAPI$new(datasets)+ } |
|
243 | -2x | +
- args <- c(args, filter_panel_api = filter_panel_api)+ ) |
|
244 | -+ | ! |
- }+ slices_global_new <- c(slices_global(), slices_added()) |
245 | -+ | ! |
-
+ slices_global(slices_global_new) |
246 | -+ | ! |
- # observe the trigger_module above to induce the module once the renderUI is triggered+ slices_added(NULL) |
247 | -27x | +
- observeEvent(+ }) |
|
248 | -27x | +
- ignoreNULL = TRUE,+ |
|
249 | -27x | +7x |
- once = TRUE,+ slices_module # returned for testing purpose |
250 | -27x | -
- eventExpr = trigger_module(),- |
- |
251 | -27x | -
- handlerExpr = {- |
- |
252 | -17x | -
- module_output <- if (is_arg_used(modules$server, "id")) {- |
- |
253 | -17x | +
- do.call(modules$server, args)+ }) |
|
254 | +251 |
- } else {- |
- |
255 | -! | -
- do.call(callModule, c(args, list(module = modules$server)))+ } |
256 | +1 |
- }+ .onLoad <- function(libname, pkgname) { # nolint |
||
257 | +2 |
- }+ # adapted from https://github.com/r-lib/devtools/blob/master/R/zzz.R |
||
258 | -+ | |||
3 | +! |
- )+ teal_default_options <- list(teal.show_js_log = FALSE) |
||
259 | +4 | |||
260 | -27x | +|||
5 | +! |
- reactive(modules)+ op <- options() |
||
261 | -+ | |||
6 | +! |
- })+ toset <- !(names(teal_default_options) %in% names(op)) |
||
262 | -+ | |||
7 | +! |
- }+ if (any(toset)) options(teal_default_options[toset]) |
||
263 | +8 | |||
264 | -- |
- #' Convert `FilteredData` to reactive list of datasets of the `teal_data` type.- |
- ||
265 | -- |
- #'- |
- ||
266 | -- |
- #' Converts `FilteredData` object to `teal_data` object containing datasets needed for a specific module.- |
- ||
267 | -+ | |||
9 | +! |
- #' Please note that if module needs dataset which has a parent, then parent will be also returned.+ options("shiny.sanitize.errors" = FALSE) |
||
268 | +10 |
- #' A hash per `dataset` is calculated internally and returned in the code.+ |
||
269 | +11 |
- #'+ # Set up the teal logger instance |
||
270 | -+ | |||
12 | +! |
- #' @param module (`teal_module`) module where needed filters are taken from+ teal.logger::register_logger("teal") |
||
271 | +13 |
- #' @param datasets (`FilteredData`) object where needed data are taken from+ |
||
272 | -+ | |||
14 | +! |
- #' @return A `teal_data` object.+ invisible() |
||
273 | +15 |
- #'+ } |
||
274 | +16 |
- #' @keywords internal+ |
||
275 | +17 |
- .datasets_to_data <- function(module, datasets) {+ .onAttach <- function(libname, pkgname) { # nolint |
||
276 | -4x | +18 | +2x |
- checkmate::assert_class(module, "teal_module")+ packageStartupMessage( |
277 | -4x | +19 | +2x |
- checkmate::assert_class(datasets, "FilteredData")+ "\nYou are using teal version ", |
278 | +20 |
-
+ # `system.file` uses the `shim` of `system.file` by `teal` |
||
279 | -4x | +|||
21 | +
- datanames <- if (is.null(module$datanames) || identical(module$datanames, "all")) {+ # we avoid `desc` dependency here to get the version |
|||
280 | -1x | +22 | +2x |
- datasets$datanames()+ read.dcf(system.file("DESCRIPTION", package = "teal"))[, "Version"] |
281 | +23 |
- } else {- |
- ||
282 | -3x | -
- unique(module$datanames) # todo: include parents! unique shouldn't be needed here!+ ) |
||
283 | +24 |
- }+ } |
||
284 | +25 | |||
285 | +26 |
- # list of reactive filtered data- |
- ||
286 | -4x | -
- data <- sapply(datanames, function(x) datasets$get_data(x, filtered = TRUE), simplify = FALSE)+ # This one is here because setdiff_teal_slice should not be exported from teal.slice. |
||
287 | +27 | - - | -||
288 | -4x | -
- hashes <- calculate_hashes(datanames, datasets)+ setdiff_teal_slices <- getFromNamespace("setdiff_teal_slices", "teal.slice") |
||
289 | +28 | - - | -||
290 | -4x | -
- code <- c(- |
- ||
291 | -4x | -
- get_rcode_str_install(),+ # This one is here because it is needed by c.teal_slices but we don't want it exported from teal.slice. |
||
292 | -4x | +|||
29 | +
- get_rcode_libraries(),+ coalesce_r <- getFromNamespace("coalesce_r", "teal.slice") |
|||
293 | -4x | +|||
30 | +
- get_datasets_code(datanames, datasets, hashes)+ # all *Block objects are private in teal.reporter |
|||
294 | +31 |
- )+ RcodeBlock <- getFromNamespace("RcodeBlock", "teal.reporter") # nolint |
||
295 | +32 | |||
296 | -4x | +|||
33 | +
- do.call(+ # Use non-exported function(s) from teal.code |
|||
297 | -4x | +|||
34 | +
- teal.data::teal_data,+ # This one is here because lang2calls should not be exported from teal.code |
|||
298 | -4x | +|||
35 | +
- args = c(data, code = list(code), join_keys = list(datasets$get_join_keys()[datanames]))+ lang2calls <- getFromNamespace("lang2calls", "teal.code") |
299 | +1 |
- )+ #' Send input validation messages to output. |
|
300 | +2 |
- }+ #' |
|
301 | +3 |
-
+ #' Captures messages from `InputValidator` objects and collates them |
|
302 | +4 |
- #' Get the hash of a dataset+ #' into one message passed to `validate`. |
|
303 | +5 |
#' |
|
304 | +6 |
- #' @param datanames (`character`) names of datasets+ #' `shiny::validate` is used to withhold rendering of an output element until |
|
305 | +7 |
- #' @param datasets (`FilteredData`) object holding the data+ #' certain conditions are met and to print a validation message in place |
|
306 | +8 |
- #'+ #' of the output element. |
|
307 | +9 |
- #' @return A list of hashes per dataset+ #' `shinyvalidate::InputValidator` allows to validate input elements |
|
308 | +10 |
- #' @keywords internal+ #' and to display specific messages in their respective input widgets. |
|
309 | +11 |
- #'+ #' `validate_inputs` provides a hybrid solution. |
|
310 | +12 |
- calculate_hashes <- function(datanames, datasets) {+ #' Given an `InputValidator` object, messages corresponding to inputs that fail validation |
|
311 | -7x | +||
13 | +
- sapply(datanames, function(x) rlang::hash(datasets$get_data(x, filtered = FALSE)), simplify = FALSE)+ #' are extracted and placed in one validation message that is passed to a `validate`/`need` call. |
||
312 | +14 |
- }+ #' This way the input `validator` messages are repeated in the output. |
1 | +15 |
- # This file contains Shiny modules useful for debugging and developing teal.+ #' |
|
2 | +16 |
- # We do not export the functions in this file. They are for+ #' The `...` argument accepts any number of `InputValidator` objects |
|
3 | +17 |
- # developers only and can be accessed via `:::`.+ #' or a nested list of such objects. |
|
4 | +18 |
-
+ #' If `validators` are passed directly, all their messages are printed together |
|
5 | +19 |
- #' Dummy module to show the filter calls generated by the right encoding panel+ #' under one (optional) header message specified by `header`. If a list is passed, |
|
6 | +20 |
- #'+ #' messages are grouped by `validator`. The list's names are used as headers |
|
7 | +21 |
- #'+ #' for their respective message groups. |
|
8 | +22 |
- #' Please do not remove, this is useful for debugging teal without+ #' If neither of the nested list elements is named, a header message is taken from `header`. |
|
9 | +23 |
- #' dependencies and simplifies `\link[devtools]{load_all}` which otherwise fails+ #' |
|
10 | +24 |
- #' and avoids session restarts!+ #' @param ... either any number of `InputValidator` objects |
|
11 | +25 |
- #'+ #' or an optionally named, possibly nested `list` of `InputValidator` |
|
12 | +26 |
- #' @param label `character` label of module+ #' objects, see `Details` |
|
13 | +27 |
- #' @keywords internal+ #' @param header `character(1)` generic validation message; set to NULL to omit |
|
14 | +28 |
#' |
|
15 | +29 |
- #' @examples+ #' @return |
|
16 | +30 |
- #' app <- init(+ #' Returns NULL if the final validation call passes and a `shiny.silent.error` if it fails. |
|
17 | +31 |
- #' data = teal_data(iris = iris, mtcars = mtcars),+ #' |
|
18 | +32 |
- #' modules = teal:::filter_calls_module(),+ #' @seealso [`shinyvalidate::InputValidator`], [`shiny::validate`] |
|
19 | +33 |
- #' header = "Simple teal app"+ #' |
|
20 | +34 |
- #' )+ #' @examples |
|
21 | +35 |
- #' if (interactive()) {+ #' library(shiny) |
|
22 | +36 |
- #' shinyApp(app$ui, app$server)+ #' library(shinyvalidate) |
|
23 | +37 |
- #' }+ #' |
|
24 | +38 |
- filter_calls_module <- function(label = "Filter Calls Module") { # nolint+ #' ui <- fluidPage( |
|
25 | -! | +||
39 | +
- checkmate::assert_string(label)+ #' selectInput("method", "validation method", c("sequential", "combined", "grouped")), |
||
26 | +40 |
-
+ #' sidebarLayout( |
|
27 | -! | +||
41 | +
- module(+ #' sidebarPanel( |
||
28 | -! | +||
42 | +
- label = label,+ #' selectInput("letter", "select a letter:", c(letters[1:3], LETTERS[4:6])), |
||
29 | -! | +||
43 | +
- server = function(input, output, session, data) {+ #' selectInput("number", "select a number:", 1:6), |
||
30 | -! | +||
44 | +
- checkmate::assert_class(data, "reactive")+ #' br(), |
||
31 | -! | +||
45 | +
- checkmate::assert_class(isolate(data()), "teal_data")+ #' selectInput("color", "select a color:", |
||
32 | +46 |
-
+ #' c("black", "indianred2", "springgreen2", "cornflowerblue"), |
|
33 | -! | +||
47 | +
- output$filter_calls <- renderText({+ #' multiple = TRUE |
||
34 | -! | +||
48 | +
- teal.data::get_code(data())+ #' ), |
||
35 | +49 |
- })+ #' sliderInput("size", "select point size:", |
|
36 | +50 |
- },+ #' min = 0.1, max = 4, value = 0.25 |
|
37 | -! | +||
51 | +
- ui = function(id, ...) {+ #' ) |
||
38 | -! | +||
52 | +
- ns <- NS(id)+ #' ), |
||
39 | -! | +||
53 | +
- div(+ #' mainPanel(plotOutput("plot")) |
||
40 | -! | +||
54 | +
- h2("The following filter calls are generated:"),+ #' ) |
||
41 | -! | +||
55 | +
- verbatimTextOutput(ns("filter_calls"))+ #' ) |
||
42 | +56 |
- )+ #' |
|
43 | +57 |
- },+ #' server <- function(input, output) { |
|
44 | -! | +||
58 | +
- datanames = "all"+ #' # set up input validation |
||
45 | +59 |
- )+ #' iv <- InputValidator$new() |
|
46 | +60 |
- }+ #' iv$add_rule("letter", sv_in_set(LETTERS, "choose a capital letter")) |
1 | +61 |
- #' Validate that dataset has a minimum number of observations+ #' iv$add_rule("number", ~ if (as.integer(.) %% 2L == 1L) "choose an even number") |
||
2 | +62 |
- #'+ #' iv$enable() |
||
3 | +63 |
- #' @description `r lifecycle::badge("stable")`+ #' # more input validation |
||
4 | +64 |
- #' @param x a data.frame+ #' iv_par <- InputValidator$new() |
||
5 | +65 |
- #' @param min_nrow minimum number of rows in \code{x}+ #' iv_par$add_rule("color", sv_required(message = "choose a color")) |
||
6 | +66 |
- #' @param complete \code{logical} default \code{FALSE} when set to \code{TRUE} then complete cases are checked.+ #' iv_par$add_rule("color", ~ if (length(.) > 1L) "choose only one color") |
||
7 | +67 |
- #' @param allow_inf \code{logical} default \code{TRUE} when set to \code{FALSE} then error thrown if any values are+ #' iv_par$add_rule( |
||
8 | +68 |
- #' infinite.+ #' "size", |
||
9 | +69 |
- #' @param msg (`character(1)`) additional message to display alongside the default message.+ #' sv_between( |
||
10 | +70 |
- #'+ #' left = 0.5, right = 3, |
||
11 | +71 |
- #' @details This function is a wrapper for `shiny::validate`.+ #' message_fmt = "choose a value between {left} and {right}" |
||
12 | +72 |
- #'+ #' ) |
||
13 | +73 |
- #' @export+ #' ) |
||
14 | +74 |
- #'+ #' iv_par$enable() |
||
15 | +75 |
- #' @examples+ #' |
||
16 | +76 |
- #' library(teal)+ #' output$plot <- renderPlot({ |
||
17 | +77 |
- #' ui <- fluidPage(+ #' # validate output |
||
18 | +78 |
- #' sliderInput("len", "Max Length of Sepal",+ #' switch(input[["method"]], |
||
19 | +79 |
- #' min = 4.3, max = 7.9, value = 5+ #' "sequential" = { |
||
20 | +80 |
- #' ),+ #' validate_inputs(iv) |
||
21 | +81 |
- #' plotOutput("plot")+ #' validate_inputs(iv_par, header = "Set proper graphical parameters") |
||
22 | +82 |
- #' )+ #' }, |
||
23 | +83 |
- #'+ #' "combined" = validate_inputs(iv, iv_par), |
||
24 | +84 |
- #' server <- function(input, output) {+ #' "grouped" = validate_inputs(list( |
||
25 | +85 |
- #' output$plot <- renderPlot({+ #' "Some inputs require attention" = iv, |
||
26 | +86 |
- #' df <- iris[iris$Sepal.Length <= input$len, ]+ #' "Set proper graphical parameters" = iv_par |
||
27 | +87 |
- #' validate_has_data(+ #' )) |
||
28 | +88 |
- #' iris_f,+ #' ) |
||
29 | +89 |
- #' min_nrow = 10,+ #' |
||
30 | +90 |
- #' complete = FALSE,+ #' plot(eruptions ~ waiting, faithful, |
||
31 | +91 |
- #' msg = "Please adjust Max Length of Sepal"+ #' las = 1, pch = 16, |
||
32 | +92 |
- #' )+ #' col = input[["color"]], cex = input[["size"]] |
||
33 | +93 |
- #'+ #' ) |
||
34 | +94 |
- #' hist(iris_f$Sepal.Length, breaks = 5)+ #' }) |
||
35 | +95 |
- #' })+ #' } |
||
36 | +96 |
- #' }+ #' |
||
37 | +97 |
#' if (interactive()) { |
||
38 | +98 |
#' shinyApp(ui, server) |
||
39 | +99 |
#' } |
||
40 | +100 |
#' |
||
41 | +101 |
- validate_has_data <- function(x,+ #' @export |
||
42 | +102 |
- min_nrow = NULL,+ #' |
||
43 | +103 |
- complete = FALSE,+ validate_inputs <- function(..., header = "Some inputs require attention") { |
||
44 | -+ | |||
104 | +36x |
- allow_inf = TRUE,+ dots <- list(...)+ |
+ ||
105 | +2x | +
+ if (!is_validators(dots)) stop("validate_inputs accepts validators or a list thereof") |
||
45 | +106 |
- msg = NULL) {+ |
||
46 | -17x | +107 | +34x |
- checkmate::assert_string(msg, null.ok = TRUE)+ messages <- extract_validator(dots, header) |
47 | -15x | +108 | +34x |
- checkmate::assert_data_frame(x)+ failings <- if (!any_names(dots)) { |
48 | -15x | +109 | +29x |
- if (!is.null(min_nrow)) {+ add_header(messages, header) |
49 | -15x | +|||
110 | +
- if (complete) {+ } else { |
|||
50 | +111 | 5x |
- complete_index <- stats::complete.cases(x)+ unlist(messages) |
|
51 | -5x | +|||
112 | +
- validate(need(+ } |
|||
52 | -5x | +|||
113 | +
- sum(complete_index) > 0 && nrow(x[complete_index, , drop = FALSE]) >= min_nrow,+ |
|||
53 | -5x | +114 | +34x |
- paste(c(paste("Number of complete cases is less than:", min_nrow), msg), collapse = "\n")+ shiny::validate(shiny::need(is.null(failings), failings)) |
54 | +115 |
- ))+ } |
||
55 | +116 |
- } else {+ |
||
56 | -10x | +|||
117 | +
- validate(need(+ ### internal functions |
|||
57 | -10x | +|||
118 | +
- nrow(x) >= min_nrow,+ |
|||
58 | -10x | +|||
119 | +
- paste(+ #' @keywords internal |
|||
59 | -10x | +|||
120 | +
- c(paste("Minimum number of records not met: >=", min_nrow, "records required."), msg),+ # recursive object type test |
|||
60 | -10x | +|||
121 | ++ |
+ # returns logical of length 1+ |
+ ||
122 | ++ |
+ is_validators <- function(x) {+ |
+ ||
123 | +118x |
- collapse = "\n"+ all(if (is.list(x)) unlist(lapply(x, is_validators)) else inherits(x, "InputValidator")) |
||
61 | +124 |
- )+ } |
||
62 | +125 |
- ))+ |
||
63 | +126 |
- }+ #' @keywords internal |
||
64 | +127 |
-
+ # test if an InputValidator object is enabled |
||
65 | -10x | +|||
128 | +
- if (!allow_inf) {+ # returns logical of length 1 |
|||
66 | -6x | +|||
129 | +
- validate(need(+ # official method requested at https://github.com/rstudio/shinyvalidate/issues/64 |
|||
67 | -6x | +|||
130 | +
- all(vapply(x, function(col) !is.numeric(col) || !any(is.infinite(col)), logical(1))),+ validator_enabled <- function(x) { |
|||
68 | -6x | +131 | +49x |
- "Dataframe contains Inf values which is not allowed."+ x$.__enclos_env__$private$enabled |
69 | +132 |
- ))+ } |
||
70 | +133 |
- }+ |
||
71 | +134 |
- }+ #' @keywords internal |
||
72 | +135 |
- }+ # recursively extract messages from validator list |
||
73 | +136 |
-
+ # returns character vector or a list of character vectors, possibly nested and named |
||
74 | +137 |
- #' Validate that dataset has unique rows for key variables+ extract_validator <- function(iv, header) { |
||
75 | -+ | |||
138 | +113x |
- #'+ if (inherits(iv, "InputValidator")) { |
||
76 | -+ | |||
139 | +49x |
- #' @description `r lifecycle::badge("stable")`+ add_header(gather_messages(iv), header) |
||
77 | +140 |
- #' @param x a data.frame+ } else { |
||
78 | -+ | |||
141 | +58x |
- #' @param key a vector of ID variables from \code{x} that identify unique records+ if (is.null(names(iv))) names(iv) <- rep("", length(iv)) |
||
79 | -+ | |||
142 | +64x |
- #'+ mapply(extract_validator, iv = iv, header = names(iv), SIMPLIFY = FALSE) |
||
80 | +143 |
- #' @details This function is a wrapper for `shiny::validate`.+ } |
||
81 | +144 |
- #'+ } |
||
82 | +145 |
- #' @export+ |
||
83 | +146 |
- #'+ #' @keywords internal |
||
84 | +147 |
- #' @examples+ # collate failing messages from validator |
||
85 | +148 |
- #' iris$id <- rep(1:50, times = 3)+ # returns list |
||
86 | +149 |
- #' ui <- fluidPage(+ gather_messages <- function(iv) { |
||
87 | -+ | |||
150 | +49x |
- #' selectInput(+ if (validator_enabled(iv)) { |
||
88 | -+ | |||
151 | +46x |
- #' inputId = "species",+ status <- iv$validate() |
||
89 | -+ | |||
152 | +46x |
- #' label = "Select species",+ failing_inputs <- Filter(Negate(is.null), status) |
||
90 | -+ | |||
153 | +46x |
- #' choices = c("setosa", "versicolor", "virginica"),+ unique(lapply(failing_inputs, function(x) x[["message"]])) |
||
91 | +154 |
- #' selected = "setosa",+ } else {+ |
+ ||
155 | +3x | +
+ logger::log_warn("Validator is disabled and will be omitted.")+ |
+ ||
156 | +3x | +
+ list() |
||
92 | +157 |
- #' multiple = TRUE+ } |
||
93 | +158 |
- #' ),+ } |
||
94 | +159 |
- #' plotOutput("plot")+ |
||
95 | +160 |
- #' )+ #' @keywords internal |
||
96 | +161 |
- #' server <- function(input, output) {+ # add optional header to failing messages |
||
97 | +162 |
- #' output$plot <- renderPlot({+ 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") |
||
98 | +166 |
- #' iris_f <- iris[iris$Species %in% input$species, ]+ }+ |
+ ||
167 | +78x | +
+ ans |
||
99 | +168 |
- #' validate_one_row_per_id(iris_f, key = c("id"))+ } |
||
100 | +169 |
- #'+ |
||
101 | +170 |
- #' hist(iris_f$Sepal.Length, breaks = 5)+ #' @keywords internal |
||
102 | +171 |
- #' })+ # recursively check if the object contains a named list |
||
103 | +172 |
- #' }+ any_names <- function(x) { |
||
104 | -+ | |||
173 | +103x |
- #' if (interactive()) {+ any( |
||
105 | -+ | |||
174 | +103x |
- #' shinyApp(ui, server)+ if (is.list(x)) { |
||
106 | -+ | |||
175 | +58x |
- #' }+ if (!is.null(names(x)) && any(names(x) != "")) TRUE else unlist(lapply(x, any_names)) |
||
107 | +176 |
- #'+ } else { |
||
108 | -+ | |||
177 | +40x |
- validate_one_row_per_id <- function(x, key = c("USUBJID", "STUDYID")) {+ FALSE |
||
109 | -! | +|||
178 | +
- validate(need(!any(duplicated(x[key])), paste("Found more than one row per id.")))+ } |
|||
110 | +179 |
- }+ ) |
||
111 | +180 |
-
+ } |
112 | +1 |
- #' Validates that vector includes all expected values+ #' Validate that dataset has a minimum number of observations |
|
113 | +2 |
#' |
|
114 | +3 |
#' @description `r lifecycle::badge("stable")` |
|
115 | +4 |
- #' @param x values to test. All must be in \code{choices}+ #' @param x a data.frame |
|
116 | +5 |
- #' @param choices a vector to test for values of \code{x}+ #' @param min_nrow minimum number of rows in \code{x} |
|
117 | +6 |
- #' @param msg warning message to display+ #' @param complete \code{logical} default \code{FALSE} when set to \code{TRUE} then complete cases are checked. |
|
118 | +7 |
- #'+ #' @param allow_inf \code{logical} default \code{TRUE} when set to \code{FALSE} then error thrown if any values are |
|
119 | +8 |
- #' @details This function is a wrapper for `shiny::validate`.+ #' infinite. |
|
120 | +9 |
- #'+ #' @param msg (`character(1)`) additional message to display alongside the default message. |
|
121 | +10 |
- #' @export+ #' |
|
122 | +11 |
- #'+ #' @details This function is a wrapper for `shiny::validate`. |
|
123 | +12 |
- #' @examples+ #' |
|
124 | +13 |
- #' ui <- fluidPage(+ #' @export |
|
125 | +14 |
- #' selectInput(+ #' |
|
126 | +15 |
- #' "species",+ #' @examples |
|
127 | +16 |
- #' "Select species",+ #' library(teal) |
|
128 | +17 |
- #' choices = c("setosa", "versicolor", "virginica", "unknown species"),+ #' ui <- fluidPage( |
|
129 | +18 |
- #' selected = "setosa",+ #' sliderInput("len", "Max Length of Sepal", |
|
130 | +19 |
- #' multiple = FALSE+ #' min = 4.3, max = 7.9, value = 5 |
|
131 | +20 |
#' ), |
|
132 | +21 |
- #' verbatimTextOutput("summary")+ #' plotOutput("plot") |
|
133 | +22 |
#' ) |
|
134 | +23 |
#' |
|
135 | +24 |
#' server <- function(input, output) { |
|
136 | +25 |
- #' output$summary <- renderPrint({+ #' output$plot <- renderPlot({ |
|
137 | +26 |
- #' validate_in(input$species, iris$Species, "Species does not exist.")+ #' df <- iris[iris$Sepal.Length <= input$len, ] |
|
138 | +27 |
- #' nrow(iris[iris$Species == input$species, ])+ #' validate_has_data( |
|
139 | +28 |
- #' })+ #' iris_f, |
|
140 | +29 |
- #' }+ #' min_nrow = 10, |
|
141 | +30 |
- #' if (interactive()) {+ #' complete = FALSE, |
|
142 | +31 |
- #' shinyApp(ui, server)+ #' msg = "Please adjust Max Length of Sepal" |
|
143 | +32 |
- #' }+ #' ) |
|
144 | +33 |
#' |
|
145 | +34 |
- validate_in <- function(x, choices, msg) {+ #' hist(iris_f$Sepal.Length, breaks = 5) |
|
146 | -! | +||
35 | +
- validate(need(length(x) > 0 && length(choices) > 0 && all(x %in% choices), msg))+ #' }) |
||
147 | +36 |
- }+ #' } |
|
148 | +37 |
-
+ #' if (interactive()) { |
|
149 | +38 |
- #' Validates that vector has length greater than 0+ #' shinyApp(ui, server) |
|
150 | +39 |
- #'+ #' } |
|
151 | +40 |
- #' @description `r lifecycle::badge("stable")`+ #' |
|
152 | +41 |
- #' @param x vector+ validate_has_data <- function(x, |
|
153 | +42 |
- #' @param msg message to display+ min_nrow = NULL, |
|
154 | +43 |
- #'+ complete = FALSE, |
|
155 | +44 |
- #' @details This function is a wrapper for `shiny::validate`.+ allow_inf = TRUE, |
|
156 | +45 |
- #'+ msg = NULL) { |
|
157 | -+ | ||
46 | +17x |
- #' @export+ checkmate::assert_string(msg, null.ok = TRUE) |
|
158 | -+ | ||
47 | +15x | +
+ checkmate::assert_data_frame(x)+ |
+ |
48 | +15x |
- #'+ if (!is.null(min_nrow)) { |
|
159 | -+ | ||
49 | +15x |
- #' @examples+ if (complete) { |
|
160 | -+ | ||
50 | +5x |
- #' data <- data.frame(+ complete_index <- stats::complete.cases(x) |
|
161 | -+ | ||
51 | +5x |
- #' id = c(1:10, 11:20, 1:10),+ validate(need( |
|
162 | -+ | ||
52 | +5x |
- #' strata = rep(c("A", "B"), each = 15)+ sum(complete_index) > 0 && nrow(x[complete_index, , drop = FALSE]) >= min_nrow, |
|
163 | -+ | ||
53 | +5x |
- #' )+ paste(c(paste("Number of complete cases is less than:", min_nrow), msg), collapse = "\n") |
|
164 | +54 |
- #' ui <- fluidPage(+ )) |
|
165 | +55 |
- #' selectInput("ref1", "Select strata1 to compare",+ } else { |
|
166 | -+ | ||
56 | +10x |
- #' choices = c("A", "B", "C"), selected = "A"+ validate(need( |
|
167 | -+ | ||
57 | +10x |
- #' ),+ nrow(x) >= min_nrow, |
|
168 | -+ | ||
58 | +10x |
- #' selectInput("ref2", "Select strata2 to compare",+ paste( |
|
169 | -+ | ||
59 | +10x |
- #' choices = c("A", "B", "C"), selected = "B"+ c(paste("Minimum number of records not met: >=", min_nrow, "records required."), msg), |
|
170 | -+ | ||
60 | +10x |
- #' ),+ collapse = "\n" |
|
171 | +61 |
- #' verbatimTextOutput("arm_summary")+ ) |
|
172 | +62 |
- #' )+ )) |
|
173 | +63 |
- #'+ } |
|
174 | +64 |
- #' server <- function(input, output) {+ |
|
175 | -+ | ||
65 | +10x |
- #' output$arm_summary <- renderText({+ if (!allow_inf) { |
|
176 | -+ | ||
66 | +6x |
- #' sample_1 <- data$id[data$strata == input$ref1]+ validate(need( |
|
177 | -+ | ||
67 | +6x |
- #' sample_2 <- data$id[data$strata == input$ref2]+ all(vapply(x, function(col) !is.numeric(col) || !any(is.infinite(col)), logical(1))), |
|
178 | -+ | ||
68 | +6x |
- #'+ "Dataframe contains Inf values which is not allowed." |
|
179 | +69 |
- #' validate_has_elements(sample_1, "No subjects in strata1.")+ )) |
|
180 | +70 |
- #' validate_has_elements(sample_2, "No subjects in strata2.")+ } |
|
181 | +71 |
- #'+ } |
|
182 | +72 |
- #' paste0(+ } |
|
183 | +73 |
- #' "Number of samples in: strata1=", length(sample_1),+ |
|
184 | +74 |
- #' " comparions strata2=", length(sample_2)+ #' Validate that dataset has unique rows for key variables |
|
185 | +75 |
- #' )+ #' |
|
186 | +76 |
- #' })+ #' @description `r lifecycle::badge("stable")` |
|
187 | +77 |
- #' }+ #' @param x a data.frame |
|
188 | +78 |
- #' if (interactive()) {+ #' @param key a vector of ID variables from \code{x} that identify unique records |
|
189 | +79 |
- #' shinyApp(ui, server)+ #' |
|
190 | +80 |
- #' }+ #' @details This function is a wrapper for `shiny::validate`. |
|
191 | +81 |
- validate_has_elements <- function(x, msg) {- |
- |
192 | -! | -
- validate(need(length(x) > 0, msg))+ #' |
|
193 | +82 |
- }+ #' @export |
|
194 | +83 |
-
+ #' |
|
195 | +84 |
- #' Validates no intersection between two vectors+ #' @examples |
|
196 | +85 |
- #'+ #' iris$id <- rep(1:50, times = 3) |
|
197 | +86 |
- #' @description `r lifecycle::badge("stable")`+ #' ui <- fluidPage( |
|
198 | +87 |
- #' @param x vector+ #' selectInput( |
|
199 | +88 |
- #' @param y vector+ #' inputId = "species", |
|
200 | +89 |
- #' @param msg message to display if \code{x} and \code{y} intersect+ #' label = "Select species", |
|
201 | +90 |
- #'+ #' choices = c("setosa", "versicolor", "virginica"), |
|
202 | +91 |
- #' @details This function is a wrapper for `shiny::validate`.+ #' selected = "setosa", |
|
203 | +92 |
- #'+ #' multiple = TRUE |
|
204 | +93 |
- #' @export+ #' ), |
|
205 | +94 |
- #'+ #' plotOutput("plot") |
|
206 | +95 |
- #' @examples+ #' ) |
|
207 | +96 |
- #' data <- data.frame(+ #' server <- function(input, output) { |
|
208 | +97 |
- #' id = c(1:10, 11:20, 1:10),+ #' output$plot <- renderPlot({ |
|
209 | +98 |
- #' strata = rep(c("A", "B", "C"), each = 10)+ #' iris_f <- iris[iris$Species %in% input$species, ] |
|
210 | +99 |
- #' )+ #' validate_one_row_per_id(iris_f, key = c("id")) |
|
211 | +100 |
#' |
|
212 | +101 |
- #' ui <- fluidPage(+ #' hist(iris_f$Sepal.Length, breaks = 5) |
|
213 | +102 |
- #' selectInput("ref1", "Select strata1 to compare",+ #' }) |
|
214 | +103 |
- #' choices = c("A", "B", "C"),+ #' } |
|
215 | +104 |
- #' selected = "A"+ #' if (interactive()) { |
|
216 | +105 |
- #' ),+ #' shinyApp(ui, server) |
|
217 | +106 |
- #' selectInput("ref2", "Select strata2 to compare",+ #' } |
|
218 | +107 |
- #' choices = c("A", "B", "C"),+ #' |
|
219 | +108 |
- #' selected = "B"+ validate_one_row_per_id <- function(x, key = c("USUBJID", "STUDYID")) {+ |
+ |
109 | +! | +
+ validate(need(!any(duplicated(x[key])), paste("Found more than one row per id."))) |
|
220 | +110 |
- #' ),+ } |
|
221 | +111 |
- #' verbatimTextOutput("summary")+ |
|
222 | +112 |
- #' )+ #' Validates that vector includes all expected values |
|
223 | +113 |
#' |
|
224 | +114 |
- #' server <- function(input, output) {+ #' @description `r lifecycle::badge("stable")` |
|
225 | +115 |
- #' output$summary <- renderText({+ #' @param x values to test. All must be in \code{choices} |
|
226 | +116 |
- #' sample_1 <- data$id[data$strata == input$ref1]+ #' @param choices a vector to test for values of \code{x} |
|
227 | +117 |
- #' sample_2 <- data$id[data$strata == input$ref2]+ #' @param msg warning message to display |
|
228 | +118 |
#' |
|
229 | +119 |
- #' validate_no_intersection(+ #' @details This function is a wrapper for `shiny::validate`. |
|
230 | +120 |
- #' sample_1, sample_2,+ #' |
|
231 | +121 |
- #' "subjects within strata1 and strata2 cannot overlap"+ #' @export |
|
232 | +122 |
- #' )+ #' |
|
233 | +123 |
- #' paste0(+ #' @examples |
|
234 | +124 |
- #' "Number of subject in: reference treatment=", length(sample_1),+ #' ui <- fluidPage( |
|
235 | +125 |
- #' " comparions treatment=", length(sample_2)+ #' selectInput( |
|
236 | +126 |
- #' )+ #' "species", |
|
237 | +127 |
- #' })+ #' "Select species", |
|
238 | +128 |
- #' }+ #' choices = c("setosa", "versicolor", "virginica", "unknown species"), |
|
239 | +129 |
- #' if (interactive()) {+ #' selected = "setosa", |
|
240 | +130 |
- #' shinyApp(ui, server)+ #' multiple = FALSE |
|
241 | +131 |
- #' }+ #' ), |
|
242 | +132 |
- #'+ #' verbatimTextOutput("summary") |
|
243 | +133 |
- validate_no_intersection <- function(x, y, msg) {+ #' ) |
|
244 | -! | +||
134 | +
- validate(need(length(intersect(x, y)) == 0, msg))+ #' |
||
245 | +135 |
- }+ #' server <- function(input, output) { |
|
246 | +136 |
-
+ #' output$summary <- renderPrint({ |
|
247 | +137 |
-
+ #' validate_in(input$species, iris$Species, "Species does not exist.") |
|
248 | +138 |
- #' Validates that dataset contains specific variable+ #' nrow(iris[iris$Species == input$species, ]) |
|
249 | +139 |
- #'+ #' }) |
|
250 | +140 |
- #' @description `r lifecycle::badge("stable")`+ #' } |
|
251 | +141 |
- #' @param data a data.frame+ #' if (interactive()) { |
|
252 | +142 |
- #' @param varname name of variable in \code{data}+ #' shinyApp(ui, server) |
|
253 | +143 |
- #' @param msg message to display if \code{data} does not include \code{varname}+ #' } |
|
254 | +144 |
#' |
|
255 | +145 |
- #' @details This function is a wrapper for `shiny::validate`.+ validate_in <- function(x, choices, msg) {+ |
+ |
146 | +! | +
+ validate(need(length(x) > 0 && length(choices) > 0 && all(x %in% choices), msg)) |
|
256 | +147 |
- #'+ } |
|
257 | +148 |
- #' @export+ |
|
258 | +149 |
- #'+ #' Validates that vector has length greater than 0 |
|
259 | +150 |
- #' @examples+ #' |
|
260 | +151 |
- #' data <- data.frame(+ #' @description `r lifecycle::badge("stable")` |
|
261 | +152 |
- #' one = rep("a", length.out = 20),+ #' @param x vector |
|
262 | +153 |
- #' two = rep(c("a", "b"), length.out = 20)+ #' @param msg message to display |
|
263 | +154 |
- #' )+ #' |
|
264 | +155 |
- #' ui <- fluidPage(+ #' @details This function is a wrapper for `shiny::validate`. |
|
265 | +156 |
- #' selectInput(+ #' |
|
266 | +157 |
- #' "var",+ #' @export |
|
267 | +158 |
- #' "Select variable",+ #' |
|
268 | +159 |
- #' choices = c("one", "two", "three", "four"),+ #' @examples |
|
269 | +160 |
- #' selected = "one"+ #' data <- data.frame( |
|
270 | +161 |
- #' ),+ #' id = c(1:10, 11:20, 1:10), |
|
271 | +162 |
- #' verbatimTextOutput("summary")+ #' strata = rep(c("A", "B"), each = 15) |
|
272 | +163 |
#' ) |
|
273 | +164 |
- #'+ #' ui <- fluidPage( |
|
274 | +165 |
- #' server <- function(input, output) {+ #' selectInput("ref1", "Select strata1 to compare", |
|
275 | +166 |
- #' output$summary <- renderText({+ #' choices = c("A", "B", "C"), selected = "A" |
|
276 | +167 |
- #' validate_has_variable(data, input$var)+ #' ), |
|
277 | +168 |
- #' paste0("Selected treatment variables: ", paste(input$var, collapse = ", "))+ #' selectInput("ref2", "Select strata2 to compare", |
|
278 | +169 |
- #' })+ #' choices = c("A", "B", "C"), selected = "B" |
|
279 | +170 |
- #' }+ #' ), |
|
280 | +171 |
- #' if (interactive()) {+ #' verbatimTextOutput("arm_summary") |
|
281 | +172 |
- #' shinyApp(ui, server)+ #' ) |
|
282 | +173 |
- #' }+ #' |
|
283 | +174 |
- validate_has_variable <- function(data, varname, msg) {+ #' server <- function(input, output) { |
|
284 | -! | +||
175 | +
- if (length(varname) != 0) {+ #' output$arm_summary <- renderText({ |
||
285 | -! | +||
176 | +
- has_vars <- varname %in% names(data)+ #' sample_1 <- data$id[data$strata == input$ref1] |
||
286 | +177 |
-
+ #' sample_2 <- data$id[data$strata == input$ref2] |
|
287 | -! | +||
178 | +
- if (!all(has_vars)) {+ #' |
||
288 | -! | +||
179 | +
- if (missing(msg)) {+ #' validate_has_elements(sample_1, "No subjects in strata1.") |
||
289 | -! | +||
180 | +
- msg <- sprintf(+ #' validate_has_elements(sample_2, "No subjects in strata2.") |
||
290 | -! | +||
181 | +
- "%s does not have the required variables: %s.",+ #' |
||
291 | -! | +||
182 | +
- deparse(substitute(data)),+ #' paste0( |
||
292 | -! | +||
183 | +
- toString(varname[!has_vars])+ #' "Number of samples in: strata1=", length(sample_1), |
||
293 | +184 |
- )+ #' " comparions strata2=", length(sample_2) |
|
294 | +185 |
- }+ #' ) |
|
295 | -! | +||
186 | +
- validate(need(FALSE, msg))+ #' }) |
||
296 | +187 |
- }+ #' } |
|
297 | +188 |
- }+ #' if (interactive()) { |
|
298 | +189 |
- }+ #' shinyApp(ui, server) |
|
299 | +190 |
-
+ #' } |
|
300 | +191 |
- #' Validate that variables has expected number of levels+ validate_has_elements <- function(x, msg) { |
|
301 | -+ | ||
192 | +! |
- #'+ validate(need(length(x) > 0, msg)) |
|
302 | +193 |
- #' @description `r lifecycle::badge("stable")`+ } |
|
303 | +194 |
- #' @param x variable name. If \code{x} is not a factor, the unique values+ |
|
304 | +195 |
- #' are treated as levels.+ #' Validates no intersection between two vectors |
|
305 | +196 |
- #' @param min_levels cutoff for minimum number of levels of \code{x}+ #' |
|
306 | +197 |
- #' @param max_levels cutoff for maximum number of levels of \code{x}+ #' @description `r lifecycle::badge("stable")` |
|
307 | +198 |
- #' @param var_name name of variable being validated for use in+ #' @param x vector |
|
308 | +199 |
- #' validation message+ #' @param y vector |
|
309 | +200 |
- #'+ #' @param msg message to display if \code{x} and \code{y} intersect |
|
310 | +201 |
- #' @details If the number of levels of \code{x} is less than \code{min_levels}+ #' |
|
311 | +202 |
- #' or greater than \code{max_levels} the validation will fail.+ #' @details This function is a wrapper for `shiny::validate`. |
|
312 | +203 |
- #' This function is a wrapper for `shiny::validate`.+ #' |
|
313 | +204 |
- #'+ #' @export |
|
314 | +205 |
- #' @export+ #' |
|
315 | +206 |
#' @examples |
|
316 | +207 |
#' data <- data.frame( |
|
317 | +208 |
- #' one = rep("a", length.out = 20),+ #' id = c(1:10, 11:20, 1:10), |
|
318 | +209 |
- #' two = rep(c("a", "b"), length.out = 20),+ #' strata = rep(c("A", "B", "C"), each = 10) |
|
319 | +210 |
- #' three = rep(c("a", "b", "c"), length.out = 20),+ #' ) |
|
320 | +211 |
- #' four = rep(c("a", "b", "c", "d"), length.out = 20),+ #' |
|
321 | +212 |
- #' stringsAsFactors = TRUE+ #' ui <- fluidPage( |
|
322 | +213 |
- #' )+ #' selectInput("ref1", "Select strata1 to compare", |
|
323 | +214 |
- #' ui <- fluidPage(+ #' choices = c("A", "B", "C"), |
|
324 | +215 |
- #' selectInput(+ #' selected = "A" |
|
325 | +216 |
- #' "var",+ #' ), |
|
326 | +217 |
- #' "Select variable",+ #' selectInput("ref2", "Select strata2 to compare", |
|
327 | +218 |
- #' choices = c("one", "two", "three", "four"),+ #' choices = c("A", "B", "C"), |
|
328 | +219 |
- #' selected = "one"+ #' selected = "B" |
|
329 | +220 |
#' ), |
|
330 | +221 |
#' verbatimTextOutput("summary") |
|
331 | +222 |
#' ) |
|
332 | +223 |
#' |
|
333 | +224 |
#' server <- function(input, output) { |
|
334 | +225 |
#' output$summary <- renderText({ |
|
335 | +226 |
- #' validate_n_levels(data[[input$var]], min_levels = 2, max_levels = 15, var_name = input$var)+ #' sample_1 <- data$id[data$strata == input$ref1] |
|
336 | +227 |
- #' paste0(+ #' sample_2 <- data$id[data$strata == input$ref2] |
|
337 | +228 |
- #' "Levels of selected treatment variable: ",+ #' |
|
338 | +229 |
- #' paste(levels(data[[input$var]]),+ #' validate_no_intersection( |
|
339 | +230 |
- #' collapse = ", "+ #' sample_1, sample_2, |
|
340 | +231 |
- #' )+ #' "subjects within strata1 and strata2 cannot overlap" |
|
341 | +232 |
#' ) |
|
342 | +233 |
- #' })+ #' paste0( |
|
343 | +234 |
- #' }+ #' "Number of subject in: reference treatment=", length(sample_1), |
|
344 | +235 |
- #' if (interactive()) {+ #' " comparions treatment=", length(sample_2) |
|
345 | +236 |
- #' shinyApp(ui, server)+ #' ) |
|
346 | +237 |
- #' }+ #' }) |
|
347 | +238 |
- validate_n_levels <- function(x, min_levels = 1, max_levels = 12, var_name) {- |
- |
348 | -! | -
- x_levels <- if (is.factor(x)) {- |
- |
349 | -! | -
- levels(x)+ #' } |
|
350 | +239 |
- } else {- |
- |
351 | -! | -
- unique(x)+ #' if (interactive()) { |
|
352 | +240 |
- }+ #' shinyApp(ui, server) |
|
353 | +241 | - - | -|
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 | -! | +||
242 | +
- sprintf(+ #' |
||
358 | -! | +||
243 | +
- "%s variable needs minimum %s level(s) and maximum %s level(s).",+ validate_no_intersection <- function(x, y, msg) { |
||
359 | +244 | ! |
- var_name, min_levels, max_levels+ validate(need(length(intersect(x, y)) == 0, msg)) |
360 | +245 |
- )+ } |
|
361 | +246 |
- ))+ |
|
362 | -! | +||
247 | +
- } else if (!is.null(min_levels)) {+ |
||
363 | -! | +||
248 | +
- validate(need(+ #' Validates that dataset contains specific variable |
||
364 | -! | +||
249 | +
- length(x_levels) >= min_levels,+ #' |
||
365 | -! | +||
250 | +
- sprintf("%s variable needs minimum %s levels(s)", var_name, min_levels)+ #' @description `r lifecycle::badge("stable")` |
||
366 | +251 |
- ))+ #' @param data a data.frame |
|
367 | -! | +||
252 | +
- } else if (!is.null(max_levels)) {+ #' @param varname name of variable in \code{data} |
||
368 | -! | +||
253 | +
- validate(need(+ #' @param msg message to display if \code{data} does not include \code{varname} |
||
369 | -! | +||
254 | +
- length(x_levels) <= max_levels,+ #' |
||
370 | -! | +||
255 | +
- sprintf("%s variable needs maximum %s level(s)", var_name, max_levels)+ #' @details This function is a wrapper for `shiny::validate`. |
||
371 | +256 |
- ))+ #' |
|
372 | +257 |
- }+ #' @export |
|
373 | +258 |
- }+ #' |
1 | +259 |
- #' Get dummy `CDISC` data+ #' @examples |
|
2 | +260 |
- #'+ #' data <- data.frame( |
|
3 | +261 |
- #' Get dummy `CDISC` data including `ADSL`, `ADAE` and `ADLB`.+ #' one = rep("a", length.out = 20), |
|
4 | +262 |
- #' Some NAs are also introduced to stress test.+ #' two = rep(c("a", "b"), length.out = 20) |
|
5 | +263 |
- #'+ #' ) |
|
6 | +264 |
- #' @return `cdisc_data`+ #' ui <- fluidPage( |
|
7 | +265 |
- #' @keywords internal+ #' selectInput( |
|
8 | +266 |
- example_cdisc_data <- function() { # nolint+ #' "var", |
|
9 | -! | +||
267 | +
- ADSL <- data.frame( # nolint+ #' "Select variable", |
||
10 | -! | +||
268 | +
- STUDYID = "study",+ #' choices = c("one", "two", "three", "four"), |
||
11 | -! | +||
269 | +
- USUBJID = 1:10,+ #' selected = "one" |
||
12 | -! | +||
270 | +
- SEX = sample(c("F", "M"), 10, replace = TRUE),+ #' ), |
||
13 | -! | +||
271 | +
- AGE = stats::rpois(10, 40)+ #' verbatimTextOutput("summary") |
||
14 | +272 |
- )+ #' ) |
|
15 | -! | +||
273 | +
- ADTTE <- rbind(ADSL, ADSL, ADSL) # nolint+ #' |
||
16 | -! | +||
274 | +
- ADTTE$PARAMCD <- rep(c("OS", "EFS", "PFS"), each = 10) # nolint+ #' server <- function(input, output) { |
||
17 | -! | +||
275 | +
- ADTTE$AVAL <- c( # nolint+ #' output$summary <- renderText({ |
||
18 | -! | +||
276 | +
- stats::rnorm(10, mean = 700, sd = 200), # dummy OS level+ #' validate_has_variable(data, input$var) |
||
19 | -! | +||
277 | +
- stats::rnorm(10, mean = 400, sd = 100), # dummy EFS level+ #' paste0("Selected treatment variables: ", paste(input$var, collapse = ", ")) |
||
20 | -! | +||
278 | +
- stats::rnorm(10, mean = 450, sd = 200) # dummy PFS level+ #' }) |
||
21 | +279 |
- )+ #' } |
|
22 | +280 |
-
+ #' if (interactive()) { |
|
23 | -! | +||
281 | +
- ADSL$logical_test <- sample(c(TRUE, FALSE, NA), size = nrow(ADSL), replace = TRUE) # nolint+ #' shinyApp(ui, server) |
||
24 | -! | +||
282 | +
- ADSL$SEX[c(2, 5)] <- NA # nolint+ #' } |
||
25 | +283 |
-
+ validate_has_variable <- function(data, varname, msg) { |
|
26 | +284 | ! |
- res <- teal.data::cdisc_data(+ if (length(varname) != 0) { |
27 | +285 | ! |
- ADSL = ADSL,+ has_vars <- varname %in% names(data) |
28 | -! | +||
286 | +
- ADTTE = ADTTE,+ |
||
29 | +287 | ! |
- code = '+ if (!all(has_vars)) { |
30 | +288 | ! |
- ADSL <- data.frame(+ if (missing(msg)) { |
31 | +289 | ! |
- STUDYID = "study",+ msg <- sprintf( |
32 | +290 | ! |
- USUBJID = 1:10,+ "%s does not have the required variables: %s.", |
33 | +291 | ! |
- SEX = sample(c("F", "M"), 10, replace = TRUE),+ deparse(substitute(data)), |
34 | +292 | ! |
- AGE = rpois(10, 40)+ toString(varname[!has_vars]) |
35 | +293 |
- )- |
- |
36 | -! | -
- ADTTE <- rbind(ADSL, ADSL, ADSL)+ ) |
|
37 | -! | +||
294 | +
- ADTTE$PARAMCD <- rep(c("OS", "EFS", "PFS"), each = 10)+ } |
||
38 | +295 | ! |
- ADTTE$AVAL <- c(+ validate(need(FALSE, msg)) |
39 | -! | +||
296 | +
- rnorm(10, mean = 700, sd = 200),+ } |
||
40 | -! | +||
297 | +
- rnorm(10, mean = 400, sd = 100),+ } |
||
41 | -! | +||
298 | +
- rnorm(10, mean = 450, sd = 200)+ } |
||
42 | +299 |
- )+ |
|
43 | +300 |
-
+ #' Validate that variables has expected number of levels |
|
44 | -! | +||
301 | +
- ADSL$logical_test <- sample(c(TRUE, FALSE, NA), size = nrow(ADSL), replace = TRUE)+ #' |
||
45 | -! | +||
302 | +
- ADSL$SEX[c(2, 5)] <- NA+ #' @description `r lifecycle::badge("stable")` |
||
46 | +303 |
- '+ #' @param x variable name. If \code{x} is not a factor, the unique values |
|
47 | +304 |
- )+ #' are treated as levels. |
|
48 | -! | +||
305 | +
- return(res)+ #' @param min_levels cutoff for minimum number of levels of \code{x} |
||
49 | +306 |
- }+ #' @param max_levels cutoff for maximum number of levels of \code{x} |
|
50 | +307 |
-
+ #' @param var_name name of variable being validated for use in |
|
51 | +308 |
- #' Get datasets to go with example modules.+ #' validation message |
|
52 | +309 |
#' |
|
53 | +310 |
- #' Creates a nested list, the structure of which matches the module hierarchy created by `example_modules`.+ #' @details If the number of levels of \code{x} is less than \code{min_levels} |
|
54 | +311 |
- #' Each list leaf is the same `FilteredData` object.+ #' or greater than \code{max_levels} the validation will fail. |
|
55 | +312 |
- #'+ #' This function is a wrapper for `shiny::validate`. |
|
56 | +313 |
- #' @return named list of `FilteredData` objects, each with `ADSL` set.+ #' |
|
57 | +314 |
- #' @keywords internal+ #' @export |
|
58 | +315 |
- example_datasets <- function() { # nolint+ #' @examples |
|
59 | -! | +||
316 | +
- dummy_cdisc_data <- example_cdisc_data()+ #' data <- data.frame( |
||
60 | -! | +||
317 | +
- datasets <- teal_data_to_filtered_data(dummy_cdisc_data)+ #' one = rep("a", length.out = 20), |
||
61 | -! | +||
318 | +
- list(+ #' two = rep(c("a", "b"), length.out = 20), |
||
62 | -! | +||
319 | +
- "d2" = list(+ #' three = rep(c("a", "b", "c"), length.out = 20), |
||
63 | -! | +||
320 | +
- "d3" = list(+ #' four = rep(c("a", "b", "c", "d"), length.out = 20), |
||
64 | -! | +||
321 | +
- "aaa1" = datasets,+ #' stringsAsFactors = TRUE |
||
65 | -! | +||
322 | +
- "aaa2" = datasets,+ #' ) |
||
66 | -! | +||
323 | +
- "aaa3" = datasets+ #' ui <- fluidPage( |
||
67 | +324 |
- ),+ #' selectInput( |
|
68 | -! | +||
325 | +
- "bbb" = datasets+ #' "var", |
||
69 | +326 |
- ),+ #' "Select variable", |
|
70 | -! | +||
327 | +
- "ccc" = datasets+ #' choices = c("one", "two", "three", "four"), |
||
71 | +328 |
- )+ #' selected = "one" |
|
72 | +329 |
- }+ #' ), |
|
73 | +330 |
-
+ #' verbatimTextOutput("summary") |
|
74 | +331 |
- #' An example `teal` module+ #' ) |
|
75 | +332 |
#' |
|
76 | +333 |
- #' @description `r lifecycle::badge("experimental")`+ #' server <- function(input, output) { |
|
77 | +334 |
- #' @inheritParams module+ #' output$summary <- renderText({ |
|
78 | +335 |
- #' @return A `teal` module which can be included in the `modules` argument to [teal::init()].+ #' validate_n_levels(data[[input$var]], min_levels = 2, max_levels = 15, var_name = input$var) |
|
79 | +336 |
- #' @examples+ #' paste0( |
|
80 | +337 |
- #' app <- init(+ #' "Levels of selected treatment variable: ", |
|
81 | +338 |
- #' data = teal_data(IRIS = iris, MTCARS = mtcars),+ #' paste(levels(data[[input$var]]), |
|
82 | +339 |
- #' modules = example_module()+ #' collapse = ", " |
|
83 | +340 |
- #' )+ #' ) |
|
84 | +341 |
- #' if (interactive()) {+ #' ) |
|
85 | +342 |
- #' shinyApp(app$ui, app$server)+ #' }) |
|
86 | +343 |
#' } |
|
87 | +344 |
- #' @export+ #' if (interactive()) { |
|
88 | +345 |
- example_module <- function(label = "example teal module", datanames = "all") {- |
- |
89 | -44x | -
- checkmate::assert_string(label)- |
- |
90 | -44x | -
- module(+ #' shinyApp(ui, server) |
|
91 | -44x | +||
346 | +
- label,+ #' } |
||
92 | -44x | +||
347 | +
- server = function(id, data) {+ validate_n_levels <- function(x, min_levels = 1, max_levels = 12, var_name) { |
||
93 | +348 | ! |
- checkmate::assert_class(data(), "teal_data")+ x_levels <- if (is.factor(x)) { |
94 | +349 | ! |
- moduleServer(id, function(input, output, session) {+ levels(x) |
95 | -! | +||
350 | +
- ns <- session$ns+ } else { |
||
96 | +351 | ! |
- updateSelectInput(session, "dataname", choices = isolate(teal.data::datanames(data())))+ unique(x) |
97 | -! | +||
352 | +
- output$text <- renderPrint({+ } |
||
98 | -! | +||
353 | +
- req(input$dataname)+ |
||
99 | +354 | ! |
- data()[[input$dataname]]+ if (!is.null(min_levels) && !(is.null(max_levels))) { |
100 | -+ | ||
355 | +! |
- })+ validate(need( |
|
101 | +356 | ! |
- teal.widgets::verbatim_popup_srv(+ length(x_levels) >= min_levels && length(x_levels) <= max_levels, |
102 | +357 | ! |
- id = "rcode",+ sprintf( |
103 | +358 | ! |
- verbatim_content = reactive(teal.code::get_code(data())),+ "%s variable needs minimum %s level(s) and maximum %s level(s).", |
104 | +359 | ! |
- title = "Association Plot"+ var_name, min_levels, max_levels |
105 | +360 |
- )+ ) |
|
106 | +361 |
- })+ )) |
|
107 | -+ | ||
362 | +! |
- },+ } else if (!is.null(min_levels)) { |
|
108 | -44x | +||
363 | +! |
- ui = function(id) {+ validate(need( |
|
109 | +364 | ! |
- ns <- NS(id)+ length(x_levels) >= min_levels, |
110 | +365 | ! |
- teal.widgets::standard_layout(+ sprintf("%s variable needs minimum %s levels(s)", var_name, min_levels)+ |
+
366 | ++ |
+ )) |
|
111 | +367 | ! |
- output = verbatimTextOutput(ns("text")),+ } else if (!is.null(max_levels)) { |
112 | +368 | ! |
- encoding = div(+ validate(need( |
113 | +369 | ! |
- selectInput(ns("dataname"), "Choose a dataset", choices = NULL),+ length(x_levels) <= max_levels, |
114 | +370 | ! |
- teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")+ sprintf("%s variable needs maximum %s level(s)", var_name, max_levels) |
115 | +371 |
- )+ )) |
|
116 | +372 |
- )+ } |
|
117 | +373 |
- },+ } |
|
118 | -44x | +
1 | +
- datanames = datanames+ #' Get dummy `CDISC` data |
||
119 | +2 |
- )+ #' |
|
120 | +3 |
- }+ #' Get dummy `CDISC` data including `ADSL`, `ADAE` and `ADLB`. |
|
121 | +4 |
-
+ #' Some NAs are also introduced to stress test. |
|
122 | +5 |
-
+ #' |
|
123 | +6 |
- #' Get example modules.+ #' @return `cdisc_data` |
|
124 | +7 |
- #'+ #' @keywords internal |
|
125 | +8 |
- #' Creates an example hierarchy of `teal_modules` from which a `teal` app can be created.+ example_cdisc_data <- function() { # nolint |
|
126 | -+ | ||
9 | +! |
- #' @param datanames (`character`)\cr+ ADSL <- data.frame( # nolint+ |
+ |
10 | +! | +
+ STUDYID = "study",+ |
+ |
11 | +! | +
+ USUBJID = 1:10,+ |
+ |
12 | +! | +
+ SEX = sample(c("F", "M"), 10, replace = TRUE),+ |
+ |
13 | +! | +
+ AGE = stats::rpois(10, 40) |
|
127 | +14 |
- #' names of the datasets to be used in the example modules. Possible choices are `ADSL`, `ADTTE`.+ )+ |
+ |
15 | +! | +
+ ADTTE <- rbind(ADSL, ADSL, ADSL) # nolint+ |
+ |
16 | +! | +
+ ADTTE$PARAMCD <- rep(c("OS", "EFS", "PFS"), each = 10) # nolint+ |
+ |
17 | +! | +
+ ADTTE$AVAL <- c( # nolint+ |
+ |
18 | +! | +
+ stats::rnorm(10, mean = 700, sd = 200), # dummy OS level+ |
+ |
19 | +! | +
+ stats::rnorm(10, mean = 400, sd = 100), # dummy EFS level+ |
+ |
20 | +! | +
+ stats::rnorm(10, mean = 450, sd = 200) # dummy PFS level |
|
128 | +21 |
- #' @return `teal_modules`+ ) |
|
129 | +22 |
- #' @keywords internal+ + |
+ |
23 | +! | +
+ ADSL$logical_test <- sample(c(TRUE, FALSE, NA), size = nrow(ADSL), replace = TRUE) # nolint+ |
+ |
24 | +! | +
+ ADSL$SEX[c(2, 5)] <- NA # nolint |
|
130 | +25 |
- example_modules <- function(datanames = c("ADSL", "ADTTE")) {+ |
|
131 | +26 | ! |
- checkmate::assert_subset(datanames, c("ADSL", "ADTTE"))+ res <- teal.data::cdisc_data( |
132 | +27 | ! |
- mods <- modules(+ ADSL = ADSL, |
133 | +28 | ! |
- label = "d1",+ ADTTE = ADTTE, |
134 | +29 | ! |
- modules(+ code = ' |
135 | +30 | ! |
- label = "d2",+ ADSL <- data.frame( |
136 | +31 | ! |
- modules(+ STUDYID = "study", |
137 | +32 | ! |
- label = "d3",+ USUBJID = 1:10, |
138 | +33 | ! |
- example_module(label = "aaa1", datanames = datanames),+ SEX = sample(c("F", "M"), 10, replace = TRUE), |
139 | +34 | ! |
- example_module(label = "aaa2", datanames = datanames),+ AGE = rpois(10, 40) |
140 | -! | +||
35 | +
- example_module(label = "aaa3", datanames = datanames)+ ) |
||
141 | -+ | ||
36 | +! |
- ),+ ADTTE <- rbind(ADSL, ADSL, ADSL) |
|
142 | +37 | ! |
- example_module(label = "bbb", datanames = datanames)+ ADTTE$PARAMCD <- rep(c("OS", "EFS", "PFS"), each = 10) |
143 | -+ | ||
38 | +! |
- ),+ ADTTE$AVAL <- c( |
|
144 | +39 | ! |
- example_module(label = "ccc", datanames = datanames)+ rnorm(10, mean = 700, sd = 200), |
145 | -+ | ||
40 | +! |
- )+ rnorm(10, mean = 400, sd = 100), |
|
146 | +41 | ! |
- return(mods)+ rnorm(10, mean = 450, sd = 200) |
147 | +42 |
- }+ ) |
1 | +43 |
- #' Show R Code Modal+ |
|
2 | -+ | ||
44 | +! |
- #'+ ADSL$logical_test <- sample(c(TRUE, FALSE, NA), size = nrow(ADSL), replace = TRUE) |
|
3 | -+ | ||
45 | +! |
- #' @export+ ADSL$SEX[c(2, 5)] <- NA |
|
4 | +46 |
- #' @description `r lifecycle::badge("stable")`+ ' |
|
5 | +47 |
- #' Use the [shiny::showModal()] function to show the R code inside.+ ) |
|
6 | -+ | ||
48 | +! |
- #'+ return(res) |
|
7 | +49 |
- #' @param title (`character(1)`)\cr+ } |
|
8 | +50 |
- #' Title of the modal, displayed in the first comment of the R-code.+ |
|
9 | +51 |
- #' @param rcode (`character`)\cr+ #' Get datasets to go with example modules. |
|
10 | +52 |
- #' vector with R code to show inside the modal.+ #' |
|
11 | +53 |
- #' @param session (`ShinySession` optional)\cr+ #' Creates a nested list, the structure of which matches the module hierarchy created by `example_modules`. |
|
12 | +54 |
- #' `shiny` Session object, if missing then [shiny::getDefaultReactiveDomain()] is used.+ #' Each list leaf is the same `FilteredData` object. |
|
13 | +55 |
#' |
|
14 | +56 |
- #' @references [shiny::showModal()]+ #' @return named list of `FilteredData` objects, each with `ADSL` set. |
|
15 | +57 |
- show_rcode_modal <- function(title = NULL, rcode, session = getDefaultReactiveDomain()) {+ #' @keywords internal |
|
16 | -! | +||
58 | +
- rcode <- paste(rcode, collapse = "\n")+ example_datasets <- function() { # nolint |
||
17 | -+ | ||
59 | +! |
-
+ dummy_cdisc_data <- example_cdisc_data() |
|
18 | +60 | ! |
- ns <- session$ns+ datasets <- teal_data_to_filtered_data(dummy_cdisc_data) |
19 | +61 | ! |
- showModal(modalDialog(+ list( |
20 | +62 | ! |
- tagList(+ "d2" = list( |
21 | +63 | ! |
- tags$div(+ "d3" = list( |
22 | +64 | ! |
- actionButton(ns("copyRCode"), "Copy to Clipboard", `data-clipboard-target` = paste0("#", ns("r_code"))),+ "aaa1" = datasets, |
23 | +65 | ! |
- modalButton("Dismiss"),+ "aaa2" = datasets, |
24 | +66 | ! |
- style = "mb-4"+ "aaa3" = datasets |
25 | +67 |
), |
|
26 | +68 | ! |
- tags$div(tags$pre(id = ns("r_code"), rcode)),+ "bbb" = datasets |
27 | +69 |
), |
|
28 | +70 | ! |
- title = title,+ "ccc" = datasets |
29 | -! | +||
71 | +
- footer = tagList(+ ) |
||
30 | -! | +||
72 | +
- actionButton(ns("copyRCode"), "Copy to Clipboard", `data-clipboard-target` = paste0("#", ns("r_code"))),+ } |
||
31 | -! | +||
73 | +
- modalButton("Dismiss")+ + |
+ ||
74 | ++ |
+ #' An example `teal` module |
|
32 | +75 |
- ),+ #' |
|
33 | -! | +||
76 | +
- size = "l",+ #' @description `r lifecycle::badge("experimental")` |
||
34 | -! | +||
77 | +
- easyClose = TRUE+ #' @inheritParams module |
||
35 | +78 |
- ))+ #' @return A `teal` module which can be included in the `modules` argument to [teal::init()]. |
|
36 | +79 |
-
+ #' @examples |
|
37 | -! | +||
80 | +
- return(NULL)+ #' app <- init( |
||
38 | +81 |
- }+ #' data = teal_data(IRIS = iris, MTCARS = mtcars), |
1 | +82 |
- #' Generates library calls from current session info+ #' modules = example_module() |
||
2 | +83 |
- #'+ #' ) |
||
3 | +84 |
- #' Function to create multiple library calls out of current session info to make reproducible code works.+ #' if (interactive()) { |
||
4 | +85 |
- #'+ #' shinyApp(app$ui, app$server) |
||
5 | +86 |
- #' @return Character object contain code+ #' } |
||
6 | +87 |
- #' @keywords internal+ #' @export |
||
7 | +88 |
- get_rcode_libraries <- function() {+ example_module <- function(label = "example teal module", datanames = "all") { |
||
8 | -5x | +89 | +44x |
- vapply(+ checkmate::assert_string(label) |
9 | -5x | +90 | +44x |
- utils::sessionInfo()$otherPkgs,+ module( |
10 | -5x | +91 | +44x |
- function(x) {+ label, |
11 | -90x | -
- paste0("library(", x$Package, ")")- |
- ||
12 | -+ | 92 | +44x |
- },+ server = function(id, data) { |
13 | -5x | +|||
93 | +! |
- character(1)+ checkmate::assert_class(data(), "teal_data") |
||
14 | -+ | |||
94 | +! |
- ) %>%+ moduleServer(id, function(input, output, session) { |
||
15 | -+ | |||
95 | +! |
- # put it into reverse order to correctly simulate executed code+ ns <- session$ns |
||
16 | -5x | +|||
96 | +! |
- rev() %>%+ updateSelectInput(session, "dataname", choices = isolate(teal.data::datanames(data()))) |
||
17 | -5x | +|||
97 | +! |
- paste0(sep = "\n") %>%+ output$text <- renderPrint({ |
||
18 | -5x | +|||
98 | +! |
- paste0(collapse = "")+ req(input$dataname) |
||
19 | -+ | |||
99 | +! |
- }+ data()[[input$dataname]] |
||
20 | +100 |
-
+ }) |
||
21 | -+ | |||
101 | +! |
-
+ teal.widgets::verbatim_popup_srv( |
||
22 | -+ | |||
102 | +! |
-
+ id = "rcode", |
||
23 | -+ | |||
103 | +! |
- get_rcode_str_install <- function() {+ verbatim_content = reactive(teal.code::get_code(data())), |
||
24 | -9x | +|||
104 | +! |
- code_string <- getOption("teal.load_nest_code")+ title = "Association Plot" |
||
25 | +105 | - - | -||
26 | -9x | -
- if (!is.null(code_string) && is.character(code_string)) {- |
- ||
27 | -2x | -
- return(code_string)+ ) |
||
28 | +106 |
- }+ }) |
||
29 | +107 |
-
+ }, |
||
30 | -7x | +108 | +44x |
- return("# Add any code to install/load your NEST environment here\n")+ ui = function(id) { |
31 | -+ | |||
109 | +! |
- }+ ns <- NS(id) |
||
32 | -+ | |||
110 | +! |
-
+ teal.widgets::standard_layout( |
||
33 | -+ | |||
111 | +! | +
+ output = verbatimTextOutput(ns("text")),+ |
+ ||
112 | +! |
- #' Get datasets code+ encoding = div( |
||
34 | -+ | |||
113 | +! |
- #'+ selectInput(ns("dataname"), "Choose a dataset", choices = NULL), |
||
35 | -+ | |||
114 | +! |
- #' Get combined code from `FilteredData` and from `CodeClass` object.+ teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code") |
||
36 | +115 |
- #'+ ) |
||
37 | +116 |
- #' @param datanames (`character`) names of datasets to extract code from+ ) |
||
38 | +117 |
- #' @param datasets (`FilteredData`) object+ }, |
||
39 | -+ | |||
118 | +44x |
- #' @param hashes named (`list`) of hashes per dataset+ datanames = datanames |
||
40 | +119 |
- #'+ ) |
||
41 | +120 |
- #' @return Character string concatenated from the following elements:+ } |
||
42 | +121 |
- #' - data pre-processing code (from `data` argument in `init`)+ |
||
43 | +122 |
- #' - hash check of loaded objects+ |
||
44 | +123 |
- #' - filter code (if any)+ #' Get example modules. |
||
45 | +124 |
#' |
||
46 | +125 |
- #' @keywords internal+ #' Creates an example hierarchy of `teal_modules` from which a `teal` app can be created. |
||
47 | +126 |
- get_datasets_code <- function(datanames, datasets, hashes) {+ #' @param datanames (`character`)\cr |
||
48 | +127 |
- # preprocessing code- |
- ||
49 | -4x | -
- str_prepro <- teal.data:::get_code_dependency(attr(datasets, "preprocessing_code"), names = datanames)- |
- ||
50 | -4x | -
- if (length(str_prepro) == 0) {- |
- ||
51 | -! | -
- str_prepro <- "message('Preprocessing is empty')"+ #' names of the datasets to be used in the example modules. Possible choices are `ADSL`, `ADTTE`. |
||
52 | +128 |
- } else {- |
- ||
53 | -4x | -
- str_prepro <- paste(str_prepro, collapse = "\n")+ #' @return `teal_modules` |
||
54 | +129 |
- }+ #' @keywords internal |
||
55 | +130 |
-
+ example_modules <- function(datanames = c("ADSL", "ADTTE")) { |
||
56 | -+ | |||
131 | +! |
- # hash checks+ checkmate::assert_subset(datanames, c("ADSL", "ADTTE")) |
||
57 | -4x | +|||
132 | +! |
- str_hash <- vapply(datanames, function(dataname) {+ mods <- modules( |
||
58 | -6x | +|||
133 | +! |
- sprintf(+ label = "d1", |
||
59 | -6x | +|||
134 | +! |
- "stopifnot(%s == %s)",+ modules( |
||
60 | -6x | +|||
135 | +! |
- deparse1(bquote(rlang::hash(.(as.name(dataname))))),+ label = "d2", |
||
61 | -6x | +|||
136 | +! |
- deparse1(hashes[[dataname]])+ modules( |
||
62 | -+ | |||
137 | +! |
- )+ label = "d3", |
||
63 | -4x | +|||
138 | +! |
- }, character(1))+ example_module(label = "aaa1", datanames = datanames), |
||
64 | -4x | +|||
139 | +! |
- str_hash <- paste(str_hash, collapse = "\n")+ example_module(label = "aaa2", datanames = datanames), |
||
65 | -+ | |||
140 | +! |
-
+ example_module(label = "aaa3", datanames = datanames) |
||
66 | +141 |
- # filter expressions- |
- ||
67 | -4x | -
- str_filter <- teal.slice::get_filter_expr(datasets, datanames)- |
- ||
68 | -4x | -
- if (str_filter == "") {+ ), |
||
69 | -3x | +|||
142 | +! |
- str_filter <- character(0)+ example_module(label = "bbb", datanames = datanames) |
||
70 | +143 |
- }+ ), |
||
71 | -+ | |||
144 | +! |
-
+ example_module(label = "ccc", datanames = datanames) |
||
72 | +145 |
- # concatenate all code- |
- ||
73 | -4x | -
- str_code <- paste(c(str_prepro, str_hash, str_filter), collapse = "\n\n")+ ) |
||
74 | -4x | +|||
146 | +! |
- sprintf("%s\n", str_code)+ return(mods) |
||
75 | +147 |
}@@ -27454,2193 +27399,2262 @@ teal coverage - 63.30% |
1 |
- #' Create a `teal` module for previewing a report+ setOldClass("teal_data_module") |
||
2 |
- #'+ |
||
3 |
- #' @description `r lifecycle::badge("experimental")`+ #' Evaluate the code in the qenv environment |
||
4 |
- #' This function wraps [teal.reporter::reporter_previewer_ui()] and+ #' @name eval_code |
||
5 |
- #' [teal.reporter::reporter_previewer_srv()] into a `teal_module` to be+ #' @description |
||
6 |
- #' used in `teal` applications.+ #' Given code is evaluated in the `qenv` environment of `teal_data` reactive defined in `teal_data_module`. |
||
7 |
- #'+ #' @param object (`teal_data_module`) |
||
8 |
- #' If you are creating a `teal` application using [teal::init()] then this+ #' @inheritParams teal.code::eval_code |
||
9 |
- #' module will be added to your application automatically if any of your `teal modules`+ #' @return Returns a `teal_data_module` object. |
||
10 |
- #' support report generation.+ #' @importMethodsFrom teal.code eval_code |
||
11 |
- #'+ #' @importFrom methods setMethod |
||
12 |
- #' @inheritParams module+ NULL |
||
13 |
- #' @param server_args (`named list`)\cr+ |
||
14 |
- #' Arguments passed to [teal.reporter::reporter_previewer_srv()].+ #' @rdname eval_code |
||
15 |
- #' @return `teal_module` (extended with `teal_module_previewer` class) containing the `teal.reporter` previewer+ #' @export |
||
16 |
- #' functionality.+ #' |
||
17 |
- #' @export+ #' @examples |
||
18 |
- reporter_previewer_module <- function(label = "Report previewer", server_args = list()) {+ #' tdm <- teal_data_module( |
||
19 | -4x | +
- checkmate::assert_string(label)+ #' ui = function(id) div(id = shiny::NS(id)("div_id")), |
|
20 | -2x | +
- checkmate::assert_list(server_args, names = "named")+ #' server = function(id) { |
|
21 | -2x | +
- checkmate::assert_true(all(names(server_args) %in% names(formals(teal.reporter::reporter_previewer_srv))))+ #' shiny::moduleServer(id, function(input, output, session) { |
|
22 |
-
+ #' shiny::reactive(teal_data(IRIS = iris)) |
||
23 | -2x | +
- logger::log_info("Initializing reporter_previewer_module")+ #' }) |
|
24 |
-
+ #' } |
||
25 | -2x | +
- srv <- function(id, reporter, ...) {+ #' ) |
|
26 | -! | +
- teal.reporter::reporter_previewer_srv(id, reporter, ...)+ #' eval_code(tdm, "IRIS <- subset(IRIS, Species == 'virginica')") |
|
27 |
- }+ setMethod("eval_code", signature = c("teal_data_module", "character"), function(object, code) { |
||
28 | -+ | 13x |
-
+ teal_data_module( |
29 | -2x | +13x |
- ui <- function(id, ...) {+ ui = function(id) { |
30 | -! | +1x |
- teal.reporter::reporter_previewer_ui(id, ...)+ ns <- NS(id) |
31 | -+ | 1x |
- }+ object$ui(ns("mutate_inner")) |
32 |
-
+ }, |
||
33 | -2x | +13x |
- module <- module(+ server = function(id) { |
34 | -2x | +11x |
- label = "temporary label",+ moduleServer(id, function(input, output, session) { |
35 | -2x | +11x |
- server = srv, ui = ui,+ teal_data_rv <- object$server("mutate_inner") |
36 | -2x | +
- server_args = server_args, ui_args = list(), datanames = NULL+ |
|
37 | -+ | 11x |
- )+ if (!is.reactive(teal_data_rv)) { |
38 | -+ | 1x |
- # Module is created with a placeholder label and the label is changed later.+ stop("The `teal_data_module` must return a reactive expression.", call. = FALSE) |
39 |
- # This is to prevent another module being labeled "Report previewer".+ } |
||
40 | -2x | +
- class(module) <- c("teal_module_previewer", class(module))+ |
|
41 | -2x | +10x |
- module$label <- label+ td <- eventReactive(teal_data_rv(), |
42 | -2x | +
- module+ { |
|
43 | +10x | +
+ if (inherits(teal_data_rv(), c("teal_data", "qenv.error"))) {+ |
+ |
44 | +6x | +
+ eval_code(teal_data_rv(), code)+ |
+ |
45 |
- }+ } else {+ |
+ ||
46 | +4x | +
+ teal_data_rv() |
1 | +47 |
- #' Landing Popup Module+ } |
|
2 | +48 |
- #'+ },+ |
+ |
49 | +10x | +
+ ignoreNULL = FALSE |
|
3 | +50 |
- #' @description Creates a landing welcome popup for `teal` applications.+ )+ |
+ |
51 | +10x | +
+ td |
|
4 | +52 |
- #'+ }) |
|
5 | +53 |
- #' This module is used to display a popup dialog when the application starts.+ } |
|
6 | +54 |
- #' The dialog blocks the access to the application and must be closed with a button before the application is viewed.+ ) |
|
7 | +55 |
- #'+ }) |
|
8 | +56 |
- #' @param label `character(1)` the label of the module.+ |
|
9 | +57 |
- #' @param title `character(1)` the text to be displayed as a title of the popup.+ #' @rdname eval_code |
|
10 | +58 |
- #' @param content The content of the popup. Passed to `...` of `shiny::modalDialog`. Can be a `character`+ #' @export |
|
11 | +59 | ++ |
+ setMethod("eval_code", signature = c("teal_data_module", "language"), function(object, code) {+ |
+
60 | +1x | +
+ eval_code(object, code = paste(lang2calls(code), collapse = "\n"))+ |
+ |
61 | ++ |
+ })+ |
+ |
62 |
- #' or a list of `shiny.tag`s. See examples.+ |
||
12 | +63 |
- #' @param buttons `shiny.tag` or a list of tags (`tagList`). Typically a `modalButton` or `actionButton`. See examples.+ #' @rdname eval_code |
|
13 | +64 |
- #'+ #' @export |
|
14 | +65 |
- #' @return A `teal_module` (extended with `teal_landing_module` class) to be used in `teal` applications.+ setMethod("eval_code", signature = c("teal_data_module", "expression"), function(object, code) { |
|
15 | -+ | ||
66 | +6x |
- #'+ eval_code(object, code = paste(lang2calls(code), collapse = "\n")) |
|
16 | +67 |
- #' @examples+ }) |
|
17 | +68 |
- #' app1 <- teal::init(+ |
|
18 | +69 |
- #' data = teal_data(iris = iris),+ #' @inherit teal.code::within.qenv params title details |
|
19 | +70 |
- #' modules = teal::modules(+ #' @description |
|
20 | +71 |
- #' teal::landing_popup_module(+ #' Convenience function for evaluating inline code inside the environment of a |
|
21 | +72 |
- #' content = "A place for the welcome message or a disclaimer statement.",+ #' `teal_data_module` |
|
22 | +73 |
- #' buttons = modalButton("Proceed")+ #' |
|
23 | +74 |
- #' ),+ #' @param data (`teal_data_module`) object |
|
24 | +75 |
- #' example_module()+ #' @return Returns a `teal_data_module` object with a delayed evaluation of `expr` |
|
25 | +76 |
- #' )+ #' when module. |
|
26 | +77 |
- #' )+ #' @export |
|
27 | +78 |
- #' if (interactive()) {+ #' @seealso [base::within()], [teal_data_module()] |
|
28 | +79 |
- #' shinyApp(app1$ui, app1$server)+ #' @examples |
|
29 | +80 |
- #' }+ #' tdm <- teal_data_module( |
|
30 | +81 |
- #'+ #' ui = function(id) div(id = shiny::NS(id)("div_id")), |
|
31 | +82 |
- #' app2 <- teal::init(+ #' server = function(id) { |
|
32 | +83 |
- #' data = teal_data(iris = iris),+ #' shiny::moduleServer(id, function(input, output, session) { |
|
33 | +84 |
- #' modules = teal::modules(+ #' shiny::reactive(teal_data(IRIS = iris)) |
|
34 | +85 |
- #' teal::landing_popup_module(+ #' }) |
|
35 | +86 |
- #' title = "Welcome",+ #' } |
|
36 | +87 |
- #' content = tags$b(+ #' ) |
|
37 | +88 |
- #' "A place for the welcome message or a disclaimer statement.",+ #' within(tdm, IRIS <- subset(IRIS, Species == "virginica")) |
|
38 | +89 |
- #' style = "color: red;"+ within.teal_data_module <- function(data, expr, ...) { |
|
39 | -+ | ||
90 | +6x |
- #' ),+ expr <- substitute(expr) |
|
40 | -+ | ||
91 | +6x |
- #' buttons = tagList(+ extras <- list(...) |
|
41 | +92 |
- #' modalButton("Proceed"),+ |
|
42 | +93 |
- #' actionButton("read", "Read more",+ # Add braces for consistency. |
|
43 | -+ | ||
94 | +6x |
- #' onclick = "window.open('http://google.com', '_blank')"+ if (!identical(as.list(expr)[[1L]], as.symbol("{"))) { |
|
44 | -+ | ||
95 | +6x |
- #' ),+ expr <- call("{", expr) |
|
45 | +96 |
- #' actionButton("close", "Reject", onclick = "window.close()")+ } |
|
46 | +97 |
- #' )+ |
|
47 | -+ | ||
98 | +6x |
- #' ),+ calls <- as.list(expr)[-1] |
|
48 | +99 |
- #' example_module()+ |
|
49 | +100 |
- #' )+ # Inject extra values into expressions. |
|
50 | -+ | ||
101 | +6x |
- #' )+ calls <- lapply(calls, function(x) do.call(substitute, list(x, env = extras))) |
|
51 | +102 |
- #'+ |
|
52 | -+ | ||
103 | +6x |
- #' if (interactive()) {+ eval_code(object = data, code = as.expression(calls)) |
|
53 | +104 |
- #' shinyApp(app2$ui, app2$server)+ } |
54 | +1 |
- #' }+ #' Generates library calls from current session info |
|
55 | +2 |
#' |
|
56 | +3 |
- #' @export+ #' Function to create multiple library calls out of current session info to make reproducible code works. |
|
57 | +4 |
- landing_popup_module <- function(label = "Landing Popup",+ #' |
|
58 | +5 |
- title = NULL,+ #' @return Character object contain code |
|
59 | +6 |
- content = NULL,+ #' @keywords internal |
|
60 | +7 |
- buttons = modalButton("Accept")) {+ get_rcode_libraries <- function() { |
|
61 | -! | +||
8 | +5x |
- checkmate::assert_string(label)+ vapply( |
|
62 | -! | +||
9 | +5x |
- checkmate::assert_string(title, null.ok = TRUE)+ utils::sessionInfo()$otherPkgs, |
|
63 | -! | +||
10 | +5x |
- checkmate::assert_multi_class(+ function(x) { |
|
64 | -! | +||
11 | +80x |
- content,+ paste0("library(", x$Package, ")") |
|
65 | -! | +||
12 | +
- classes = c("character", "shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE+ }, |
||
66 | -+ | ||
13 | +5x |
- )+ character(1) |
|
67 | -! | +||
14 | +
- checkmate::assert_multi_class(buttons, classes = c("shiny.tag", "shiny.tag.list"))+ ) %>% |
||
68 | +15 |
-
+ # put it into reverse order to correctly simulate executed code |
|
69 | -! | +||
16 | +5x |
- logger::log_info("Initializing landing_popup_module")+ rev() %>% |
|
70 | -+ | ||
17 | +5x |
-
+ paste0(sep = "\n") %>% |
|
71 | -! | +||
18 | +5x |
- module <- module(+ paste0(collapse = "") |
|
72 | -! | +||
19 | +
- label = label,+ } |
||
73 | -! | +||
20 | +
- server = function(id) {+ |
||
74 | -! | +||
21 | +
- moduleServer(id, function(input, output, session) {+ |
||
75 | -! | +||
22 | +
- showModal(+ |
||
76 | -! | +||
23 | +
- modalDialog(+ get_rcode_str_install <- function() { |
||
77 | -! | +||
24 | +9x |
- id = "landingpopup",+ code_string <- getOption("teal.load_nest_code") |
|
78 | -! | +||
25 | +
- title = title,+ |
||
79 | -! | +||
26 | +9x |
- content,+ if (!is.null(code_string) && is.character(code_string)) { |
|
80 | -! | +||
27 | +2x |
- footer = buttons+ return(code_string) |
|
81 | +28 |
- )+ } |
|
82 | +29 |
- )+ |
|
83 | -+ | ||
30 | +7x |
- })+ return("# Add any code to install/load your NEST environment here\n") |
|
84 | +31 |
- }+ } |
|
85 | +32 |
- )+ |
|
86 | -! | +||
33 | +
- class(module) <- c("teal_module_landing", class(module))+ #' Get datasets code |
||
87 | -! | +||
34 | +
- module+ #' |
||
88 | +35 |
- }+ #' Get combined code from `FilteredData` and from `CodeClass` object. |
1 | +36 |
- #' Send input validation messages to output.+ #' |
|
2 | +37 |
- #'+ #' @param datanames (`character`) names of datasets to extract code from |
|
3 | +38 |
- #' Captures messages from `InputValidator` objects and collates them+ #' @param datasets (`FilteredData`) object |
|
4 | +39 |
- #' into one message passed to `validate`.+ #' @param hashes named (`list`) of hashes per dataset |
|
5 | +40 |
#' |
|
6 | +41 |
- #' `shiny::validate` is used to withhold rendering of an output element until+ #' @return Character string concatenated from the following elements: |
|
7 | +42 |
- #' certain conditions are met and to print a validation message in place+ #' - data pre-processing code (from `data` argument in `init`) |
|
8 | +43 |
- #' of the output element.+ #' - hash check of loaded objects |
|
9 | +44 |
- #' `shinyvalidate::InputValidator` allows to validate input elements+ #' - filter code (if any) |
|
10 | +45 |
- #' and to display specific messages in their respective input widgets.+ #' |
|
11 | +46 |
- #' `validate_inputs` provides a hybrid solution.+ #' @keywords internal |
|
12 | +47 |
- #' Given an `InputValidator` object, messages corresponding to inputs that fail validation+ get_datasets_code <- function(datanames, datasets, hashes) { |
|
13 | +48 |
- #' are extracted and placed in one validation message that is passed to a `validate`/`need` call.+ # preprocessing code |
|
14 | -+ | ||
49 | +4x |
- #' This way the input `validator` messages are repeated in the output.+ str_prepro <- teal.data:::get_code_dependency(attr(datasets, "preprocessing_code"), names = datanames) |
|
15 | -+ | ||
50 | +4x |
- #'+ if (length(str_prepro) == 0) {+ |
+ |
51 | +! | +
+ str_prepro <- "message('Preprocessing is empty')" |
|
16 | +52 |
- #' The `...` argument accepts any number of `InputValidator` objects+ } else { |
|
17 | -+ | ||
53 | +4x |
- #' or a nested list of such objects.+ str_prepro <- paste(str_prepro, collapse = "\n") |
|
18 | +54 |
- #' If `validators` are passed directly, all their messages are printed together+ } |
|
19 | +55 |
- #' under one (optional) header message specified by `header`. If a list is passed,+ |
|
20 | +56 |
- #' messages are grouped by `validator`. The list's names are used as headers+ # hash checks |
|
21 | -+ | ||
57 | +4x |
- #' for their respective message groups.+ str_hash <- vapply(datanames, function(dataname) { |
|
22 | -+ | ||
58 | +6x |
- #' If neither of the nested list elements is named, a header message is taken from `header`.+ sprintf( |
|
23 | -+ | ||
59 | +6x |
- #'+ "stopifnot(%s == %s)", |
|
24 | -+ | ||
60 | +6x |
- #' @param ... either any number of `InputValidator` objects+ deparse1(bquote(rlang::hash(.(as.name(dataname))))), |
|
25 | -+ | ||
61 | +6x |
- #' or an optionally named, possibly nested `list` of `InputValidator`+ deparse1(hashes[[dataname]]) |
|
26 | +62 |
- #' objects, see `Details`+ ) |
|
27 | -+ | ||
63 | +4x |
- #' @param header `character(1)` generic validation message; set to NULL to omit+ }, character(1)) |
|
28 | -+ | ||
64 | +4x |
- #'+ str_hash <- paste(str_hash, collapse = "\n") |
|
29 | +65 |
- #' @return+ |
|
30 | +66 |
- #' Returns NULL if the final validation call passes and a `shiny.silent.error` if it fails.+ # filter expressions |
|
31 | -+ | ||
67 | +4x |
- #'+ str_filter <- teal.slice::get_filter_expr(datasets, datanames) |
|
32 | -+ | ||
68 | +4x |
- #' @seealso [`shinyvalidate::InputValidator`], [`shiny::validate`]+ if (str_filter == "") { |
|
33 | -+ | ||
69 | +3x |
- #'+ str_filter <- character(0) |
|
34 | +70 |
- #' @examples+ } |
|
35 | +71 |
- #' library(shiny)+ |
|
36 | +72 |
- #' library(shinyvalidate)+ # concatenate all code |
|
37 | -+ | ||
73 | +4x |
- #'+ str_code <- paste(c(str_prepro, str_hash, str_filter), collapse = "\n\n")+ |
+ |
74 | +4x | +
+ sprintf("%s\n", str_code) |
|
38 | +75 |
- #' ui <- fluidPage(+ } |
39 | +1 |
- #' selectInput("method", "validation method", c("sequential", "combined", "grouped")),+ #' Landing Popup Module |
|
40 | +2 |
- #' sidebarLayout(+ #' |
|
41 | +3 |
- #' sidebarPanel(+ #' @description Creates a landing welcome popup for `teal` applications. |
|
42 | +4 |
- #' selectInput("letter", "select a letter:", c(letters[1:3], LETTERS[4:6])),+ #' |
|
43 | +5 |
- #' selectInput("number", "select a number:", 1:6),+ #' This module is used to display a popup dialog when the application starts. |
|
44 | +6 |
- #' br(),+ #' The dialog blocks the access to the application and must be closed with a button before the application is viewed. |
|
45 | +7 |
- #' selectInput("color", "select a color:",+ #' |
|
46 | +8 |
- #' c("black", "indianred2", "springgreen2", "cornflowerblue"),+ #' @param label `character(1)` the label of the module. |
|
47 | +9 |
- #' multiple = TRUE+ #' @param title `character(1)` the text to be displayed as a title of the popup. |
|
48 | +10 |
- #' ),+ #' @param content The content of the popup. Passed to `...` of `shiny::modalDialog`. Can be a `character` |
|
49 | +11 |
- #' sliderInput("size", "select point size:",+ #' or a list of `shiny.tag`s. See examples. |
|
50 | +12 |
- #' min = 0.1, max = 4, value = 0.25+ #' @param buttons `shiny.tag` or a list of tags (`tagList`). Typically a `modalButton` or `actionButton`. See examples. |
|
51 | +13 |
- #' )+ #' |
|
52 | +14 |
- #' ),+ #' @return A `teal_module` (extended with `teal_landing_module` class) to be used in `teal` applications. |
|
53 | +15 |
- #' mainPanel(plotOutput("plot"))+ #' |
|
54 | +16 |
- #' )+ #' @examples |
|
55 | +17 |
- #' )+ #' app1 <- teal::init( |
|
56 | +18 |
- #'+ #' data = teal_data(iris = iris), |
|
57 | +19 |
- #' server <- function(input, output) {+ #' modules = teal::modules( |
|
58 | +20 |
- #' # set up input validation+ #' teal::landing_popup_module( |
|
59 | +21 |
- #' iv <- InputValidator$new()+ #' content = "A place for the welcome message or a disclaimer statement.", |
|
60 | +22 |
- #' iv$add_rule("letter", sv_in_set(LETTERS, "choose a capital letter"))+ #' buttons = modalButton("Proceed") |
|
61 | +23 |
- #' iv$add_rule("number", ~ if (as.integer(.) %% 2L == 1L) "choose an even number")+ #' ), |
|
62 | +24 |
- #' iv$enable()+ #' example_module() |
|
63 | +25 |
- #' # more input validation+ #' ) |
|
64 | +26 |
- #' iv_par <- InputValidator$new()+ #' ) |
|
65 | +27 |
- #' iv_par$add_rule("color", sv_required(message = "choose a color"))+ #' if (interactive()) { |
|
66 | +28 |
- #' iv_par$add_rule("color", ~ if (length(.) > 1L) "choose only one color")+ #' shinyApp(app1$ui, app1$server) |
|
67 | +29 |
- #' iv_par$add_rule(+ #' } |
|
68 | +30 |
- #' "size",+ #' |
|
69 | +31 |
- #' sv_between(+ #' app2 <- teal::init( |
|
70 | +32 |
- #' left = 0.5, right = 3,+ #' data = teal_data(iris = iris), |
|
71 | +33 |
- #' message_fmt = "choose a value between {left} and {right}"+ #' modules = teal::modules( |
|
72 | +34 |
- #' )+ #' teal::landing_popup_module( |
|
73 | +35 |
- #' )+ #' title = "Welcome", |
|
74 | +36 |
- #' iv_par$enable()+ #' content = tags$b( |
|
75 | +37 |
- #'+ #' "A place for the welcome message or a disclaimer statement.", |
|
76 | +38 |
- #' output$plot <- renderPlot({+ #' style = "color: red;" |
|
77 | +39 |
- #' # validate output+ #' ), |
|
78 | +40 |
- #' switch(input[["method"]],+ #' buttons = tagList( |
|
79 | +41 |
- #' "sequential" = {+ #' modalButton("Proceed"), |
|
80 | +42 |
- #' validate_inputs(iv)+ #' actionButton("read", "Read more", |
|
81 | +43 |
- #' validate_inputs(iv_par, header = "Set proper graphical parameters")+ #' onclick = "window.open('http://google.com', '_blank')" |
|
82 | +44 |
- #' },+ #' ), |
|
83 | +45 |
- #' "combined" = validate_inputs(iv, iv_par),+ #' actionButton("close", "Reject", onclick = "window.close()") |
|
84 | +46 |
- #' "grouped" = validate_inputs(list(+ #' ) |
|
85 | +47 |
- #' "Some inputs require attention" = iv,+ #' ), |
|
86 | +48 |
- #' "Set proper graphical parameters" = iv_par+ #' example_module() |
|
87 | +49 |
- #' ))+ #' ) |
|
88 | +50 |
- #' )+ #' ) |
|
89 | +51 |
#' |
|
90 | +52 |
- #' plot(eruptions ~ waiting, faithful,+ #' if (interactive()) { |
|
91 | +53 |
- #' las = 1, pch = 16,+ #' shinyApp(app2$ui, app2$server) |
|
92 | +54 |
- #' col = input[["color"]], cex = input[["size"]]+ #' } |
|
93 | +55 |
- #' )+ #' |
|
94 | +56 |
- #' })+ #' @export |
|
95 | +57 |
- #' }+ landing_popup_module <- function(label = "Landing Popup", |
|
96 | +58 |
- #'+ title = NULL, |
|
97 | +59 |
- #' if (interactive()) {+ content = NULL, |
|
98 | +60 |
- #' shinyApp(ui, server)+ buttons = modalButton("Accept")) { |
|
99 | -+ | ||
61 | +! |
- #' }+ checkmate::assert_string(label) |
|
100 | -+ | ||
62 | +! |
- #'+ checkmate::assert_string(title, null.ok = TRUE) |
|
101 | -+ | ||
63 | +! |
- #' @export+ checkmate::assert_multi_class( |
|
102 | -+ | ||
64 | +! |
- #'+ content,+ |
+ |
65 | +! | +
+ classes = c("character", "shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE |
|
103 | +66 |
- validate_inputs <- function(..., header = "Some inputs require attention") {+ ) |
|
104 | -36x | +||
67 | +! |
- dots <- list(...)+ checkmate::assert_multi_class(buttons, classes = c("shiny.tag", "shiny.tag.list")) |
|
105 | -2x | +||
68 | +
- if (!is_validators(dots)) stop("validate_inputs accepts validators or a list thereof")+ + |
+ ||
69 | +! | +
+ logger::log_info("Initializing landing_popup_module") |
|
106 | +70 | ||
107 | -34x | +||
71 | +! |
- messages <- extract_validator(dots, header)+ module <- module( |
|
108 | -34x | +||
72 | +! |
- failings <- if (!any_names(dots)) {+ label = label, |
|
109 | -29x | +||
73 | +! |
- add_header(messages, header)+ server = function(id) { |
|
110 | -+ | ||
74 | +! |
- } else {+ moduleServer(id, function(input, output, session) { |
|
111 | -5x | +||
75 | +! |
- unlist(messages)+ showModal( |
|
112 | -+ | ||
76 | +! |
- }+ modalDialog( |
|
113 | -+ | ||
77 | +! |
-
+ id = "landingpopup", |
|
114 | -34x | +||
78 | +! |
- shiny::validate(shiny::need(is.null(failings), failings))+ title = title, |
|
115 | -+ | ||
79 | +! |
- }+ content, |
|
116 | -+ | ||
80 | +! |
-
+ footer = buttons |
|
117 | +81 |
- ### internal functions+ ) |
|
118 | +82 |
-
+ ) |
|
119 | +83 |
- #' @keywords internal+ }) |
|
120 | +84 |
- # recursive object type test+ } |
|
121 | +85 |
- # returns logical of length 1+ ) |
|
122 | -+ | ||
86 | +! |
- is_validators <- function(x) {+ class(module) <- c("teal_module_landing", class(module)) |
|
123 | -118x | +||
87 | +! |
- all(if (is.list(x)) unlist(lapply(x, is_validators)) else inherits(x, "InputValidator"))+ module |
|
124 | +88 |
} |
125 | +1 |
-
+ #' Data module for `teal` applications |
||
126 | +2 |
- #' @keywords internal+ #' |
||
127 | +3 |
- # test if an InputValidator object is enabled+ #' @description `r lifecycle::badge("experimental")` |
||
128 | +4 |
- # returns logical of length 1+ #' Creates `teal_data_module` object - a `shiny` module to supply or modify data in a `teal` application. |
||
129 | +5 |
- # official method requested at https://github.com/rstudio/shinyvalidate/issues/64+ #' |
||
130 | +6 |
- validator_enabled <- function(x) {- |
- ||
131 | -49x | -
- x$.__enclos_env__$private$enabled+ #' This function creates a `shiny` module that allows for running data pre-processing code after the app starts. |
||
132 | +7 |
- }+ #' The body of the server function will be run in the app rather than in the global environment. |
||
133 | +8 |
-
+ #' This means it will be run every time the app starts, so use sparingly. |
||
134 | +9 |
- #' @keywords internal+ #' |
||
135 | +10 |
- # recursively extract messages from validator list+ #' Pass this module instead of a `teal_data` object in a call to [init()]. |
||
136 | +11 |
- # returns character vector or a list of character vectors, possibly nested and named+ #' |
||
137 | +12 |
- extract_validator <- function(iv, header) {- |
- ||
138 | -113x | -
- if (inherits(iv, "InputValidator")) {+ #' See vignette \code{vignette("data-as-shiny-module", package = "teal")} for more details. |
||
139 | -49x | +|||
13 | +
- add_header(gather_messages(iv), header)+ #' |
|||
140 | +14 |
- } else {+ #' @param ui (`function(id)`)\cr |
||
141 | -58x | +|||
15 | +
- if (is.null(names(iv))) names(iv) <- rep("", length(iv))+ #' `shiny` module `ui` function; must only take `id` argument |
|||
142 | -64x | +|||
16 | +
- mapply(extract_validator, iv = iv, header = names(iv), SIMPLIFY = FALSE)+ #' @param server (`function(id)`)\cr |
|||
143 | +17 |
- }+ #' `shiny` module `ui` function; must only take `id` argument; |
||
144 | +18 |
- }+ #' must return reactive expression containing `teal_data` object |
||
145 | +19 |
-
+ #' |
||
146 | +20 |
- #' @keywords internal+ #' @return Object of class `teal_data_module`. |
||
147 | +21 |
- # collate failing messages from validator+ #' |
||
148 | +22 |
- # returns list+ #' @examples |
||
149 | +23 |
- gather_messages <- function(iv) {+ #' data <- teal_data_module( |
||
150 | -49x | +|||
24 | +
- if (validator_enabled(iv)) {+ #' ui = function(id) { |
|||
151 | -46x | +|||
25 | +
- status <- iv$validate()+ #' ns <- NS(id) |
|||
152 | -46x | +|||
26 | +
- failing_inputs <- Filter(Negate(is.null), status)+ #' actionButton(ns("submit"), label = "Load data") |
|||
153 | -46x | +|||
27 | +
- unique(lapply(failing_inputs, function(x) x[["message"]]))+ #' }, |
|||
154 | +28 |
- } else {+ #' server = function(id) { |
||
155 | -3x | +|||
29 | +
- logger::log_warn("Validator is disabled and will be omitted.")+ #' moduleServer(id, function(input, output, session) { |
|||
156 | -3x | +|||
30 | +
- list()+ #' eventReactive(input$submit, { |
|||
157 | +31 |
- }+ #' data <- within( |
||
158 | +32 |
- }+ #' teal_data(), |
||
159 | +33 |
-
+ #' { |
||
160 | +34 |
- #' @keywords internal+ #' dataset1 <- iris |
||
161 | +35 |
- # add optional header to failing messages+ #' dataset2 <- mtcars |
||
162 | +36 |
- add_header <- function(messages, header = "") {+ #' } |
||
163 | -78x | +|||
37 | +
- ans <- unlist(messages)+ #' ) |
|||
164 | -78x | +|||
38 | +
- if (length(ans) != 0L && isTRUE(nchar(header) > 0L)) {+ #' datanames(data) <- c("dataset1", "dataset2") |
|||
165 | -31x | +|||
39 | +
- ans <- c(paste0(header, "\n"), ans, "\n")+ #' |
|||
166 | +40 |
- }+ #' data |
||
167 | -78x | +|||
41 | +
- ans+ #' }) |
|||
168 | +42 |
- }+ #' }) |
||
169 | +43 |
-
+ #' } |
||
170 | +44 |
- #' @keywords internal+ #' ) |
||
171 | +45 |
- # recursively check if the object contains a named list+ #' @export |
||
172 | +46 |
- any_names <- function(x) {+ teal_data_module <- function(ui, server) { |
||
173 | -103x | +47 | +35x |
- any(+ checkmate::assert_function(ui, args = "id", nargs = 1) |
174 | -103x | +48 | +34x |
- if (is.list(x)) {+ checkmate::assert_function(server, args = "id", nargs = 1) |
175 | -58x | -
- if (!is.null(names(x)) && any(names(x) != "")) TRUE else unlist(lapply(x, any_names))- |
- ||
176 | -+ | 49 | +33x |
- } else {+ structure( |
177 | -40x | +50 | +33x |
- FALSE+ list(ui = ui, server = server), |
178 | -+ | |||
51 | +33x |
- }+ class = "teal_data_module" |
||
179 | +52 |
) |
||
180 | +53 |
}@@ -29649,77 +29663,77 @@ teal coverage - 63.30% |
1 |
- #' Data module for `teal` applications+ # This file contains Shiny modules useful for debugging and developing teal. |
||
2 |
- #'+ # We do not export the functions in this file. They are for |
||
3 |
- #' @description `r lifecycle::badge("experimental")`+ # developers only and can be accessed via `:::`. |
||
4 |
- #' Creates `teal_data_module` object - a `shiny` module to supply or modify data in a `teal` application.+ |
||
5 |
- #'+ #' Dummy module to show the filter calls generated by the right encoding panel |
||
6 |
- #' This function creates a `shiny` module that allows for running data pre-processing code after the app starts.+ #' |
||
7 |
- #' The body of the server function will be run in the app rather than in the global environment.+ #' |
||
8 |
- #' This means it will be run every time the app starts, so use sparingly.+ #' Please do not remove, this is useful for debugging teal without |
||
9 |
- #'+ #' dependencies and simplifies `\link[devtools]{load_all}` which otherwise fails |
||
10 |
- #' Pass this module instead of a `teal_data` object in a call to [init()].+ #' and avoids session restarts! |
||
12 |
- #' See vignette \code{vignette("data-as-shiny-module", package = "teal")} for more details.+ #' @param label `character` label of module |
||
13 |
- #'+ #' @keywords internal |
||
14 |
- #' @param ui (`function(id)`)\cr+ #' |
||
15 |
- #' `shiny` module `ui` function; must only take `id` argument+ #' @examples |
||
16 |
- #' @param server (`function(id)`)\cr+ #' app <- init( |
||
17 |
- #' `shiny` module `ui` function; must only take `id` argument;+ #' data = teal_data(iris = iris, mtcars = mtcars), |
||
18 |
- #' must return reactive expression containing `teal_data` object+ #' modules = teal:::filter_calls_module(), |
||
19 |
- #'+ #' header = "Simple teal app" |
||
20 |
- #' @return Object of class `teal_data_module`.+ #' ) |
||
21 |
- #'+ #' if (interactive()) { |
||
22 |
- #' @examples+ #' shinyApp(app$ui, app$server) |
||
23 |
- #' data <- teal_data_module(+ #' } |
||
24 |
- #' ui = function(id) {+ filter_calls_module <- function(label = "Filter Calls Module") { # nolint |
||
25 | -+ | ! |
- #' ns <- NS(id)+ checkmate::assert_string(label) |
26 |
- #' actionButton(ns("submit"), label = "Load data")+ |
||
27 | -+ | ! |
- #' },+ module( |
28 | -+ | ! |
- #' server = function(id) {+ label = label, |
29 | -+ | ! |
- #' moduleServer(id, function(input, output, session) {+ server = function(input, output, session, data) { |
30 | -+ | ! |
- #' eventReactive(input$submit, {+ checkmate::assert_class(data, "reactive") |
31 | -+ | ! |
- #' data <- within(+ checkmate::assert_class(isolate(data()), "teal_data") |
32 |
- #' teal_data(),+ |
||
33 | -+ | ! |
- #' {+ output$filter_calls <- renderText({ |
34 | -- |
- #' dataset1 <- iris- |
- |
35 | -- |
- #' dataset2 <- mtcars- |
- |
36 | -- |
- #' }- |
- |
37 | -- |
- #' )- |
- |
38 | -- |
- #' datanames(data) <- c("dataset1", "dataset2")- |
- |
39 | -- |
- #'- |
- |
40 | -- |
- #' data- |
- |
41 | -+ | ! |
- #' })+ teal.data::get_code(data()) |
42 | +35 |
- #' })+ }) |
|
43 | +36 |
- #' }+ }, |
|
44 | -+ | ||
37 | +! |
- #' )+ ui = function(id, ...) { |
|
45 | -+ | ||
38 | +! |
- #' @export+ ns <- NS(id) |
|
46 | -+ | ||
39 | +! |
- teal_data_module <- function(ui, server) {+ div( |
|
47 | -35x | +||
40 | +! |
- checkmate::assert_function(ui, args = "id", nargs = 1)+ h2("The following filter calls are generated:"), |
|
48 | -34x | +||
41 | +! |
- checkmate::assert_function(server, args = "id", nargs = 1)+ verbatimTextOutput(ns("filter_calls")) |
|
49 | -33x | +||
42 | +
- structure(+ ) |
||
50 | -33x | +||
43 | +
- list(ui = ui, server = server),+ }, |
||
51 | -33x | +||
44 | +! |
- class = "teal_data_module"+ datanames = "all" |
|
52 | +45 |
) |
|
53 | +46 |
}@@ -30026,252 +29991,273 @@ teal coverage - 63.30% |
1 |
- .onLoad <- function(libname, pkgname) { # nolint+ #' Show R Code Modal |
||
2 |
- # adapted from https://github.com/r-lib/devtools/blob/master/R/zzz.R+ #' |
||
3 | -! | +
- teal_default_options <- list(teal.show_js_log = FALSE)+ #' @export |
|
4 |
-
+ #' @description `r lifecycle::badge("stable")` |
||
5 | -! | +
- op <- options()+ #' Use the [shiny::showModal()] function to show the R code inside. |
|
6 | -! | +
- toset <- !(names(teal_default_options) %in% names(op))+ #' |
|
7 | -! | +
- if (any(toset)) options(teal_default_options[toset])+ #' @param title (`character(1)`)\cr |
|
8 |
-
+ #' Title of the modal, displayed in the first comment of the R-code. |
||
9 | -! | +
- options("shiny.sanitize.errors" = FALSE)+ #' @param rcode (`character`)\cr |
|
10 |
-
+ #' vector with R code to show inside the modal. |
||
11 |
- # Set up the teal logger instance+ #' @param session (`ShinySession` optional)\cr |
||
12 | -! | +
- teal.logger::register_logger("teal")+ #' `shiny` Session object, if missing then [shiny::getDefaultReactiveDomain()] is used. |
|
13 |
-
+ #' |
||
14 | -! | +
- invisible()+ #' @references [shiny::showModal()] |
|
15 |
- }+ show_rcode_modal <- function(title = NULL, rcode, session = getDefaultReactiveDomain()) { |
||
16 | -+ | ! |
-
+ rcode <- paste(rcode, collapse = "\n") |
17 |
- .onAttach <- function(libname, pkgname) { # nolint+ |
||
18 | -2x | +! |
- packageStartupMessage(+ ns <- session$ns |
19 | -2x | +! |
- "\nYou are using teal version ",+ showModal(modalDialog( |
20 | -+ | ! |
- # `system.file` uses the `shim` of `system.file` by `teal`+ tagList( |
21 | -+ | ! |
- # we avoid `desc` dependency here to get the version+ tags$div( |
22 | -2x | +! |
- read.dcf(system.file("DESCRIPTION", package = "teal"))[, "Version"]+ actionButton(ns("copyRCode"), "Copy to Clipboard", `data-clipboard-target` = paste0("#", ns("r_code"))), |
23 | -+ | ! |
- )+ modalButton("Dismiss"), |
24 | -+ | ! |
- }+ style = "mb-4" |
25 |
-
+ ), |
||
26 | -+ | ! |
- # This one is here because setdiff_teal_slice should not be exported from teal.slice.+ tags$div(tags$pre(id = ns("r_code"), rcode)), |
27 |
- setdiff_teal_slices <- getFromNamespace("setdiff_teal_slices", "teal.slice")+ ), |
||
28 | -+ | ! |
- # This one is here because it is needed by c.teal_slices but we don't want it exported from teal.slice.+ title = title, |
29 | -+ | ! |
- coalesce_r <- getFromNamespace("coalesce_r", "teal.slice")+ footer = tagList( |
30 | -+ | ! |
- # all *Block objects are private in teal.reporter+ actionButton(ns("copyRCode"), "Copy to Clipboard", `data-clipboard-target` = paste0("#", ns("r_code"))), |
31 | -+ | ! |
- RcodeBlock <- getFromNamespace("RcodeBlock", "teal.reporter") # nolint+ modalButton("Dismiss") |
32 |
-
+ ), |
||
33 | +! | +
+ size = "l",+ |
+ |
34 | +! | +
+ easyClose = TRUE+ |
+ |
35 |
- # Use non-exported function(s) from teal.code+ )) |
||
34 | +36 |
- # This one is here because lang2calls should not be exported from teal.code+ + |
+ |
37 | +! | +
+ return(NULL) |
|
35 | +38 |
- lang2calls <- getFromNamespace("lang2calls", "teal.code")+ } |