createAlert creates an alert and inserts it in the DOM.
closeAlert closes an alert created via createAlert.
Usage
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
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"
)
)
})
}
)
}