| 1 |
#' Enable/disable busy indication |
| 2 |
#' |
| 3 |
#' Busy indicators provide a visual cue to users when the server is busy |
| 4 |
#' calculating outputs or otherwise performing tasks (e.g., producing |
| 5 |
#' downloads). When enabled, a spinner is shown on each |
| 6 |
#' calculating/recalculating output, and a pulsing banner is shown at the top of |
| 7 |
#' the page when the app is otherwise busy. Busy indication is enabled by |
| 8 |
#' default for UI created with \pkg{bslib}, but must be enabled otherwise. To |
| 9 |
#' enable/disable, include the result of this function in anywhere in the app's |
| 10 |
#' UI. |
| 11 |
#' |
| 12 |
#' When both `spinners` and `pulse` are set to `TRUE`, the pulse is |
| 13 |
#' automatically disabled when spinner(s) are active. When both `spinners` and |
| 14 |
#' `pulse` are set to `FALSE`, no busy indication is shown (other than the |
| 15 |
#' graying out of recalculating outputs). |
| 16 |
#' |
| 17 |
#' @param ... Currently ignored. |
| 18 |
#' @param spinners Whether to show a spinner on each calculating/recalculating |
| 19 |
#' output. |
| 20 |
#' @param pulse Whether to show a pulsing banner at the top of the page when the |
| 21 |
#' app is busy. |
| 22 |
#' @param fade Whether to fade recalculating outputs. A value of `FALSE` is |
| 23 |
#' equivalent to `busyIndicatorOptions(fade_opacity=1)`. |
| 24 |
#' |
| 25 |
#' @export |
| 26 |
#' @seealso [busyIndicatorOptions()] for customizing the appearance of the busy |
| 27 |
#' indicators. |
| 28 |
#' @examplesIf rlang::is_interactive() |
| 29 |
#' |
| 30 |
#' library(bslib) |
| 31 |
#' |
| 32 |
#' ui <- page_fillable( |
| 33 |
#' useBusyIndicators(), |
| 34 |
#' card( |
| 35 |
#' card_header( |
| 36 |
#' "A plot", |
| 37 |
#' input_task_button("simulate", "Simulate"), |
| 38 |
#' class = "d-flex justify-content-between align-items-center" |
| 39 |
#' ), |
| 40 |
#' plotOutput("p"), |
| 41 |
#' ) |
| 42 |
#' ) |
| 43 |
#' |
| 44 |
#' server <- function(input, output) { |
| 45 |
#' output$p <- renderPlot({ |
| 46 |
#' input$simulate |
| 47 |
#' Sys.sleep(4) |
| 48 |
#' plot(x = rnorm(100), y = rnorm(100)) |
| 49 |
#' }) |
| 50 |
#' } |
| 51 |
#' |
| 52 |
#' shinyApp(ui, server) |
| 53 |
useBusyIndicators <- function(..., spinners = TRUE, pulse = TRUE, fade = TRUE) { |
| 54 |
|
| 55 |
rlang::check_dots_empty() |
| 56 |
|
| 57 |
attrs <- list("shinyBusySpinners" = spinners, "shinyBusyPulse" = pulse) |
| 58 |
|
| 59 |
js <- vapply(names(attrs), character(1), FUN = function(key) { |
| 60 |
if (attrs[[key]]) { |
| 61 |
sprintf("document.documentElement.dataset.%s = 'true';", key) |
| 62 |
} else { |
| 63 |
sprintf("delete document.documentElement.dataset.%s;", key) |
| 64 |
} |
| 65 |
}) |
| 66 |
|
| 67 |
# TODO: it'd be nice if htmltools had something like a page_attrs() that allowed us |
| 68 |
# to do this without needing to inject JS into the head. |
| 69 |
res <- tags$script(HTML(paste(js, collapse = "\n"))) |
| 70 |
|
| 71 |
if (!fade) { |
| 72 |
res <- tagList(res, fadeOptions(opacity = 1)) |
| 73 |
} |
| 74 |
|
| 75 |
res |
| 76 |
} |
| 77 |
|
| 78 |
#' Customize busy indicator options |
| 79 |
#' |
| 80 |
#' @description |
| 81 |
#' Shiny automatically includes busy indicators, which more specifically means: |
| 82 |
#' 1. Calculating/recalculating outputs have a spinner overlay. |
| 83 |
#' 2. Outputs fade out/in when recalculating. |
| 84 |
#' 3. When no outputs are calculating/recalculating, but Shiny is busy |
| 85 |
#' doing something else (e.g., a download, side-effect, etc), a page-level |
| 86 |
#' pulsing banner is shown. |
| 87 |
#' |
| 88 |
#' This function allows you to customize the appearance of these busy indicators |
| 89 |
#' by including the result of this function inside the app's UI. Note that, |
| 90 |
#' unless `spinner_selector` (or `fade_selector`) is specified, the spinner/fade |
| 91 |
#' customization applies to the parent element. If the customization should |
| 92 |
#' instead apply to the entire page, set `spinner_selector = 'html'` and |
| 93 |
#' `fade_selector = 'html'`. |
| 94 |
#' |
| 95 |
#' @param ... Currently ignored. |
| 96 |
#' @param spinner_type The type of spinner. Pre-bundled types include: |
| 97 |
#' '`r paste0(.busySpinnerTypes, collapse = "', '")`'. |
| 98 |
#' |
| 99 |
#' A path to a local SVG file can also be provided. The SVG should adhere to |
| 100 |
#' the following rules: |
| 101 |
#' * The SVG itself should contain the animation. |
| 102 |
#' * It should avoid absolute sizes (the spinner's containing DOM element |
| 103 |
#' size is set in CSS by `spinner_size`, so it should fill that container). |
| 104 |
#' * It should avoid setting absolute colors (the spinner's containing DOM element |
| 105 |
#' color is set in CSS by `spinner_color`, so it should inherit that color). |
| 106 |
#' @param spinner_color The color of the spinner. This can be any valid CSS |
| 107 |
#' color. Defaults to the app's "primary" color if Bootstrap is on the page. |
| 108 |
#' @param spinner_size The size of the spinner. This can be any valid CSS size. |
| 109 |
#' @param spinner_delay The amount of time to wait before showing the spinner. |
| 110 |
#' This can be any valid CSS time and can be useful for not showing the spinner |
| 111 |
#' if the computation finishes quickly. |
| 112 |
#' @param spinner_selector A character string containing a CSS selector for |
| 113 |
#' scoping the spinner customization. The default (`NULL`) will apply the |
| 114 |
#' spinner customization to the parent element of the spinner. |
| 115 |
#' @param fade_opacity The opacity (a number between 0 and 1) for recalculating |
| 116 |
#' output. Set to 1 to "disable" the fade. |
| 117 |
#' @param fade_selector A character string containing a CSS selector for |
| 118 |
#' scoping the spinner customization. The default (`NULL`) will apply the |
| 119 |
#' spinner customization to the parent element of the spinner. |
| 120 |
#' @param pulse_background A CSS background definition for the pulse. The |
| 121 |
#' default uses a |
| 122 |
#' [linear-gradient](https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/linear-gradient) |
| 123 |
#' of the theme's indigo, purple, and pink colors. |
| 124 |
#' @param pulse_height The height of the pulsing banner. This can be any valid |
| 125 |
#' CSS size. |
| 126 |
#' @param pulse_speed The speed of the pulsing banner. This can be any valid CSS |
| 127 |
#' time. |
| 128 |
#' |
| 129 |
#' @export |
| 130 |
#' @seealso [useBusyIndicators()] to disable/enable busy indicators. |
| 131 |
#' @examplesIf rlang::is_interactive() |
| 132 |
#' |
| 133 |
#' library(bslib) |
| 134 |
#' |
| 135 |
#' card_ui <- function(id, spinner_type = id) { |
| 136 |
#' card( |
| 137 |
#' busyIndicatorOptions(spinner_type = spinner_type), |
| 138 |
#' card_header(paste("Spinner:", spinner_type)), |
| 139 |
#' plotOutput(shiny::NS(id, "plot")) |
| 140 |
#' ) |
| 141 |
#' } |
| 142 |
#' |
| 143 |
#' card_server <- function(id, simulate = reactive()) { |
| 144 |
#' moduleServer( |
| 145 |
#' id = id, |
| 146 |
#' function(input, output, session) { |
| 147 |
#' output$plot <- renderPlot({ |
| 148 |
#' Sys.sleep(1) |
| 149 |
#' simulate() |
| 150 |
#' plot(x = rnorm(100), y = rnorm(100)) |
| 151 |
#' }) |
| 152 |
#' } |
| 153 |
#' ) |
| 154 |
#' } |
| 155 |
#' |
| 156 |
#' ui <- page_fillable( |
| 157 |
#' useBusyIndicators(), |
| 158 |
#' input_task_button("simulate", "Simulate", icon = icon("refresh")), |
| 159 |
#' layout_columns( |
| 160 |
#' card_ui("ring"), |
| 161 |
#' card_ui("bars"), |
| 162 |
#' card_ui("dots"), |
| 163 |
#' card_ui("pulse"), |
| 164 |
#' col_widths = 6 |
| 165 |
#' ) |
| 166 |
#' ) |
| 167 |
#' |
| 168 |
#' server <- function(input, output, session) { |
| 169 |
#' simulate <- reactive(input$simulate) |
| 170 |
#' card_server("ring", simulate) |
| 171 |
#' card_server("bars", simulate) |
| 172 |
#' card_server("dots", simulate) |
| 173 |
#' card_server("pulse", simulate) |
| 174 |
#' } |
| 175 |
#' |
| 176 |
#' shinyApp(ui, server) |
| 177 |
#' |
| 178 |
busyIndicatorOptions <- function( |
| 179 |
..., |
| 180 |
spinner_type = NULL, |
| 181 |
spinner_color = NULL, |
| 182 |
spinner_size = NULL, |
| 183 |
spinner_delay = NULL, |
| 184 |
spinner_selector = NULL, |
| 185 |
fade_opacity = NULL, |
| 186 |
fade_selector = NULL, |
| 187 |
pulse_background = NULL, |
| 188 |
pulse_height = NULL, |
| 189 |
pulse_speed = NULL |
| 190 |
) { |
| 191 |
|
| 192 |
rlang::check_dots_empty() |
| 193 |
|
| 194 |
res <- tagList( |
| 195 |
spinnerOptions( |
| 196 |
type = spinner_type, |
| 197 |
color = spinner_color, |
| 198 |
size = spinner_size, |
| 199 |
delay = spinner_delay, |
| 200 |
selector = spinner_selector |
| 201 |
), |
| 202 |
fadeOptions(opacity = fade_opacity, selector = fade_selector), |
| 203 |
pulseOptions( |
| 204 |
background = pulse_background, |
| 205 |
height = pulse_height, |
| 206 |
speed = pulse_speed |
| 207 |
) |
| 208 |
) |
| 209 |
|
| 210 |
bslib::as.card_item(dropNulls(res)) |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
spinnerOptions <- function(type = NULL, color = NULL, size = NULL, delay = NULL, selector = NULL) { |
| 215 |
if (is.null(type) && is.null(color) && is.null(size) && is.null(delay) && is.null(selector)) { |
| 216 |
return(NULL) |
| 217 |
} |
| 218 |
|
| 219 |
url <- NULL |
| 220 |
if (!is.null(type)) { |
| 221 |
stopifnot(is.character(type) && length(type) == 1) |
| 222 |
if (file.exists(type) && grepl("\\.svg$", type)) { |
| 223 |
typeRaw <- readBin(type, "raw", n = file.info(type)$size) |
| 224 |
url <- sprintf("url('data:image/svg+xml;base64,%s')", rawToBase64(typeRaw)) |
| 225 |
} else { |
| 226 |
type <- rlang::arg_match(type, .busySpinnerTypes) |
| 227 |
url <- sprintf("url('spinners/%s.svg')", type) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
# Options controlled via CSS variables. |
| 232 |
css_vars <- htmltools::css( |
| 233 |
"--shiny-spinner-url" = url, |
| 234 |
"--shiny-spinner-color" = htmltools::parseCssColors(color), |
| 235 |
"--shiny-spinner-size" = htmltools::validateCssUnit(size), |
| 236 |
"--shiny-spinner-delay" = delay |
| 237 |
) |
| 238 |
|
| 239 |
id <- NULL |
| 240 |
if (is.null(selector)) { |
| 241 |
id <- paste0("spinner-options-", p_randomInt(100, 1000000)) |
| 242 |
selector <- sprintf(":has(> #%s)", id) |
| 243 |
} |
| 244 |
|
| 245 |
css <- HTML(paste0(selector, " {", css_vars, "}")) |
| 246 |
|
| 247 |
tags$style(css, id = id) |
| 248 |
} |
| 249 |
|
| 250 |
fadeOptions <- function(opacity = NULL, selector = NULL) { |
| 251 |
if (is.null(opacity) && is.null(selector)) { |
| 252 |
return(NULL) |
| 253 |
} |
| 254 |
|
| 255 |
css_vars <- htmltools::css( |
| 256 |
"--shiny-fade-opacity" = opacity |
| 257 |
) |
| 258 |
|
| 259 |
id <- NULL |
| 260 |
if (is.null(selector)) { |
| 261 |
id <- paste0("fade-options-", p_randomInt(100, 1000000)) |
| 262 |
selector <- sprintf(":has(> #%s)", id) |
| 263 |
} |
| 264 |
|
| 265 |
css <- HTML(paste0(selector, " {", css_vars, "}")) |
| 266 |
|
| 267 |
tags$style(css, id = id) |
| 268 |
} |
| 269 |
|
| 270 |
pulseOptions <- function(background = NULL, height = NULL, speed = NULL) { |
| 271 |
if (is.null(background) && is.null(height) && is.null(speed)) { |
| 272 |
return(NULL) |
| 273 |
} |
| 274 |
|
| 275 |
css_vars <- htmltools::css( |
| 276 |
"--shiny-pulse-background" = background, |
| 277 |
"--shiny-pulse-height" = htmltools::validateCssUnit(height), |
| 278 |
"--shiny-pulse-speed" = speed |
| 279 |
) |
| 280 |
|
| 281 |
tags$style(HTML(paste0(":root {", css_vars, "}"))) |
| 282 |
} |
| 283 |
|
| 284 |
busyIndicatorDependency <- function() { |
| 285 |
htmlDependency( |
| 286 |
name = "shiny-busy-indicators", |
| 287 |
version = get_package_version("shiny"), |
| 288 |
src = "www/shared/busy-indicators", |
| 289 |
package = "shiny", |
| 290 |
stylesheet = "busy-indicators.css", |
| 291 |
# TODO-future: In next release make spinners and pulse opt-out |
| 292 |
# head = as.character(useBusyIndicators()) |
| 293 |
) |
| 294 |
} |