| 1 |
#' Show or remove a notification |
| 2 |
#' |
| 3 |
#' These functions show and remove notifications in a Shiny application. |
| 4 |
#' |
| 5 |
#' @param ui Content of message. |
| 6 |
#' @param action Message content that represents an action. For example, this |
| 7 |
#' could be a link that the user can click on. This is separate from `ui` |
| 8 |
#' so customized layouts can handle the main notification content separately |
| 9 |
#' from action content. |
| 10 |
#' @param duration Number of seconds to display the message before it |
| 11 |
#' disappears. Use `NULL` to make the message not automatically |
| 12 |
#' disappear. |
| 13 |
#' @param closeButton If `TRUE`, display a button which will make the |
| 14 |
#' notification disappear when clicked. If `FALSE` do not display. |
| 15 |
#' @param id A unique identifier for the notification. |
| 16 |
#' |
| 17 |
#' `id` is optional for `showNotification()`: Shiny will automatically create |
| 18 |
#' one if needed. If you do supply it, Shiny will update an existing |
| 19 |
#' notification if it exists, otherwise it will create a new one. |
| 20 |
#' |
| 21 |
#' `id` is required for `removeNotification()`. |
| 22 |
#' @param type A string which controls the color of the notification. One of |
| 23 |
#' "default" (gray), "message" (blue), "warning" (yellow), or "error" (red). |
| 24 |
#' @param session Session object to send notification to. |
| 25 |
#' |
| 26 |
#' @return An ID for the notification. |
| 27 |
#' |
| 28 |
#' @examples |
| 29 |
#' ## Only run examples in interactive R sessions |
| 30 |
#' if (interactive()) { |
| 31 |
#' # Show a message when button is clicked |
| 32 |
#' shinyApp( |
| 33 |
#' ui = fluidPage( |
| 34 |
#' actionButton("show", "Show") |
| 35 |
#' ), |
| 36 |
#' server = function(input, output) { |
| 37 |
#' observeEvent(input$show, { |
| 38 |
#' showNotification("Message text", |
| 39 |
#' action = a(href = "javascript:location.reload();", "Reload page") |
| 40 |
#' ) |
| 41 |
#' }) |
| 42 |
#' } |
| 43 |
#' ) |
| 44 |
#' |
| 45 |
#' # App with show and remove buttons |
| 46 |
#' shinyApp( |
| 47 |
#' ui = fluidPage( |
| 48 |
#' actionButton("show", "Show"), |
| 49 |
#' actionButton("remove", "Remove") |
| 50 |
#' ), |
| 51 |
#' server = function(input, output) { |
| 52 |
#' # A queue of notification IDs |
| 53 |
#' ids <- character(0) |
| 54 |
#' # A counter |
| 55 |
#' n <- 0 |
| 56 |
#' |
| 57 |
#' observeEvent(input$show, { |
| 58 |
#' # Save the ID for removal later |
| 59 |
#' id <- showNotification(paste("Message", n), duration = NULL) |
| 60 |
#' ids <<- c(ids, id) |
| 61 |
#' n <<- n + 1 |
| 62 |
#' }) |
| 63 |
#' |
| 64 |
#' observeEvent(input$remove, { |
| 65 |
#' if (length(ids) > 0) |
| 66 |
#' removeNotification(ids[1]) |
| 67 |
#' ids <<- ids[-1] |
| 68 |
#' }) |
| 69 |
#' } |
| 70 |
#' ) |
| 71 |
#' } |
| 72 |
#' @export |
| 73 |
showNotification <- function(ui, action = NULL, duration = 5, |
| 74 |
closeButton = TRUE, id = NULL, |
| 75 |
type = c("default", "message", "warning", "error"), |
| 76 |
session = getDefaultReactiveDomain()) |
| 77 |
{ |
| 78 |
|
| 79 |
if (is.null(id)) |
| 80 |
id <- createUniqueId(8) |
| 81 |
|
| 82 |
res <- processDeps(ui, session) |
| 83 |
actionRes <- processDeps(action, session) |
| 84 |
|
| 85 |
session$sendNotification("show", |
| 86 |
list( |
| 87 |
html = res$html, |
| 88 |
action = actionRes$html, |
| 89 |
deps = c(res$deps, actionRes$deps), |
| 90 |
duration = if (!is.null(duration)) duration * 1000, |
| 91 |
closeButton = closeButton, |
| 92 |
id = id, |
| 93 |
type = match.arg(type) |
| 94 |
) |
| 95 |
) |
| 96 |
|
| 97 |
id |
| 98 |
} |
| 99 |
|
| 100 |
#' @rdname showNotification |
| 101 |
#' @export |
| 102 |
removeNotification <- function(id, session = getDefaultReactiveDomain()) { |
| 103 |
force(id) |
| 104 |
session$sendNotification("remove", id) |
| 105 |
id |
| 106 |
} |