1 |
#' Panel group widget |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("experimental")` |
|
4 |
#' @param id optional, (`character`)\cr |
|
5 |
#' @param ... (`shiny.tag`)\cr |
|
6 |
#' panels created by [panel_group()] |
|
7 |
#' |
|
8 |
#' @return (`shiny.tag`) |
|
9 |
#' |
|
10 |
#' @export |
|
11 |
panel_group <- function(..., id = NULL) { |
|
12 | ! |
checkmate::assert_string(id, null.ok = TRUE) |
13 | ||
14 |
# panel-group |
|
15 |
# div |
|
16 | ||
17 | ! |
tags$div( |
18 | ! |
id = id, |
19 |
..., |
|
20 | ! |
.renderHook = function(res_tag) { |
21 | ! |
bs_version <- get_bs_version() |
22 | ! |
if (bs_version == "3") { |
23 | ! |
htmltools::tagAppendAttributes(res_tag, class = "panel-group") |
24 | ! |
} else if (bs_version %in% c("4", "5")) { |
25 | ! |
res_tag <- htmltools::tagAppendAttributes(res_tag, class = "my-4") |
26 | ! |
htmltools::tagQuery(res_tag)$ |
27 | ! |
find(".card")$ |
28 | ! |
removeClass("my-2")$ |
29 | ! |
allTags() |
30 |
} else { |
|
31 | ! |
stop("Bootstrap 3, 4, and 5 are supported.") |
32 |
} |
|
33 |
} |
|
34 |
) |
|
35 |
} |
|
36 | ||
37 |
#' Panel widget |
|
38 |
#' @md |
|
39 |
#' |
|
40 |
#' @description `r lifecycle::badge("experimental")` |
|
41 |
#' @param title (`character`)\cr title of panel |
|
42 |
#' @param ... content of panel |
|
43 |
#' @param collapsed (`logical`, optional)\cr |
|
44 |
#' whether to initially collapse panel |
|
45 |
#' @param input_id (`character`, optional)\cr |
|
46 |
#' name of the panel item element. If supplied, this will register a shiny input variable that |
|
47 |
#' indicates whether the panel item is open or collapsed and is accessed with `input$input_id`. |
|
48 |
#' |
|
49 |
#' @return (`shiny.tag`) |
|
50 |
#' |
|
51 |
#' @export |
|
52 |
panel_item <- function(title, ..., collapsed = TRUE, input_id = NULL) { |
|
53 | ! |
stopifnot(checkmate::test_character(title, len = 1) || inherits(title, c("shiny.tag", "shiny.tag.list", "html"))) |
54 | ! |
checkmate::assert_flag(collapsed) |
55 | ! |
checkmate::assert_string(input_id, null.ok = TRUE) |
56 | ||
57 | ! |
div_id <- paste0(input_id, "_div") |
58 | ! |
panel_id <- paste0(input_id, "_panel_body_", sample(1:10000, 1)) |
59 | ||
60 | ||
61 | ! |
tags$div(.renderHook = function(res_tag) { |
62 | ! |
bs_version <- get_bs_version() |
63 | ||
64 |
# alter tag structure |
|
65 | ! |
if (bs_version == "3") { |
66 | ! |
res_tag$children <- list( |
67 | ! |
tags$div( |
68 | ! |
id = div_id, |
69 | ! |
class = "panel panel-default", |
70 | ! |
tags$div( |
71 | ! |
class = paste("panel-heading", ifelse(collapsed, "collapsed", "")), |
72 | ! |
`data-toggle` = "collapse", |
73 | ! |
href = paste0("#", panel_id), |
74 | ! |
`aria-expanded` = ifelse(collapsed, "false", "true"), |
75 | ! |
icon("angle-down", class = "dropdown-icon"), |
76 | ! |
tags$label( |
77 | ! |
class = "panel-title inline", |
78 | ! |
title, |
79 |
) |
|
80 |
), |
|
81 | ! |
tags$div( |
82 | ! |
class = paste("panel-collapse collapse", ifelse(collapsed, "", "in")), |
83 | ! |
id = panel_id, |
84 | ! |
tags$div( |
85 | ! |
class = "panel-body", |
86 |
... |
|
87 |
) |
|
88 |
) |
|
89 |
) |
|
90 |
) |
|
91 | ! |
} else if (bs_version %in% c("4", "5")) { |
92 | ! |
res_tag$children <- list( |
93 | ! |
tags$div( |
94 | ! |
class = "card my-2", |
95 | ! |
tags$div( |
96 | ! |
class = "card-header", |
97 | ! |
tags$div( |
98 | ! |
class = ifelse(collapsed, "collapsed", ""), |
99 |
# bs4 |
|
100 | ! |
`data-toggle` = "collapse", |
101 |
# bs5 |
|
102 | ! |
`data-bs-toggle` = "collapse", |
103 | ! |
href = paste0("#", panel_id), |
104 | ! |
`aria-expanded` = ifelse(collapsed, "false", "true"), |
105 | ! |
icon("angle-down", class = "dropdown-icon"), |
106 | ! |
tags$label( |
107 | ! |
class = "card-title inline", |
108 | ! |
title, |
109 |
) |
|
110 |
) |
|
111 |
), |
|
112 | ! |
tags$div( |
113 | ! |
id = panel_id, |
114 | ! |
class = paste("collapse", ifelse(collapsed, "", "show")), |
115 | ! |
tags$div( |
116 | ! |
class = "card-body", |
117 |
... |
|
118 |
) |
|
119 |
) |
|
120 |
) |
|
121 |
) |
|
122 |
} else { |
|
123 | ! |
stop("Bootstrap 3, 4, and 5 are supported.") |
124 |
} |
|
125 | ||
126 | ! |
tagList( |
127 | ! |
include_css_files(pattern = "panel.css"), |
128 | ! |
res_tag |
129 |
) |
|
130 |
}) |
|
131 |
} |
1 |
#' @title Draggable Buckets |
|
2 |
#' @description `r lifecycle::badge("experimental")` |
|
3 |
#' A custom widget with draggable elements that can be put into buckets. |
|
4 |
#' |
|
5 |
#' @param input_id (`character(1)`) the `HTML` id of this widget |
|
6 |
#' @param label (`character(1)` or `shiny.tag`) the header of this widget |
|
7 |
#' @param elements (`character`) the elements to drag into buckets |
|
8 |
#' @param buckets (`character` or `list`) the names of the buckets the elements can be put in or a list of key-pair |
|
9 |
#' values where key is a name of a bucket and value is a character vector of elements in a bucket |
|
10 |
#' |
|
11 |
#' @return the `HTML` code comprising an instance of this widget |
|
12 |
#' @export |
|
13 |
#' |
|
14 |
#' @details `shinyvalidate` validation can be used with this widget. See example below. |
|
15 |
#' |
|
16 |
#' @examples |
|
17 |
#' |
|
18 |
#' ui <- shiny::fluidPage( |
|
19 |
#' draggable_buckets("id", "Choices #1", c("a", "b"), c("bucket1", "bucket2")), |
|
20 |
#' draggable_buckets("id2", "Choices #2", letters, c("vowels", "consonants")), |
|
21 |
#' shiny::verbatimTextOutput("out"), |
|
22 |
#' shiny::verbatimTextOutput("out2") |
|
23 |
#' ) |
|
24 |
#' server <- function(input, output) { |
|
25 |
#' iv <- shinyvalidate::InputValidator$new() |
|
26 |
#' iv$add_rule( |
|
27 |
#' "id", |
|
28 |
#' function(data) if (length(data[["bucket1"]]) == 0) "There should be stuff in bucket 1" |
|
29 |
#' ) |
|
30 |
#' iv$enable() |
|
31 |
#' |
|
32 |
#' shiny::observeEvent(list(input$id, input$id2), { |
|
33 |
#' print(isolate(input$id)) |
|
34 |
#' print(isolate(input$id2)) |
|
35 |
#' }) |
|
36 |
#' output$out <- shiny::renderPrint({ |
|
37 |
#' iv$is_valid() |
|
38 |
#' input$id |
|
39 |
#' }) |
|
40 |
#' output$out2 <- shiny::renderPrint(input$id2) |
|
41 |
#' } |
|
42 |
#' if (interactive()) shiny::shinyApp(ui, server) |
|
43 |
#' |
|
44 |
#' # With default elements in the bucket |
|
45 |
#' ui <- shiny::fluidPage( |
|
46 |
#' draggable_buckets("id", "Choices #1", c("a", "b"), list(bucket1 = character(), bucket2 = c("c"))), |
|
47 |
#' shiny::verbatimTextOutput("out") |
|
48 |
#' ) |
|
49 |
#' server <- function(input, output) { |
|
50 |
#' shiny::observeEvent(input$id, { |
|
51 |
#' print(shiny::isolate(input$id)) |
|
52 |
#' }) |
|
53 |
#' output$out <- shiny::renderPrint(input$id) |
|
54 |
#' } |
|
55 |
#' if (interactive()) shiny::shinyApp(ui, server) |
|
56 |
draggable_buckets <- function(input_id, label, elements = character(), buckets) { |
|
57 | ! |
checkmate::assert_string(input_id) |
58 | ! |
checkmate::assert_true(inherits(label, "character") || inherits(label, "shiny.tag")) |
59 | ! |
checkmate::assert_character(c(elements, unlist(buckets)), min.len = 0, null.ok = TRUE, unique = TRUE) |
60 | ! |
checkmate::assert( |
61 | ! |
checkmate::check_character(buckets, min.len = 1), |
62 | ! |
checkmate::check_list(buckets, types = "character", names = "unique") |
63 |
) |
|
64 | ||
65 | ! |
elements_iterator <- new.env(parent = emptyenv()) |
66 | ! |
elements_iterator$it <- 0 |
67 | ||
68 | ! |
shiny::tagList( |
69 | ! |
shiny::singleton(shiny::tags$head( |
70 | ! |
shiny::includeScript(system.file("widgets/draggable_buckets.js", package = "teal.widgets")) |
71 |
)), |
|
72 | ! |
include_css_files("draggable_buckets.css"), |
73 | ! |
shiny::div( |
74 | ! |
shiny::tags$span(label), |
75 | ! |
render_unbucketed_elements(elements = elements, elements_iterator = elements_iterator, widget_id = input_id), |
76 | ! |
render_buckets(buckets = buckets, elements_iterator = elements_iterator, widget_id = input_id), |
77 | ! |
class = "draggableBuckets", |
78 | ! |
id = input_id |
79 |
) |
|
80 |
) |
|
81 |
} |
|
82 | ||
83 |
render_unbucketed_elements <- function(elements, elements_iterator, widget_id) { |
|
84 | ! |
shiny::tags$div( |
85 | ! |
lapply(elements, function(element) { |
86 | ! |
elements_iterator$it <- elements_iterator$it + 1 |
87 | ! |
render_draggable_element( |
88 | ! |
value = element, |
89 | ! |
id = paste0(widget_id, "draggable", elements_iterator$it), |
90 | ! |
widget_id = widget_id |
91 |
) |
|
92 |
}), |
|
93 | ! |
id = paste0(widget_id, "elements"), |
94 | ! |
class = c("form-control", "elements"), |
95 | ! |
ondragover = "allowDrop(event)", |
96 | ! |
ondrop = "drop(event)", |
97 | ! |
`data-widget` = widget_id |
98 |
) |
|
99 |
} |
|
100 | ||
101 |
render_buckets <- function(buckets, elements_iterator, widget_id) { |
|
102 | ! |
buckets <- `if`( |
103 | ! |
is.list(buckets), |
104 | ! |
lapply(names(buckets), function(bucket_name) { |
105 | ! |
render_bucket( |
106 | ! |
name = bucket_name, |
107 | ! |
elements = buckets[[bucket_name]], |
108 | ! |
elements_iterator = elements_iterator, |
109 | ! |
widget_id = widget_id |
110 |
) |
|
111 |
}), |
|
112 | ! |
lapply(buckets, render_bucket, widget_id = widget_id, elements_iterator = elements_iterator) |
113 |
) |
|
114 | ! |
shiny::tagList(buckets) |
115 |
} |
|
116 | ||
117 |
render_draggable_element <- function(value, id, widget_id) { |
|
118 | ! |
shiny::tags$div( |
119 | ! |
value, |
120 | ! |
id = id, |
121 | ! |
class = "element", |
122 | ! |
draggable = "true", |
123 | ! |
ondragstart = "drag(event)", |
124 | ! |
ondragover = "allowDrop(event)", |
125 | ! |
ondrop = "dropReorder(event)", |
126 | ! |
`data-widget` = widget_id |
127 |
) |
|
128 |
} |
|
129 | ||
130 |
render_bucket <- function(name, elements = NULL, elements_iterator = NULL, widget_id = NULL) { |
|
131 | ! |
shiny::tags$div( |
132 | ! |
shiny::tags$div( |
133 | ! |
paste0(name, ":"), |
134 | ! |
class = "bucket-name", |
135 | ! |
ondragover = "allowDrop(event)", |
136 | ! |
ondrop = "dropBucketName(event)", |
137 | ! |
`data-widget` = widget_id |
138 |
), |
|
139 | ! |
lapply(elements, function(element) { |
140 | ! |
elements_iterator$it <- elements_iterator$it + 1 |
141 | ! |
render_draggable_element( |
142 | ! |
value = element, |
143 | ! |
id = paste0(widget_id, "draggable", elements_iterator$it), |
144 | ! |
widget_id = widget_id |
145 |
) |
|
146 |
}), |
|
147 | ! |
class = c("form-control", "bucket"), |
148 | ! |
ondragover = "allowDrop(event)", |
149 | ! |
ondrop = "drop(event)", |
150 | ! |
`data-label` = name, |
151 | ! |
`data-widget` = widget_id |
152 |
) |
|
153 |
} |
1 |
#' Hide, Show Label only or display a `pickerInput` |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("stable")` |
|
4 |
#' Hidden input widgets are useful to have the `input[[inputId]]` variable |
|
5 |
#' on available in the server function but no corresponding visual clutter from |
|
6 |
#' input widgets that provide only a single choice. |
|
7 |
#' |
|
8 |
#' @inheritParams shinyWidgets::pickerInput |
|
9 |
#' @param choices (`character`, `NULL`)\cr |
|
10 |
#' If `choices` is `NULL` no `pickerInput` widget is displayed and `input[[inputId]]` |
|
11 |
#' will be `""`. If `choices` is of length 1 then a label and character string will be |
|
12 |
#' displayed and the `pickerInput` widget will be hidden. If the length of `choices` |
|
13 |
#' is more than one the `pickerInput` element will be displayed. |
|
14 |
#' If elements of the list are named then that name rather than the value |
|
15 |
#' is displayed to the user. |
|
16 |
#' |
|
17 |
#' @param sep (`character(1)`)\cr |
|
18 |
#' A separator string to split the `choices` or `selected` inputs into the values of the different |
|
19 |
#' columns. |
|
20 |
#' |
|
21 |
#' @param label_help (`shiny.tag` optional)\cr |
|
22 |
#' e.g. an object returned by [shiny::helpText()]. |
|
23 |
#' |
|
24 |
#' @param fixed (`logical(1)` optional)\cr |
|
25 |
#' whether to block user to select choices. |
|
26 |
#' |
|
27 |
#' @param width (`character(1)`)\cr |
|
28 |
#' The width of the input passed to `pickerInput` e.g. `'auto'`, `'fit'`, `'100px'` or `'75%'` |
|
29 |
#' |
|
30 |
#' @export |
|
31 |
#' |
|
32 |
#' @examples |
|
33 |
#' \dontrun{ |
|
34 |
#' optionalSelectInput(inputId = "xvar", label = "x variable", choices = "A", selected = "A") |
|
35 |
#' optionalSelectInput( |
|
36 |
#' inputId = "xvar", |
|
37 |
#' label = "x variable", |
|
38 |
#' choices = LETTERS[1:5], |
|
39 |
#' selected = "A" |
|
40 |
#' ) |
|
41 |
#' optionalSelectInput( |
|
42 |
#' inputId = "xvar", |
|
43 |
#' label = "x variable", |
|
44 |
#' choices = c("A - value A" = "A"), |
|
45 |
#' selected = "A" |
|
46 |
#' ) |
|
47 |
#' |
|
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 |
#' optionalSelectInput( |
|
59 |
#' inputId = "xvar", |
|
60 |
#' label = "x variable", |
|
61 |
#' choices = teal.transform::variable_choices(data = data, subset = c("AGE", "SEX", "PARAMCD")), |
|
62 |
#' selected = "PARAMCD" |
|
63 |
#' ) |
|
64 |
#' |
|
65 |
#' selected_value <- paste0(lapply(data[1, c("PARAMCD", "AVISIT")], as.character), collapse = " - ") |
|
66 |
#' optionalSelectInput( |
|
67 |
#' inputId = "xvar", |
|
68 |
#' label = "x variable", |
|
69 |
#' choices = teal.transform::value_choices( |
|
70 |
#' data = data, |
|
71 |
#' var_choices = c("PARAMCD", "AVISIT"), |
|
72 |
#' var_label = c("PARAM", "AVISIT") |
|
73 |
#' ), |
|
74 |
#' selected = selected_value |
|
75 |
#' ) |
|
76 |
#' } |
|
77 |
optionalSelectInput <- function(inputId, # nolint |
|
78 |
label = NULL, |
|
79 |
choices = NULL, |
|
80 |
selected = NULL, |
|
81 |
multiple = FALSE, |
|
82 |
sep = NULL, |
|
83 |
options = list(), |
|
84 |
label_help = NULL, |
|
85 |
fixed = FALSE, |
|
86 |
width = NULL) { |
|
87 | ! |
checkmate::assert_string(inputId) |
88 | ||
89 | ! |
checkmate::assert( |
90 | ! |
checkmate::check_string(label, null.ok = TRUE), |
91 | ! |
checkmate::check_class(label, "shiny.tag"), |
92 | ! |
checkmate::check_class(label, "shiny.tag.list"), |
93 | ! |
checkmate::check_class(label, "html") |
94 |
) |
|
95 | ! |
stopifnot(is.null(choices) || length(choices) >= 1) |
96 | ! |
stopifnot( |
97 | ! |
is.null(selected) || |
98 | ! |
length(selected) == 0 || |
99 | ! |
all(selected %in% choices) || |
100 | ! |
all(selected %in% unlist(choices, recursive = FALSE)) |
101 |
) |
|
102 | ! |
checkmate::assert_flag(multiple) |
103 | ! |
checkmate::assert_string(sep, null.ok = TRUE) |
104 | ! |
stopifnot(is.list(options)) |
105 | ! |
checkmate::assert( |
106 | ! |
checkmate::check_string(label_help, null.ok = TRUE), |
107 | ! |
checkmate::check_class(label_help, "shiny.tag"), |
108 | ! |
checkmate::check_class(label_help, "shiny.tag.list"), |
109 | ! |
checkmate::check_class(label_help, "html") |
110 |
) |
|
111 | ! |
checkmate::assert_flag(fixed) |
112 | ||
113 | ! |
if (!is.null(width)) { |
114 | ! |
validateCssUnit(width) |
115 |
} |
|
116 | ||
117 | ! |
default_options <- list( |
118 | ! |
"actions-box" = multiple, |
119 | ! |
"none-selected-text" = "- Nothing selected -", |
120 | ! |
"max-options" = ifelse(multiple, Inf, 1), |
121 | ! |
"show-subtext" = TRUE, |
122 | ! |
"live-search" = ifelse(length(choices) > 10, TRUE, FALSE) |
123 |
) |
|
124 | ||
125 |
# if called outside the fluidPage then will assume bs 3 |
|
126 | ! |
bs_version <- get_bs_version() |
127 | ! |
if (isTRUE(bs_version != "3")) default_options[["style"]] <- "btn-outline-secondary" |
128 | ||
129 | ! |
options <- if (!identical(options, list())) { |
130 | ! |
c(options, default_options[setdiff(names(default_options), names(options))]) |
131 |
} else { |
|
132 | ! |
default_options |
133 |
} |
|
134 | ||
135 | ! |
if (is.null(choices)) { |
136 | ! |
choices <- "" |
137 | ! |
selected <- NULL |
138 |
} |
|
139 | ||
140 | ! |
raw_choices <- extract_raw_choices(choices, attr(choices, "sep")) |
141 | ! |
raw_selected <- extract_raw_choices(selected, attr(choices, "sep")) |
142 | ||
143 | ! |
ui <- shinyWidgets::pickerInput( |
144 | ! |
inputId = inputId, |
145 | ! |
label = label, |
146 | ! |
choices = raw_choices, |
147 | ! |
selected = raw_selected, |
148 | ! |
multiple = TRUE, |
149 | ! |
width = width, |
150 | ! |
options = options, |
151 | ! |
choicesOpt = picker_options(choices) |
152 |
) |
|
153 | ||
154 | ! |
if (!is.null(label_help)) { |
155 | ! |
ui[[3]] <- append(ui[[3]], list(div(class = "label-help", label_help)), after = 1) |
156 |
} |
|
157 | ||
158 | ! |
shiny::tagList( |
159 | ! |
include_css_files(pattern = "picker_input"), |
160 | ! |
if (is.null(choices)) { |
161 | ! |
shinyjs::hidden(ui) |
162 |
} else { |
|
163 | ! |
if (fixed) { |
164 | ! |
div( |
165 | ! |
shinyjs::hidden(ui), |
166 | ! |
tags$label(id = paste0(inputId, "_textonly"), class = "control-label", sub(":[[:space:]]+$", "", label)), |
167 | ! |
if (length(selected) > 0) { |
168 | ! |
tags$code( |
169 | ! |
id = paste0(inputId, "_valueonly"), |
170 | ! |
paste(selected, collapse = ", ") |
171 |
) |
|
172 |
}, |
|
173 | ! |
label_help |
174 |
) |
|
175 |
} else { |
|
176 | ! |
ui |
177 |
} |
|
178 |
} |
|
179 |
) |
|
180 |
} |
|
181 | ||
182 |
#' Update `optionalSelectInput` |
|
183 |
#' |
|
184 |
#' @description `r lifecycle::badge("stable")` |
|
185 |
#' |
|
186 |
#' @inheritParams shinyWidgets::updatePickerInput |
|
187 |
#' |
|
188 |
#' @return `NULL` |
|
189 |
#' |
|
190 |
#' @export |
|
191 |
#' |
|
192 |
updateOptionalSelectInput <- function(session, # nolint |
|
193 |
inputId, # nolint |
|
194 |
label = NULL, |
|
195 |
selected = NULL, |
|
196 |
choices = NULL) { |
|
197 | ! |
raw_choices <- extract_raw_choices(choices, attr(choices, "sep")) |
198 | ! |
raw_selected <- extract_raw_choices(selected, attr(choices, "sep")) |
199 | ||
200 | ! |
shinyWidgets::updatePickerInput( |
201 | ! |
session = session, |
202 | ! |
inputId = inputId, |
203 | ! |
label = label, |
204 | ! |
selected = as.character(raw_selected), |
205 | ! |
choices = raw_choices, |
206 | ! |
choicesOpt = picker_options(choices) |
207 |
) |
|
208 | ||
209 | ! |
shinyjs::show(inputId) |
210 | ! |
shinyjs::hide(paste0(inputId, "_textonly")) |
211 | ||
212 | ! |
invisible(NULL) |
213 |
} |
|
214 | ||
215 |
#' Get icons to represent variable types in dataset |
|
216 |
#' |
|
217 |
#' @param var_type (`character`)\cr |
|
218 |
#' of R internal types (classes). |
|
219 |
#' |
|
220 |
#' @return (`character`)\cr |
|
221 |
#' vector of HTML icons corresponding to data type in each column. |
|
222 |
#' @keywords internal |
|
223 |
#' |
|
224 |
#' @examples |
|
225 |
#' teal.widgets:::variable_type_icons(c( |
|
226 |
#' "integer", "numeric", "logical", "Date", "POSIXct", "POSIXlt", |
|
227 |
#' "factor", "character", "unknown", "" |
|
228 |
#' )) |
|
229 |
variable_type_icons <- function(var_type) { |
|
230 | ! |
checkmate::assert_character(var_type, any.missing = FALSE) |
231 | ||
232 | ! |
class_to_icon <- list( |
233 | ! |
numeric = "arrow-up-1-9", |
234 | ! |
integer = "arrow-up-1-9", |
235 | ! |
logical = "pause", |
236 | ! |
Date = "calendar", |
237 | ! |
POSIXct = "calendar", |
238 | ! |
POSIXlt = "calendar", |
239 | ! |
factor = "chart-bar", |
240 | ! |
character = "keyboard", |
241 | ! |
primary_key = "key", |
242 | ! |
unknown = "circle-question" |
243 |
) |
|
244 | ! |
class_to_icon <- lapply(class_to_icon, function(icon_name) toString(icon(icon_name, lib = "font-awesome"))) |
245 | ||
246 | ! |
res <- unname(vapply( |
247 | ! |
var_type, |
248 | ! |
FUN.VALUE = character(1), |
249 | ! |
FUN = function(class) { |
250 | ! |
if (class == "") { |
251 | ! |
class |
252 | ! |
} else if (is.null(class_to_icon[[class]])) { |
253 | ! |
class_to_icon[["unknown"]] |
254 |
} else { |
|
255 | ! |
class_to_icon[[class]] |
256 |
} |
|
257 |
} |
|
258 |
)) |
|
259 | ||
260 | ! |
return(res) |
261 |
} |
|
262 | ||
263 |
#' Optional content for `optionalSelectInput` |
|
264 |
#' |
|
265 |
#' Prepares content to be displayed in `optionalSelectInput` with icons and labels |
|
266 |
#' |
|
267 |
#' @param var_name (`character`)\cr |
|
268 |
#' variable name |
|
269 |
#' @param var_label (`character`)\cr |
|
270 |
#' variable alternative name - for example variable label |
|
271 |
#' @param var_type (`character`) |
|
272 |
#' class of the variable. |
|
273 |
#' |
|
274 |
#' @return (`character`) HTML contents with all elements combined |
|
275 |
#' @keywords internal |
|
276 |
#' |
|
277 |
#' @examples |
|
278 |
#' teal.widgets:::picker_options_content( |
|
279 |
#' var_name = c("SEX", "BRMRKR1"), |
|
280 |
#' var_label = c("Sex", "Biomarker 1"), |
|
281 |
#' var_type = c("factor", "numeric") |
|
282 |
#' ) |
|
283 |
picker_options_content <- function(var_name, var_label, var_type) { |
|
284 | ! |
if (length(var_name) == 0) { |
285 | ! |
return(character(0)) |
286 |
} |
|
287 | ! |
if (length(var_type) == 0 && length(var_label) == 0) { |
288 | ! |
return(var_name) |
289 |
} |
|
290 | ! |
checkmate::assert_character(var_name, min.len = 1, any.missing = FALSE) |
291 | ! |
stopifnot( |
292 | ! |
identical(var_type, character(0)) || length(var_type) == length(var_name), |
293 | ! |
identical(var_label, character(0)) || length(var_label) == length(var_name) |
294 |
) |
|
295 | ||
296 | ! |
var_icon <- variable_type_icons(var_type) |
297 | ||
298 | ! |
res <- trimws(paste( |
299 | ! |
var_icon, |
300 | ! |
var_name, |
301 | ! |
vapply( |
302 | ! |
var_label, |
303 | ! |
function(x) { |
304 | ! |
ifelse(x == "", "", toString(tags$small(x, class = "text-muted"))) |
305 |
}, |
|
306 | ! |
character(1) |
307 |
) |
|
308 |
)) |
|
309 | ||
310 | ! |
return(res) |
311 |
} |
|
312 | ||
313 |
#' Create `choicesOpt` for `pickerInput` |
|
314 |
#' |
|
315 |
#' @param choices (`choices_labeled` or `character`)\cr |
|
316 |
#' choices vector |
|
317 |
#' |
|
318 |
#' @return (`list`)\cr |
|
319 |
#' to be passed as `choicesOpt` argument. |
|
320 |
#' @keywords internal |
|
321 |
picker_options <- function(choices) { |
|
322 | ! |
if (inherits(choices, "choices_labeled")) { |
323 | ! |
raw_choices <- extract_raw_choices(choices, sep = attr(choices, "sep")) |
324 | ! |
return( |
325 | ! |
list( |
326 | ! |
content = picker_options_content( |
327 | ! |
var_name = raw_choices, |
328 | ! |
var_label = extract_choices_labels(choices), |
329 | ! |
var_type = if (is.null(attr(choices, "types"))) character(0) else attr(choices, "types") |
330 |
) |
|
331 |
) |
|
332 |
) |
|
333 | ! |
} else if (all(vapply(choices, inherits, logical(1), "choices_labeled"))) { |
334 | ! |
choices <- unlist(unname(choices)) |
335 | ! |
return( |
336 | ! |
list(content = picker_options_content( |
337 | ! |
var_name = choices, |
338 | ! |
var_label = extract_choices_labels(choices), |
339 | ! |
var_type = if (is.null(attr(choices, "types"))) character(0) else attr(choices, "types") |
340 |
)) |
|
341 |
) |
|
342 |
} else { |
|
343 | ! |
return(NULL) |
344 |
} |
|
345 |
} |
|
346 | ||
347 |
#' Extract raw values from choices |
|
348 |
#' |
|
349 |
#' @param choices (`choices_labeled`, `list` or `character`)\cr |
|
350 |
#' object containing choices |
|
351 |
#' @param sep (`character(1)`)\cr |
|
352 |
#' A separator string to split the `choices` or `selected` inputs into the values of |
|
353 |
#' the different columns. |
|
354 |
#' @return choices simplified |
|
355 |
#' @keywords internal |
|
356 |
extract_raw_choices <- function(choices, sep) { |
|
357 | ! |
if (!is.null(sep)) { |
358 | ! |
vapply(choices, paste, collapse = sep, character(1)) |
359 | ! |
} else if (inherits(choices, "choices_labeled")) { |
360 | ! |
unname(unlist(choices)) |
361 |
} else { |
|
362 | ! |
choices |
363 |
} |
|
364 |
} |
|
365 | ||
366 |
#' if min or max are `NA` then the slider widget will be hidden |
|
367 |
#' |
|
368 |
#' @description `r lifecycle::badge("stable")` |
|
369 |
#' Hidden input widgets are useful to have the `input[[inputId]]` variable |
|
370 |
#' on available in the server function but no corresponding visual clutter from |
|
371 |
#' input widgets that provide only a single choice. |
|
372 |
#' |
|
373 |
#' @inheritParams shiny::sliderInput |
|
374 |
#' @param label_help (`shiny.tag`, optional)\cr |
|
375 |
#' an object of class `shiny.tag`. E.g. an object returned by [shiny::helpText()] |
|
376 |
#' @param ... optional arguments to `sliderInput` |
|
377 |
#' |
|
378 |
#' @export |
|
379 |
#' |
|
380 |
#' @examples |
|
381 |
#' optionalSliderInput("a", "b", 0, 1, 0.2) |
|
382 |
optionalSliderInput <- function(inputId, label, min, max, value, label_help = NULL, ...) { # nolint |
|
383 | 25x |
checkmate::assert_number(min, na.ok = TRUE) |
384 | 25x |
checkmate::assert_number(max, na.ok = TRUE) |
385 | 25x |
checkmate::assert_numeric(value, min.len = 1, max.len = 2, any.missing = FALSE) |
386 | ||
387 | 25x |
is_na_min <- is.na(min) |
388 | 25x |
is_na_max <- is.na(max) |
389 | ||
390 | 25x |
hide <- is_na_min || is_na_max |
391 | ||
392 | 25x |
if (length(value) == 2) { |
393 | 2x |
value1 <- value[1] |
394 | 2x |
value2 <- value[2] |
395 |
} else { |
|
396 | 23x |
value1 <- value |
397 | 23x |
value2 <- value |
398 |
} |
|
399 | ||
400 | 25x |
if (is_na_min) { |
401 | 2x |
min <- value1 - 1 |
402 |
} |
|
403 | 25x |
if (is_na_max) { |
404 | 1x |
max <- value2 + 1 |
405 |
} |
|
406 | ||
407 | 25x |
if (min > value1 || max < value2) { |
408 | 2x |
stop("arguments inconsistent: min <= value <= max expected") |
409 |
} |
|
410 | ||
411 | 23x |
slider <- sliderInput(inputId, label, min, max, value, ...) |
412 | ||
413 | 23x |
if (!is.null(label_help)) { |
414 | ! |
slider[[3]] <- append(slider[[3]], list(div(class = "label-help", label_help)), after = 1) |
415 |
} |
|
416 | ||
417 | 23x |
if (hide) { |
418 | 2x |
shinyjs::hidden(slider) |
419 |
} else { |
|
420 | 21x |
slider |
421 |
} |
|
422 |
} |
|
423 | ||
424 |
#' For teal modules we parameterize an \code{optionalSliderInput} with one argument |
|
425 |
#' \code{value_min_max} |
|
426 |
#' |
|
427 |
#' @description `r lifecycle::badge("stable")` |
|
428 |
#' The [optionalSliderInput()] function needs three arguments to decided |
|
429 |
#' whether to hide the `sliderInput` widget or not. For teal modules we specify an |
|
430 |
#' optional slider input with one argument here called `value_min_max`. |
|
431 |
#' |
|
432 |
#' @inheritParams optionalSliderInput |
|
433 |
#' |
|
434 |
#' @param value_min_max (`numeric(1)` or `numeric(3)`)\cr |
|
435 |
#' If of length 1 then the value gets set to that number and the `sliderInput` will be hidden. |
|
436 |
#' Otherwise, if it is of length three the three elements will map to `value`, `min` and `max` of |
|
437 |
#' the [optionalSliderInput()] function. |
|
438 |
#' |
|
439 |
#' @export |
|
440 |
#' |
|
441 |
#' @examples |
|
442 |
#' |
|
443 |
#' optionalSliderInputValMinMax("a", "b", 1) |
|
444 |
#' optionalSliderInputValMinMax("a", "b", c(3, 1, 5)) |
|
445 |
optionalSliderInputValMinMax <- function(inputId, label, value_min_max, label_help = NULL, ...) { # nolint |
|
446 | ||
447 | 18x |
x <- value_min_max |
448 | ||
449 | ! |
if (!is.numeric(x)) stop("value_min_max is expected to be numeric") |
450 | ||
451 | 18x |
vals <- if (length(x) == 3) { |
452 | 18x |
if (any(diff(x[c(2, 1, 3)]) < 0)) { |
453 | ! |
stop(paste("value_min_max is expected to be (value, min, max) where min <= value <= max")) |
454 |
} |
|
455 | 18x |
list(value = x[1], min = x[2], max = x[3]) |
456 | 18x |
} else if (length(x) == 1) { |
457 | ! |
list(value = x, min = NA_real_, max = NA_real_) |
458 |
} else { |
|
459 | ! |
stop(paste("value_min_max is expected to be of length 1 (value) or of length 3 (value, min, max)")) |
460 |
} |
|
461 | ||
462 | 18x |
slider <- optionalSliderInput(inputId, label, vals$min, vals$max, vals$value, ...) |
463 | ||
464 | 18x |
if (!is.null(label_help)) { |
465 | ! |
slider[[3]] <- append(slider[[3]], list(div(class = "label-help", label_help)), after = 1) |
466 |
} |
|
467 | 18x |
return(slider) |
468 |
} |
|
469 | ||
470 |
#' Extract labels from choices basing on attributes and names |
|
471 |
#' |
|
472 |
#' @param choices (`list` or `vector`)\cr |
|
473 |
#' select choices |
|
474 |
#' @param values (optional)\cr |
|
475 |
#' choices subset for which labels should be extracted, `NULL` for all choices. |
|
476 |
#' |
|
477 |
#' @return (`character`) vector with labels |
|
478 |
#' @keywords internal |
|
479 |
extract_choices_labels <- function(choices, values = NULL) { |
|
480 | ! |
res <- if (inherits(choices, "choices_labeled")) { |
481 | ! |
attr(choices, "raw_labels") |
482 | ! |
} else if (!is.null(names(choices)) && !setequal(names(choices), unlist(unname(choices)))) { |
483 | ! |
names(choices) |
484 |
} else { |
|
485 | ! |
NULL |
486 |
} |
|
487 | ||
488 | ! |
if (!is.null(values) && !is.null(res)) { |
489 | ! |
stopifnot(all(values %in% choices)) |
490 | ! |
res <- res[vapply(values, function(val) which(val == choices), numeric(1))] |
491 |
} |
|
492 | ||
493 | ! |
return(res) |
494 |
} |
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 |
#' lab = 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 |
#' teal option variable. The `NULL` value is supported. |
|
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 |
#' lab = list(title = "TITLE"), |
|
79 |
#' theme = list(title = ggplot2::element_text(size = 20)) |
|
80 |
#' ), |
|
81 |
#' user_default = ggplot2_args( |
|
82 |
#' lab = 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 |
#' lab = 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 |
#' lab = 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 |
#' A `shiny` module that pops up verbatim text. |
|
2 |
#' @name verbatim_popup |
|
3 |
#' @description `r lifecycle::badge("experimental")` |
|
4 |
#' This module consists of a button that once clicked pops up a |
|
5 |
#' modal window with verbatim-styled text. |
|
6 |
#' |
|
7 |
#' @param id (`character(1)`) the `shiny` id |
|
8 |
#' @param button_label (`character(1)`) the text printed on the button |
|
9 |
#' @param type (`character(1)`) specifying whether to use `[shiny::actionButton()]` or `[shiny::actionLink()]`. |
|
10 |
#' @param ... additional arguments to `[shiny::actionButton()]`(or `[shiny::actionLink()]`). |
|
11 |
#' |
|
12 |
#' @return the UI function returns a `shiny.tag.list` object |
|
13 |
#' @export |
|
14 |
#' |
|
15 |
#' @examples |
|
16 |
#' ui <- shiny::fluidPage(verbatim_popup_ui("my_id", button_label = "Open popup")) |
|
17 |
#' srv <- function(input, output) { |
|
18 |
#' verbatim_popup_srv( |
|
19 |
#' "my_id", |
|
20 |
#' "if (TRUE) { print('Popups are the best') }", |
|
21 |
#' title = "My custom title", |
|
22 |
#' style = TRUE |
|
23 |
#' ) |
|
24 |
#' } |
|
25 |
#' if (interactive()) shiny::shinyApp(ui, srv) |
|
26 |
#' |
|
27 |
verbatim_popup_ui <- function(id, button_label, type = c("button", "link"), ...) { |
|
28 | 4x |
checkmate::assert_string(id) |
29 | 4x |
checkmate::assert_string(button_label) |
30 | ||
31 | 4x |
ui_function <- switch(match.arg(type), |
32 | 4x |
"button" = shiny::actionButton, |
33 | 4x |
"link" = shiny::actionLink, |
34 | 4x |
stop("Argument 'type' should be 'button' or 'link'") |
35 |
) |
|
36 | ||
37 | 3x |
ns <- shiny::NS(id) |
38 | 3x |
ui_args <- list( |
39 | 3x |
inputId = ns("button"), |
40 | 3x |
label = button_label |
41 |
) |
|
42 | ||
43 | 3x |
shiny::tagList( |
44 | 3x |
shiny::singleton( |
45 | 3x |
shiny::tags$head(shiny::includeScript(system.file("js/verbatim_popup.js", package = "teal.widgets"))) |
46 |
), |
|
47 | 3x |
shinyjs::useShinyjs(), |
48 | 3x |
do.call(ui_function, c(ui_args, list(...))) |
49 |
) |
|
50 |
} |
|
51 | ||
52 |
#' @name verbatim_popup |
|
53 |
#' @export |
|
54 |
#' |
|
55 |
#' @param verbatim_content (`character`, `expression`, `condition` or `reactive(1)` |
|
56 |
#' holding any of the above) the content to show in the popup modal window |
|
57 |
#' @param title (`character(1)`) the title of the modal window |
|
58 |
#' @param style (`logical(1)`) whether to style the `verbatim_content` using `styler::style_text`. |
|
59 |
#' If `verbatim_content` is a `condition` or `reactive` holding `condition` then this argument is ignored |
|
60 |
#' @param disabled (`reactive(1)`) the `shiny` reactive value holding a `logical`. The popup button is disabled |
|
61 |
#' when the flag is `TRUE` and enabled otherwise. |
|
62 |
#' |
|
63 |
verbatim_popup_srv <- function(id, verbatim_content, title, style = FALSE, disabled = shiny::reactiveVal(FALSE)) { |
|
64 | ! |
checkmate::assert_string(id) |
65 | ! |
checkmate::assert_string(title) |
66 | ! |
checkmate::assert_flag(style) |
67 | ! |
checkmate::assert_class(disabled, classes = "reactive") |
68 | ! |
moduleServer(id, function(input, output, session) { |
69 | ! |
ns <- session$ns |
70 | ! |
modal_content <- format_content(verbatim_content, style) |
71 | ! |
button_click_observer( |
72 | ! |
click_event = shiny::reactive(input$button), |
73 | ! |
copy_button_id = ns("copy_button"), |
74 | ! |
copied_area_id = ns("verbatim_content"), |
75 | ! |
modal_title = title, |
76 | ! |
modal_content = modal_content, |
77 | ! |
disabled = disabled |
78 |
) |
|
79 |
}) |
|
80 |
} |
|
81 | ||
82 |
#' Creates a `shiny` observer handling button clicks. |
|
83 |
#' |
|
84 |
#' @description |
|
85 |
#' When the button is clicked it pop up a modal window with the text. |
|
86 |
#' |
|
87 |
#' @keywords internal |
|
88 |
#' @param click_event `reactive` the click event |
|
89 |
#' @param copy_button_id (`character(1)`) the id of the button to copy the modal content. |
|
90 |
#' Automatically appended with a 1 and 2 suffix for top and bottom buttons respectively. |
|
91 |
#' @param copied_area_id (`character(1)`) the id of the element which contents are copied |
|
92 |
#' @param modal_title (`character(1)`) the title of the modal window |
|
93 |
#' @param modal_content (`reactive`) the content of the modal window |
|
94 |
#' @param disabled (`reactive(1)`) the `shiny` reactive value holding a `logical`. The popup button is disabled |
|
95 |
#' when the flag is `TRUE` and enabled otherwise. |
|
96 |
button_click_observer <- function(click_event, |
|
97 |
copy_button_id, |
|
98 |
copied_area_id, |
|
99 |
modal_title, |
|
100 |
modal_content, |
|
101 |
disabled) { |
|
102 | 1x |
shiny::observeEvent( |
103 | 1x |
disabled(), |
104 | 1x |
handlerExpr = { |
105 | ! |
if (disabled()) { |
106 | ! |
shinyjs::disable("button") |
107 |
} else { |
|
108 | ! |
shinyjs::enable("button") |
109 |
} |
|
110 |
} |
|
111 |
) |
|
112 | ||
113 | 1x |
shiny::observeEvent( |
114 | 1x |
click_event(), |
115 | 1x |
handlerExpr = { |
116 | ! |
req(modal_content()) |
117 | ! |
shiny::showModal( |
118 | ! |
shiny::modalDialog( |
119 | ! |
shiny::tagList( |
120 | ! |
include_css_files(pattern = "verbatim_popup"), |
121 | ! |
shiny::tags$div( |
122 | ! |
class = "mb-4", |
123 | ! |
shiny::actionButton( |
124 | ! |
paste0(copy_button_id, 1), |
125 | ! |
"Copy to Clipboard", |
126 | ! |
onclick = paste0("copyToClipboard('", copied_area_id, "')") |
127 |
), |
|
128 | ! |
shiny::modalButton("Dismiss") |
129 |
), |
|
130 | ! |
shiny::tags$pre(id = copied_area_id, modal_content()), |
131 |
), |
|
132 | ! |
title = modal_title, |
133 | ! |
footer = shiny::tagList( |
134 | ! |
shiny::actionButton( |
135 | ! |
paste0(copy_button_id, 2), |
136 | ! |
"Copy to Clipboard", |
137 | ! |
onclick = paste0("copyToClipboard('", copied_area_id, "')") |
138 |
), |
|
139 | ! |
shiny::modalButton("Dismiss") |
140 |
), |
|
141 | ! |
size = "l", |
142 | ! |
easyClose = TRUE |
143 |
) |
|
144 |
) |
|
145 |
} |
|
146 |
) |
|
147 |
} |
|
148 | ||
149 |
#' Formats the content of the modal popup window. |
|
150 |
#' |
|
151 |
#' @details |
|
152 |
#' Formats the content: |
|
153 |
#' * concatenates if needed |
|
154 |
#' * styles if `style` is TRUE |
|
155 |
#' |
|
156 |
#' @keywords internal |
|
157 |
#' @inheritParams verbatim_popup |
|
158 |
#' @return `reactive` with the formatted content |
|
159 |
format_content <- function(verbatim_content, style = FALSE) { |
|
160 | 11x |
shiny::reactive({ |
161 | 4x |
content <- if (inherits(verbatim_content, "reactive")) { |
162 | 2x |
tryCatch( |
163 | 2x |
verbatim_content(), |
164 | 2x |
error = function(e) { |
165 | ! |
e |
166 |
} |
|
167 |
) |
|
168 |
} else { |
|
169 | 2x |
verbatim_content |
170 |
} |
|
171 | 4x |
shiny::validate(shiny::need( |
172 | 4x |
checkmate::test_multi_class(content, classes = c("expression", "character", "condition")), |
173 | 4x |
"verbatim_content should be an expression, character or condition" |
174 |
)) |
|
175 | ||
176 | 4x |
content <- paste(as.character(content), collapse = "\n") |
177 | ||
178 | 4x |
if (style && !checkmate::test_class(content, "condition")) { |
179 | 3x |
content <- paste(styler::style_text(content), collapse = "\n") |
180 |
} |
|
181 | 4x |
content |
182 |
}) |
|
183 |
} |
1 |
#' UI part of plot-with-settings module |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("stable")` |
|
4 |
#' @export |
|
5 |
#' |
|
6 |
#' @param id (\code{character}) module ID |
|
7 |
plot_with_settings_ui <- function(id) { |
|
8 | 2x |
checkmate::assert_string(id) |
9 | ||
10 | 2x |
ns <- NS(id) |
11 | ||
12 | 2x |
tagList( |
13 | 2x |
shiny::singleton(shiny::tags$head( |
14 | 2x |
shiny::tags$script( |
15 |
# nolint start |
|
16 | 2x |
sprintf( |
17 | 2x |
'$(document).on("shiny:connected", function(e) { |
18 | 2x |
Shiny.onInputChange("%s", document.getElementById("%s").clientWidth); |
19 | 2x |
Shiny.onInputChange("%s", 0.87*window.innerWidth); |
20 | 2x |
//based on modal CSS property, also accounting for margins |
21 |
}); |
|
22 | 2x |
$(window).resize(function(e) { |
23 | 2x |
Shiny.onInputChange("%s", document.getElementById("%s").clientWidth); |
24 | 2x |
Shiny.onInputChange("%s", 0.87*window.innerWidth); |
25 | 2x |
//based on modal CSS property, also accounting for margins |
26 |
});', |
|
27 |
# nolint end |
|
28 | 2x |
ns("flex_width"), # session input$ variable name |
29 | 2x |
ns("plot_out_main"), # graph parent id |
30 | 2x |
ns("plot_modal_width"), # session input$ variable name |
31 | 2x |
ns("flex_width"), # session input$ variable name |
32 | 2x |
ns("plot_out_main"), # graph parent id |
33 | 2x |
ns("plot_modal_width") # session input$ variable name |
34 |
) |
|
35 |
) |
|
36 |
)), |
|
37 | 2x |
include_css_files("plot_with_settings"), |
38 | 2x |
tags$div( |
39 | 2x |
id = ns("plot-with-settings"), |
40 | 2x |
tags$div( |
41 | 2x |
class = "plot-settings-buttons", |
42 | 2x |
type_download_ui(ns("downbutton")), |
43 | 2x |
actionButton( |
44 | 2x |
ns("expand"), |
45 | 2x |
label = character(0), |
46 | 2x |
icon = icon("up-right-and-down-left-from-center"), |
47 | 2x |
class = "btn-sm" |
48 |
), |
|
49 | 2x |
shinyWidgets::dropdownButton( |
50 | 2x |
circle = FALSE, |
51 | 2x |
icon = icon("maximize"), |
52 | 2x |
inline = TRUE, |
53 | 2x |
right = TRUE, |
54 | 2x |
label = "", |
55 | 2x |
inputId = ns("expbut"), |
56 | 2x |
uiOutput(ns("slider_ui")), |
57 | 2x |
uiOutput(ns("width_warning")) |
58 |
) |
|
59 |
), |
|
60 | 2x |
uiOutput(ns("plot_out_main"), class = "plot_out_container", width = "100%") |
61 |
) |
|
62 |
) |
|
63 |
} |
|
64 | ||
65 |
#' Server part of plot-with-settings module |
|
66 |
#' |
|
67 |
#' @description `r lifecycle::badge("stable")` |
|
68 |
#' @export |
|
69 |
#' |
|
70 |
#' @inheritParams shiny::moduleServer |
|
71 |
#' @param plot_r (`reactive` or `function`)\cr |
|
72 |
#' `reactive` expression or a simple `function` to draw a plot. |
|
73 |
#' A simple `function` is needed e.g. for base plots like `plot(1)` as the output can not be caught when downloading. |
|
74 |
#' Take into account that simple functions are less efficient than reactive, as not catching the result. |
|
75 |
#' @param height (`numeric`, optional)\cr |
|
76 |
#' vector with three elements c(VAL, MIN, MAX), where VAL is the starting value of the slider in |
|
77 |
#' the main and modal plot display. The value in the modal display is taken from the value of the |
|
78 |
#' slider in the main plot display. |
|
79 |
#' @param width (`numeric`, optional)\cr |
|
80 |
#' vector with three elements `c(VAL, MIN, MAX)`, where VAL is the starting value of the slider in |
|
81 |
#' the main and modal plot display; `NULL` for default display. The value in the modal |
|
82 |
#' display is taken from the value of the slider in the main plot display. |
|
83 |
#' @param show_hide_signal optional, (\code{reactive logical} a mechanism to allow modules which call this |
|
84 |
#' module to show/hide the plot_with_settings UI) |
|
85 |
#' @param brushing (`logical`, optional)\cr |
|
86 |
#' a mechanism to enable / disable brushing on the main plot (in particular: not the one displayed |
|
87 |
#' in modal). All the brushing data is stored as a reactive object in the `"brush"` element of |
|
88 |
#' returned list. See the example for details. |
|
89 |
#' @param clicking (`logical`)\cr |
|
90 |
#' a mechanism to enable / disable clicking on data points on the main plot (in particular: not the |
|
91 |
#' one displayed in modal). All the clicking data is stored as a reactive object in the `"click"` |
|
92 |
#' element of returned list. See the example for details. |
|
93 |
#' @param dblclicking (`logical`, optional)\cr |
|
94 |
#' a mechanism to enable / disable double-clicking on data points on the main plot (in particular: |
|
95 |
#' not the one displayed in modal). All the double clicking data is stored as a reactive object in |
|
96 |
#' the `"dblclick"` element of returned list. See the example for details. |
|
97 |
#' @param hovering (`logical(1)`, optional)\cr |
|
98 |
#' a mechanism to enable / disable hovering over data points on the main plot (in particular: not |
|
99 |
#' the one displayed in modal). All the hovering data is stored as a reactive object in the |
|
100 |
#' `"hover"` element of returned list. See the example for details. |
|
101 |
#' @param graph_align (`character(1)`, optional)\cr |
|
102 |
#' one of `"left"` (default), `"center"`, `"right"` or `"justify"`. The alignment of the graph on |
|
103 |
#' the main page. |
|
104 |
#' |
|
105 |
#' @details By default the plot is rendered with `72 dpi`. In order to change this, to for example 96 set |
|
106 |
#' `options(teal.plot_dpi = 96)`. The minimum allowed `dpi` value is `24` and it must be a whole number. |
|
107 |
#' If an invalid value is set then the default value is used and a warning is outputted to the console. |
|
108 |
#' |
|
109 |
#' @examples |
|
110 |
#' \dontrun{ |
|
111 |
#' # Example using a reactive as input to plot_r |
|
112 |
#' library(shiny) |
|
113 |
#' shinyApp( |
|
114 |
#' ui = fluidPage( |
|
115 |
#' plot_with_settings_ui( |
|
116 |
#' id = "plot_with_settings" |
|
117 |
#' ) |
|
118 |
#' ), |
|
119 |
#' server = function(input, output, session) { |
|
120 |
#' plot_r <- reactive({ |
|
121 |
#' ggplot2::qplot(x = 1, y = 1) |
|
122 |
#' }) |
|
123 |
#' |
|
124 |
#' plot_with_settings_srv( |
|
125 |
#' id = "plot_with_settings", |
|
126 |
#' plot_r = plot_r, |
|
127 |
#' height = c(400, 100, 1200), |
|
128 |
#' width = c(500, 250, 750) |
|
129 |
#' ) |
|
130 |
#' } |
|
131 |
#' ) |
|
132 |
#' |
|
133 |
#' # Example using a function as input to plot_r |
|
134 |
#' shinyApp( |
|
135 |
#' ui = fluidPage( |
|
136 |
#' radioButtons("download_option", "Select the Option", list("ggplot", "trellis", "grob", "base")), |
|
137 |
#' plot_with_settings_ui( |
|
138 |
#' id = "plot_with_settings" |
|
139 |
#' ), |
|
140 |
#' sliderInput("nums", "Value", 1, 10, 1) |
|
141 |
#' ), |
|
142 |
#' server = function(input, output, session) { |
|
143 |
#' plot_r <- function() { |
|
144 |
#' numbers <- seq_len(input$nums) |
|
145 |
#' if (input$download_option == "ggplot") { |
|
146 |
#' ggplot2::ggplot(data.frame(n = numbers), ggplot2::aes(n)) + |
|
147 |
#' ggplot2::geom_bar() |
|
148 |
#' } else if (input$download_option == "trellis") { |
|
149 |
#' lattice::densityplot(numbers) |
|
150 |
#' } else if (input$download_option == "grob") { |
|
151 |
#' tr_plot <- lattice::densityplot(numbers) |
|
152 |
#' ggplot2::ggplotGrob( |
|
153 |
#' ggplot2::ggplot(data.frame(n = numbers), ggplot2::aes(n)) + |
|
154 |
#' ggplot2::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 |
#' |
|
170 |
#' # Example with brushing/hovering/clicking/double-clicking |
|
171 |
#' shinyApp( |
|
172 |
#' ui = fluidPage( |
|
173 |
#' plot_with_settings_ui( |
|
174 |
#' id = "plot_with_settings" |
|
175 |
#' ), |
|
176 |
#' fluidRow( |
|
177 |
#' column(4, h3("Brush"), verbatimTextOutput("brushing_data")), |
|
178 |
#' column(4, h3("Click"), verbatimTextOutput("clicking_data")), |
|
179 |
#' column(4, h3("DblClick"), verbatimTextOutput("dblclicking_data")), |
|
180 |
#' column(4, h3("Hover"), verbatimTextOutput("hovering_data")) |
|
181 |
#' ) |
|
182 |
#' ), |
|
183 |
#' server = function(input, output, session) { |
|
184 |
#' plot_r <- reactive({ |
|
185 |
#' ggplot2::qplot(x = 1:5, y = 1:5) |
|
186 |
#' }) |
|
187 |
#' |
|
188 |
#' plot_data <- plot_with_settings_srv( |
|
189 |
#' id = "plot_with_settings", |
|
190 |
#' plot_r = plot_r, |
|
191 |
#' height = c(400, 100, 1200), |
|
192 |
#' brushing = TRUE, |
|
193 |
#' clicking = TRUE, |
|
194 |
#' dblclicking = TRUE, |
|
195 |
#' hovering = TRUE |
|
196 |
#' ) |
|
197 |
#' |
|
198 |
#' output$brushing_data <- renderPrint(plot_data$brush()) |
|
199 |
#' output$clicking_data <- renderPrint(plot_data$click()) |
|
200 |
#' output$dblclicking_data <- renderPrint(plot_data$dblclick()) |
|
201 |
#' output$hovering_data <- renderPrint(plot_data$hover()) |
|
202 |
#' } |
|
203 |
#' ) |
|
204 |
#' |
|
205 |
#' # Example which allows module to be hidden/shown |
|
206 |
#' library("shinyjs") |
|
207 |
#' |
|
208 |
#' shinyApp( |
|
209 |
#' ui = fluidPage( |
|
210 |
#' useShinyjs(), |
|
211 |
#' plot_with_settings_ui( |
|
212 |
#' id = "plot_with_settings" |
|
213 |
#' ), |
|
214 |
#' actionButton("button", "Show/Hide") |
|
215 |
#' ), |
|
216 |
#' server = function(input, output, session) { |
|
217 |
#' plot_r <- reactive(ggplot2::qplot(x = 1, y = 1)) |
|
218 |
#' |
|
219 |
#' show_hide_signal_rv <- reactiveVal(TRUE) |
|
220 |
#' |
|
221 |
#' observeEvent(input$button, show_hide_signal_rv(!show_hide_signal_rv())) |
|
222 |
#' |
|
223 |
#' plot_with_settings_srv( |
|
224 |
#' id = "plot_with_settings", |
|
225 |
#' plot_r = plot_r, |
|
226 |
#' height = c(400, 100, 1200), |
|
227 |
#' width = c(500, 250, 750), |
|
228 |
#' show_hide_signal = reactive(show_hide_signal_rv()) |
|
229 |
#' ) |
|
230 |
#' } |
|
231 |
#' ) |
|
232 |
#' } |
|
233 |
plot_with_settings_srv <- function(id, |
|
234 |
plot_r, |
|
235 |
height = c(600, 200, 2000), |
|
236 |
width = NULL, |
|
237 |
show_hide_signal = reactive(TRUE), |
|
238 |
brushing = FALSE, |
|
239 |
clicking = FALSE, |
|
240 |
dblclicking = FALSE, |
|
241 |
hovering = FALSE, |
|
242 |
graph_align = "left") { |
|
243 | 24x |
checkmate::assert_string(id) |
244 | 24x |
checkmate::assert( |
245 | 24x |
checkmate::check_class(plot_r, "function"), |
246 | 24x |
checkmate::check_class(plot_r, "reactive") |
247 |
) |
|
248 | 23x |
checkmate::assert_numeric(height, min.len = 1, any.missing = FALSE) |
249 | 22x |
checkmate::assert_numeric(height, len = 3, any.missing = FALSE, finite = TRUE) |
250 | 22x |
checkmate::assert_numeric(height[1], lower = height[2], upper = height[3], .var.name = "height") |
251 | 22x |
checkmate::assert_numeric(width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE) |
252 | 22x |
checkmate::assert_numeric(width[1], lower = width[2], upper = width[3], null.ok = TRUE, .var.name = "width") |
253 | ||
254 | 21x |
checkmate::assert_class(show_hide_signal, c("reactive", "function")) |
255 | 20x |
checkmate::assert_flag(brushing) |
256 | 19x |
checkmate::assert_flag(clicking) |
257 | 18x |
checkmate::assert_flag(dblclicking) |
258 | 17x |
checkmate::assert_flag(hovering) |
259 | 16x |
checkmate::assert_string(graph_align) |
260 | 16x |
checkmate::assert_subset(graph_align, c("left", "right", "center", "justify")) |
261 | ||
262 | 15x |
moduleServer(id, function(input, output, session) { |
263 | 15x |
ns <- session$ns |
264 | 15x |
default_w <- function() session$clientData[[paste0("output_", ns("plot_main_width"))]] |
265 | 15x |
default_h <- function() session$clientData[[paste0("output_", ns("plot_main_height"))]] |
266 | ||
267 | 15x |
default_slider_width <- reactiveVal(width) |
268 | 15x |
delayed_flex_width <- debounce(reactive(input$flex_width), millis = 100) |
269 | ||
270 | 15x |
if (is.null(width)) { |
271 |
# if width = NULL then set default_slider_width to be the value of the plot width on load |
|
272 | ! |
observeEvent(session$clientData[[paste0("output_", ns("plot_main_width"))]], |
273 | ! |
handlerExpr = { |
274 | ! |
default_slider_width(default_w() * c(1, 0.5, 2.8)) |
275 |
}, |
|
276 | ! |
once = TRUE, |
277 | ! |
ignoreNULL = TRUE |
278 |
) |
|
279 | ||
280 | ! |
observeEvent(delayed_flex_width(), { |
281 | ! |
if (delayed_flex_width() > 0 && !isFALSE(input$width_resize_switch)) { |
282 | ! |
default_slider_width(delayed_flex_width() * c(1, 0.5, 2.8)) |
283 | ! |
updateSliderInput(session, inputId = "width", value = delayed_flex_width()) |
284 |
} |
|
285 |
}) |
|
286 |
} |
|
287 | ||
288 | 15x |
plot_type <- reactive({ |
289 | 15x |
if (inherits(plot_r(), "ggplot")) { |
290 | 3x |
"gg" |
291 | 12x |
} else if (inherits(plot_r(), "trellis")) { |
292 | 2x |
"trel" |
293 | 10x |
} else if (inherits(plot_r(), "grob")) { |
294 | 2x |
"grob" |
295 | 8x |
} else if (inherits(plot_r(), c("NULL", "histogram", "list")) && !inherits(plot_r, "reactive")) { |
296 | 6x |
"base" |
297 |
} else { |
|
298 | 2x |
"other" |
299 |
} |
|
300 |
}) |
|
301 | ||
302 |
# allow modules which use this module to turn on and off the UI |
|
303 | 15x |
observeEvent(show_hide_signal(), { |
304 | 10x |
if (show_hide_signal()) { |
305 | 9x |
shinyjs::show("plot-with-settings") |
306 |
} else { |
|
307 | 1x |
shinyjs::hide("plot-with-settings") |
308 |
} |
|
309 |
}) |
|
310 | ||
311 | 15x |
output$slider_ui <- renderUI({ |
312 | 8x |
div( |
313 | 8x |
optionalSliderInputValMinMax( |
314 | 8x |
inputId = ns("height"), |
315 | 8x |
label = "Plot height", |
316 | 8x |
value_min_max = round(height), |
317 | 8x |
ticks = FALSE, |
318 | 8x |
step = 1L, |
319 | 8x |
round = TRUE |
320 |
), |
|
321 | 8x |
tags$b("Plot width"), |
322 | 8x |
shinyWidgets::switchInput( |
323 | 8x |
inputId = ns("width_resize_switch"), |
324 | 8x |
onLabel = "ON", |
325 | 8x |
offLabel = "OFF", |
326 | 8x |
label = "Auto width", |
327 | 8x |
value = `if`(is.null(width), TRUE, FALSE), |
328 | 8x |
size = "mini", |
329 | 8x |
labelWidth = "80px" |
330 |
), |
|
331 | 8x |
optionalSliderInputValMinMax( |
332 | 8x |
inputId = ns("width"), |
333 | 8x |
label = NULL, |
334 | 8x |
value_min_max = round(isolate(default_slider_width())), |
335 | 8x |
ticks = FALSE, |
336 | 8x |
step = 1L, |
337 | 8x |
round = TRUE |
338 |
) |
|
339 |
) |
|
340 |
}) |
|
341 | ||
342 | 15x |
observeEvent(input$width_resize_switch | delayed_flex_width(), { |
343 | 12x |
if (length(input$width_resize_switch) && input$width_resize_switch) { |
344 | ! |
shinyjs::disable("width") |
345 | ! |
updateSliderInput(session, inputId = "width", value = delayed_flex_width()) |
346 |
} else { |
|
347 | 12x |
shinyjs::enable("width") |
348 |
} |
|
349 |
}) |
|
350 | ||
351 | 15x |
ranges <- reactiveValues(x = NULL, y = NULL) |
352 | ||
353 | 15x |
observeEvent(input$plot_dblclick, { |
354 | 1x |
brush <- input$plot_brush |
355 | 1x |
if (!is.null(brush)) { |
356 | ! |
ranges$x <- c(brush$xmin, brush$xmax) |
357 | ! |
ranges$y <- c(brush$ymin, brush$ymax) |
358 |
} else { |
|
359 | 1x |
ranges$x <- NULL |
360 | 1x |
ranges$y <- NULL |
361 |
} |
|
362 |
}) |
|
363 | ||
364 | 15x |
p_height <- reactive(`if`(!is.null(input$height), input$height, height[1])) |
365 | 15x |
p_width <- reactive(`if`(!is.null(input$width), input$width, default_slider_width()[1])) |
366 | 15x |
output$plot_main <- renderPlot( |
367 | 15x |
apply_plot_modifications( |
368 | 15x |
plot_obj = plot_r(), |
369 | 15x |
plot_type = plot_type(), |
370 | 15x |
dblclicking = dblclicking, |
371 | 15x |
ranges = ranges |
372 |
), |
|
373 | 15x |
res = get_plot_dpi(), |
374 | 15x |
height = p_height, |
375 | 15x |
width = p_width |
376 |
) |
|
377 | ||
378 | 15x |
output$plot_modal <- renderPlot( |
379 | 15x |
apply_plot_modifications( |
380 | 15x |
plot_obj = plot_r(), |
381 | 15x |
plot_type = plot_type(), |
382 | 15x |
dblclicking = dblclicking, |
383 | 15x |
ranges = ranges |
384 |
), |
|
385 | 15x |
res = get_plot_dpi(), |
386 | 15x |
height = reactive(input$height_in_modal), |
387 | 15x |
width = reactive(input$width_in_modal) |
388 |
) |
|
389 | ||
390 | 15x |
output$plot_out_main <- renderUI({ |
391 | 14x |
div( |
392 | 14x |
align = graph_align, |
393 | 14x |
plotOutput( |
394 | 14x |
ns("plot_main"), |
395 | 14x |
height = p_height(), |
396 | 14x |
width = p_width(), |
397 | 14x |
brush = `if`(brushing, brushOpts(ns("plot_brush"), resetOnNew = TRUE), NULL), |
398 | 14x |
click = `if`(clicking, clickOpts(ns("plot_click")), NULL), |
399 | 14x |
dblclick = `if`(dblclicking, dblclickOpts(ns("plot_dblclick")), NULL), |
400 | 14x |
hover = `if`(hovering, hoverOpts(ns("plot_hover")), NULL) |
401 |
) |
|
402 |
) |
|
403 |
}) |
|
404 | ||
405 | 15x |
output$width_warning <- renderUI({ |
406 | 8x |
grDevices::pdf(NULL) # reset Rplots.pdf for shiny server |
407 | 8x |
w <- grDevices::dev.size("px")[1] |
408 | 8x |
grDevices::dev.off() |
409 | 8x |
if (`if`(!is.null(input$width), input$width, default_slider_width()[1]) < w) { |
410 | 8x |
helpText( |
411 | 8x |
icon("triangle-exclamation"), |
412 | 8x |
"Plot might be cut off for small widths." |
413 |
) |
|
414 |
} |
|
415 |
}) |
|
416 | ||
417 | 15x |
type_download_srv( |
418 | 15x |
id = "downbutton", |
419 | 15x |
plot_reactive = plot_r, |
420 | 15x |
plot_type = plot_type, |
421 | 15x |
plot_w = reactive(`if`(!is.null(input$width), input$width, default_slider_width()[1])), |
422 | 15x |
default_w = default_w, |
423 | 15x |
plot_h = reactive(`if`(!is.null(input$height), input$height, height[1])), |
424 | 15x |
default_h = default_h |
425 |
) |
|
426 | ||
427 | 15x |
output$plot_out_modal <- renderUI({ |
428 | 9x |
plotOutput(ns("plot_modal"), height = input$height_in_modal, width = input$width_in_modal) |
429 |
}) |
|
430 | ||
431 | 15x |
observeEvent(input$expand, { |
432 | 1x |
showModal( |
433 | 1x |
div( |
434 | 1x |
class = "plot-modal", |
435 | 1x |
modalDialog( |
436 | 1x |
easyClose = TRUE, |
437 | 1x |
div( |
438 | 1x |
class = "plot-modal-sliders", |
439 | 1x |
optionalSliderInputValMinMax( |
440 | 1x |
inputId = ns("height_in_modal"), |
441 | 1x |
label = "Plot height", |
442 | 1x |
value_min_max = round(c(`if`(!is.null(input$height), input$height, height[1]), height[2:3])), |
443 | 1x |
ticks = FALSE, |
444 | 1x |
step = 1L, |
445 | 1x |
round = TRUE |
446 |
), |
|
447 | 1x |
optionalSliderInputValMinMax( |
448 | 1x |
inputId = ns("width_in_modal"), |
449 | 1x |
label = "Plot width", |
450 | 1x |
value_min_max = round(c( |
451 | 1x |
ifelse( |
452 | 1x |
is.null(input$width) || !isFALSE(input$width_resize_switch), |
453 | 1x |
ifelse( |
454 | 1x |
is.null(input$plot_modal_width) || input$plot_modal_width > default_slider_width()[3], |
455 | 1x |
default_slider_width()[1], |
456 | 1x |
input$plot_modal_width |
457 |
), |
|
458 | 1x |
input$width |
459 |
), |
|
460 | 1x |
default_slider_width()[2:3] |
461 |
)), |
|
462 | 1x |
ticks = FALSE, |
463 | 1x |
step = 1L, |
464 | 1x |
round = TRUE |
465 |
) |
|
466 |
), |
|
467 | 1x |
div( |
468 | 1x |
class = "float-right", |
469 | 1x |
type_download_ui(ns("modal_downbutton")) |
470 |
), |
|
471 | 1x |
div( |
472 | 1x |
align = "center", |
473 | 1x |
uiOutput(ns("plot_out_modal"), class = "plot_out_container") |
474 |
) |
|
475 |
) |
|
476 |
) |
|
477 |
) |
|
478 |
}) |
|
479 | ||
480 | 15x |
type_download_srv( |
481 | 15x |
id = "modal_downbutton", |
482 | 15x |
plot_reactive = plot_r, |
483 | 15x |
plot_type = plot_type, |
484 | 15x |
plot_w = reactive(input$width_in_modal), |
485 | 15x |
default_w = default_w, |
486 | 15x |
plot_h = reactive(input$height_in_modal), |
487 | 15x |
default_h = default_h |
488 |
) |
|
489 | ||
490 | 15x |
return( |
491 | 15x |
list( |
492 | 15x |
brush = reactive({ |
493 |
# refresh brush data on the main plot size change |
|
494 | 1x |
input$height |
495 | 1x |
input$width |
496 | 1x |
input$plot_brush |
497 |
}), |
|
498 | 15x |
click = reactive({ |
499 |
# refresh click data on the main plot size change |
|
500 | 1x |
input$height |
501 | 1x |
input$width |
502 | 1x |
input$plot_click |
503 |
}), |
|
504 | 15x |
dblclick = reactive({ |
505 |
# refresh double click data on the main plot size change |
|
506 | 1x |
input$height |
507 | 1x |
input$width |
508 | 1x |
input$plot_dblclick |
509 |
}), |
|
510 | 15x |
hover = reactive({ |
511 |
# refresh hover data on the main plot size change |
|
512 | 1x |
input$height |
513 | 1x |
input$width |
514 | 1x |
input$plot_hover |
515 |
}), |
|
516 | 15x |
dim = reactive(c(input$width, input$height)) |
517 |
) |
|
518 |
) |
|
519 |
}) |
|
520 |
} |
|
521 | ||
522 |
#' @keywords internal |
|
523 |
type_download_ui <- function(id) { |
|
524 | 4x |
ns <- NS(id) |
525 | 4x |
shinyWidgets::dropdownButton( |
526 | 4x |
circle = FALSE, |
527 | 4x |
icon = icon("download"), |
528 | 4x |
inline = TRUE, |
529 | 4x |
right = TRUE, |
530 | 4x |
label = "", |
531 | 4x |
inputId = ns("downl"), |
532 | 4x |
div( |
533 | 4x |
radioButtons(ns("file_format"), |
534 | 4x |
label = "File type", |
535 | 4x |
choices = c("png" = "png", "pdf" = "pdf", "svg" = "svg"), |
536 |
), |
|
537 | 4x |
textInput(ns("file_name"), |
538 | 4x |
label = "File name (without extension)", |
539 | 4x |
value = paste0("plot_", strftime(Sys.time(), format = "%Y%m%d_%H%M%S")) |
540 |
), |
|
541 | 4x |
conditionalPanel( |
542 | 4x |
condition = paste0("input['", ns("file_name"), "'] != ''"), |
543 | 4x |
downloadButton(ns("data_download"), label = character(0), class = "btn-sm w-full") |
544 |
) |
|
545 |
) |
|
546 |
) |
|
547 |
} |
|
548 | ||
549 |
#' @keywords internal |
|
550 |
type_download_srv <- function(id, plot_reactive, plot_type, plot_w, default_w, plot_h, default_h) { |
|
551 | 34x |
moduleServer( |
552 | 34x |
id, |
553 | 34x |
function(input, output, session) { |
554 | 34x |
output$data_download <- downloadHandler( |
555 | 34x |
filename = function() { |
556 | 20x |
paste(input$file_name, input$file_format, sep = ".") |
557 |
}, |
|
558 | 34x |
content = function(file) { |
559 | 20x |
width <- `if`(!is.null(plot_w()), plot_w(), default_w()) |
560 | 20x |
height <- `if`(!is.null(plot_h()), plot_h(), default_h()) |
561 | ||
562 |
# svg and pdf have width in inches and 1 inch = get_plot_dpi() pixels |
|
563 | 20x |
switch(input$file_format, |
564 | 12x |
png = grDevices::png(file, width, height), |
565 | 4x |
pdf = grDevices::pdf(file, width / get_plot_dpi(), height / get_plot_dpi()), |
566 | 4x |
svg = grDevices::svg(file, width / get_plot_dpi(), height / get_plot_dpi()) |
567 |
) |
|
568 | ||
569 | 20x |
print_plot(plot_reactive, plot_type) |
570 | ||
571 | 20x |
grDevices::dev.off() |
572 |
} |
|
573 |
) |
|
574 |
} |
|
575 |
) |
|
576 |
} |
|
577 | ||
578 |
#' Cleans and organizes output to account for NAs and remove empty rows. |
|
579 |
#' |
|
580 |
#' @description `r lifecycle::badge("stable")` |
|
581 |
#' @param data (`data.frame`)\cr |
|
582 |
#' A dataframe from which to select rows. |
|
583 |
#' @param brush (`list`)\cr |
|
584 |
#' The data from a brush e.g. input$plot_brush. |
|
585 |
#' |
|
586 |
#' @return A dataframe of selected rows. |
|
587 |
#' @export |
|
588 |
#' |
|
589 |
clean_brushedPoints <- function(data, brush) { # nolintr |
|
590 | 6x |
checkmate::assert_data_frame(data) |
591 | 4x |
checkmate::assert_list(brush, null.ok = TRUE) |
592 | ||
593 |
# define original panelvar1 and panelvar2 before getting overwritten |
|
594 | 4x |
original_panelvar1 <- brush$mapping$panelvar1 |
595 | 4x |
original_panelvar2 <- brush$mapping$panelvar2 |
596 | ||
597 |
# Assign NULL to `mapping$panelvar1` and `mapping$panelvar1` if `brush$panelvar1` and `brush$panelvar1` are NULL |
|
598 |
# This will not evaluate the `panelMatch` step in `brushedPoints` and thus will return a non empty dataframe |
|
599 | 4x |
if (is.null(brush$panelvar1)) brush$mapping$panelvar1 <- NULL |
600 | 4x |
if (is.null(brush$panelvar2)) brush$mapping$panelvar2 <- NULL |
601 | ||
602 | 4x |
bp_df <- brushedPoints(data, brush) |
603 | ||
604 |
# Keep required rows only based on the value of `brush$panelvar1` |
|
605 | 3x |
df <- if (is.null(brush$panelvar1) && is.character(original_panelvar1) && |
606 | 3x |
is.null(brush$panelvar2) && is.character(original_panelvar2)) { |
607 | ! |
df_var1 <- bp_df[is.na(bp_df[[original_panelvar1]]), ] |
608 | ! |
df_var1[is.na(df_var1[[original_panelvar2]]), ] |
609 | 3x |
} else if (is.null(brush$panelvar1) && is.character(original_panelvar1)) { |
610 | ! |
bp_df[is.na(bp_df[[original_panelvar1]]), ] |
611 | 3x |
} else if (is.null(brush$panelvar2) && is.character(original_panelvar2)) { |
612 | ! |
bp_df[is.na(bp_df[[original_panelvar2]]), ] |
613 |
} else { |
|
614 | 3x |
bp_df |
615 |
} |
|
616 | ||
617 |
# filter out rows that are only NAs |
|
618 | 3x |
df <- df[rowSums(is.na(df)) != ncol(df), ] |
619 | 3x |
df |
620 |
} |
|
621 | ||
622 |
#' @keywords internal |
|
623 |
#' |
|
624 |
get_plot_dpi <- function() { |
|
625 | 55x |
default_dpi <- 72 |
626 | 55x |
dpi <- getOption("teal.plot_dpi", default_dpi) |
627 | 55x |
if (!checkmate::test_integerish(dpi, lower = 24, any.missing = FALSE, len = 1)) { |
628 | 4x |
warning(paste("Invalid value for option 'teal.plot_dpi', therefore defaulting to", default_dpi, "dpi")) |
629 | 4x |
dpi <- default_dpi |
630 |
} |
|
631 | 55x |
dpi |
632 |
} |
|
633 | ||
634 |
#' Print plot for download functionality |
|
635 |
#' |
|
636 |
#' @param plot (`reactive`)\cr |
|
637 |
#' reactive expression to draw a plot |
|
638 |
#' @param plot_type (`reactive`)\cr |
|
639 |
#' reactive plot type (`gg`, `trel`, `grob`, `other`) |
|
640 |
#' |
|
641 |
#' @return Nothing returned, the plot is printed. |
|
642 |
#' @keywords internal |
|
643 |
#' |
|
644 |
print_plot <- function(plot, plot_type) { |
|
645 | 26x |
switch(plot_type(), |
646 | 2x |
"grob" = grid::grid.draw(plot()), |
647 |
"other" = { |
|
648 | 2x |
graphics::plot.new() |
649 | 2x |
graphics::text( |
650 | 2x |
x = graphics::grconvertX(0.5, from = "npc"), |
651 | 2x |
y = graphics::grconvertY(0.5, from = "npc"), |
652 | 2x |
labels = "This plot graphic type is not yet supported to download" |
653 |
) |
|
654 |
}, |
|
655 | 18x |
"base" = plot(), |
656 | 4x |
print(plot()) |
657 |
) |
|
658 |
} |
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 | 14x |
if (plot_type == "gg" && dblclicking) { |
25 | 2x |
plot_obj + |
26 | 2x |
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 |
} |
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 |
#' teal option variable. The `NULL` value is supported. |
|
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 |
#' @name table_with_settings |
|
2 |
#' |
|
3 |
#' @title table_with_settings module |
|
4 |
#' |
|
5 |
#' @description `r lifecycle::badge("stable")` |
|
6 |
#' @inheritParams shiny::moduleServer |
|
7 |
#' @param ... (`character`)\cr |
|
8 |
#' Useful for providing additional HTML classes for the output tag. |
|
9 |
#' |
|
10 |
#' @rdname table_with_settings |
|
11 |
#' @export |
|
12 |
#' |
|
13 |
table_with_settings_ui <- function(id, ...) { |
|
14 | 1x |
checkmate::assert_string(id) |
15 | ||
16 | 1x |
ns <- NS(id) |
17 | ||
18 | 1x |
tagList( |
19 | 1x |
include_css_files("table_with_settings"), |
20 | 1x |
tags$div( |
21 | 1x |
id = ns("table-with-settings"), |
22 | 1x |
tags$div( |
23 | 1x |
class = "table-settings-buttons", |
24 | 1x |
type_download_ui_table(ns("downbutton")), |
25 | 1x |
actionButton( |
26 | 1x |
inputId = ns("expand"), label = character(0), |
27 | 1x |
icon = icon("up-right-and-down-left-from-center"), class = "btn-sm" |
28 |
), |
|
29 |
), |
|
30 | 1x |
tags$div( |
31 | 1x |
class = "table-settings-table", |
32 | 1x |
uiOutput(ns("table_out_main"), width = "100%", ...) |
33 |
) |
|
34 |
) |
|
35 |
) |
|
36 |
} |
|
37 | ||
38 |
#' @inheritParams shiny::moduleServer |
|
39 |
#' @param table_r (`reactive`)\cr |
|
40 |
#' reactive expression that yields an `rtable` object (`ElementaryTable` or `TableTree`) |
|
41 |
#' @param show_hide_signal (`reactive logical`, optional)\cr |
|
42 |
#' a mechanism to allow modules which call this module to show/hide the table_with_settings UI. |
|
43 |
#' |
|
44 |
#' @rdname table_with_settings |
|
45 |
#' @export |
|
46 |
#' |
|
47 |
#' @examples |
|
48 |
#' \dontrun{ |
|
49 |
#' library(shiny) |
|
50 |
#' library(rtables) |
|
51 |
#' library(magrittr) |
|
52 |
#' shinyApp( |
|
53 |
#' ui = fluidPage( |
|
54 |
#' table_with_settings_ui( |
|
55 |
#' id = "table_with_settings" |
|
56 |
#' ) |
|
57 |
#' ), |
|
58 |
#' server = function(input, output, session) { |
|
59 |
#' table_r <- reactive({ |
|
60 |
#' l <- basic_table() %>% |
|
61 |
#' split_cols_by("ARM") %>% |
|
62 |
#' analyze(c("SEX", "AGE")) |
|
63 |
#' |
|
64 |
#' tbl <- build_table(l, DM) |
|
65 |
#' |
|
66 |
#' tbl |
|
67 |
#' }) |
|
68 |
#' |
|
69 |
#' table_with_settings_srv(id = "table_with_settings", table_r = table_r) |
|
70 |
#' } |
|
71 |
#' ) |
|
72 |
#' } |
|
73 |
#' |
|
74 |
table_with_settings_srv <- function(id, table_r, show_hide_signal = reactive(TRUE)) { |
|
75 | 6x |
checkmate::assert_class(table_r, c("reactive", "function")) |
76 | 5x |
checkmate::assert_class(show_hide_signal, c("reactive", "function")) |
77 | ||
78 | 4x |
if (!requireNamespace("rtables", quietly = TRUE)) { |
79 | ! |
stop("package rtables is required, please install") |
80 |
} |
|
81 | ||
82 | 4x |
moduleServer(id, function(input, output, session) { |
83 | 4x |
ns <- session$ns |
84 |
# Turn on and off the UI |
|
85 | 4x |
observeEvent(show_hide_signal(), { |
86 | 4x |
if (show_hide_signal()) { |
87 | 3x |
shinyjs::show("table-with-settings") |
88 |
} else { |
|
89 | 1x |
shinyjs::hide("table-with-settings") |
90 |
} |
|
91 |
}) |
|
92 | ||
93 | 4x |
output$table_out_main <- output$table_out_modal <- renderUI({ |
94 | 8x |
rtables::as_html(table_r()) |
95 |
}) |
|
96 | ||
97 | 4x |
type_download_srv_table( |
98 | 4x |
id = "downbutton", |
99 | 4x |
table_reactive = table_r |
100 |
) |
|
101 | ||
102 | 4x |
observeEvent(input$expand, { |
103 | 2x |
showModal( |
104 | 2x |
div( |
105 | 2x |
class = "table-modal", |
106 | 2x |
modalDialog( |
107 | 2x |
easyClose = TRUE, |
108 | 2x |
div( |
109 | 2x |
class = "float-right", |
110 | 2x |
type_download_ui_table(ns("modal_downbutton")) |
111 |
), |
|
112 | 2x |
uiOutput(ns("table_out_modal"), class = "table_out_container") |
113 |
) |
|
114 |
) |
|
115 |
) |
|
116 |
}) |
|
117 | ||
118 | 4x |
type_download_srv_table( |
119 | 4x |
id = "modal_downbutton", |
120 | 4x |
table_reactive = table_r |
121 |
) |
|
122 |
}) |
|
123 |
} |
|
124 | ||
125 |
type_download_ui_table <- function(id) { |
|
126 | 3x |
ns <- NS(id) |
127 | 3x |
shinyWidgets::dropdownButton( |
128 | 3x |
circle = FALSE, |
129 | 3x |
icon = icon("download"), |
130 | 3x |
inline = TRUE, |
131 | 3x |
right = TRUE, |
132 | 3x |
label = "", |
133 | 3x |
inputId = ns("dwnl"), |
134 | 3x |
div( |
135 | 3x |
class = "modal-download-ui-table-container", |
136 | 3x |
radioButtons(ns("file_format"), |
137 | 3x |
label = "File type", |
138 | 3x |
choices = c("formatted txt" = ".txt", "csv" = ".csv", "pdf" = ".pdf"), |
139 |
), |
|
140 | 3x |
textInput(ns("file_name"), |
141 | 3x |
label = "File name (without extension)", |
142 | 3x |
value = paste0("table_", strftime(Sys.time(), format = "%Y%m%d_%H%M%S")) |
143 |
), |
|
144 | 3x |
conditionalPanel( |
145 | 3x |
condition = paste0("input['", ns("file_format"), "'] != '.csv'"), |
146 | 3x |
div( |
147 | 3x |
class = "lock-btn", |
148 | 3x |
title = "on / off", |
149 | 3x |
shinyWidgets::prettyToggle( |
150 | 3x |
ns("pagination_switch"), |
151 | 3x |
value = FALSE, |
152 | 3x |
label_on = NULL, |
153 | 3x |
label_off = NULL, |
154 | 3x |
status_on = "default", |
155 | 3x |
status_off = "default", |
156 | 3x |
outline = FALSE, |
157 | 3x |
plain = TRUE, |
158 | 3x |
icon_on = icon("fas fa-toggle-off"), |
159 | 3x |
icon_off = icon("fas fa-toggle-on"), |
160 | 3x |
animation = "pulse" |
161 |
) |
|
162 |
), |
|
163 | 3x |
div( |
164 | 3x |
class = "paginate-ui", |
165 | 3x |
shinyWidgets::numericInputIcon( |
166 | 3x |
inputId = ns("lpp"), |
167 | 3x |
label = "Paginate table:", |
168 | 3x |
value = 70, |
169 | 3x |
icon = list("lines / page") |
170 |
), |
|
171 | 3x |
uiOutput(ns("lpp_warning")) |
172 |
) |
|
173 |
), |
|
174 | 3x |
conditionalPanel( |
175 | 3x |
condition = paste0("input['", ns("file_name"), "'] != ''"), |
176 | 3x |
downloadButton(ns("data_download"), label = character(0), class = "btn-sm w-full") |
177 |
) |
|
178 |
) |
|
179 |
) |
|
180 |
} |
|
181 | ||
182 |
type_download_srv_table <- function(id, table_reactive) { |
|
183 | 14x |
moduleServer( |
184 | 14x |
id, |
185 | 14x |
function(input, output, session) { |
186 | 14x |
observeEvent(input$pagination_switch, { |
187 | 14x |
if (input$pagination_switch) { |
188 | 6x |
shinyjs::enable("lpp") |
189 |
} else { |
|
190 | 8x |
shinyjs::disable("lpp") |
191 |
} |
|
192 |
}) |
|
193 | ||
194 | 14x |
output$lpp_warning <- renderUI({ |
195 | 30x |
catch_warning <- if (input$file_format != ".csv" && input$pagination_switch) { |
196 | 6x |
try(rtables::paginate_table( |
197 | 6x |
tt = table_reactive(), |
198 | 6x |
lpp = as.numeric(input$lpp) |
199 |
)) |
|
200 |
} |
|
201 | ||
202 | 20x |
if (inherits(catch_warning, "try-error")) { |
203 | 1x |
helpText( |
204 | 1x |
class = "error", |
205 | 1x |
icon("triangle-exclamation"), |
206 | 1x |
"Maximum lines per page includes the reprinted header. Please enter a numeric value or increase the value." |
207 |
) |
|
208 |
} |
|
209 |
}) |
|
210 | ||
211 | 14x |
output$data_download <- downloadHandler( |
212 | 14x |
filename = function() { |
213 | 21x |
paste0(input$file_name, input$file_format) |
214 |
}, |
|
215 | 14x |
content = function(file) { |
216 | 21x |
if (input$file_format == ".txt") { |
217 | 8x |
rtables::export_as_txt( |
218 | 8x |
x = table_reactive(), |
219 | 8x |
file = file, |
220 | 8x |
paginate = input$pagination_switch, |
221 | 8x |
lpp = if (input$pagination_switch) as.numeric(input$lpp) |
222 |
) |
|
223 | 13x |
} else if (input$file_format == ".csv") { |
224 | 7x |
result <- rtables::matrix_form(table_reactive())$strings |
225 | 7x |
utils::write.table( |
226 | 7x |
x = result, |
227 | 7x |
file = file, |
228 | 7x |
sep = ",", |
229 | 7x |
col.names = FALSE, |
230 | 7x |
row.names = TRUE, |
231 | 7x |
append = FALSE |
232 |
) |
|
233 |
} else { |
|
234 | 6x |
rtables::export_as_pdf( |
235 | 6x |
tt = table_reactive(), |
236 | 6x |
file = file, |
237 | 6x |
paginate = input$pagination_switch, |
238 | 6x |
lpp = if (input$pagination_switch) as.numeric(input$lpp) |
239 |
) |
|
240 |
} |
|
241 |
} |
|
242 |
) |
|
243 |
} |
|
244 |
) |
|
245 |
} |
1 |
#' Include `CSS` files from `/inst/css/` package directory to application header |
|
2 |
#' |
|
3 |
#' `system.file` should not be used to access files in other packages, it does |
|
4 |
#' not work with `devtools`. Therefore, we redefine this method in each package |
|
5 |
#' as needed. Thus, we do not export this method |
|
6 |
#' |
|
7 |
#' @param pattern (`character`) pattern of files to be included |
|
8 |
#' |
|
9 |
#' @return HTML code that includes `CSS` files |
|
10 |
#' @keywords internal |
|
11 |
include_css_files <- function(pattern = "*") { |
|
12 | 3x |
css_files <- list.files( |
13 | 3x |
system.file("css", package = "teal.widgets", mustWork = TRUE), |
14 | 3x |
pattern = pattern, full.names = TRUE |
15 |
) |
|
16 | 3x |
if (length(css_files) == 0) { |
17 | ! |
return(NULL) |
18 |
} |
|
19 | 3x |
return(shiny::singleton(shiny::tags$head(lapply(css_files, shiny::includeCSS)))) |
20 |
} |
1 |
#' Maps the `lengthMenu`selected value property of `DT::datatable` to a Shiny variable. |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("stable")` |
|
4 |
#' @param dt_name \code{ns()} of `inputId` of the `DT::datatable` |
|
5 |
#' @param dt_rows \code{ns()} of `inputId` of the variable that holds the current selected value of `lengthMenu` |
|
6 |
#' |
|
7 |
#' @name get_dt_rows |
|
8 |
#' |
|
9 |
#' @examples |
|
10 |
#' \dontrun{ |
|
11 |
#' library(shiny) |
|
12 |
#' # in Shiny UI function |
|
13 |
#' tagList( |
|
14 |
#' get_dt_rows(ns("data_table"), ns("dt_rows")), |
|
15 |
#' ... |
|
16 |
#' ) |
|
17 |
#' |
|
18 |
#' # use the input$dt_rows in the Shiny Server function |
|
19 |
#' if (!is.null(input$dt_rows)) { |
|
20 |
#' dt_args$options$pageLength <- input$dt_rows |
|
21 |
#' } # nolint |
|
22 |
#' do.call(DT::datatable, dt_args) |
|
23 |
#' } |
|
24 |
#' |
|
25 |
#' @export |
|
26 |
get_dt_rows <- function(dt_name, dt_rows) { |
|
27 | ! |
tags$head( |
28 | ! |
tags$script( |
29 | ! |
sprintf( |
30 | ! |
"$(document).ready(function() { |
31 | ! |
$('%s').on('length.dt', function(e, settings, len) { |
32 | ! |
Shiny.onInputChange('%s', len); |
33 |
}); |
|
34 |
});", |
|
35 | ! |
paste0("#", dt_name), |
36 | ! |
dt_rows |
37 |
) |
|
38 |
) |
|
39 |
) |
|
40 |
} |
1 |
#' Create a standard UI layout with output on the right and an encoding panel on |
|
2 |
#' the left |
|
3 |
#' |
|
4 |
#' @description `r lifecycle::badge("stable")` |
|
5 |
#' This is the layout used for the teal modules in the `beam` package |
|
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 \code{shiny.tag} with the UI code. |
|
21 |
#' |
|
22 |
#' @export |
|
23 |
standard_layout <- function(output, |
|
24 |
encoding = NULL, |
|
25 |
forms = NULL, |
|
26 |
pre_output = NULL, |
|
27 |
post_output = NULL) { |
|
28 |
# checking arguments |
|
29 | 11x |
if (!inherits(output, c("shiny.tag", "shiny.tag.list", "html"))) { |
30 | 2x |
stop("output is supposed to be of class shiny.tag or shiny.tag.list") |
31 |
} |
|
32 | 9x |
for (name in names(l <- list( |
33 | 9x |
"encoding" = encoding, |
34 | 9x |
"pre_output" = pre_output, |
35 | 9x |
"post_output" = post_output |
36 |
))) { |
|
37 | 24x |
if (!is.null(l[[name]]) && !inherits(l[[name]], c("shiny.tag", "shiny.tag.list", "html"))) { |
38 | 3x |
stop(paste(name, "is supposed to be NULL, shiny.tag or shiny.tag.list.")) |
39 |
} |
|
40 |
} |
|
41 | ||
42 |
# if encoding=NULL then forms is placed below output |
|
43 | ||
44 | 6x |
tag_output <- div( |
45 | 6x |
class = "well", |
46 | 6x |
div(class = "pre-output", pre_output), |
47 | 6x |
div(class = "output", output), |
48 | 6x |
div(class = "post-output", post_output) |
49 |
) |
|
50 | ||
51 | 6x |
tag_enc_out <- if (!is.null(encoding)) { |
52 | 2x |
tagList( |
53 | 2x |
div( |
54 | 2x |
class = "col-md-3", |
55 | 2x |
div(class = "well", encoding), |
56 | 2x |
if (is.null(forms)) { |
57 | 1x |
NULL |
58 |
} else { |
|
59 | 1x |
div(class = "form-group", forms) |
60 |
} |
|
61 |
), |
|
62 | 2x |
div(class = "col-md-9", tag_output) |
63 |
) |
|
64 |
} else { |
|
65 | 4x |
div( |
66 | 4x |
class = "col-md-12", |
67 | 4x |
tag_output, |
68 | 4x |
if (is.null(forms)) { |
69 | 3x |
NULL |
70 |
} else { |
|
71 | 1x |
div(class = "form-group", forms) |
72 |
} |
|
73 |
) |
|
74 |
} |
|
75 | ||
76 | 6x |
fluidRow(tag_enc_out) |
77 |
} |
1 |
#' Adds Class Small Well and overflow-x property to HTML output element |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("stable")` |
|
4 |
#' @param ... other arguments to pass to tag object's div attributes. |
|
5 |
#' |
|
6 |
#' @details `white_small_well` is intended to be used with [shiny::uiOutput()]. |
|
7 |
#' The overflow-x property is set to auto so that a scroll bar is added |
|
8 |
#' when the content overflows at the left and right edges of the output window. |
|
9 |
#' For example, this is useful for displaying wide tables. |
|
10 |
#' |
|
11 |
#' @return An HTML output element with class Small Well and overflow-x property |
|
12 |
#' @export |
|
13 |
#' |
|
14 |
#' @examples |
|
15 |
#' |
|
16 |
#' white_small_well(shiny::htmlOutput("summary")) |
|
17 |
white_small_well <- function(...) { |
|
18 | ! |
shiny::tagList( |
19 | ! |
include_css_files(pattern = "custom"), |
20 | ! |
tags$div( |
21 | ! |
class = "well well-sm bg-white", |
22 |
... |
|
23 |
) |
|
24 |
) |
|
25 |
} |