Notifications

#| standalone: true
#| components: [viewer]
#| viewerHeight: 200

library(shiny)
library(bslib)

types <- c("default", "message", "warning", "error")

ui <- page_fluid(
  radioButtons("type" , "Notification Type", choices = types, inline = TRUE),
  actionButton("show", "Show Notification")
)

server <- function(input, output, session) {

  observe({ 
    type_txt <- ifelse(input$type == "default", "notification", input$type)
    showNotification( 
      paste("This", type_txt, "will disappear after 2 seconds."), 
      type = input$type, 
      duration = 2 
    ) 
  }) |> 
    bindEvent(input$show) 
    
}

shinyApp(ui, server)
library(shiny)
library(bslib)

types <- c("default", "message", "warning", "error")

ui <- page_fluid(
  radioButtons("type" , "Notification Type", choices = types, inline = TRUE),
  actionButton("show", "Show Notification")
)

server <- function(input, output, session) {

  observe({ 
    type_txt <- ifelse(input$type == "default", "notification", input$type)
    showNotification( 
      paste("This", type_txt, "will disappear after 2 seconds."), 
      type = input$type, 
      duration = 2 
    ) 
  }) |> 
    bindEvent(input$show) 
    
}

shinyApp(ui, server)
No matching items

Relevant Functions

  • showNotification
    showNotification(ui, action = NULL, duration = 5, closeButton = TRUE, id = NULL, type = c("default", "message", "warning", "error"), session = getDefaultReactiveDomain())

  • removeNotification
    removeNotification(id, session = getDefaultReactiveDomain())

  • observe
    observe(x, env = parent.frame(), quoted = FALSE, ..., label = NULL, suspended = FALSE, priority = 0, domain = getDefaultReactiveDomain(), autoDestroy = TRUE, ..stacktraceon = TRUE)

  • bindEvent
    bindEvent(x, ..., ignoreNULL = TRUE, ignoreInit = FALSE, once = FALSE, label = NULL)

No matching items

Details

A notification is a message that appears near the bottom corner of the app. Notifications normally disappear after a short period of time, and should multiple notifications appear together, they will stack on top of one another.

To create a notification, call showNotification(). Typically, you will want to create an observer and bind it to an event to call showNotification() whenever a particular event occurs. For example, the observer below will create a notification whenever the value of input$show changes.

observe({
  showNotification("You've been notified.")
}) |>
  bindEvent(input$show)

You can call removeNotification() to remove a notification programatically, but usually app developers will let notifications expire on their own. Also, notifications come by default with an icon that the user can click to close the notification prematurely.

Duration

By default, Shiny notifications will disappear after five seconds. To change how long a notification appears for, set the duration argument of showNotification() to an integer number of seconds. Set duration to NULL to have the notification appear until the user closes it.

Type

Shiny notifications come in four types: default, messages, warnings and errors. To set the type of a notification, use the type argument of showNotification().

See also

  • Modal messages provide a similar, but alternative way to display information to the user.

Variations

Replace/update a notification

Assign a notification an id to replace any existing notification with the same id. In the example below, a persistant notification is created with duration = NULL and updated each time you click the notification button.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(shiny)
library(bslib)

ui <- page_fluid(
  actionButton("show", "Show Notification")
)

server <- function(input, output, session) {
  
  observe({
    type_txt <- ifelse(input$type == "default", "notification", input$type)
    showNotification(
      paste("You clicked the show button", input$show, "times."),
      duration = NULL,
      id = "message"
    )
  }) |>
    bindEvent(input$show)
    
}

shinyApp(ui, server)
library(shiny)
library(bslib)

ui <- page_fluid(
  actionButton("show", "Show Notification")
)

server <- function(input, output, session) {
  
  observe({
    type_txt <- ifelse(input$type == "default", "notification", input$type)
    showNotification(
      paste("You clicked the show button", input$show, "times."),
      duration = NULL,
      id = "message"
    )
  }) |>
    bindEvent(input$show)
    
}

shinyApp(ui, server)

Track and remove notifications

Track notifications, and use removeNotification() to remove notifications one at a time.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 225

library(shiny)
library(bslib)

ui <- page_fluid(
  actionButton("show", "Show"),
  actionButton("remove", "Remove")
)

server <- function(input, output) {
  # A queue of notification IDs 
  ids <- character(0) 
  # A counter 
  n <- 0 

  observe({ 
    # Save the ID for removal later 
    id <- showNotification(paste("Message", n), duration = NULL) 
    ids <<- c(ids, id) 
    n <<- n + 1 
  }) |> 
    bindEvent(input$show) 

  observe({ 
    if (length(ids) > 0) 
      removeNotification(ids[1]) 
    ids <<- ids[-1] 
  }) |>
    bindEvent(input$remove) 

}

shinyApp(ui, server)
library(shiny)
library(bslib)

ui <- page_fluid(
  actionButton("show", "Show"),
  actionButton("remove", "Remove")
)

server <- function(input, output) {
  # A queue of notification IDs 
  ids <- character(0) 
  # A counter 
  n <- 0 

  observe({ 
    # Save the ID for removal later 
    id <- showNotification(paste("Message", n), duration = NULL) 
    ids <<- c(ids, id) 
    n <<- n + 1 
  }) |> 
    bindEvent(input$show) 

  observe({ 
    if (length(ids) > 0) 
      removeNotification(ids[1]) 
    ids <<- ids[-1] 
  }) |>
    bindEvent(input$remove) 

}

shinyApp(ui, server)
No matching items