8.2 Notifications

Use showNotification(), if there is no problem, but you want the user to know what is happening.

  • show a transient notification that automatically disappears after a fixed amount of time
  • show a notification when a process starts and remove it when the process ends
  • update a single notification with progressive updates

8.2.1 Transient notifications

ui <- fluidPage(
  actionButton("goodnight", "Good night")
)

server <- function(input, output, session) {
  observeEvent(input$goodnight, {
    showNotification("So long")
    Sys.sleep(1)
    showNotification("Farewell")
    Sys.sleep(1)
    showNotification("Auf Wiedersehen")
    Sys.sleep(1)
    showNotification("Adieu")
  })
}

–> example app

8.2.2 Removing on completion

Show the notification when the task starts, and remove the notification when the task completes.

  • Set duration = NULL and closeButton = FALSE so that the notification stays visible until the task is complete
  • Store the id returned by showNotification(), and then pass this value to removeNotification() (& on.exit())
server <- function(input, output, session) {
  data <- reactive({
    id <- showNotification("Reading data...", duration = NULL, closeButton = FALSE)
    on.exit(removeNotification(id), add = TRUE)
    
    read.csv(input$file$datapath)
  })
}

8.2.3 Progressive updates

  • multiple calls to showNotification() –> multiple notifications
  • capture id from first call, use it in subsequent calls
ui <- fluidPage(
  tableOutput("data")
)

server <- function(input, output, session) {
  notify <- function(msg, id = NULL) {
    showNotification(msg, id = id, duration = NULL, closeButton = FALSE)
  }

  data <- reactive({ 
    id <- notify("Reading data...")
    on.exit(removeNotification(id), add = TRUE)
    Sys.sleep(1)
      
    notify("Reticulating splines...", id = id)
    Sys.sleep(1)
    
    notify("Herding llamas...", id = id)
    Sys.sleep(1)

    notify("Orthogonalizing matrices...", id = id)
    Sys.sleep(1)
        
    mtcars
  })
  
  output$data <- renderTable(head(data()))
}