Alerts

Description

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.

Details

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.

Components

bsAlert

bsAlert creates an anchor point in your UI definition. This anchor point is where alerts created in your Server logic will be displayed.

Usage

bsAlert(anchorId)

Parameters

anchorId
A unique id the identifies the anchor.

createAlert

createAlert is used within the Server logic of your Shiny app to display an alert to the user.

Usage

createAlert(session, anchorId, alertId = NULL, title = NULL,
  content = NULL, style = NULL, dismiss = TRUE, append = TRUE)

Parameters

session
The session object passed to function given to shinyServer.
anchorId
The unique identifier of the anchor where the alert should be displayed.
alertId
Optional A unique identifier for the Alert.
title
Optional A title for the Alert.
content
The main body of the Alert. HTML tags are allowed.
style
A bootstrap style to apply. Defaults to info.
dismiss
logical Should the Alert be user dismissable? Defaults to TRUE.
append
logical Should the Alert be appended below existing Alerts? Default to TRUE.

closeAlert

closeAlert is used within your Server logic to close an alert that you created with list("createAlert").

Usage

closeAlert(session, alertId)

Parameters

session
The session object passed to function given to shinyServer.
alertId
Optional A unique identifier for the Alert.

Changes

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.

Example



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)
       }

     })
   }
)