1 |
#' Creates `ggplot2_args` object |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("experimental")` |
|
4 |
#' Constructor of `ggplot2_args` class of objects. |
|
5 |
#' The `ggplot2_args` argument should be a part of every module which contains any `ggplot2` graphics. |
|
6 |
#' The function arguments are validated to match their `ggplot2` equivalents. |
|
7 |
#' |
|
8 |
#' For more details see the vignette: `vignette("custom-ggplot2-arguments", package = "teal.widgets")`. |
|
9 |
#' |
|
10 |
#' @seealso |
|
11 |
#' * [resolve_ggplot2_args()] to resolve multiple objects into one using pre-defined priorities. |
|
12 |
#' * [parse_ggplot2_args()] to parse resolved list into list of calls. |
|
13 |
#' |
|
14 |
#' @param labs (named `list`)\cr |
|
15 |
#' where all fields have to match [ggplot2::labs()] arguments. |
|
16 |
#' @param theme (named `list`)\cr |
|
17 |
#' where all fields have to match [ggplot2::theme()] arguments. |
|
18 |
#' |
|
19 |
#' @return (`ggplot2_args`) object. |
|
20 |
#' @export |
|
21 |
#' @examples |
|
22 |
#' ggplot2_args( |
|
23 |
#' labs = list(title = "TITLE"), |
|
24 |
#' theme = list(title = ggplot2::element_text(size = 20)) |
|
25 |
#' ) |
|
26 |
ggplot2_args <- function(labs = list(), theme = list()) { |
|
27 | 92x |
checkmate::assert_list(labs) |
28 | 92x |
checkmate::assert_list(theme) |
29 | 92x |
checkmate::assert_character(names(labs), unique = TRUE, null.ok = TRUE) |
30 | 92x |
checkmate::assert_character(names(theme), unique = TRUE, null.ok = TRUE) |
31 | ||
32 | 92x |
ggplot2_theme <- methods::formalArgs(ggplot2::theme) |
33 |
# utils::getFromNamespace is not recommended nevertheless needed as it is replacing `:::`. |
|
34 |
# usage of static values will be vulnerable to any changes in ggplot2 aesthetics. |
|
35 | 92x |
ggplot2_labs <- c( |
36 | 92x |
utils::getFromNamespace(".all_aesthetics", "ggplot2"), |
37 | 92x |
methods::formalArgs(ggplot2::labs) |
38 |
) |
|
39 | 92x |
checkmate::assert_subset(names(labs), choices = ggplot2_labs, empty.ok = TRUE) |
40 | 91x |
checkmate::assert_subset(names(theme), choices = ggplot2_theme, empty.ok = TRUE) |
41 | ||
42 | 90x |
structure(list(labs = labs, theme = theme), class = "ggplot2_args") |
43 |
} |
|
44 | ||
45 |
#' Resolving and reducing multiple `ggplot2_args` objects |
|
46 |
#' |
|
47 |
#' @description `r lifecycle::badge("experimental")` |
|
48 |
#' Resolving and reducing multiple `ggplot2_args` objects. |
|
49 |
#' This function is intended to utilize user provided settings, defaults provided by the module creator and |
|
50 |
#' also `teal` option. See `Details`, below, to understand the logic. |
|
51 |
#' |
|
52 |
#' @seealso [parse_ggplot2_args()] to parse resolved list into list of calls. |
|
53 |
#' |
|
54 |
#' @param user_plot (`ggplot2_args`)\cr |
|
55 |
#' end user setup for theme and labs in the specific plot. |
|
56 |
#' Created with the [ggplot2_args()] function. The `NULL` value is supported. |
|
57 |
#' @param user_default (`ggplot2_args`)\cr |
|
58 |
#' end user setup for module default theme and labs. |
|
59 |
#' Created with the [ggplot2_args()] function. The `NULL` value is supported. |
|
60 |
#' @param module_plot (`ggplot2_args`)\cr |
|
61 |
#' module creator setup for theme and labs in the specific plot. |
|
62 |
#' Created with the [ggplot2_args()] function. The `NULL` value is supported. |
|
63 |
#' @param app_default (`ggplot2_args`)\cr |
|
64 |
#' Application level setting. Can be `NULL`. |
|
65 |
#' |
|
66 |
#' @return `ggplot2_args` object. |
|
67 |
#' |
|
68 |
#' @details |
|
69 |
#' The function picks the first non `NULL` value for each argument, checking in the following order: |
|
70 |
#' 1. `ggplot2_args` argument provided by the end user. |
|
71 |
#' Per plot (`user_plot`) and then default (`user_default`) setup. |
|
72 |
#' 2. `app_default` global R variable, `teal.ggplot2_args`. |
|
73 |
#' 3. `module_plot` which is a module creator setup. |
|
74 |
#' @export |
|
75 |
#' @examples |
|
76 |
#' resolve_ggplot2_args( |
|
77 |
#' user_plot = ggplot2_args( |
|
78 |
#' labs = list(title = "TITLE"), |
|
79 |
#' theme = list(title = ggplot2::element_text(size = 20)) |
|
80 |
#' ), |
|
81 |
#' user_default = ggplot2_args( |
|
82 |
#' labs = list(x = "XLAB") |
|
83 |
#' ) |
|
84 |
#' ) |
|
85 |
resolve_ggplot2_args <- function(user_plot = ggplot2_args(), |
|
86 |
user_default = ggplot2_args(), |
|
87 |
module_plot = ggplot2_args(), |
|
88 |
app_default = getOption("teal.ggplot2_args", ggplot2_args())) { |
|
89 | 18x |
checkmate::assert_class(user_plot, "ggplot2_args", null.ok = TRUE) |
90 | 17x |
checkmate::assert_class(user_default, "ggplot2_args", null.ok = TRUE) |
91 | 17x |
checkmate::assert_class(module_plot, "ggplot2_args", null.ok = TRUE) |
92 | 17x |
checkmate::assert_class(app_default, "ggplot2_args", null.ok = TRUE) |
93 | ||
94 | 17x |
ggplot2_args_all <- list( |
95 | 17x |
"plot" = user_plot, |
96 | 17x |
"default" = user_default, |
97 | 17x |
"teal" = app_default, |
98 | 17x |
"module" = module_plot |
99 |
) |
|
100 | ||
101 | 17x |
labs_args <- Reduce(`c`, lapply(ggplot2_args_all, function(x) x$labs)) |
102 | 17x |
labs_args <- if (is.null(labs_args)) list() else labs_args[!duplicated(names(labs_args))] |
103 | ||
104 | 17x |
theme_args <- Reduce(`c`, lapply(ggplot2_args_all, function(x) x$theme)) |
105 | 17x |
theme_args <- if (is.null(theme_args)) list() else theme_args[!duplicated(names(theme_args))] |
106 | ||
107 | 17x |
ggplot2_args(labs = labs_args, theme = theme_args) |
108 |
} |
|
109 | ||
110 |
#' Parse `ggplot2_args` object into the `ggplot2` expression |
|
111 |
#' |
|
112 |
#' @description `r lifecycle::badge("experimental")` |
|
113 |
#' A function to parse expression from the `ggplot2_args` object. |
|
114 |
#' @param ggplot2_args (`ggplot2_args`)\cr |
|
115 |
#' This argument could be a result of the [resolve_ggplot2_args()]. |
|
116 |
#' @param ggtheme (`character(1)`)\cr |
|
117 |
#' name of the `ggplot2` theme to be applied, e.g. `"dark"`. |
|
118 |
#' |
|
119 |
#' @return (`list`) of up to three elements of class `languange`: `"labs"`, `"ggtheme"` and `"theme"`. |
|
120 |
#' @export |
|
121 |
#' @examples |
|
122 |
#' parse_ggplot2_args( |
|
123 |
#' resolve_ggplot2_args(ggplot2_args( |
|
124 |
#' labs = list(title = "TITLE"), |
|
125 |
#' theme = list(title = ggplot2::element_text(size = 20)) |
|
126 |
#' )) |
|
127 |
#' ) |
|
128 |
#' |
|
129 |
#' parse_ggplot2_args( |
|
130 |
#' resolve_ggplot2_args( |
|
131 |
#' ggplot2_args( |
|
132 |
#' labs = list(title = "TITLE"), |
|
133 |
#' theme = list(title = ggplot2::element_text(size = 20)) |
|
134 |
#' ) |
|
135 |
#' ), |
|
136 |
#' ggtheme = "gray" |
|
137 |
#' ) |
|
138 |
parse_ggplot2_args <- function(ggplot2_args = teal.widgets::ggplot2_args(), |
|
139 |
ggtheme = c( |
|
140 |
"default", |
|
141 |
"gray", |
|
142 |
"bw", |
|
143 |
"linedraw", |
|
144 |
"light", |
|
145 |
"dark", |
|
146 |
"minimal", |
|
147 |
"classic", |
|
148 |
"void", |
|
149 |
"test" |
|
150 |
)) { |
|
151 | 10x |
checkmate::assert_class(ggplot2_args, "ggplot2_args") |
152 | 9x |
ggtheme <- match.arg(ggtheme) |
153 | ||
154 | 9x |
res_list <- list() |
155 | ||
156 | 9x |
labs_args <- ggplot2_args$labs |
157 | ||
158 | 9x |
labs_f <- if (length(labs_args)) { |
159 | 5x |
as.call(c(list(quote(ggplot2::labs)), labs_args)) |
160 |
} else { |
|
161 | 4x |
NULL |
162 |
} |
|
163 | ||
164 | 9x |
default_theme <- if (ggtheme != "default") { |
165 | 1x |
as.call(list(str2lang(paste0("ggplot2::theme_", ggtheme)))) |
166 |
} else { |
|
167 | 8x |
NULL |
168 |
} |
|
169 | ||
170 | 9x |
theme_args <- ggplot2_args$theme |
171 | ||
172 | 9x |
theme_f <- if (length(theme_args)) { |
173 | 2x |
as.call(c(list(quote(ggplot2::theme)), theme_args)) |
174 |
} else { |
|
175 | 7x |
NULL |
176 |
} |
|
177 | ||
178 | 9x |
final_list <- Filter(Negate(is.null), list(labs = labs_f, ggtheme = default_theme, theme = theme_f)) |
179 |
# For empty final_list we want to return empty list, not empty named list |
|
180 | 3x |
`if`(length(final_list) == 0, list(), final_list) |
181 |
} |
1 |
#' @keywords internal |
|
2 |
#' @noRd |
|
3 |
optional_select_input_deps <- function() { |
|
4 | ! |
htmltools::htmlDependency( |
5 | ! |
name = "teal-widgets-optional-select-input", |
6 | ! |
version = utils::packageVersion("teal.widgets"), |
7 | ! |
package = "teal.widgets", |
8 | ! |
src = "optional-select-input", |
9 | ! |
stylesheet = "optional-select-input.css" |
10 |
) |
|
11 |
} |
|
12 | ||
13 |
#' Wrapper for `pickerInput` |
|
14 |
#' |
|
15 |
#' @description `r lifecycle::badge("stable")` |
|
16 |
#' Wrapper for [shinyWidgets::pickerInput()] with additional features. |
|
17 |
#' When `fixed = TRUE` or when the number of `choices` is less or equal to 1 (see `fixed_on_single`), |
|
18 |
#' the `pickerInput` widget is hidden and non-interactive widget will be displayed |
|
19 |
#' instead. Toggle of `HTML` elements is just the visual effect to avoid displaying |
|
20 |
#' `pickerInput` widget when there is only one choice. |
|
21 |
#' |
|
22 |
#' @inheritParams shinyWidgets::pickerInput |
|
23 |
#' |
|
24 |
#' @param sep (`character(1)`)\cr |
|
25 |
#' A separator string to split the `choices` or `selected` inputs into the values of the different |
|
26 |
#' columns. |
|
27 |
#' |
|
28 |
#' @param label_help (`shiny.tag`) optional,\cr |
|
29 |
#' e.g. an object returned by [shiny::helpText()]. |
|
30 |
#' |
|
31 |
#' @param fixed (`logical(1)`) optional,\cr |
|
32 |
#' whether to block user to select choices. |
|
33 |
#' |
|
34 |
#' @param fixed_on_single (`logical(1)`) optional,\cr |
|
35 |
#' whether to block user to select a choice when there is only one or less choice. |
|
36 |
#' When `FALSE`, user is still able to select or deselect the choice. |
|
37 |
#' |
|
38 |
#' @param width (`character(1)`)\cr |
|
39 |
#' The width of the input passed to `pickerInput` e.g. `'auto'`, `'fit'`, `'100px'` or `'75%'` |
|
40 |
#' |
|
41 |
#' @return (`shiny.tag`) HTML tag with `pickerInput` widget and |
|
42 |
#' non-interactive element listing selected values. |
|
43 |
#' |
|
44 |
#' @export |
|
45 |
#' |
|
46 |
#' @examples |
|
47 |
#' library(shiny) |
|
48 |
#' |
|
49 |
#' # Create a minimal example data frame |
|
50 |
#' data <- data.frame( |
|
51 |
#' AGE = c(25, 30, 40, 35, 28), |
|
52 |
#' SEX = c("Male", "Female", "Male", "Female", "Male"), |
|
53 |
#' PARAMCD = c("Val1", "Val2", "Val3", "Val4", "Val5"), |
|
54 |
#' PARAM = c("Param1", "Param2", "Param3", "Param4", "Param5"), |
|
55 |
#' AVISIT = c("Visit1", "Visit2", "Visit3", "Visit4", "Visit5"), |
|
56 |
#' stringsAsFactors = TRUE |
|
57 |
#' ) |
|
58 |
#' |
|
59 |
#' ui_grid <- function(...) { |
|
60 |
#' fluidPage( |
|
61 |
#' fluidRow( |
|
62 |
#' lapply(list(...), function(x) column(4, wellPanel(x))) |
|
63 |
#' ) |
|
64 |
#' ) |
|
65 |
#' } |
|
66 |
#' |
|
67 |
#' ui <- ui_grid( |
|
68 |
#' tags$div( |
|
69 |
#' optionalSelectInput( |
|
70 |
#' inputId = "c1", |
|
71 |
#' label = "Fixed choices", |
|
72 |
#' choices = LETTERS[1:5], |
|
73 |
#' selected = c("A", "B"), |
|
74 |
#' fixed = TRUE |
|
75 |
#' ), |
|
76 |
#' verbatimTextOutput(outputId = "c1_out") |
|
77 |
#' ), |
|
78 |
#' tags$div( |
|
79 |
#' optionalSelectInput( |
|
80 |
#' inputId = "c2", |
|
81 |
#' label = "Single choice", |
|
82 |
#' choices = "A", |
|
83 |
#' selected = "A" |
|
84 |
#' ), |
|
85 |
#' verbatimTextOutput(outputId = "c2_out") |
|
86 |
#' ), |
|
87 |
#' tags$div( |
|
88 |
#' optionalSelectInput( |
|
89 |
#' inputId = "c3", |
|
90 |
#' label = "NULL choices", |
|
91 |
#' choices = NULL |
|
92 |
#' ), |
|
93 |
#' verbatimTextOutput(outputId = "c3_out") |
|
94 |
#' ), |
|
95 |
#' tags$div( |
|
96 |
#' optionalSelectInput( |
|
97 |
#' inputId = "c4", |
|
98 |
#' label = "Default", |
|
99 |
#' choices = LETTERS[1:5], |
|
100 |
#' selected = "A" |
|
101 |
#' ), |
|
102 |
#' verbatimTextOutput(outputId = "c4_out") |
|
103 |
#' ), |
|
104 |
#' tags$div( |
|
105 |
#' optionalSelectInput( |
|
106 |
#' inputId = "c5", |
|
107 |
#' label = "Named vector", |
|
108 |
#' choices = c(`A - value A` = "A", `B - value B` = "B", `C - value C` = "C"), |
|
109 |
#' selected = "A" |
|
110 |
#' ), |
|
111 |
#' verbatimTextOutput(outputId = "c5_out") |
|
112 |
#' ), |
|
113 |
#' tags$div( |
|
114 |
#' selectInput( |
|
115 |
#' inputId = "c6_choices", label = "Update choices", choices = letters, multiple = TRUE |
|
116 |
#' ), |
|
117 |
#' optionalSelectInput( |
|
118 |
#' inputId = "c6", |
|
119 |
#' label = "Updated choices", |
|
120 |
#' choices = NULL, |
|
121 |
#' multiple = TRUE, |
|
122 |
#' fixed_on_single = TRUE |
|
123 |
#' ), |
|
124 |
#' verbatimTextOutput(outputId = "c6_out") |
|
125 |
#' ) |
|
126 |
#' ) |
|
127 |
#' |
|
128 |
#' server <- function(input, output, session) { |
|
129 |
#' observeEvent(input$c6_choices, ignoreNULL = FALSE, { |
|
130 |
#' updateOptionalSelectInput( |
|
131 |
#' session = session, |
|
132 |
#' inputId = "c6", |
|
133 |
#' choices = input$c6_choices, |
|
134 |
#' selected = input$c6_choices |
|
135 |
#' ) |
|
136 |
#' }) |
|
137 |
#' |
|
138 |
#' output$c1_out <- renderPrint(input$c1) |
|
139 |
#' output$c2_out <- renderPrint(input$c2) |
|
140 |
#' output$c3_out <- renderPrint(input$c3) |
|
141 |
#' output$c4_out <- renderPrint(input$c4) |
|
142 |
#' output$c5_out <- renderPrint(input$c5) |
|
143 |
#' output$c6_out <- renderPrint(input$c6) |
|
144 |
#' } |
|
145 |
#' |
|
146 |
#' if (interactive()) { |
|
147 |
#' shinyApp(ui, server) |
|
148 |
#' } |
|
149 |
#' |
|
150 |
optionalSelectInput <- function(inputId, # nolint |
|
151 |
label = NULL, |
|
152 |
choices = NULL, |
|
153 |
selected = NULL, |
|
154 |
multiple = FALSE, |
|
155 |
sep = NULL, |
|
156 |
options = list(), |
|
157 |
label_help = NULL, |
|
158 |
fixed = FALSE, |
|
159 |
fixed_on_single = FALSE, |
|
160 |
width = NULL) { |
|
161 | ! |
checkmate::assert_string(inputId) |
162 | ! |
checkmate::assert( |
163 | ! |
checkmate::check_string(label, null.ok = TRUE), |
164 | ! |
checkmate::check_class(label, "shiny.tag"), |
165 | ! |
checkmate::check_class(label, "shiny.tag.list"), |
166 | ! |
checkmate::check_class(label, "html") |
167 |
) |
|
168 | ! |
stopifnot(is.null(choices) || length(choices) >= 1) |
169 | ! |
stopifnot( |
170 | ! |
is.null(selected) || |
171 | ! |
length(selected) == 0 || |
172 | ! |
all(selected %in% choices) || |
173 | ! |
all(selected %in% unlist(choices, recursive = FALSE)) |
174 |
) |
|
175 | ! |
checkmate::assert_flag(multiple) |
176 | ! |
checkmate::assert_string(sep, null.ok = TRUE) |
177 | ! |
checkmate::assert_list(options) |
178 | ! |
checkmate::assert( |
179 | ! |
checkmate::check_string(label_help, null.ok = TRUE), |
180 | ! |
checkmate::check_class(label_help, "shiny.tag"), |
181 | ! |
checkmate::check_class(label_help, "shiny.tag.list"), |
182 | ! |
checkmate::check_class(label_help, "html") |
183 |
) |
|
184 | ! |
checkmate::assert_flag(fixed) |
185 | ! |
checkmate::assert_flag(fixed_on_single) |
186 | ||
187 | ! |
if (!is.null(width)) { |
188 | ! |
validateCssUnit(width) |
189 |
} |
|
190 | ||
191 | ! |
default_options <- list( |
192 | ! |
"actions-box" = multiple, |
193 | ! |
"none-selected-text" = "- Nothing selected -", |
194 | ! |
"max-options" = ifelse(multiple, Inf, 1), |
195 | ! |
"show-subtext" = TRUE, |
196 | ! |
"live-search" = ifelse(length(choices) > 10, TRUE, FALSE) |
197 |
) |
|
198 | ||
199 |
# if called outside the fluidPage then will assume bs 3 |
|
200 | ! |
bs_version <- get_bs_version() |
201 | ! |
if (isTRUE(bs_version != "3")) default_options[["style"]] <- "btn-outline-secondary" |
202 | ||
203 | ! |
options <- if (!identical(options, list())) { |
204 | ! |
c(options, default_options[setdiff(names(default_options), names(options))]) |
205 |
} else { |
|
206 | ! |
default_options |
207 |
} |
|
208 | ||
209 | ! |
if (is.null(choices)) { |
210 | ! |
choices <- "" |
211 | ! |
selected <- NULL |
212 |
} |
|
213 | ||
214 | ! |
if (length(choices) <= 1 && fixed_on_single) fixed <- TRUE |
215 | ||
216 | ! |
raw_choices <- extract_raw_choices(choices, attr(choices, "sep")) |
217 | ! |
raw_selected <- extract_raw_choices(selected, attr(choices, "sep")) |
218 | ||
219 | ! |
ui_picker <- tags$div( |
220 | ! |
id = paste0(inputId, "_input"), |
221 |
# visibility feature marked with display: none/block instead of shinyjs::hide/show |
|
222 |
# as mechanism to hide/show is handled by javascript code |
|
223 | ! |
style = if (fixed) "display: none;" else "display: block;", |
224 | ! |
shinyWidgets::pickerInput( |
225 | ! |
inputId = inputId, |
226 | ! |
label = label, |
227 | ! |
choices = raw_choices, |
228 | ! |
selected = raw_selected, |
229 | ! |
multiple = TRUE, |
230 | ! |
width = width, |
231 | ! |
options = options, |
232 | ! |
choicesOpt = picker_options(choices) |
233 |
) |
|
234 |
) |
|
235 | ||
236 | ! |
if (!is.null(label_help)) { |
237 | ! |
ui_picker[[3]] <- append(ui_picker[[3]], list(tags$div(class = "label-help", label_help)), after = 1) |
238 |
} |
|
239 | ||
240 | ! |
ui_fixed <- tags$div( |
241 | ! |
id = paste0(inputId, "_fixed"), |
242 |
# visibility feature marked with display: none/block instead of shinyjs::hide/show |
|
243 |
# as mechanism to hide/show is handled by javascript code |
|
244 | ! |
style = if (fixed) "display: block;" else "display: none;", |
245 | ! |
tags$label(class = "control-label", label), |
246 |
# selected values as verbatim text |
|
247 | ! |
tags$code( |
248 | ! |
id = paste0(inputId, "_selected_text"), |
249 | ! |
if (length(selected) > 0) { |
250 | ! |
toString(selected) |
251 |
} else { |
|
252 | ! |
"NULL" |
253 |
} |
|
254 |
), |
|
255 | ! |
label_help |
256 |
) |
|
257 | ||
258 | ! |
tags$div( |
259 | ! |
optional_select_input_deps(), |
260 | ||
261 |
# when selected values in ui_picker change |
|
262 |
# then update ui_fixed - specifically, update '{id}_selected_text' element |
|
263 | ! |
tags$script( |
264 | ! |
sprintf( |
265 |
" |
|
266 | ! |
$(function() { |
267 | ! |
$('#%1$s').on('change', function(e) { |
268 | ! |
var select_concat = $(this).val().length ? $(this).val().join(', ') : 'NULL'; |
269 | ! |
$('#%1$s_selected_text').html(select_concat); |
270 |
}) |
|
271 |
})", |
|
272 | ! |
inputId |
273 |
) |
|
274 |
), |
|
275 | ||
276 |
# if ui_picker has only one or less option or is fixed then hide {id}_input and show {id}_fixed |
|
277 | ! |
if (fixed_on_single) { |
278 | ! |
js <- sprintf( |
279 | ! |
"$(function() { |
280 | ! |
$('#%1$s').on('change', function(e) { |
281 | ! |
var options = $('#%1$s').find('option'); |
282 | ! |
if (options.length == 1) { |
283 | ! |
$('#%1$s_input').hide(); |
284 | ! |
$('#%1$s_fixed').show(); |
285 |
} else { |
|
286 | ! |
$('#%1$s_input').show(); |
287 | ! |
$('#%1$s_fixed').hide(); |
288 |
} |
|
289 |
}) |
|
290 |
})", |
|
291 | ! |
inputId |
292 |
) |
|
293 | ! |
tags$script(js) |
294 |
}, |
|
295 | ! |
tags$div(ui_picker, ui_fixed) |
296 |
) |
|
297 |
} |
|
298 | ||
299 |
#' @rdname optionalSelectInput |
|
300 |
#' @param session (`shiny.session`)\cr |
|
301 |
#' @export |
|
302 |
updateOptionalSelectInput <- function(session, # nolint |
|
303 |
inputId, # nolint |
|
304 |
label = NULL, |
|
305 |
selected = NULL, |
|
306 |
choices = NULL) { |
|
307 | ! |
raw_choices <- extract_raw_choices(choices, attr(choices, "sep")) |
308 | ! |
raw_selected <- extract_raw_choices(selected, attr(choices, "sep")) |
309 | ||
310 |
# update picker input |
|
311 | ! |
shinyWidgets::updatePickerInput( |
312 | ! |
session = session, |
313 | ! |
inputId = inputId, |
314 | ! |
label = label, |
315 | ! |
selected = as.character(raw_selected), |
316 | ! |
choices = raw_choices, |
317 | ! |
choicesOpt = picker_options(choices) |
318 |
) |
|
319 | ||
320 | ! |
invisible(NULL) |
321 |
} |
|
322 | ||
323 |
#' Get icons to represent variable types in dataset |
|
324 |
#' |
|
325 |
#' @param var_type (`character`)\cr |
|
326 |
#' of R internal types (classes). |
|
327 |
#' |
|
328 |
#' @return (`character`)\cr |
|
329 |
#' vector of HTML icons corresponding to data type in each column. |
|
330 |
#' @keywords internal |
|
331 |
#' |
|
332 |
variable_type_icons <- function(var_type) { |
|
333 | ! |
checkmate::assert_character(var_type, any.missing = FALSE) |
334 | ||
335 | ! |
class_to_icon <- list( |
336 | ! |
numeric = "arrow-up-1-9", |
337 | ! |
integer = "arrow-up-1-9", |
338 | ! |
logical = "pause", |
339 | ! |
Date = "calendar", |
340 | ! |
POSIXct = "calendar", |
341 | ! |
POSIXlt = "calendar", |
342 | ! |
factor = "chart-bar", |
343 | ! |
character = "keyboard", |
344 | ! |
primary_key = "key", |
345 | ! |
unknown = "circle-question" |
346 |
) |
|
347 | ! |
class_to_icon <- lapply(class_to_icon, function(icon_name) toString(icon(icon_name, lib = "font-awesome"))) |
348 | ||
349 | ! |
res <- unname(vapply( |
350 | ! |
var_type, |
351 | ! |
FUN.VALUE = character(1), |
352 | ! |
FUN = function(class) { |
353 | ! |
if (class == "") { |
354 | ! |
class |
355 | ! |
} else if (is.null(class_to_icon[[class]])) { |
356 | ! |
class_to_icon[["unknown"]] |
357 |
} else { |
|
358 | ! |
class_to_icon[[class]] |
359 |
} |
|
360 |
} |
|
361 |
)) |
|
362 | ||
363 | ! |
return(res) |
364 |
} |
|
365 | ||
366 |
#' Optional content for `optionalSelectInput` |
|
367 |
#' |
|
368 |
#' Prepares content to be displayed in `optionalSelectInput` with icons and labels |
|
369 |
#' |
|
370 |
#' @param var_name (`character`)\cr |
|
371 |
#' variable name |
|
372 |
#' @param var_label (`character`)\cr |
|
373 |
#' variable alternative name - for example variable label |
|
374 |
#' @param var_type (`character`) |
|
375 |
#' class of the variable. |
|
376 |
#' |
|
377 |
#' @return (`character`) HTML contents with all elements combined |
|
378 |
#' @keywords internal |
|
379 |
#' |
|
380 |
picker_options_content <- function(var_name, var_label, var_type) { |
|
381 | ! |
if (length(var_name) == 0) { |
382 | ! |
return(character(0)) |
383 |
} |
|
384 | ! |
if (length(var_type) == 0 && length(var_label) == 0) { |
385 | ! |
return(var_name) |
386 |
} |
|
387 | ! |
checkmate::assert_character(var_name, min.len = 1, any.missing = FALSE) |
388 | ! |
stopifnot( |
389 | ! |
identical(var_type, character(0)) || length(var_type) == length(var_name), |
390 | ! |
identical(var_label, character(0)) || length(var_label) == length(var_name) |
391 |
) |
|
392 | ||
393 | ! |
var_icon <- variable_type_icons(var_type) |
394 | ||
395 | ! |
res <- trimws(paste( |
396 | ! |
var_icon, |
397 | ! |
var_name, |
398 | ! |
vapply( |
399 | ! |
var_label, |
400 | ! |
function(x) { |
401 | ! |
ifelse(x == "", "", toString(tags$small(x, class = "text-muted"))) |
402 |
}, |
|
403 | ! |
character(1) |
404 |
) |
|
405 |
)) |
|
406 | ||
407 | ! |
return(res) |
408 |
} |
|
409 | ||
410 |
#' Create `choicesOpt` for `pickerInput` |
|
411 |
#' |
|
412 |
#' @param choices (`choices_labeled` or `character`)\cr |
|
413 |
#' choices vector |
|
414 |
#' |
|
415 |
#' @return (`list`)\cr |
|
416 |
#' to be passed as `choicesOpt` argument. |
|
417 |
#' @keywords internal |
|
418 |
picker_options <- function(choices) { |
|
419 | ! |
if (inherits(choices, "choices_labeled")) { |
420 | ! |
raw_choices <- extract_raw_choices(choices, sep = attr(choices, "sep")) |
421 | ! |
return( |
422 | ! |
list( |
423 | ! |
content = picker_options_content( |
424 | ! |
var_name = raw_choices, |
425 | ! |
var_label = extract_choices_labels(choices), |
426 | ! |
var_type = if (is.null(attr(choices, "types"))) character(0) else attr(choices, "types") |
427 |
) |
|
428 |
) |
|
429 |
) |
|
430 | ! |
} else if (all(vapply(choices, inherits, logical(1), "choices_labeled"))) { |
431 | ! |
choices <- unlist(unname(choices)) |
432 | ! |
return( |
433 | ! |
list(content = picker_options_content( |
434 | ! |
var_name = choices, |
435 | ! |
var_label = extract_choices_labels(choices), |
436 | ! |
var_type = if (is.null(attr(choices, "types"))) character(0) else attr(choices, "types") |
437 |
)) |
|
438 |
) |
|
439 |
} else { |
|
440 | ! |
return(NULL) |
441 |
} |
|
442 |
} |
|
443 | ||
444 |
#' Extract raw values from choices |
|
445 |
#' |
|
446 |
#' @param choices (`choices_labeled`, `list` or `character`)\cr |
|
447 |
#' object containing choices |
|
448 |
#' @param sep (`character(1)`)\cr |
|
449 |
#' A separator string to split the `choices` or `selected` inputs into the values of |
|
450 |
#' the different columns. |
|
451 |
#' @return choices simplified |
|
452 |
#' @keywords internal |
|
453 |
extract_raw_choices <- function(choices, sep) { |
|
454 | ! |
if (!is.null(sep)) { |
455 | ! |
vapply(choices, paste, collapse = sep, character(1)) |
456 | ! |
} else if (inherits(choices, "choices_labeled")) { |
457 | ! |
unname(unlist(choices)) |
458 |
} else { |
|
459 | ! |
choices |
460 |
} |
|
461 |
} |
|
462 | ||
463 |
#' if min or max are `NA` then the slider widget will be hidden |
|
464 |
#' |
|
465 |
#' @description `r lifecycle::badge("stable")`\cr |
|
466 |
#' Hidden input widgets are useful to have the `input[[inputId]]` variable |
|
467 |
#' on available in the server function but no corresponding visual clutter from |
|
468 |
#' input widgets that provide only a single choice. |
|
469 |
#' |
|
470 |
#' @inheritParams shiny::sliderInput |
|
471 |
#' @param label_help (`shiny.tag`) optional\cr |
|
472 |
#' object of class `shiny.tag`, e.g. an object returned by [shiny::helpText()] |
|
473 |
#' @param ... optional arguments to `sliderInput` |
|
474 |
#' |
|
475 |
#' @return (`shiny.tag`) HTML tag with `sliderInput` widget. |
|
476 |
#' |
|
477 |
#' @export |
|
478 |
#' |
|
479 |
#' @examples |
|
480 |
#' optionalSliderInput("a", "b", 0, 1, 0.2) |
|
481 |
optionalSliderInput <- function(inputId, label, min, max, value, label_help = NULL, ...) { # nolint |
|
482 | 25x |
checkmate::assert_number(min, na.ok = TRUE) |
483 | 25x |
checkmate::assert_number(max, na.ok = TRUE) |
484 | 25x |
checkmate::assert_numeric(value, min.len = 1, max.len = 2, any.missing = FALSE) |
485 | ||
486 | 25x |
is_na_min <- is.na(min) |
487 | 25x |
is_na_max <- is.na(max) |
488 | ||
489 | 25x |
hide <- is_na_min || is_na_max |
490 | ||
491 | 25x |
if (length(value) == 2) { |
492 | 2x |
value1 <- value[1] |
493 | 2x |
value2 <- value[2] |
494 |
} else { |
|
495 | 23x |
value1 <- value |
496 | 23x |
value2 <- value |
497 |
} |
|
498 | ||
499 | 25x |
if (is_na_min) { |
500 | 2x |
min <- value1 - 1 |
501 |
} |
|
502 | 25x |
if (is_na_max) { |
503 | 1x |
max <- value2 + 1 |
504 |
} |
|
505 | ||
506 | 25x |
if (min > value1 || max < value2) { |
507 | 2x |
stop("arguments inconsistent: min <= value <= max expected") |
508 |
} |
|
509 | ||
510 | 23x |
slider <- sliderInput(inputId, label, min, max, value, ...) |
511 | ||
512 | 23x |
if (!is.null(label_help)) { |
513 | ! |
slider[[3]] <- append(slider[[3]], list(tags$div(class = "label-help", label_help)), after = 1) |
514 |
} |
|
515 | ||
516 | 23x |
if (hide) { |
517 | 2x |
shinyjs::hidden(slider) |
518 |
} else { |
|
519 | 21x |
slider |
520 |
} |
|
521 |
} |
|
522 | ||
523 |
#' For `teal` modules we parameterize an `optionalSliderInput` with one argument |
|
524 |
#' `value_min_max` |
|
525 |
#' |
|
526 |
#' @description `r lifecycle::badge("stable")` |
|
527 |
#' The [optionalSliderInput()] function needs three arguments to determine |
|
528 |
#' whether to hide the `sliderInput` widget or not. For `teal` modules we specify an |
|
529 |
#' optional slider input with one argument here called `value_min_max`. |
|
530 |
#' |
|
531 |
#' @inheritParams optionalSliderInput |
|
532 |
#' |
|
533 |
#' @param value_min_max (`numeric(1)` or `numeric(3)`)\cr |
|
534 |
#' If of length 1 then the value gets set to that number and the `sliderInput` will be hidden. |
|
535 |
#' Otherwise, if it is of length three the three elements will map to `value`, `min` and `max` of |
|
536 |
#' the [optionalSliderInput()] function. |
|
537 |
#' |
|
538 |
#' @return (`shiny.tag`) HTML tag with range `sliderInput` widget. |
|
539 |
#' |
|
540 |
#' @export |
|
541 |
#' |
|
542 |
#' @examples |
|
543 |
#' |
|
544 |
#' optionalSliderInputValMinMax("a", "b", 1) |
|
545 |
#' optionalSliderInputValMinMax("a", "b", c(3, 1, 5)) |
|
546 |
optionalSliderInputValMinMax <- function(inputId, label, value_min_max, label_help = NULL, ...) { # nolint |
|
547 | 18x |
checkmate::assert( |
548 | 18x |
checkmate::check_numeric( |
549 | 18x |
value_min_max, |
550 | 18x |
finite = TRUE, |
551 | 18x |
len = 3 |
552 |
), |
|
553 | 18x |
checkmate::check_numeric( |
554 | 18x |
value_min_max, |
555 | 18x |
finite = TRUE, |
556 | 18x |
len = 1 |
557 |
) |
|
558 |
) |
|
559 | ||
560 | 18x |
x <- value_min_max |
561 | ||
562 | 18x |
vals <- if (length(x) == 3) { |
563 | 18x |
checkmate::assert_number(x[1], lower = x[2], upper = x[3], .var.name = "value_min_max") |
564 | 18x |
list(value = x[1], min = x[2], max = x[3]) |
565 | 18x |
} else if (length(x) == 1) { |
566 | ! |
list(value = x, min = NA_real_, max = NA_real_) |
567 |
} |
|
568 | ||
569 | 18x |
slider <- optionalSliderInput(inputId, label, vals$min, vals$max, vals$value, ...) |
570 | ||
571 | 18x |
if (!is.null(label_help)) { |
572 | ! |
slider[[3]] <- append(slider[[3]], list(tags$div(class = "label-help", label_help)), after = 1) |
573 |
} |
|
574 | 18x |
return(slider) |
575 |
} |
|
576 | ||
577 |
#' Extract labels from choices basing on attributes and names |
|
578 |
#' |
|
579 |
#' @param choices (`list` or `vector`)\cr |
|
580 |
#' select choices |
|
581 |
#' @param values optional\cr |
|
582 |
#' choices subset for which labels should be extracted, `NULL` for all choices. |
|
583 |
#' |
|
584 |
#' @return (`character`) vector with labels |
|
585 |
#' @keywords internal |
|
586 |
extract_choices_labels <- function(choices, values = NULL) { |
|
587 | ! |
res <- if (inherits(choices, "choices_labeled")) { |
588 | ! |
attr(choices, "raw_labels") |
589 | ! |
} else if (!is.null(names(choices)) && !setequal(names(choices), unlist(unname(choices)))) { |
590 | ! |
names(choices) |
591 |
} else { |
|
592 | ! |
NULL |
593 |
} |
|
594 | ||
595 | ! |
if (!is.null(values) && !is.null(res)) { |
596 | ! |
stopifnot(all(values %in% choices)) |
597 | ! |
res <- res[vapply(values, function(val) which(val == choices), numeric(1))] |
598 |
} |
|
599 | ||
600 | ! |
return(res) |
601 |
} |
1 |
#' @keywords internal |
|
2 |
#' @noRd |
|
3 |
plot_with_settings_deps <- function() { |
|
4 | 1x |
htmltools::htmlDependency( |
5 | 1x |
name = "teal-widgets-plot-with-settings", |
6 | 1x |
version = utils::packageVersion("teal.widgets"), |
7 | 1x |
package = "teal.widgets", |
8 | 1x |
src = "plot-with-settings", |
9 | 1x |
script = "plot-with-settings.js", |
10 | 1x |
stylesheet = "plot-with-settings.css" |
11 |
) |
|
12 |
} |
|
13 | ||
14 |
#' @name plot_with_settings |
|
15 |
#' @rdname plot_with_settings |
|
16 |
#' @export |
|
17 |
plot_with_settings_ui <- function(id) { |
|
18 | 1x |
checkmate::assert_string(id) |
19 | ||
20 | 1x |
ns <- NS(id) |
21 | ||
22 | 1x |
tagList( |
23 | 1x |
plot_with_settings_deps(), |
24 | 1x |
tags$div( |
25 | 1x |
id = ns("plot-with-settings"), |
26 | 1x |
tags$div( |
27 | 1x |
class = "plot-settings-buttons", |
28 | 1x |
type_download_ui(ns("downbutton")), |
29 | 1x |
actionButton( |
30 | 1x |
ns("expand"), |
31 | 1x |
label = character(0), |
32 | 1x |
icon = icon("up-right-and-down-left-from-center"), |
33 | 1x |
class = "btn-sm" |
34 |
), |
|
35 | 1x |
shinyWidgets::dropdownButton( |
36 | 1x |
circle = FALSE, |
37 | 1x |
size = "sm", |
38 | 1x |
icon = icon("maximize"), |
39 | 1x |
inline = TRUE, |
40 | 1x |
right = TRUE, |
41 | 1x |
label = "", |
42 | 1x |
inputId = ns("expbut"), |
43 | 1x |
uiOutput(ns("slider_ui")), |
44 | 1x |
uiOutput(ns("width_warning")) |
45 |
) |
|
46 |
), |
|
47 | 1x |
uiOutput(ns("plot_out_main"), class = "plot_out_container", width = "100%") |
48 |
) |
|
49 |
) |
|
50 |
} |
|
51 | ||
52 |
#' Plot-with-settings module |
|
53 |
#' |
|
54 |
#' @rdname plot_with_settings |
|
55 |
#' @description `r lifecycle::badge("stable")`\cr |
|
56 |
#' Universal module for plots with settings for height, width, and download. |
|
57 |
#' |
|
58 |
#' @export |
|
59 |
#' |
|
60 |
#' @param id (`character(1)`) `shiny` module id. |
|
61 |
#' |
|
62 |
#' @param plot_r (`reactive` or `function`)\cr |
|
63 |
#' `reactive` expression or a simple `function` to draw a plot. |
|
64 |
#' A simple `function` is needed e.g. for base plots like `plot(1)` as the output can not be caught when downloading. |
|
65 |
#' Take into account that simple functions are less efficient than reactive, as not catching the result. |
|
66 |
#' @param height (`numeric`) optional\cr |
|
67 |
#' vector with three elements c(VAL, MIN, MAX), where VAL is the starting value of the slider in |
|
68 |
#' the main and modal plot display. The value in the modal display is taken from the value of the |
|
69 |
#' slider in the main plot display. |
|
70 |
#' @param width (`numeric`) optional\cr |
|
71 |
#' vector with three elements `c(VAL, MIN, MAX)`, where VAL is the starting value of the slider in |
|
72 |
#' the main and modal plot display; `NULL` for default display. The value in the modal |
|
73 |
#' display is taken from the value of the slider in the main plot display. |
|
74 |
#' @param show_hide_signal optional, (`reactive logical` a mechanism to allow modules which call this |
|
75 |
#' module to show/hide the plot_with_settings UI) |
|
76 |
#' @param brushing (`logical`) optional\cr |
|
77 |
#' mechanism to enable / disable brushing on the main plot (in particular: not the one displayed |
|
78 |
#' in modal). All the brushing data is stored as a reactive object in the `"brush"` element of |
|
79 |
#' returned list. See the example for details. |
|
80 |
#' @param clicking (`logical`)\cr |
|
81 |
#' a mechanism to enable / disable clicking on data points on the main plot (in particular: not the |
|
82 |
#' one displayed in modal). All the clicking data is stored as a reactive object in the `"click"` |
|
83 |
#' element of returned list. See the example for details. |
|
84 |
#' @param dblclicking (`logical`) optional\cr |
|
85 |
#' mechanism to enable / disable double-clicking on data points on the main plot (in particular: |
|
86 |
#' not the one displayed in modal). All the double clicking data is stored as a reactive object in |
|
87 |
#' the `"dblclick"` element of returned list. See the example for details. |
|
88 |
#' @param hovering (`logical(1)`) optional\cr |
|
89 |
#' mechanism to enable / disable hovering over data points on the main plot (in particular: not |
|
90 |
#' the one displayed in modal). All the hovering data is stored as a reactive object in the |
|
91 |
#' `"hover"` element of returned list. See the example for details. |
|
92 |
#' @param graph_align (`character(1)`) optional,\cr |
|
93 |
#' one of `"left"` (default), `"center"`, `"right"` or `"justify"`. The alignment of the graph on |
|
94 |
#' the main page. |
|
95 |
#' |
|
96 |
#' @details By default the plot is rendered with `72 dpi`. In order to change this, to for example 96 set |
|
97 |
#' `options(teal.plot_dpi = 96)`. The minimum allowed `dpi` value is `24` and it must be a whole number. |
|
98 |
#' If an invalid value is set then the default value is used and a warning is outputted to the console. |
|
99 |
#' |
|
100 |
#' @return A `shiny` module. |
|
101 |
#' |
|
102 |
#' @examples |
|
103 |
#' # Example using a reactive as input to plot_r |
|
104 |
#' library(shiny) |
|
105 |
#' library(ggplot2) |
|
106 |
#' |
|
107 |
#' ui <- fluidPage( |
|
108 |
#' plot_with_settings_ui( |
|
109 |
#' id = "plot_with_settings" |
|
110 |
#' ) |
|
111 |
#' ) |
|
112 |
#' |
|
113 |
#' server <- function(input, output, session) { |
|
114 |
#' plot_r <- reactive({ |
|
115 |
#' ggplot(faithful, aes(x = .data$waiting, y = .data$eruptions)) + |
|
116 |
#' geom_point() |
|
117 |
#' }) |
|
118 |
#' |
|
119 |
#' plot_with_settings_srv( |
|
120 |
#' id = "plot_with_settings", |
|
121 |
#' plot_r = plot_r, |
|
122 |
#' height = c(400, 100, 1200), |
|
123 |
#' width = c(500, 250, 750) |
|
124 |
#' ) |
|
125 |
#' } |
|
126 |
#' |
|
127 |
#' if (interactive()) { |
|
128 |
#' shinyApp(ui, server) |
|
129 |
#' } |
|
130 |
#' |
|
131 |
#' # Example using a function as input to plot_r |
|
132 |
#' library(lattice) |
|
133 |
#' |
|
134 |
#' ui <- fluidPage( |
|
135 |
#' radioButtons("download_option", "Select the Option", list("ggplot", "trellis", "grob", "base")), |
|
136 |
#' plot_with_settings_ui( |
|
137 |
#' id = "plot_with_settings" |
|
138 |
#' ), |
|
139 |
#' sliderInput("nums", "Value", 1, 10, 1) |
|
140 |
#' ) |
|
141 |
#' |
|
142 |
#' server <- function(input, output, session) { |
|
143 |
#' plot_r <- function() { |
|
144 |
#' numbers <- seq_len(input$nums) |
|
145 |
#' if (input$download_option == "ggplot") { |
|
146 |
#' ggplot(data.frame(n = numbers), aes(.data$n)) + |
|
147 |
#' geom_bar() |
|
148 |
#' } else if (input$download_option == "trellis") { |
|
149 |
#' densityplot(numbers) |
|
150 |
#' } else if (input$download_option == "grob") { |
|
151 |
#' tr_plot <- densityplot(numbers) |
|
152 |
#' ggplotGrob( |
|
153 |
#' ggplot(data.frame(n = numbers), aes(.data$n)) + |
|
154 |
#' geom_bar() |
|
155 |
#' ) |
|
156 |
#' } else if (input$download_option == "base") { |
|
157 |
#' plot(numbers) |
|
158 |
#' } |
|
159 |
#' } |
|
160 |
#' |
|
161 |
#' plot_with_settings_srv( |
|
162 |
#' id = "plot_with_settings", |
|
163 |
#' plot_r = plot_r, |
|
164 |
#' height = c(400, 100, 1200), |
|
165 |
#' width = c(500, 250, 750) |
|
166 |
#' ) |
|
167 |
#' } |
|
168 |
#' |
|
169 |
#' if (interactive()) { |
|
170 |
#' shinyApp(ui, server) |
|
171 |
#' } |
|
172 |
#' |
|
173 |
#' # Example with brushing/hovering/clicking/double-clicking |
|
174 |
#' ui <- fluidPage( |
|
175 |
#' plot_with_settings_ui( |
|
176 |
#' id = "plot_with_settings" |
|
177 |
#' ), |
|
178 |
#' fluidRow( |
|
179 |
#' column(4, tags$h3("Brush"), verbatimTextOutput("brushing_data")), |
|
180 |
#' column(4, tags$h3("Click"), verbatimTextOutput("clicking_data")), |
|
181 |
#' column(4, tags$h3("DblClick"), verbatimTextOutput("dblclicking_data")), |
|
182 |
#' column(4, tags$h3("Hover"), verbatimTextOutput("hovering_data")) |
|
183 |
#' ) |
|
184 |
#' ) |
|
185 |
#' |
|
186 |
#' server <- function(input, output, session) { |
|
187 |
#' plot_r <- reactive({ |
|
188 |
#' ggplot(faithful, aes(x = .data$waiting, y = .data$eruptions)) + |
|
189 |
#' geom_point() |
|
190 |
#' }) |
|
191 |
#' |
|
192 |
#' plot_data <- plot_with_settings_srv( |
|
193 |
#' id = "plot_with_settings", |
|
194 |
#' plot_r = plot_r, |
|
195 |
#' height = c(400, 100, 1200), |
|
196 |
#' brushing = TRUE, |
|
197 |
#' clicking = TRUE, |
|
198 |
#' dblclicking = TRUE, |
|
199 |
#' hovering = TRUE |
|
200 |
#' ) |
|
201 |
#' |
|
202 |
#' output$brushing_data <- renderPrint(plot_data$brush()) |
|
203 |
#' output$clicking_data <- renderPrint(plot_data$click()) |
|
204 |
#' output$dblclicking_data <- renderPrint(plot_data$dblclick()) |
|
205 |
#' output$hovering_data <- renderPrint(plot_data$hover()) |
|
206 |
#' } |
|
207 |
#' |
|
208 |
#' if (interactive()) { |
|
209 |
#' shinyApp(ui, server) |
|
210 |
#' } |
|
211 |
#' |
|
212 |
#' # Example which allows module to be hidden/shown |
|
213 |
#' library("shinyjs") |
|
214 |
#' |
|
215 |
#' ui <- fluidPage( |
|
216 |
#' useShinyjs(), |
|
217 |
#' actionButton("button", "Show/Hide"), |
|
218 |
#' plot_with_settings_ui( |
|
219 |
#' id = "plot_with_settings" |
|
220 |
#' ) |
|
221 |
#' ) |
|
222 |
#' |
|
223 |
#' server <- function(input, output, session) { |
|
224 |
#' plot_r <- plot_r <- reactive( |
|
225 |
#' ggplot(faithful, aes(x = .data$waiting, y = .data$eruptions)) + |
|
226 |
#' geom_point() |
|
227 |
#' ) |
|
228 |
#' |
|
229 |
#' show_hide_signal_rv <- reactiveVal(TRUE) |
|
230 |
#' |
|
231 |
#' observeEvent(input$button, show_hide_signal_rv(!show_hide_signal_rv())) |
|
232 |
#' |
|
233 |
#' plot_with_settings_srv( |
|
234 |
#' id = "plot_with_settings", |
|
235 |
#' plot_r = plot_r, |
|
236 |
#' height = c(400, 100, 1200), |
|
237 |
#' width = c(500, 250, 750), |
|
238 |
#' show_hide_signal = reactive(show_hide_signal_rv()) |
|
239 |
#' ) |
|
240 |
#' } |
|
241 |
#' |
|
242 |
#' if (interactive()) { |
|
243 |
#' shinyApp(ui, server) |
|
244 |
#' } |
|
245 |
#' |
|
246 |
plot_with_settings_srv <- function(id, |
|
247 |
plot_r, |
|
248 |
height = c(600, 200, 2000), |
|
249 |
width = NULL, |
|
250 |
show_hide_signal = reactive(TRUE), |
|
251 |
brushing = FALSE, |
|
252 |
clicking = FALSE, |
|
253 |
dblclicking = FALSE, |
|
254 |
hovering = FALSE, |
|
255 |
graph_align = "left") { |
|
256 | 23x |
checkmate::assert_string(id) |
257 | 23x |
checkmate::assert( |
258 | 23x |
checkmate::check_class(plot_r, "function"), |
259 | 23x |
checkmate::check_class(plot_r, "reactive") |
260 |
) |
|
261 | 22x |
checkmate::assert_numeric(height, min.len = 1, any.missing = FALSE) |
262 | 21x |
checkmate::assert_numeric(height, len = 3, any.missing = FALSE, finite = TRUE) |
263 | 21x |
checkmate::assert_numeric(height[1], lower = height[2], upper = height[3], .var.name = "height") |
264 | 21x |
checkmate::assert_numeric(width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE) |
265 | 21x |
checkmate::assert_numeric(width[1], lower = width[2], upper = width[3], null.ok = TRUE, .var.name = "width") |
266 | ||
267 | 20x |
checkmate::assert_class(show_hide_signal, c("reactive", "function")) |
268 | 19x |
checkmate::assert_flag(brushing) |
269 | 18x |
checkmate::assert_flag(clicking) |
270 | 17x |
checkmate::assert_flag(dblclicking) |
271 | 16x |
checkmate::assert_flag(hovering) |
272 | 15x |
checkmate::assert_string(graph_align) |
273 | 15x |
checkmate::assert_subset(graph_align, c("left", "right", "center", "justify")) |
274 | ||
275 | 14x |
moduleServer(id, function(input, output, session) { |
276 | 14x |
ns <- session$ns |
277 | 14x |
shinyjs::runjs( |
278 | 14x |
sprintf( |
279 | 14x |
'establishPlotResizing("%s", "%s", "%s");', |
280 | 14x |
ns("plot_main"), # graph parent id |
281 | 14x |
ns("flex_width"), # session input$ variable name |
282 | 14x |
ns("plot_modal_width") # session input$ variable name |
283 |
) |
|
284 |
) |
|
285 | 14x |
default_w <- function() session$clientData[[paste0("output_", ns("plot_main_width"))]] |
286 | 14x |
default_h <- function() session$clientData[[paste0("output_", ns("plot_main_height"))]] |
287 | ||
288 | 14x |
default_slider_width <- reactiveVal(width) |
289 | 14x |
delayed_flex_width <- debounce(reactive(input$flex_width), millis = 100) |
290 | ||
291 | 14x |
if (is.null(width)) { |
292 |
# if width = NULL then set default_slider_width to be the value of the plot width on load |
|
293 | ! |
observeEvent(session$clientData[[paste0("output_", ns("plot_main_width"))]], |
294 | ! |
handlerExpr = { |
295 | ! |
default_slider_width(default_w() * c(1, 0.5, 2.8)) |
296 |
}, |
|
297 | ! |
once = TRUE, |
298 | ! |
ignoreNULL = TRUE |
299 |
) |
|
300 | ||
301 | ! |
observeEvent(delayed_flex_width(), { |
302 | ! |
if (delayed_flex_width() > 0 && !isFALSE(input$width_resize_switch)) { |
303 | ! |
default_slider_width(delayed_flex_width() * c(1, 0.5, 2.8)) |
304 | ! |
updateSliderInput(session, inputId = "width", value = delayed_flex_width()) |
305 |
} |
|
306 |
}) |
|
307 |
} |
|
308 | ||
309 | 14x |
plot_type <- reactive({ |
310 | 14x |
if (inherits(plot_r(), "ggplot")) { |
311 | 2x |
"gg" |
312 | 12x |
} else if (inherits(plot_r(), "trellis")) { |
313 | 2x |
"trel" |
314 | 10x |
} else if (inherits(plot_r(), "grob")) { |
315 | 2x |
"grob" |
316 | 8x |
} else if (inherits(plot_r(), c("NULL", "histogram", "list")) && !inherits(plot_r, "reactive")) { |
317 | 6x |
"base" |
318 |
} else { |
|
319 | 2x |
"other" |
320 |
} |
|
321 |
}) |
|
322 | ||
323 |
# allow modules which use this module to turn on and off the UI |
|
324 | 14x |
observeEvent(show_hide_signal(), { |
325 | 8x |
if (show_hide_signal()) { |
326 | 8x |
shinyjs::show("plot-with-settings") |
327 |
} else { |
|
328 | ! |
shinyjs::hide("plot-with-settings") |
329 |
} |
|
330 |
}) |
|
331 | ||
332 | 14x |
output$slider_ui <- renderUI({ |
333 | 8x |
tags$div( |
334 | 8x |
optionalSliderInputValMinMax( |
335 | 8x |
inputId = ns("height"), |
336 | 8x |
label = "Plot height", |
337 | 8x |
value_min_max = round(height), |
338 | 8x |
ticks = FALSE, |
339 | 8x |
step = 1L, |
340 | 8x |
round = TRUE |
341 |
), |
|
342 | 8x |
tags$b("Plot width"), |
343 | 8x |
shinyWidgets::switchInput( |
344 | 8x |
inputId = ns("width_resize_switch"), |
345 | 8x |
onLabel = "ON", |
346 | 8x |
offLabel = "OFF", |
347 | 8x |
label = "Auto width", |
348 | 8x |
value = `if`(is.null(width), TRUE, FALSE), |
349 | 8x |
size = "mini", |
350 | 8x |
labelWidth = "80px" |
351 |
), |
|
352 | 8x |
optionalSliderInputValMinMax( |
353 | 8x |
inputId = ns("width"), |
354 | 8x |
label = NULL, |
355 | 8x |
value_min_max = round(isolate(default_slider_width())), |
356 | 8x |
ticks = FALSE, |
357 | 8x |
step = 1L, |
358 | 8x |
round = TRUE |
359 |
) |
|
360 |
) |
|
361 |
}) |
|
362 | ||
363 | 14x |
observeEvent(input$width_resize_switch | delayed_flex_width(), { |
364 | 8x |
if (length(input$width_resize_switch) && input$width_resize_switch) { |
365 | ! |
shinyjs::disable("width") |
366 | ! |
updateSliderInput(session, inputId = "width", value = delayed_flex_width()) |
367 |
} else { |
|
368 | 8x |
shinyjs::enable("width") |
369 |
} |
|
370 |
}) |
|
371 | ||
372 | 14x |
ranges <- reactiveValues(x = NULL, y = NULL) |
373 | ||
374 | 14x |
observeEvent(input$plot_dblclick, { |
375 | 1x |
brush <- input$plot_brush |
376 | 1x |
if (!is.null(brush)) { |
377 | ! |
ranges$x <- c(brush$xmin, brush$xmax) |
378 | ! |
ranges$y <- c(brush$ymin, brush$ymax) |
379 |
} else { |
|
380 | 1x |
ranges$x <- NULL |
381 | 1x |
ranges$y <- NULL |
382 |
} |
|
383 |
}) |
|
384 | ||
385 | 14x |
p_height <- reactive(if (!is.null(input$height)) input$height else height[1]) |
386 | 14x |
p_width <- reactive( |
387 | 14x |
if (!is.null(input$width)) { |
388 | 5x |
input$width |
389 |
} else { |
|
390 | 3x |
if (!is.null(default_slider_width()[1])) { |
391 | 3x |
default_slider_width()[1] |
392 |
} else { |
|
393 |
# Fallback to "auto" |
|
394 | ! |
"auto" |
395 |
} |
|
396 |
} |
|
397 |
) |
|
398 | 14x |
output$plot_main <- renderPlot( |
399 | 14x |
apply_plot_modifications( |
400 | 14x |
plot_obj = plot_suppress(plot_r()), |
401 | 14x |
plot_type = plot_suppress(plot_type()), |
402 | 14x |
dblclicking = dblclicking, |
403 | 14x |
ranges = ranges |
404 |
), |
|
405 | 14x |
res = get_plot_dpi(), |
406 | 14x |
height = p_height, |
407 | 14x |
width = p_width |
408 |
) |
|
409 | ||
410 | 14x |
output$plot_modal <- renderPlot( |
411 | 14x |
apply_plot_modifications( |
412 | 14x |
plot_obj = plot_suppress(plot_r()), |
413 | 14x |
plot_type = plot_suppress(plot_type()), |
414 | 14x |
dblclicking = dblclicking, |
415 | 14x |
ranges = ranges |
416 |
), |
|
417 | 14x |
res = get_plot_dpi(), |
418 | 14x |
height = reactive(input$height_in_modal), |
419 | 14x |
width = reactive(input$width_in_modal) |
420 |
) |
|
421 | ||
422 | 14x |
output$plot_out_main <- renderUI({ |
423 | 8x |
req(plot_suppress(plot_r())) |
424 | 4x |
tags$div( |
425 | 4x |
align = graph_align, |
426 | 4x |
plotOutput( |
427 | 4x |
ns("plot_main"), |
428 | 4x |
height = "100%", |
429 | 4x |
brush = `if`(brushing, brushOpts(ns("plot_brush"), resetOnNew = FALSE), NULL), |
430 | 4x |
click = `if`(clicking, clickOpts(ns("plot_click")), NULL), |
431 | 4x |
dblclick = `if`(dblclicking, dblclickOpts(ns("plot_dblclick")), NULL), |
432 | 4x |
hover = `if`(hovering, hoverOpts(ns("plot_hover")), NULL) |
433 |
) |
|
434 |
) |
|
435 |
}) |
|
436 | ||
437 | 14x |
output$width_warning <- renderUI({ |
438 | 8x |
grDevices::pdf(nullfile()) # reset Rplots.pdf for shiny server |
439 | 8x |
w <- grDevices::dev.size("px")[1] |
440 | 8x |
grDevices::dev.off() |
441 | 8x |
if (p_width() < w) { |
442 | 8x |
helpText( |
443 | 8x |
icon("triangle-exclamation"), |
444 | 8x |
"Plot might be cut off for small widths." |
445 |
) |
|
446 |
} |
|
447 |
}) |
|
448 | ||
449 | 14x |
type_download_srv( |
450 | 14x |
id = "downbutton", |
451 | 14x |
plot_reactive = plot_r, |
452 | 14x |
plot_type = plot_type, |
453 | 14x |
plot_w = p_width, |
454 | 14x |
default_w = default_w, |
455 | 14x |
plot_h = p_height, |
456 | 14x |
default_h = default_h |
457 |
) |
|
458 | ||
459 | 14x |
output$plot_out_modal <- renderUI({ |
460 | 9x |
plotOutput(ns("plot_modal"), height = input$height_in_modal, width = input$width_in_modal) |
461 |
}) |
|
462 | ||
463 | 14x |
observeEvent(input$expand, { |
464 | 1x |
showModal( |
465 | 1x |
tags$div( |
466 | 1x |
class = "plot-modal", |
467 | 1x |
modalDialog( |
468 | 1x |
easyClose = TRUE, |
469 | 1x |
tags$div( |
470 | 1x |
class = "plot-modal-sliders", |
471 | 1x |
optionalSliderInputValMinMax( |
472 | 1x |
inputId = ns("height_in_modal"), |
473 | 1x |
label = "Plot height", |
474 | 1x |
value_min_max = round(c(p_height(), height[2:3])), |
475 | 1x |
ticks = FALSE, |
476 | 1x |
step = 1L, |
477 | 1x |
round = TRUE |
478 |
), |
|
479 | 1x |
optionalSliderInputValMinMax( |
480 | 1x |
inputId = ns("width_in_modal"), |
481 | 1x |
label = "Plot width", |
482 | 1x |
value_min_max = round(c( |
483 | 1x |
ifelse( |
484 | 1x |
is.null(input$width) || !isFALSE(input$width_resize_switch), |
485 | 1x |
ifelse( |
486 | 1x |
is.null(input$plot_modal_width) || input$plot_modal_width > default_slider_width()[3], |
487 | 1x |
default_slider_width()[1], |
488 | 1x |
input$plot_modal_width |
489 |
), |
|
490 | 1x |
input$width |
491 |
), |
|
492 | 1x |
default_slider_width()[2:3] |
493 |
)), |
|
494 | 1x |
ticks = FALSE, |
495 | 1x |
step = 1L, |
496 | 1x |
round = TRUE |
497 |
) |
|
498 |
), |
|
499 | 1x |
tags$div( |
500 | 1x |
class = "float-right", |
501 | 1x |
type_download_ui(ns("modal_downbutton")) |
502 |
), |
|
503 | 1x |
tags$div( |
504 | 1x |
align = "center", |
505 | 1x |
uiOutput(ns("plot_out_modal"), class = "plot_out_container") |
506 |
) |
|
507 |
) |
|
508 |
) |
|
509 |
) |
|
510 |
}) |
|
511 | ||
512 | 14x |
type_download_srv( |
513 | 14x |
id = "modal_downbutton", |
514 | 14x |
plot_reactive = plot_r, |
515 | 14x |
plot_type = plot_type, |
516 | 14x |
plot_w = reactive(input$width_in_modal), |
517 | 14x |
default_w = default_w, |
518 | 14x |
plot_h = reactive(input$height_in_modal), |
519 | 14x |
default_h = default_h |
520 |
) |
|
521 | ||
522 | 14x |
return( |
523 | 14x |
list( |
524 | 14x |
brush = reactive({ |
525 |
# refresh brush data on the main plot size change |
|
526 | 1x |
input$height |
527 | 1x |
input$width |
528 | 1x |
input$plot_brush |
529 |
}), |
|
530 | 14x |
click = reactive({ |
531 |
# refresh click data on the main plot size change |
|
532 | 1x |
input$height |
533 | 1x |
input$width |
534 | 1x |
input$plot_click |
535 |
}), |
|
536 | 14x |
dblclick = reactive({ |
537 |
# refresh double click data on the main plot size change |
|
538 | 1x |
input$height |
539 | 1x |
input$width |
540 | 1x |
input$plot_dblclick |
541 |
}), |
|
542 | 14x |
hover = reactive({ |
543 |
# refresh hover data on the main plot size change |
|
544 | 1x |
input$height |
545 | 1x |
input$width |
546 | 1x |
input$plot_hover |
547 |
}), |
|
548 | 14x |
dim = reactive(c(p_width(), p_height())) |
549 |
) |
|
550 |
) |
|
551 |
}) |
|
552 |
} |
|
553 | ||
554 |
#' @keywords internal |
|
555 |
type_download_ui <- function(id) { |
|
556 | 3x |
ns <- NS(id) |
557 | 3x |
shinyWidgets::dropdownButton( |
558 | 3x |
circle = FALSE, |
559 | 3x |
size = "sm", |
560 | 3x |
icon = icon("download"), |
561 | 3x |
inline = TRUE, |
562 | 3x |
right = TRUE, |
563 | 3x |
label = "", |
564 | 3x |
inputId = ns("downl"), |
565 | 3x |
tags$div( |
566 | 3x |
radioButtons(ns("file_format"), |
567 | 3x |
label = "File type", |
568 | 3x |
choices = c("png" = "png", "pdf" = "pdf", "svg" = "svg"), |
569 |
), |
|
570 | 3x |
textInput(ns("file_name"), |
571 | 3x |
label = "File name (without extension)", |
572 | 3x |
value = paste0("plot_", strftime(Sys.time(), format = "%Y%m%d_%H%M%S")) |
573 |
), |
|
574 | 3x |
conditionalPanel( |
575 | 3x |
condition = paste0("input['", ns("file_name"), "'] != ''"), |
576 | 3x |
downloadButton(ns("data_download"), label = character(0), class = "btn-sm w-full") |
577 |
) |
|
578 |
) |
|
579 |
) |
|
580 |
} |
|
581 | ||
582 |
#' @keywords internal |
|
583 |
type_download_srv <- function(id, plot_reactive, plot_type, plot_w, default_w, plot_h, default_h) { |
|
584 | 32x |
moduleServer( |
585 | 32x |
id, |
586 | 32x |
function(input, output, session) { |
587 | 32x |
output$data_download <- downloadHandler( |
588 | 32x |
filename = function() { |
589 | 20x |
paste(input$file_name, input$file_format, sep = ".") |
590 |
}, |
|
591 | 32x |
content = function(file) { |
592 | 20x |
width <- `if`(!is.null(plot_w()), plot_w(), default_w()) |
593 | 20x |
height <- `if`(!is.null(plot_h()), plot_h(), default_h()) |
594 | ||
595 |
# svg and pdf have width in inches and 1 inch = get_plot_dpi() pixels |
|
596 | 20x |
switch(input$file_format, |
597 | 12x |
png = grDevices::png(file, width, height), |
598 | 4x |
pdf = grDevices::pdf(file, width / get_plot_dpi(), height / get_plot_dpi()), |
599 | 4x |
svg = grDevices::svg(file, width / get_plot_dpi(), height / get_plot_dpi()) |
600 |
) |
|
601 | ||
602 | 20x |
print_plot(plot_reactive, plot_type) |
603 | ||
604 | 20x |
grDevices::dev.off() |
605 |
} |
|
606 |
) |
|
607 |
} |
|
608 |
) |
|
609 |
} |
|
610 | ||
611 |
#' Clean brushed points |
|
612 |
#' |
|
613 |
#' @description `r lifecycle::badge("stable")`\cr |
|
614 |
#' Cleans and organizes output to account for NAs and remove empty rows. Wrapper around `shiny::brushedPoints`. |
|
615 |
#' @param data (`data.frame`)\cr |
|
616 |
#' A data.frame from which to select rows. |
|
617 |
#' @param brush (`list`)\cr |
|
618 |
#' The data from a brush e.g. `input$plot_brush`. |
|
619 |
#' |
|
620 |
#' @return A `data.frame` of selected rows. |
|
621 |
#' |
|
622 |
#' @examples |
|
623 |
#' |
|
624 |
#' brush <- list( |
|
625 |
#' mapping = list( |
|
626 |
#' x = "AGE", |
|
627 |
#' y = "BMRKR1" |
|
628 |
#' ), |
|
629 |
#' xmin = 30, xmax = 40, |
|
630 |
#' ymin = 0.7, ymax = 10, |
|
631 |
#' direction = "xy" |
|
632 |
#' ) |
|
633 |
#' |
|
634 |
#' data <- data.frame( |
|
635 |
#' STUDYID = letters[1:20], |
|
636 |
#' USUBJID = LETTERS[1:20], |
|
637 |
#' AGE = sample(25:40, size = 20, replace = TRUE), |
|
638 |
#' BMRKR1 = runif(20, min = 0, max = 12) |
|
639 |
#' ) |
|
640 |
#' nrow(clean_brushedPoints(data, brush)) |
|
641 |
#' data$AGE[1:10] <- NA |
|
642 |
#' nrow(clean_brushedPoints(data, brush)) |
|
643 |
#' |
|
644 |
#' @export |
|
645 |
#' |
|
646 |
clean_brushedPoints <- function(data, brush) { # nolint object_name_linter. |
|
647 | 6x |
checkmate::assert_data_frame(data) |
648 | 4x |
checkmate::assert_list(brush, null.ok = TRUE) |
649 | ||
650 |
# define original panelvar1 and panelvar2 before getting overwritten |
|
651 | 4x |
original_panelvar1 <- brush$mapping$panelvar1 |
652 | 4x |
original_panelvar2 <- brush$mapping$panelvar2 |
653 | ||
654 |
# Assign NULL to `mapping$panelvar1` and `mapping$panelvar1` if `brush$panelvar1` and `brush$panelvar1` are NULL |
|
655 |
# This will not evaluate the `panelMatch` step in `brushedPoints` and thus will return a non empty dataframe |
|
656 | 4x |
if (is.null(brush$panelvar1)) brush$mapping$panelvar1 <- NULL |
657 | 4x |
if (is.null(brush$panelvar2)) brush$mapping$panelvar2 <- NULL |
658 | ||
659 | 4x |
bp_df <- brushedPoints(data, brush) |
660 | ||
661 |
# Keep required rows only based on the value of `brush$panelvar1` |
|
662 | 3x |
df <- if (is.null(brush$panelvar1) && is.character(original_panelvar1) && |
663 | 3x |
is.null(brush$panelvar2) && is.character(original_panelvar2)) { |
664 | ! |
df_var1 <- bp_df[is.na(bp_df[[original_panelvar1]]), ] |
665 | ! |
df_var1[is.na(df_var1[[original_panelvar2]]), ] |
666 | 3x |
} else if (is.null(brush$panelvar1) && is.character(original_panelvar1)) { |
667 | ! |
bp_df[is.na(bp_df[[original_panelvar1]]), ] |
668 | 3x |
} else if (is.null(brush$panelvar2) && is.character(original_panelvar2)) { |
669 | ! |
bp_df[is.na(bp_df[[original_panelvar2]]), ] |
670 |
} else { |
|
671 | 3x |
bp_df |
672 |
} |
|
673 | ||
674 |
# filter out rows that are only NAs |
|
675 | 3x |
df <- df[rowSums(is.na(df)) != ncol(df), ] |
676 | 3x |
df |
677 |
} |
|
678 | ||
679 |
#' @keywords internal |
|
680 |
#' |
|
681 |
get_plot_dpi <- function() { |
|
682 | 53x |
default_dpi <- 72 |
683 | 53x |
dpi <- getOption("teal.plot_dpi", default_dpi) |
684 | 53x |
if (!checkmate::test_integerish(dpi, lower = 24, any.missing = FALSE, len = 1)) { |
685 | 4x |
warning(paste("Invalid value for option 'teal.plot_dpi', therefore defaulting to", default_dpi, "dpi")) |
686 | 4x |
dpi <- default_dpi |
687 |
} |
|
688 | 53x |
dpi |
689 |
} |
|
690 | ||
691 |
#' Print plot for download functionality |
|
692 |
#' |
|
693 |
#' @param plot (`reactive`)\cr |
|
694 |
#' reactive expression to draw a plot |
|
695 |
#' @param plot_type (`reactive`)\cr |
|
696 |
#' reactive plot type (`gg`, `trel`, `grob`, `other`) |
|
697 |
#' |
|
698 |
#' @return Nothing returned, the plot is printed. |
|
699 |
#' @keywords internal |
|
700 |
#' |
|
701 |
print_plot <- function(plot, plot_type) { |
|
702 | 26x |
switch(plot_type(), |
703 | 2x |
"grob" = grid::grid.draw(plot()), |
704 |
"other" = { |
|
705 | 2x |
graphics::plot.new() |
706 | 2x |
graphics::text( |
707 | 2x |
x = graphics::grconvertX(0.5, from = "npc"), |
708 | 2x |
y = graphics::grconvertY(0.5, from = "npc"), |
709 | 2x |
labels = "This plot graphic type is not yet supported to download" |
710 |
) |
|
711 |
}, |
|
712 | 18x |
"base" = plot(), |
713 | 4x |
print(plot()) |
714 |
) |
|
715 |
} |
1 |
#' @keywords internal |
|
2 |
#' @noRd |
|
3 |
draggable_buckets_deps <- function() { |
|
4 | ! |
htmltools::htmlDependency( |
5 | ! |
name = "teal-widgets-draggable-buckets", |
6 | ! |
version = utils::packageVersion("teal.widgets"), |
7 | ! |
package = "teal.widgets", |
8 | ! |
src = "draggable-buckets", |
9 | ! |
script = "draggable-buckets.js", |
10 | ! |
stylesheet = "draggable-buckets.css" |
11 |
) |
|
12 |
} |
|
13 | ||
14 |
#' @title Draggable Buckets |
|
15 |
#' @description `r lifecycle::badge("experimental")` |
|
16 |
#' A custom widget with draggable elements that can be put into buckets. |
|
17 |
#' |
|
18 |
#' @param input_id (`character(1)`) the `HTML` id of this widget |
|
19 |
#' @param label (`character(1)` or `shiny.tag`) the header of this widget |
|
20 |
#' @param elements (`character`) the elements to drag into buckets |
|
21 |
#' @param buckets (`character` or `list`) the names of the buckets the elements can be put in or a list of key-pair |
|
22 |
#' values where key is a name of a bucket and value is a character vector of elements in a bucket |
|
23 |
#' |
|
24 |
#' @return the `HTML` code comprising an instance of this widget |
|
25 |
#' @export |
|
26 |
#' |
|
27 |
#' @details `shinyvalidate` validation can be used with this widget. See example below. |
|
28 |
#' |
|
29 |
#' @examples |
|
30 |
#' library(shiny) |
|
31 |
#' |
|
32 |
#' ui <- fluidPage( |
|
33 |
#' draggable_buckets("id", "Choices #1", c("a", "b"), c("bucket1", "bucket2")), |
|
34 |
#' draggable_buckets("id2", "Choices #2", letters, c("vowels", "consonants")), |
|
35 |
#' verbatimTextOutput("out"), |
|
36 |
#' verbatimTextOutput("out2") |
|
37 |
#' ) |
|
38 |
#' server <- function(input, output) { |
|
39 |
#' iv <- shinyvalidate::InputValidator$new() |
|
40 |
#' iv$add_rule( |
|
41 |
#' "id", |
|
42 |
#' function(data) if (length(data[["bucket1"]]) == 0) "There should be stuff in bucket 1" |
|
43 |
#' ) |
|
44 |
#' iv$enable() |
|
45 |
#' |
|
46 |
#' observeEvent(list(input$id, input$id2), { |
|
47 |
#' print(isolate(input$id)) |
|
48 |
#' print(isolate(input$id2)) |
|
49 |
#' }) |
|
50 |
#' output$out <- renderPrint({ |
|
51 |
#' iv$is_valid() |
|
52 |
#' input$id |
|
53 |
#' }) |
|
54 |
#' output$out2 <- renderPrint(input$id2) |
|
55 |
#' } |
|
56 |
#' if (interactive()) shinyApp(ui, server) |
|
57 |
#' |
|
58 |
#' # With default elements in the bucket |
|
59 |
#' ui <- fluidPage( |
|
60 |
#' draggable_buckets("id", "Choices #1", c("a", "b"), list(bucket1 = character(), bucket2 = c("c"))), |
|
61 |
#' verbatimTextOutput("out") |
|
62 |
#' ) |
|
63 |
#' server <- function(input, output) { |
|
64 |
#' observeEvent(input$id, { |
|
65 |
#' print(isolate(input$id)) |
|
66 |
#' }) |
|
67 |
#' output$out <- renderPrint(input$id) |
|
68 |
#' } |
|
69 |
#' if (interactive()) shinyApp(ui, server) |
|
70 |
draggable_buckets <- function(input_id, label, elements = character(), buckets) { |
|
71 | ! |
checkmate::assert_string(input_id) |
72 | ! |
checkmate::assert_true(inherits(label, "character") || inherits(label, "shiny.tag")) |
73 | ! |
checkmate::assert_character(c(elements, unlist(buckets)), min.len = 0, null.ok = TRUE, unique = TRUE) |
74 | ! |
checkmate::assert( |
75 | ! |
checkmate::check_character(buckets, min.len = 1), |
76 | ! |
checkmate::check_list(buckets, types = "character", names = "unique") |
77 |
) |
|
78 | ||
79 | ! |
elements_iterator <- new.env(parent = emptyenv()) |
80 | ! |
elements_iterator$it <- 0 |
81 | ||
82 | ! |
shiny::tagList( |
83 | ! |
draggable_buckets_deps(), |
84 | ! |
shiny::div( |
85 | ! |
tags$span(label), |
86 | ! |
render_unbucketed_elements(elements = elements, elements_iterator = elements_iterator, widget_id = input_id), |
87 | ! |
render_buckets(buckets = buckets, elements_iterator = elements_iterator, widget_id = input_id), |
88 | ! |
class = "draggableBuckets", |
89 | ! |
id = input_id |
90 |
) |
|
91 |
) |
|
92 |
} |
|
93 | ||
94 |
render_unbucketed_elements <- function(elements, elements_iterator, widget_id) { |
|
95 | ! |
tags$div( |
96 | ! |
lapply(elements, function(element) { |
97 | ! |
elements_iterator$it <- elements_iterator$it + 1 |
98 | ! |
render_draggable_element( |
99 | ! |
value = element, |
100 | ! |
id = paste0(widget_id, "draggable", elements_iterator$it), |
101 | ! |
widget_id = widget_id |
102 |
) |
|
103 |
}), |
|
104 | ! |
id = paste0(widget_id, "elements"), |
105 | ! |
class = c("form-control", "elements"), |
106 | ! |
ondragover = "allowDrop(event)", |
107 | ! |
ondrop = "drop(event)", |
108 | ! |
`data-widget` = widget_id |
109 |
) |
|
110 |
} |
|
111 | ||
112 |
render_buckets <- function(buckets, elements_iterator, widget_id) { |
|
113 | ! |
buckets <- `if`( |
114 | ! |
is.list(buckets), |
115 | ! |
lapply(names(buckets), function(bucket_name) { |
116 | ! |
render_bucket( |
117 | ! |
name = bucket_name, |
118 | ! |
elements = buckets[[bucket_name]], |
119 | ! |
elements_iterator = elements_iterator, |
120 | ! |
widget_id = widget_id |
121 |
) |
|
122 |
}), |
|
123 | ! |
lapply(buckets, render_bucket, widget_id = widget_id, elements_iterator = elements_iterator) |
124 |
) |
|
125 | ! |
shiny::tagList(buckets) |
126 |
} |
|
127 | ||
128 |
render_draggable_element <- function(value, id, widget_id) { |
|
129 | ! |
tags$div( |
130 | ! |
value, |
131 | ! |
id = id, |
132 | ! |
class = "element", |
133 | ! |
draggable = "true", |
134 | ! |
ondragstart = "drag(event)", |
135 | ! |
ondragover = "allowDrop(event)", |
136 | ! |
ondrop = "dropReorder(event)", |
137 | ! |
`data-widget` = widget_id |
138 |
) |
|
139 |
} |
|
140 | ||
141 |
render_bucket <- function(name, elements = NULL, elements_iterator = NULL, widget_id = NULL) { |
|
142 | ! |
tags$div( |
143 | ! |
tags$div( |
144 | ! |
paste0(name, ":"), |
145 | ! |
class = "bucket-name", |
146 | ! |
ondragover = "allowDrop(event)", |
147 | ! |
ondrop = "dropBucketName(event)", |
148 | ! |
`data-widget` = widget_id |
149 |
), |
|
150 | ! |
lapply(elements, function(element) { |
151 | ! |
elements_iterator$it <- elements_iterator$it + 1 |
152 | ! |
render_draggable_element( |
153 | ! |
value = element, |
154 | ! |
id = paste0(widget_id, "draggable", elements_iterator$it), |
155 | ! |
widget_id = widget_id |
156 |
) |
|
157 |
}), |
|
158 | ! |
class = c("form-control", "bucket"), |
159 | ! |
ondragover = "allowDrop(event)", |
160 | ! |
ondrop = "drop(event)", |
161 | ! |
`data-label` = name, |
162 | ! |
`data-widget` = widget_id |
163 |
) |
|
164 |
} |
1 |
#' @keywords internal |
|
2 |
#' @noRd |
|
3 |
verbatim_popup_deps <- function() { |
|
4 | 4x |
htmltools::htmlDependency( |
5 | 4x |
name = "teal-widgets-verbatim-popup", |
6 | 4x |
version = utils::packageVersion("teal.widgets"), |
7 | 4x |
package = "teal.widgets", |
8 | 4x |
src = "verbatim-popup", |
9 | 4x |
script = "verbatim-popup.js" |
10 |
) |
|
11 |
} |
|
12 | ||
13 |
#' A `shiny` module that pops up verbatim text. |
|
14 |
#' @name verbatim_popup |
|
15 |
#' @description `r lifecycle::badge("experimental")` |
|
16 |
#' This module consists of a button that once clicked pops up a |
|
17 |
#' modal window with verbatim-styled text. |
|
18 |
#' |
|
19 |
#' @param id (`character(1)`) the `shiny` id |
|
20 |
#' @param button_label (`character(1)`) the text printed on the button |
|
21 |
#' @param type (`character(1)`) specifying whether to use `[shiny::actionButton()]` or `[shiny::actionLink()]`. |
|
22 |
#' @param ... additional arguments to `[shiny::actionButton()]`(or `[shiny::actionLink()]`). |
|
23 |
#' |
|
24 |
#' @return the UI function returns a `shiny.tag.list` object |
|
25 |
#' @export |
|
26 |
#' |
|
27 |
#' @examples |
|
28 |
#' library(shiny) |
|
29 |
#' |
|
30 |
#' ui <- fluidPage(verbatim_popup_ui("my_id", button_label = "Open popup")) |
|
31 |
#' srv <- function(input, output) { |
|
32 |
#' verbatim_popup_srv( |
|
33 |
#' "my_id", |
|
34 |
#' "if (TRUE) { print('Popups are the best') }", |
|
35 |
#' title = "My custom title", |
|
36 |
#' style = TRUE |
|
37 |
#' ) |
|
38 |
#' } |
|
39 |
#' if (interactive()) shinyApp(ui, srv) |
|
40 |
#' |
|
41 |
verbatim_popup_ui <- function(id, button_label, type = c("button", "link"), ...) { |
|
42 | 5x |
checkmate::assert_string(id) |
43 | 5x |
checkmate::assert_string(button_label) |
44 | ||
45 | 5x |
ui_function <- switch(match.arg(type), |
46 | 5x |
"button" = shiny::actionButton, |
47 | 5x |
"link" = shiny::actionLink |
48 |
) |
|
49 | ||
50 | 4x |
ns <- shiny::NS(id) |
51 | 4x |
ui_args <- list( |
52 | 4x |
inputId = ns("button"), |
53 | 4x |
label = button_label |
54 |
) |
|
55 | ||
56 | 4x |
shiny::tagList( |
57 | 4x |
verbatim_popup_deps(), |
58 | 4x |
shinyjs::useShinyjs(), |
59 | 4x |
do.call(ui_function, c(ui_args, list(...))) |
60 |
) |
|
61 |
} |
|
62 | ||
63 |
#' @name verbatim_popup |
|
64 |
#' @export |
|
65 |
#' |
|
66 |
#' @param verbatim_content (`character`, `expression`, `condition` or `reactive(1)` |
|
67 |
#' holding any of the above) the content to show in the popup modal window |
|
68 |
#' @param title (`character(1)`) the title of the modal window |
|
69 |
#' @param style (`logical(1)`) whether to style the `verbatim_content` using `styler::style_text`. |
|
70 |
#' If `verbatim_content` is a `condition` or `reactive` holding `condition` then this argument is ignored |
|
71 |
#' @param disabled (`reactive(1)`) the `shiny` reactive value holding a `logical`. The popup button is disabled |
|
72 |
#' when the flag is `TRUE` and enabled otherwise. |
|
73 |
#' |
|
74 |
verbatim_popup_srv <- function(id, verbatim_content, title, style = FALSE, disabled = shiny::reactiveVal(FALSE)) { |
|
75 | ! |
checkmate::assert_string(id) |
76 | ! |
checkmate::assert_string(title) |
77 | ! |
checkmate::assert_flag(style) |
78 | ! |
checkmate::assert_class(disabled, classes = "reactive") |
79 | ! |
moduleServer(id, function(input, output, session) { |
80 | ! |
ns <- session$ns |
81 | ! |
modal_content <- format_content(verbatim_content, style) |
82 | ! |
button_click_observer( |
83 | ! |
click_event = shiny::reactive(input$button), |
84 | ! |
copy_button_id = ns("copy_button"), |
85 | ! |
copied_area_id = ns("verbatim_content"), |
86 | ! |
modal_title = title, |
87 | ! |
modal_content = modal_content, |
88 | ! |
disabled = disabled |
89 |
) |
|
90 |
}) |
|
91 |
} |
|
92 | ||
93 |
#' Creates a `shiny` observer handling button clicks. |
|
94 |
#' |
|
95 |
#' @description |
|
96 |
#' When the button is clicked it pop up a modal window with the text. |
|
97 |
#' |
|
98 |
#' @keywords internal |
|
99 |
#' @param click_event `reactive` the click event |
|
100 |
#' @param copy_button_id (`character(1)`) the id of the button to copy the modal content. |
|
101 |
#' Automatically appended with a 1 and 2 suffix for top and bottom buttons respectively. |
|
102 |
#' @param copied_area_id (`character(1)`) the id of the element which contents are copied |
|
103 |
#' @param modal_title (`character(1)`) the title of the modal window |
|
104 |
#' @param modal_content (`reactive`) the content of the modal window |
|
105 |
#' @param disabled (`reactive(1)`) the `shiny` reactive value holding a `logical`. The popup button is disabled |
|
106 |
#' when the flag is `TRUE` and enabled otherwise. |
|
107 |
button_click_observer <- function(click_event, |
|
108 |
copy_button_id, |
|
109 |
copied_area_id, |
|
110 |
modal_title, |
|
111 |
modal_content, |
|
112 |
disabled) { |
|
113 | 1x |
shiny::observeEvent( |
114 | 1x |
disabled(), |
115 | 1x |
handlerExpr = { |
116 | ! |
if (disabled()) { |
117 | ! |
shinyjs::disable("button") |
118 |
} else { |
|
119 | ! |
shinyjs::enable("button") |
120 |
} |
|
121 |
} |
|
122 |
) |
|
123 | ||
124 | 1x |
shiny::observeEvent( |
125 | 1x |
click_event(), |
126 | 1x |
handlerExpr = { |
127 | ! |
req(modal_content()) |
128 | ! |
shiny::showModal( |
129 | ! |
shiny::modalDialog( |
130 | ! |
shiny::tagList( |
131 | ! |
tags$div( |
132 | ! |
class = "mb-4", |
133 | ! |
style = "margin-bottom: 1rem;", |
134 | ! |
shiny::actionButton( |
135 | ! |
paste0(copy_button_id, 1), |
136 | ! |
"Copy to Clipboard", |
137 | ! |
onclick = paste0("copyToClipboard('", copied_area_id, "')") |
138 |
), |
|
139 | ! |
shiny::modalButton("Dismiss") |
140 |
), |
|
141 | ! |
tags$pre(id = copied_area_id, modal_content()), |
142 |
), |
|
143 | ! |
title = modal_title, |
144 | ! |
footer = shiny::tagList( |
145 | ! |
shiny::actionButton( |
146 | ! |
paste0(copy_button_id, 2), |
147 | ! |
"Copy to Clipboard", |
148 | ! |
onclick = paste0("copyToClipboard('", copied_area_id, "')") |
149 |
), |
|
150 | ! |
shiny::modalButton("Dismiss") |
151 |
), |
|
152 | ! |
size = "l", |
153 | ! |
easyClose = TRUE |
154 |
) |
|
155 |
) |
|
156 |
} |
|
157 |
) |
|
158 |
} |
|
159 | ||
160 |
#' Formats the content of the modal popup window. |
|
161 |
#' |
|
162 |
#' @details |
|
163 |
#' Formats the content: |
|
164 |
#' * concatenates if needed |
|
165 |
#' * styles if `style` is TRUE |
|
166 |
#' |
|
167 |
#' @keywords internal |
|
168 |
#' @inheritParams verbatim_popup |
|
169 |
#' @return `reactive` with the formatted content |
|
170 |
format_content <- function(verbatim_content, style = FALSE) { |
|
171 | 11x |
shiny::reactive({ |
172 | 4x |
content <- if (inherits(verbatim_content, "reactive")) { |
173 | 2x |
tryCatch( |
174 | 2x |
verbatim_content(), |
175 | 2x |
error = function(e) { |
176 | ! |
e |
177 |
} |
|
178 |
) |
|
179 |
} else { |
|
180 | 2x |
verbatim_content |
181 |
} |
|
182 | 4x |
shiny::validate(shiny::need( |
183 | 4x |
checkmate::test_multi_class(content, classes = c("expression", "character", "condition")), |
184 | 4x |
"verbatim_content should be an expression, character or condition" |
185 |
)) |
|
186 | ||
187 | 4x |
content <- paste(as.character(content), collapse = "\n") |
188 | ||
189 | 4x |
if (style && !checkmate::test_class(content, "condition")) { |
190 | 3x |
content <- paste(styler::style_text(content), collapse = "\n") |
191 |
} |
|
192 | 4x |
content |
193 |
}) |
|
194 |
} |
1 |
#' @keywords internal |
|
2 |
#' @noRd |
|
3 |
table_with_settings_deps <- function() { |
|
4 | 1x |
htmltools::htmlDependency( |
5 | 1x |
name = "teal-widgets-table-with-settings", |
6 | 1x |
version = utils::packageVersion("teal.widgets"), |
7 | 1x |
package = "teal.widgets", |
8 | 1x |
src = "table-with-settings", |
9 | 1x |
stylesheet = "table-with-settings.css" |
10 |
) |
|
11 |
} |
|
12 | ||
13 |
#' @name table_with_settings |
|
14 |
#' |
|
15 |
#' @title `table_with_settings` module |
|
16 |
#' |
|
17 |
#' @description `r lifecycle::badge("stable")`\cr |
|
18 |
#' Module designed to create a `shiny` table output based on `rtable` object (`ElementaryTable` or `TableTree`) input. |
|
19 |
#' @inheritParams shiny::moduleServer |
|
20 |
#' @param ... (`character`)\cr |
|
21 |
#' Useful for providing additional HTML classes for the output tag. |
|
22 |
#' |
|
23 |
#' @rdname table_with_settings |
|
24 |
#' @export |
|
25 |
#' |
|
26 |
table_with_settings_ui <- function(id, ...) { |
|
27 | 1x |
checkmate::assert_string(id) |
28 | ||
29 | 1x |
ns <- NS(id) |
30 | ||
31 | 1x |
tagList( |
32 | 1x |
table_with_settings_deps(), |
33 | 1x |
tags$div( |
34 | 1x |
id = ns("table-with-settings"), |
35 | 1x |
tags$div( |
36 | 1x |
class = "table-settings-buttons", |
37 | 1x |
type_download_ui_table(ns("downbutton")), |
38 | 1x |
actionButton( |
39 | 1x |
inputId = ns("expand"), label = character(0), |
40 | 1x |
icon = icon("up-right-and-down-left-from-center"), class = "btn-sm" |
41 |
), |
|
42 |
), |
|
43 | 1x |
tags$div( |
44 | 1x |
class = "table-settings-table", |
45 | 1x |
uiOutput(ns("table_out_main"), width = "100%", ...) |
46 |
) |
|
47 |
) |
|
48 |
) |
|
49 |
} |
|
50 | ||
51 |
#' @inheritParams shiny::moduleServer |
|
52 |
#' @param table_r (`reactive`)\cr |
|
53 |
#' reactive expression that yields an `rtable` object (`ElementaryTable` or `TableTree`) |
|
54 |
#' @param show_hide_signal (`reactive logical`) optional\cr |
|
55 |
#' mechanism to allow modules which call this module to show/hide the table_with_settings UI. |
|
56 |
#' |
|
57 |
#' @rdname table_with_settings |
|
58 |
#' |
|
59 |
#' @return A `shiny` module. |
|
60 |
#' |
|
61 |
#' @export |
|
62 |
#' |
|
63 |
#' @examples |
|
64 |
#' library(shiny) |
|
65 |
#' library(rtables) |
|
66 |
#' library(magrittr) |
|
67 |
#' |
|
68 |
#' ui <- fluidPage( |
|
69 |
#' table_with_settings_ui( |
|
70 |
#' id = "table_with_settings" |
|
71 |
#' ) |
|
72 |
#' ) |
|
73 |
#' |
|
74 |
#' server <- function(input, output, session) { |
|
75 |
#' table_r <- reactive({ |
|
76 |
#' l <- basic_table() %>% |
|
77 |
#' split_cols_by("ARM") %>% |
|
78 |
#' analyze(c("SEX", "AGE")) |
|
79 |
#' |
|
80 |
#' tbl <- build_table(l, DM) |
|
81 |
#' |
|
82 |
#' tbl |
|
83 |
#' }) |
|
84 |
#' |
|
85 |
#' table_with_settings_srv(id = "table_with_settings", table_r = table_r) |
|
86 |
#' } |
|
87 |
#' |
|
88 |
#' if (interactive()) { |
|
89 |
#' shinyApp(ui, server) |
|
90 |
#' } |
|
91 |
#' |
|
92 |
table_with_settings_srv <- function(id, table_r, show_hide_signal = reactive(TRUE)) { |
|
93 | 5x |
checkmate::assert_class(table_r, c("reactive", "function")) |
94 | 4x |
checkmate::assert_class(show_hide_signal, c("reactive", "function")) |
95 | ||
96 | 3x |
if (!requireNamespace("rtables", quietly = TRUE)) { |
97 | ! |
stop("package rtables is required, please install") |
98 |
} |
|
99 | ||
100 | 3x |
moduleServer(id, function(input, output, session) { |
101 | 3x |
ns <- session$ns |
102 |
# Turn on and off the UI |
|
103 | 3x |
observeEvent(show_hide_signal(), { |
104 | 3x |
if (show_hide_signal()) { |
105 | 2x |
shinyjs::show("table-with-settings") |
106 |
} else { |
|
107 | 1x |
shinyjs::hide("table-with-settings") |
108 |
} |
|
109 |
}) |
|
110 | ||
111 | 3x |
output$table_out_main <- output$table_out_modal <- renderUI({ |
112 | 6x |
rtables::as_html(table_r()) |
113 |
}) |
|
114 | ||
115 | 3x |
type_download_srv_table( |
116 | 3x |
id = "downbutton", |
117 | 3x |
table_reactive = table_r |
118 |
) |
|
119 | ||
120 | 3x |
observeEvent(input$expand, { |
121 | 1x |
showModal( |
122 | 1x |
tags$div( |
123 | 1x |
class = "table-modal", |
124 | 1x |
modalDialog( |
125 | 1x |
easyClose = TRUE, |
126 | 1x |
tags$div( |
127 | 1x |
class = "float-right", |
128 | 1x |
type_download_ui_table(ns("modal_downbutton")) |
129 |
), |
|
130 | 1x |
uiOutput(ns("table_out_modal"), class = "table_out_container") |
131 |
) |
|
132 |
) |
|
133 |
) |
|
134 |
}) |
|
135 | ||
136 | 3x |
type_download_srv_table( |
137 | 3x |
id = "modal_downbutton", |
138 | 3x |
table_reactive = table_r |
139 |
) |
|
140 |
}) |
|
141 |
} |
|
142 | ||
143 |
type_download_ui_table <- function(id) { |
|
144 | 2x |
ns <- NS(id) |
145 | 2x |
shinyWidgets::dropdownButton( |
146 | 2x |
circle = FALSE, |
147 | 2x |
icon = icon("download"), |
148 | 2x |
inline = TRUE, |
149 | 2x |
right = TRUE, |
150 | 2x |
label = "", |
151 | 2x |
inputId = ns("dwnl"), |
152 | 2x |
tags$div( |
153 | 2x |
class = "modal-download-ui-table-container", |
154 | 2x |
radioButtons(ns("file_format"), |
155 | 2x |
label = "File type", |
156 | 2x |
choices = c("formatted txt" = ".txt", "csv" = ".csv", "pdf" = ".pdf"), |
157 |
), |
|
158 | 2x |
textInput(ns("file_name"), |
159 | 2x |
label = "File name (without extension)", |
160 | 2x |
value = paste0("table_", strftime(Sys.time(), format = "%Y%m%d_%H%M%S")) |
161 |
), |
|
162 | 2x |
conditionalPanel( |
163 | 2x |
condition = paste0("input['", ns("file_format"), "'] != '.csv'"), |
164 | 2x |
tags$div( |
165 | 2x |
class = "lock-btn", |
166 | 2x |
title = "on / off", |
167 | 2x |
shinyWidgets::prettyToggle( |
168 | 2x |
ns("pagination_switch"), |
169 | 2x |
value = FALSE, |
170 | 2x |
label_on = NULL, |
171 | 2x |
label_off = NULL, |
172 | 2x |
status_on = "default", |
173 | 2x |
status_off = "default", |
174 | 2x |
outline = FALSE, |
175 | 2x |
plain = TRUE, |
176 | 2x |
icon_on = icon("fas fa-toggle-off"), |
177 | 2x |
icon_off = icon("fas fa-toggle-on"), |
178 | 2x |
animation = "pulse" |
179 |
) |
|
180 |
), |
|
181 | 2x |
tags$div( |
182 | 2x |
class = "paginate-ui", |
183 | 2x |
shinyWidgets::numericInputIcon( |
184 | 2x |
inputId = ns("lpp"), |
185 | 2x |
label = "Paginate table:", |
186 | 2x |
value = 70, |
187 | 2x |
icon = list("lines / page") |
188 |
), |
|
189 | 2x |
uiOutput(ns("lpp_warning")) |
190 |
) |
|
191 |
), |
|
192 | 2x |
conditionalPanel( |
193 | 2x |
condition = paste0("input['", ns("file_name"), "'] != ''"), |
194 | 2x |
downloadButton(ns("data_download"), label = character(0), class = "btn-sm w-full") |
195 |
) |
|
196 |
) |
|
197 |
) |
|
198 |
} |
|
199 | ||
200 |
type_download_srv_table <- function(id, table_reactive) { |
|
201 | 12x |
moduleServer( |
202 | 12x |
id, |
203 | 12x |
function(input, output, session) { |
204 | 12x |
observeEvent(input$pagination_switch, { |
205 | 13x |
if (input$pagination_switch) { |
206 | 7x |
shinyjs::enable("lpp") |
207 |
} else { |
|
208 | 6x |
shinyjs::disable("lpp") |
209 |
} |
|
210 |
}) |
|
211 | ||
212 | 12x |
output$lpp_warning <- renderUI({ |
213 | 31x |
catch_warning <- if (input$file_format != ".csv" && input$pagination_switch) { |
214 | 9x |
try(rtables::paginate_table( |
215 | 9x |
tt = table_reactive(), |
216 | 9x |
lpp = as.numeric(input$lpp) |
217 | 9x |
), silent = TRUE) |
218 |
} |
|
219 | ||
220 | 21x |
if (inherits(catch_warning, "try-error")) { |
221 | 4x |
helpText( |
222 | 4x |
class = "error", |
223 | 4x |
icon("triangle-exclamation"), |
224 | 4x |
"Maximum lines per page includes the reprinted header. Please enter a numeric value or increase the value." |
225 |
) |
|
226 |
} |
|
227 |
}) |
|
228 | ||
229 | 12x |
output$data_download <- downloadHandler( |
230 | 12x |
filename = function() { |
231 | 22x |
paste0(input$file_name, input$file_format) |
232 |
}, |
|
233 | 12x |
content = function(file) { |
234 | 22x |
if (input$file_format == ".txt") { |
235 | 8x |
rtables::export_as_txt( |
236 | 8x |
x = table_reactive(), |
237 | 8x |
file = file, |
238 | 8x |
paginate = input$pagination_switch, |
239 | 8x |
lpp = if (input$pagination_switch) as.numeric(input$lpp) |
240 |
) |
|
241 | 14x |
} else if (input$file_format == ".csv") { |
242 | 7x |
result <- rtables::matrix_form(table_reactive())$strings |
243 | 7x |
utils::write.table( |
244 | 7x |
x = result, |
245 | 7x |
file = file, |
246 | 7x |
sep = ",", |
247 | 7x |
col.names = FALSE, |
248 | 7x |
row.names = TRUE, |
249 | 7x |
append = FALSE |
250 |
) |
|
251 |
} else { |
|
252 | 7x |
rtables::export_as_pdf( |
253 | 7x |
x = table_reactive(), |
254 | 7x |
file = file, |
255 | 7x |
paginate = input$pagination_switch, |
256 | 7x |
lpp = if (input$pagination_switch) as.numeric(input$lpp) |
257 |
) |
|
258 |
} |
|
259 |
} |
|
260 |
) |
|
261 |
} |
|
262 |
) |
|
263 |
} |
1 |
#' Standard UI layout |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("stable")`\cr |
|
4 |
#' Create a standard UI layout with output on the right and an encoding panel on |
|
5 |
#' the left. This is the layout used by the `teal` modules. |
|
6 |
#' |
|
7 |
#' @param output (`shiny.tag`)\cr |
|
8 |
#' object with the output element (table, plot, listing) such as for example returned |
|
9 |
#' by [shiny::plotOutput()]. |
|
10 |
#' @param encoding (`shiny.tag`)\cr |
|
11 |
#' object containing the encoding elements. If this element is `NULL` then no encoding side |
|
12 |
#' panel on the right is created. |
|
13 |
#' @param forms (`tagList`)\cr |
|
14 |
#' for example [shiny::actionButton()] that are placed below the encodings panel |
|
15 |
#' @param pre_output (`shiny.tag`) optional,\cr |
|
16 |
#' with text placed before the output to put the output into context. For example a title. |
|
17 |
#' @param post_output (`shiny.tag`) optional, with text placed after the output to put the output |
|
18 |
#' into context. For example the [shiny::helpText()] elements are useful. |
|
19 |
#' |
|
20 |
#' @return an object of class `shiny.tag` with the UI code. |
|
21 |
#' |
|
22 |
#' @examples |
|
23 |
#' library(shiny) |
|
24 |
#' standard_layout( |
|
25 |
#' output = white_small_well(tags$h3("Tests")), |
|
26 |
#' encoding = tags$div( |
|
27 |
#' tags$label("Encodings", class = "text-primary"), |
|
28 |
#' panel_item( |
|
29 |
#' "Tests", |
|
30 |
#' optionalSelectInput( |
|
31 |
#' "tests", |
|
32 |
#' "Tests:", |
|
33 |
#' choices = c( |
|
34 |
#' "Shapiro-Wilk", |
|
35 |
#' "Kolmogorov-Smirnov (one-sample)" |
|
36 |
#' ), |
|
37 |
#' selected = "Shapiro-Wilk" |
|
38 |
#' ) |
|
39 |
#' ) |
|
40 |
#' ), |
|
41 |
#' forms = tagList( |
|
42 |
#' verbatim_popup_ui("warning", "Show Warnings"), |
|
43 |
#' verbatim_popup_ui("rcode", "Show R code") |
|
44 |
#' ) |
|
45 |
#' ) |
|
46 |
#' |
|
47 |
#' @export |
|
48 |
standard_layout <- function(output, |
|
49 |
encoding = NULL, |
|
50 |
forms = NULL, |
|
51 |
pre_output = NULL, |
|
52 |
post_output = NULL) { |
|
53 |
# checking arguments |
|
54 | 11x |
checkmate::assert_multi_class(output, c("shiny.tag", "shiny.tag.list", "html")) |
55 | 9x |
checkmate::assert_multi_class(encoding, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE) |
56 | 8x |
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE) |
57 | 7x |
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE) |
58 | ||
59 |
# if encoding=NULL then forms is placed below output |
|
60 | ||
61 | 6x |
tag_output <- tags$div( |
62 | 6x |
class = "well", |
63 | 6x |
tags$div(class = "pre-output", pre_output), |
64 | 6x |
tags$div(class = "output", output), |
65 | 6x |
tags$div(class = "post-output", post_output) |
66 |
) |
|
67 | ||
68 | 6x |
tag_enc_out <- if (!is.null(encoding)) { |
69 | 2x |
tagList( |
70 | 2x |
tags$div( |
71 | 2x |
class = "col-md-3", |
72 | 2x |
tags$div(class = "well", encoding), |
73 | 2x |
if (is.null(forms)) { |
74 | 1x |
NULL |
75 |
} else { |
|
76 | 1x |
tags$div(class = "form-group", forms) |
77 |
} |
|
78 |
), |
|
79 | 2x |
tags$div(class = "col-md-9", tag_output) |
80 |
) |
|
81 |
} else { |
|
82 | 4x |
tags$div( |
83 | 4x |
class = "col-md-12", |
84 | 4x |
tag_output, |
85 | 4x |
if (is.null(forms)) { |
86 | 3x |
NULL |
87 |
} else { |
|
88 | 1x |
tags$div(class = "form-group", forms) |
89 |
} |
|
90 |
) |
|
91 |
} |
|
92 | ||
93 | 6x |
fluidRow(tag_enc_out) |
94 |
} |
1 |
#' Get bootstrap current version |
|
2 |
#' @note will work properly mainly inside a tag `.renderHook` |
|
3 |
#' @keywords internal |
|
4 |
get_bs_version <- function() { |
|
5 | 1x |
theme <- bslib::bs_current_theme() |
6 | 1x |
if (bslib::is_bs_theme(theme)) { |
7 | ! |
bslib::theme_version(theme) |
8 |
} else { |
|
9 | 1x |
"3" |
10 |
} |
|
11 |
} |
|
12 | ||
13 |
#' This function checks the plot type and applies specific modifications |
|
14 |
#' to the plot object based on the provided parameters. |
|
15 |
#' |
|
16 |
#' @param plot_obj The original plot object. |
|
17 |
#' @param plot_type The type of the plot, either `gg` (`ggplot2`) or `grob` (`grid`, `graphics`). |
|
18 |
#' @param dblclicking A logical value indicating whether double-clicking on data points on |
|
19 |
#' the main plot is enabled or disabled. |
|
20 |
#' @param ranges A list containing x and y values of ranges. |
|
21 |
#' |
|
22 |
#' @keywords internal |
|
23 |
apply_plot_modifications <- function(plot_obj, plot_type, dblclicking, ranges) { |
|
24 | 13x |
if (plot_type == "gg" && dblclicking) { |
25 | 1x |
plot_obj + |
26 | 1x |
ggplot2::coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = FALSE) |
27 | 12x |
} else if (plot_type == "grob") { |
28 | 2x |
grid::grid.newpage() |
29 | 2x |
grid::grid.draw(plot_obj) |
30 |
} else { |
|
31 | 10x |
plot_obj |
32 |
} |
|
33 |
} |
|
34 | ||
35 |
#' This function opens a PDF graphics device using [grDevices::pdf()] to suppress |
|
36 |
#' the plot display in the IDE. The purpose of this function is to avoid opening graphic devices |
|
37 |
#' directly in the IDE. |
|
38 |
#' |
|
39 |
#' @param x lazy binding which generates the plot(s) |
|
40 |
#' |
|
41 |
#' @keywords internal |
|
42 |
plot_suppress <- function(x) { |
|
43 | 32x |
grDevices::pdf(nullfile()) |
44 | 32x |
on.exit(grDevices::dev.off()) |
45 | 32x |
force(x) |
46 |
} |
1 |
#' Panel group widget |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("experimental")`\cr |
|
4 |
#' Designed to group [`panel_item`] elements. Used to handle `shiny` inputs in the encoding panel. |
|
5 |
#' @param id optional, (`character`)\cr |
|
6 |
#' @param ... (`shiny.tag`)\cr |
|
7 |
#' panels created by [panel_group()] |
|
8 |
#' |
|
9 |
#' @return (`shiny.tag`) |
|
10 |
#' |
|
11 |
#' @examples |
|
12 |
#' |
|
13 |
#' library(shiny) |
|
14 |
#' panel_group( |
|
15 |
#' panel_item( |
|
16 |
#' title = "Display", |
|
17 |
#' collapsed = FALSE, |
|
18 |
#' checkboxGroupInput( |
|
19 |
#' "check", |
|
20 |
#' "Tables display", |
|
21 |
#' choices = LETTERS[1:3], |
|
22 |
#' selected = LETTERS[1] |
|
23 |
#' ), |
|
24 |
#' radioButtons( |
|
25 |
#' "radio", |
|
26 |
#' label = "Plot type", |
|
27 |
#' choices = letters[1:2], |
|
28 |
#' selected = letters[1] |
|
29 |
#' ) |
|
30 |
#' ), |
|
31 |
#' panel_item( |
|
32 |
#' title = "Pre-processing", |
|
33 |
#' radioButtons( |
|
34 |
#' "filtering", |
|
35 |
#' "What to filter", |
|
36 |
#' choices = LETTERS[1:4], |
|
37 |
#' selected = LETTERS[1] |
|
38 |
#' ), |
|
39 |
#' radioButtons( |
|
40 |
#' "na_action", |
|
41 |
#' "NA action", |
|
42 |
#' choices = letters[1:3], |
|
43 |
#' selected = letters[1] |
|
44 |
#' ) |
|
45 |
#' ) |
|
46 |
#' ) |
|
47 |
#' |
|
48 |
#' @export |
|
49 |
panel_group <- function(..., id = NULL) { |
|
50 | ! |
checkmate::assert_string(id, null.ok = TRUE) |
51 | ||
52 |
# panel-group |
|
53 |
# div |
|
54 | ||
55 | ! |
tags$div( |
56 | ! |
id = id, |
57 |
..., |
|
58 | ! |
.renderHook = function(res_tag) { |
59 | ! |
bs_version <- get_bs_version() |
60 | ! |
if (bs_version == "3") { |
61 | ! |
htmltools::tagAppendAttributes(res_tag, class = "panel-group") |
62 | ! |
} else if (bs_version %in% c("4", "5")) { |
63 | ! |
res_tag <- htmltools::tagAppendAttributes(res_tag, class = "my-4") |
64 | ! |
htmltools::tagQuery(res_tag)$ |
65 | ! |
find(".card")$ |
66 | ! |
removeClass("my-2")$ |
67 | ! |
allTags() |
68 |
} else { |
|
69 | ! |
stop("Bootstrap 3, 4, and 5 are supported.") |
70 |
} |
|
71 |
} |
|
72 |
) |
|
73 |
} |
|
74 | ||
75 |
#' @keywords internal |
|
76 |
#' @noRd |
|
77 |
panel_item_deps <- function() { |
|
78 | ! |
htmltools::htmlDependency( |
79 | ! |
name = "teal-widgets-panel-item", |
80 | ! |
version = utils::packageVersion("teal.widgets"), |
81 | ! |
package = "teal.widgets", |
82 | ! |
src = "panel-item", |
83 | ! |
script = "panel-item.js", |
84 | ! |
stylesheet = "panel-item.css" |
85 |
) |
|
86 |
} |
|
87 | ||
88 |
#' Panel item widget |
|
89 |
#' |
|
90 |
#' @description `r lifecycle::badge("experimental")`\cr |
|
91 |
#' Designed to be grouped using [`panel_group`] element. Used to handle `shiny` inputs in the encoding panel. |
|
92 |
#' @param title (`character`)\cr title of panel |
|
93 |
#' @param ... content of panel |
|
94 |
#' @param collapsed (`logical`) optional,\cr |
|
95 |
#' whether to initially collapse panel |
|
96 |
#' @param input_id (`character`) optional\cr |
|
97 |
#' name of the panel item element. If supplied, this will register a shiny input variable that |
|
98 |
#' indicates whether the panel item is open or collapsed and is accessed with `input$input_id`. |
|
99 |
#' |
|
100 |
#' @return (`shiny.tag`) |
|
101 |
#' |
|
102 |
#' @examples |
|
103 |
#' |
|
104 |
#' library(shiny) |
|
105 |
#' panel_item( |
|
106 |
#' title = "Display", |
|
107 |
#' collapsed = FALSE, |
|
108 |
#' checkboxGroupInput( |
|
109 |
#' "check", |
|
110 |
#' "Tables display", |
|
111 |
#' choices = LETTERS[1:3], |
|
112 |
#' selected = LETTERS[1] |
|
113 |
#' ), |
|
114 |
#' radioButtons( |
|
115 |
#' "radio", |
|
116 |
#' label = "Plot type", |
|
117 |
#' choices = letters[1:2], |
|
118 |
#' selected = letters[1] |
|
119 |
#' ) |
|
120 |
#' ) |
|
121 |
#' |
|
122 |
#' @export |
|
123 |
panel_item <- function(title, ..., collapsed = TRUE, input_id = NULL) { |
|
124 | ! |
stopifnot(checkmate::test_character(title, len = 1) || inherits(title, c("shiny.tag", "shiny.tag.list", "html"))) |
125 | ! |
checkmate::assert_flag(collapsed) |
126 | ! |
checkmate::assert_string(input_id, null.ok = TRUE) |
127 | ||
128 | ! |
div_id <- paste0(input_id, "_div") |
129 | ! |
panel_id <- paste0(input_id, "_panel_body_", sample(1:10000, 1)) |
130 | ||
131 | ||
132 | ! |
tags$div(.renderHook = function(res_tag) { |
133 | ! |
bs_version <- get_bs_version() |
134 | ||
135 |
# alter tag structure |
|
136 | ! |
if (bs_version == "3") { |
137 | ! |
res_tag$children <- list( |
138 | ! |
tags$div( |
139 | ! |
id = div_id, |
140 | ! |
class = "panel panel-default", |
141 | ! |
tags$div( |
142 | ! |
class = paste("panel-heading", ifelse(collapsed, "collapsed", "")), |
143 | ! |
`data-toggle` = "collapse", |
144 | ! |
href = paste0("#", panel_id), |
145 | ! |
`aria-expanded` = ifelse(collapsed, "false", "true"), |
146 | ! |
icon(ifelse(collapsed, "angle-right", "angle-down"), class = "dropdown-icon"), |
147 | ! |
tags$label( |
148 | ! |
class = "panel-title inline", |
149 | ! |
title, |
150 |
) |
|
151 |
), |
|
152 | ! |
tags$div( |
153 | ! |
class = paste("panel-collapse collapse", ifelse(collapsed, "", "in")), |
154 | ! |
id = panel_id, |
155 | ! |
tags$div( |
156 | ! |
class = "panel-body", |
157 |
... |
|
158 |
) |
|
159 |
) |
|
160 |
) |
|
161 |
) |
|
162 | ! |
} else if (bs_version %in% c("4", "5")) { |
163 | ! |
res_tag$children <- list( |
164 | ! |
tags$div( |
165 | ! |
class = "card my-2", |
166 | ! |
tags$div( |
167 | ! |
class = "card-header", |
168 | ! |
tags$div( |
169 | ! |
class = paste("card-heading", ifelse(collapsed, "collapsed", "")), |
170 |
# bs4 |
|
171 | ! |
`data-toggle` = "collapse", |
172 |
# bs5 |
|
173 | ! |
`data-bs-toggle` = "collapse", |
174 | ! |
href = paste0("#", panel_id), |
175 | ! |
`aria-expanded` = ifelse(collapsed, "false", "true"), |
176 | ! |
icon(ifelse(collapsed, "angle-right", "angle-down"), class = "dropdown-icon"), |
177 | ! |
tags$label( |
178 | ! |
class = "card-title inline", |
179 | ! |
title, |
180 |
) |
|
181 |
) |
|
182 |
), |
|
183 | ! |
tags$div( |
184 | ! |
id = panel_id, |
185 | ! |
class = paste("collapse", ifelse(collapsed, "", "show")), |
186 | ! |
tags$div( |
187 | ! |
class = "card-body", |
188 |
... |
|
189 |
) |
|
190 |
) |
|
191 |
) |
|
192 |
) |
|
193 |
} else { |
|
194 | ! |
stop("Bootstrap 3, 4, and 5 are supported.") |
195 |
} |
|
196 | ||
197 | ||
198 | ! |
tagList( |
199 | ! |
panel_item_deps(), |
200 | ! |
res_tag |
201 |
) |
|
202 |
}) |
|
203 |
} |
1 |
#' Nested Closeable Modal Popup |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("experimental")` |
|
4 |
#' Alternative to `shiny::modalDialog`. Create a nested modal popup that can be shown/hidden |
|
5 |
#' using `jQuery` and modal `id`, without disturbing the parent modal. |
|
6 |
#' |
|
7 |
#' @param id (`character(1)`) `shiny` module id for the component.\cr |
|
8 |
#' Note that this id can be used to show/hide this modal |
|
9 |
#' with the appended `jQuery` methods show/hide. |
|
10 |
#' @param ... (`shiny.tag`) `shiny` UI elements that will be displayed in the modal UI |
|
11 |
#' @param modal_args (`list`) optional list of arguments for the `shiny::modalDialog` function |
|
12 |
#' to customize the modal. Has `easyClose` set to `TRUE` as default |
|
13 |
#' |
|
14 |
#' @return (`shiny.tag`) returns `HTML` for `shiny` module UI which can be nested into a modal popup |
|
15 |
#' @export |
|
16 |
#' |
|
17 |
#' @examples |
|
18 |
#' library(shiny) |
|
19 |
#' library(shinyjs) |
|
20 |
#' |
|
21 |
#' ui <- fluidPage( |
|
22 |
#' useShinyjs(), |
|
23 |
#' actionButton("show_1", "$(\"#modal_1\").modal(\"show\")"), |
|
24 |
#' nested_closeable_modal( |
|
25 |
#' "modal_1", |
|
26 |
#' modal_args = list( |
|
27 |
#' size = "l", |
|
28 |
#' title = "First Modal", |
|
29 |
#' easyClose = TRUE, |
|
30 |
#' footer = NULL |
|
31 |
#' ), |
|
32 |
#' tags$div( |
|
33 |
#' "This modal can be closed by running", tags$code("$(\"#modal_1\").modal(\"hide\")"), |
|
34 |
#' "in the JS console!", |
|
35 |
#' tags$br(), |
|
36 |
#' "Note that the second modal is placed right within this modal", |
|
37 |
#' tags$br(), |
|
38 |
#' "Alternatively, calling the", tags$code("removeModal()"), |
|
39 |
#' "will remove all the active modal popups", |
|
40 |
#' tags$br(), tags$br(), |
|
41 |
#' actionButton("show_2", "$(\"#modal_2\").modal(\"show\")"), |
|
42 |
#' actionButton("hide_1", "$(\"#modal_1\").modal(\"hide\")"), |
|
43 |
#' nested_closeable_modal( |
|
44 |
#' id = "modal_2", |
|
45 |
#' modal_args = list( |
|
46 |
#' size = "m", |
|
47 |
#' title = "Second Modal", |
|
48 |
#' footer = NULL, |
|
49 |
#' easyClose = TRUE |
|
50 |
#' ), |
|
51 |
#' tags$div( |
|
52 |
#' "This modal can be closed by running", tags$code("$(\"#modal_1\").modal(\"hide\")"), |
|
53 |
#' "in the JS console!", |
|
54 |
#' "Note that removing the parent will remove the child. |
|
55 |
#' But, reopening will remember the open state of child", |
|
56 |
#' actionButton("hide_2", "$(\"#modal_2\").modal(\"hide\")"), |
|
57 |
#' actionButton("hide_all", "$(\"#modal_1\").modal(\"hide\")") |
|
58 |
#' ) |
|
59 |
#' ) |
|
60 |
#' ) |
|
61 |
#' ) |
|
62 |
#' ) |
|
63 |
#' |
|
64 |
#' server <- function(input, output) { |
|
65 |
#' observeEvent(input$show_1, { |
|
66 |
#' runjs("$(\"#modal_1\").modal(\"show\")") |
|
67 |
#' }) |
|
68 |
#' observeEvent(input$show_2, { |
|
69 |
#' runjs("$(\"#modal_2\").modal(\"show\")") |
|
70 |
#' }) |
|
71 |
#' observeEvent(c(input$hide_1, input$hide_all), { |
|
72 |
#' runjs("$(\"#modal_1\").modal(\"hide\")") |
|
73 |
#' }) |
|
74 |
#' observeEvent(input$hide_2, { |
|
75 |
#' runjs("$(\"#modal_2\").modal(\"hide\")") |
|
76 |
#' }) |
|
77 |
#' } |
|
78 |
#' |
|
79 |
#' if (interactive()) { |
|
80 |
#' shinyApp(ui, server) |
|
81 |
#' } |
|
82 |
nested_closeable_modal <- function(id, ..., modal_args = list(easyClose = TRUE)) { |
|
83 | ! |
checkmate::assert_string(id) |
84 | ! |
checkmate::assert_list(modal_args) |
85 | ! |
modal_args <- append(list(...), modal_args) |
86 | ! |
tagList( |
87 | ! |
htmltools::tagQuery(do.call(modalDialog, modal_args))$ |
88 | ! |
removeAttrs("id")$ |
89 | ! |
addAttrs(id = id, `aria-hidden` = "true", class = "custom-modal", `data-backdrop` = "false")$ |
90 | ! |
children("div")$ |
91 | ! |
children("div")$ |
92 | ! |
children("div")$ |
93 | ! |
siblings(".modal-footer")$ |
94 | ! |
find("button")$ |
95 | ! |
removeAttrs(c("data-dismiss", "data-bs-dismiss"))$ |
96 | ! |
addAttrs(onclick = paste0("$('#", id, "').modal('hide');"))$ |
97 | ! |
allTags() |
98 |
) |
|
99 |
} |
1 |
#' Builds a `basic_table_args` object |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("experimental")` |
|
4 |
#' This function has to be used to build an input for a `basic_table_args` argument. |
|
5 |
#' The `basic_table_args` argument should be a part of every module which contains any `rtables` object. |
|
6 |
#' Arguments are validated to match their `rtables` equivalents. |
|
7 |
#' |
|
8 |
#' For more details see the vignette: `vignette("custom-basic-table-arguments", package = "teal.widgets")`. |
|
9 |
#' |
|
10 |
#' @seealso |
|
11 |
#' * [resolve_basic_table_args()] to resolve multiple objects into one using pre-defined priorities. |
|
12 |
#' * [parse_basic_table_args()] to parse resolved list into list of calls. |
|
13 |
#' |
|
14 |
#' @param ... arguments compatible with [rtables::basic_table()]. |
|
15 |
#' |
|
16 |
#' @return (`basic_table_args`) object. |
|
17 |
#' @export |
|
18 |
#' @examples |
|
19 |
#' basic_table_args(subtitles = "SUBTITLE") |
|
20 |
basic_table_args <- function(...) { |
|
21 | 118x |
table_args_l <- list(...) |
22 | 118x |
checkmate::assert_character(names(table_args_l), unique = TRUE, null.ok = TRUE) |
23 | ||
24 | 118x |
basic_table_formals <- methods::formalArgs(rtables::basic_table) |
25 | 118x |
checkmate::assert_subset(names(table_args_l), choices = basic_table_formals, empty.ok = TRUE) |
26 | ||
27 | 114x |
structure(table_args_l, class = "basic_table_args") |
28 |
} |
|
29 | ||
30 |
#' Resolves and reduces multiple `basic_table_args` objects |
|
31 |
#' |
|
32 |
#' @description `r lifecycle::badge("experimental")` |
|
33 |
#' Resolving and reducing multiple `basic_table_args` objects. |
|
34 |
#' This function is intended to utilize user provided settings, defaults provided by the module creator and |
|
35 |
#' also `teal` option. See `Details`, below, to understand the logic. |
|
36 |
#' |
|
37 |
#' @seealso [parse_basic_table_args()] to parse resolved list into list of calls. |
|
38 |
#' |
|
39 |
#' @param user_table (`basic_table_args`)\cr |
|
40 |
#' end user setup for [rtables::basic_table()] of a specific table. |
|
41 |
#' Created with the [basic_table_args()] function. The `NULL` value is supported. |
|
42 |
#' @param user_default (`basic_table_args`)\cr |
|
43 |
#' end user default setup for [rtables::basic_table()] |
|
44 |
#' of a specific table. Created with the [basic_table_args()] function. The `NULL` value is supported. |
|
45 |
#' @param module_table (`ggplot2_args`)\cr |
|
46 |
#' module creator setup for [rtables::basic_table()] of a specific table. |
|
47 |
#' Created with the [basic_table_args()] function. The `NULL` value is supported. |
|
48 |
#' @param app_default (`basic_table_args`)\cr |
|
49 |
#' Application level setting. Can be `NULL`. |
|
50 |
#' |
|
51 |
#' @return `basic_table_args` object. |
|
52 |
#' @details |
|
53 |
#' The function picks the first non `NULL` value for each argument, checking in the following order: |
|
54 |
#' 1. `basic_table_args` argument provided by the end user. |
|
55 |
#' Per table (`user_table`) and then default (`user_default`) setup. |
|
56 |
#' 2. `app_default` global R variable, `teal.basic_table_args`. |
|
57 |
#' 3. `module_table` which is a module creator setup. |
|
58 |
#' @export |
|
59 |
#' @examples |
|
60 |
#' resolve_basic_table_args( |
|
61 |
#' user_table = basic_table_args(title = "TITLE"), |
|
62 |
#' user_default = basic_table_args(title = "DEFAULT_TITLE", subtitles = "SUBTITLE") |
|
63 |
#' ) |
|
64 |
resolve_basic_table_args <- function(user_table = basic_table_args(), |
|
65 |
user_default = basic_table_args(), |
|
66 |
module_table = basic_table_args(), |
|
67 |
app_default = getOption("teal.basic_table_args", basic_table_args())) { |
|
68 | 24x |
checkmate::assert_class(user_table, "basic_table_args", null.ok = TRUE) |
69 | 23x |
checkmate::assert_class(user_default, "basic_table_args", null.ok = TRUE) |
70 | 23x |
checkmate::assert_class(module_table, "basic_table_args", null.ok = TRUE) |
71 | 23x |
checkmate::assert_class(app_default, "basic_table_args", null.ok = TRUE) |
72 | ||
73 | 23x |
table_args_all <- list( |
74 | 23x |
"table" = user_table, |
75 | 23x |
"default" = user_default, |
76 | 23x |
"teal" = app_default, |
77 | 23x |
"module" = module_table |
78 |
) |
|
79 | ||
80 | 23x |
table_args_f <- Reduce(`c`, table_args_all) |
81 | ||
82 | 23x |
if (length(table_args_f) == 0) { |
83 | 9x |
basic_table_args() |
84 |
} else { |
|
85 | 14x |
do.call(basic_table_args, table_args_f[!duplicated(names(table_args_f))]) |
86 |
} |
|
87 |
} |
|
88 | ||
89 |
#' Parses `basic_table_args` object into the `basic_table` expression |
|
90 |
#' |
|
91 |
#' @description `r lifecycle::badge("experimental")` |
|
92 |
#' A function to parse expression from the `basic_table_args` object. |
|
93 |
#' @param basic_table_args (`basic_table_args`)\cr |
|
94 |
#' This argument could be a result of the [`resolve_basic_table_args()`]. |
|
95 |
#' |
|
96 |
#' @return (`language`) the `rtables::basic_table()` filled with additional arguments. |
|
97 |
#' @export |
|
98 |
#' @examples |
|
99 |
#' parse_basic_table_args( |
|
100 |
#' resolve_basic_table_args( |
|
101 |
#' user_table = basic_table_args(title = "TITLE"), |
|
102 |
#' user_default = basic_table_args(title = "DEFAULT_TITLE", subtitles = "SUBTITLE") |
|
103 |
#' ) |
|
104 |
#' ) |
|
105 |
parse_basic_table_args <- function(basic_table_args = teal.widgets::basic_table_args()) { |
|
106 | 8x |
checkmate::assert_class(basic_table_args, "basic_table_args", null.ok = TRUE) |
107 | ||
108 | 7x |
if (length(basic_table_args) == 0) { |
109 | 3x |
quote(rtables::basic_table()) |
110 |
} else { |
|
111 | 4x |
as.call(c(list(quote(rtables::basic_table)), basic_table_args)) |
112 |
} |
|
113 |
} |
1 |
#' Small well class for HTML |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("stable")`\cr |
|
4 |
#' Adds Small Well class and overflow-x property to HTML output element. |
|
5 |
#' @param ... other arguments to pass to tag object's div attributes. |
|
6 |
#' |
|
7 |
#' @details `white_small_well` is intended to be used with [shiny::uiOutput()]. |
|
8 |
#' The overflow-x property is set to auto so that a scroll bar is added |
|
9 |
#' when the content overflows at the left and right edges of the output window. |
|
10 |
#' For example, this is useful for displaying wide tables. |
|
11 |
#' |
|
12 |
#' @return An HTML output element with class Small Well and overflow-x property |
|
13 |
#' @export |
|
14 |
#' |
|
15 |
#' @examples |
|
16 |
#' |
|
17 |
#' white_small_well(shiny::htmlOutput("summary")) |
|
18 |
white_small_well <- function(...) { |
|
19 | ! |
shiny::tagList( |
20 | ! |
tags$div( |
21 | ! |
class = "well well-sm", |
22 | ! |
style = "background-color: white;", |
23 |
... |
|
24 |
) |
|
25 |
) |
|
26 |
} |
1 |
#' Map `lenghtMenu` property |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("stable")`\cr |
|
4 |
#' Maps the `lengthMenu` selected value property of `DT::datatable` to a `shiny` variable. |
|
5 |
#' @param dt_name `ns()` of `inputId` of the `DT::datatable` |
|
6 |
#' @param dt_rows `ns()` of `inputId` of the variable that holds the current selected value of `lengthMenu` |
|
7 |
#' |
|
8 |
#' @name get_dt_rows |
|
9 |
#' |
|
10 |
#' @return (`shiny::tagList`) A `shiny tagList`. |
|
11 |
#' |
|
12 |
#' @examplesIf require("DT") |
|
13 |
#' library(shiny) |
|
14 |
#' library(DT) |
|
15 |
#' |
|
16 |
#' ui <- function(id) { |
|
17 |
#' ns <- NS(id) |
|
18 |
#' tagList( |
|
19 |
#' DTOutput(ns("data_table")), |
|
20 |
#' get_dt_rows(ns("data_table"), ns("dt_rows")) |
|
21 |
#' ) |
|
22 |
#' } |
|
23 |
#' |
|
24 |
#' # use the input$dt_rows in the Shiny Server function |
|
25 |
#' server <- function(id) { |
|
26 |
#' moduleServer(id, function(input, output, session) { |
|
27 |
#' output$data_table <- renderDataTable( |
|
28 |
#' { |
|
29 |
#' iris |
|
30 |
#' }, |
|
31 |
#' options = list(pageLength = input$dt_rows) |
|
32 |
#' ) |
|
33 |
#' }) |
|
34 |
#' } |
|
35 |
#' |
|
36 |
#' if (interactive()) { |
|
37 |
#' shinyApp( |
|
38 |
#' ui = ui("my_table_module"), |
|
39 |
#' server = function(input, output, session) server("my_table_module") |
|
40 |
#' ) |
|
41 |
#' } |
|
42 |
#' @export |
|
43 |
get_dt_rows <- function(dt_name, dt_rows) { |
|
44 | ! |
tags$head( |
45 | ! |
tags$script( |
46 | ! |
sprintf( |
47 | ! |
"$(document).ready(function() { |
48 | ! |
$('%s').on('length.dt', function(e, settings, len) { |
49 | ! |
Shiny.onInputChange('%s', len); |
50 |
}); |
|
51 |
});", |
|
52 | ! |
paste0("#", dt_name), |
53 | ! |
dt_rows |
54 |
) |
|
55 |
) |
|
56 |
) |
|
57 |
} |