Description Details Components Changes Note See Also Examples
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.
To create alerts in your Shiny app you must place 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.
There are three functions in the Alerts family:
bsAlertUsed in the UI to create an anchor where your Alerts will be displayed.
createAlertUsed in the Server logic to create alerts. This would be used within a reactive context to display error or success messages to the user based on the status of that context.
closeAlertUsed in the Server logic to close an alert that is already open. By default, Alerts are dismissable by the user, but this offers you a way to close them programmatically.
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.
Run bsExample("Alerts") for an example
of Alerts functionality.
Other Alerts: bsAlert;
closeAlert; createAlert
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 | library(shiny)
library(shinyBS)
app = 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)
}
})
}
)
## Not run:
runApp(app)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.