alert: Create a Bootstrap 4 alert on the server side

Description Usage Arguments Note Examples

Description

createAlert creates an alert and inserts it in the DOM.

closeAlert closes an alert created via createAlert.

Usage

1
2
3
4
5
6
7
8
createAlert(
  id = NULL,
  selector = NULL,
  options,
  session = shiny::getDefaultReactiveDomain()
)

closeAlert(id, session = shiny::getDefaultReactiveDomain())

Arguments

id

Anchor id.

selector

jQuery selector. Allow more customization for the anchor (nested tags).

options

List of options to pass to the alert. See below:

  • content: Alert content.

  • title: Alert title.

  • closable: Whether to allow the user to close the alert. FALSE by default.

  • width: Alert width. Between 1 and 12.

  • elevation: Alert elevation.

  • status: Alert status. "primary", "success", "warning", "danger" or "info".

session

Shiny session object.

Note

Unlike shinyBS, there is no need to specify an anchorId and an alertId. id refers to the anchorId, and the alertId is simply "anchorId-alert". On the server side, one can access the alert status by input$<id>. If TRUE, the alert has been created and is visible, if FALSE the alert has just been closed.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
if (interactive()) {
  library(shiny)
  library(bs4Dash)

  shinyApp(
    ui = dashboardPage(
      header = dashboardHeader(),
      sidebar = dashboardSidebar(),
      body = dashboardBody(
        tooltip(
          sliderInput("obs", "Observations:", 10, min = 1, max = 100),
          placement = "right",
          title = "Set me higher than 50!"
        ),
        div(id = "myalert", style = "position: absolute; bottom: 0; right: 0;")
      ),
      controlbar = dashboardControlbar(),
      title = "Alerts",
    ),
    server = function(input, output, session) {
      observeEvent(input$obs, {
        if (input$obs > 50) {
          createAlert(
            id = "myalert",
            options = list(
              title = "Alert",
              closable = TRUE,
              width = 12,
              elevations = 4,
              status = "primary",
              content = "Alert content ..."
            )
          )
        } else {
          closeAlert(id = "myalert")
        }
      })

      observe(print(input$myalert))

      observeEvent(input$myalert, {
        alertStatus <- if (input$myalert) "opened" else "closed"
        toastColor <- if (input$myalert) "bg-lime" else "bg-fuchsia"
        toast(
          title = sprintf("Alert succesfully %s!", alertStatus),
          options = list(
            class = toastColor,
            autohide = TRUE,
            position = "topRight"
          )
        )
      })
    }
  )
}

bs4Dash documentation built on July 23, 2021, 5:06 p.m.