Alerts allow you to communicate information to the user on the fly. Standard Bootstrap styling options give the user a hint at the type of information contained in the Alert.
bsAlert in your ui.
This serves as an anchor that tells shinyBS where to place the alerts created
with createAlert.
Use createAlert in your server script to add alerts to the anchor
you created with bsAlert in your ui. You can place createAlert
in observers, reactives, or outputs. A common usage may be to have logic that
validates a user's inputs. If they are valid produce the requested output, if
not use createAlert to give the user info about what they need to
change.
bsAlert creates an anchor point in your UI definition. This anchor point
is where alerts created in your Server logic will be displayed.
bsAlert(anchorId)
anchorId
createAlert is used within the Server logic of your Shiny app to display
an alert to the user.
createAlert(session, anchorId, alertId = NULL, title = NULL,
content = NULL, style = NULL, dismiss = TRUE, append = TRUE)
sessionanchorIdalertIdtitlecontentstyleinfo.dismissTRUE.appendlogical Should the Alert be appended below existing Alerts? Default to TRUE.
closeAlert is used within your Server logic to close an alert that you
created with list("createAlert").
closeAlert(session, alertId)
sessionalertId
style was called type in previous versions of shinyBS.
anchorId was called inputId in previous versions of shinyBS.
content was called message in previous versions of shinyBS.
library(shiny)
library(shinyBS)
shinyApp(
ui =
fluidPage(
sidebarLayout(
sidebarPanel(textInput("num1", NULL, value = 100),
"divided by", textInput("num2", NULL, value = 20),
"equals", textOutput("exampleOutput")),
mainPanel(
bsAlert("alert")
)
)
),
server =
function(input, output, session) {
output$exampleOutput <- renderText({
num1 <- as.numeric(input$num1)
num2 <- as.numeric(input$num2)
if(is.na(num1) | is.na(num2)) {
createAlert(session, "alert", "exampleAlert", title = "Oops",
content = "Both inputs should be numeric.", append = FALSE)
} else if(num2 == 0) {
createAlert(session, "alert", "exampleAlert", title = "Oops",
content = "You cannot divide by 0.", append = FALSE)
} else {
closeAlert(session, "exampleAlert")
return(num1/num2)
}
})
}
)