-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
405 additions
and
150 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# https://github.com/daattali/advanced-shiny/tree/master/busy-indicator | ||
# Copyright 2016 Dean Attali. Licensed under the MIT license. | ||
|
||
# All the code in this file needs to be copied to your Shiny app, and you need | ||
# to call `withBusyIndicatorUI()` and `withBusyIndicatorServer()` in your app. | ||
# You can also include the `appCSS` in your UI, as the example app shows. | ||
|
||
# ============================================= | ||
|
||
# Set up a button to have an animated loading indicator and a checkmark | ||
# for better user experience | ||
# Need to use with the corresponding `withBusyIndicator` server function | ||
withBusyIndicatorUI <- function(button) { | ||
id <- button[['attribs']][['id']] | ||
div( | ||
`data-for-btn` = id, | ||
button, | ||
span( | ||
class = "btn-loading-container", | ||
hidden( | ||
img(src = "ajax-loader-bar.gif", class = "btn-loading-indicator"), | ||
icon("check", class = "btn-done-indicator") | ||
) | ||
), | ||
hidden( | ||
div(class = "btn-err", | ||
div(icon("exclamation-circle"), | ||
tags$b("Error: "), | ||
span(class = "btn-err-msg") | ||
) | ||
) | ||
) | ||
) | ||
} | ||
|
||
# Call this function from the server with the button id that is clicked and the | ||
# expression to run when the button is clicked | ||
withBusyIndicatorServer <- function(buttonId, expr) { | ||
# UX stuff: show the "busy" message, hide the other messages, disable the button | ||
loadingEl <- sprintf("[data-for-btn=%s] .btn-loading-indicator", buttonId) | ||
doneEl <- sprintf("[data-for-btn=%s] .btn-done-indicator", buttonId) | ||
errEl <- sprintf("[data-for-btn=%s] .btn-err", buttonId) | ||
disable(buttonId) | ||
show(selector = loadingEl) | ||
hide(selector = doneEl) | ||
hide(selector = errEl) | ||
on.exit({ | ||
enable(buttonId) | ||
hide(selector = loadingEl) | ||
}) | ||
|
||
# Try to run the code when the button is clicked and show an error message if | ||
# an error occurs or a success message if it completes | ||
tryCatch({ | ||
value <- expr | ||
show(selector = doneEl) | ||
delay(2000, hide(selector = doneEl, anim = TRUE, animType = "fade", | ||
time = 0.5)) | ||
value | ||
}, error = function(err) { errorFunc(err, buttonId) }) | ||
} | ||
|
||
# When an error happens after a button click, show the error | ||
errorFunc <- function(err, buttonId) { | ||
errEl <- sprintf("[data-for-btn=%s] .btn-err", buttonId) | ||
errElMsg <- sprintf("[data-for-btn=%s] .btn-err-msg", buttonId) | ||
errMessage <- gsub("^ddpcr: (.*)", "\\1", err$message) | ||
html(html = errMessage, selector = errElMsg) | ||
show(selector = errEl, anim = TRUE, animType = "fade") | ||
} | ||
|
||
appCSS <- " | ||
.btn-loading-container { | ||
margin-left: 10px; | ||
font-size: 1.2em; | ||
} | ||
.btn-done-indicator { | ||
color: green; | ||
} | ||
.btn-err { | ||
margin-top: 10px; | ||
color: red; | ||
} | ||
" |
Oops, something went wrong.