sendSweetAlert: Display a Sweet Alert to the user

Description Usage Arguments See Also Examples

Description

Send a message from the server and launch a sweet alert in the UI.

Usage

1
2
3
sendSweetAlert(session, title = "Title", text = NULL, type = NULL,
  btn_labels = "Ok", btn_colors = "#3085d6", html = FALSE,
  closeOnClickOutside = TRUE, showCloseButton = FALSE, width = NULL)

Arguments

session

The session object passed to function given to shinyServer.

title

Title of the alert.

text

Text of the alert.

type

Type of the alert : info, success, warning or error.

btn_labels

Label(s) for button(s), can be of length 2, in which case the alert will have two buttons. Use NA for no buttons.s

btn_colors

Color(s) for the buttons.

html

Does text contains HTML tags ?

closeOnClickOutside

Decide whether the user should be able to dismiss the modal by clicking outside of it, or not.

showCloseButton

Show close button in top right corner of the modal.

width

Width of the modal (in pixel).

See Also

confirmSweetAlert, inputSweetAlert

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
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
## Not run: 
if (interactive()) {

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  tags$h2("Sweet Alert examples"),
  actionButton(
    inputId = "success",
    label = "Launch a success sweet alert",
    icon = icon("check")
  ),
  actionButton(
    inputId = "error",
    label = "Launch an error sweet alert",
    icon = icon("remove")
  ),
  actionButton(
    inputId = "sw_html",
    label = "Sweet alert with HTML",
    icon = icon("thumbs-up")
  )
)

server <- function(input, output, session) {

  observeEvent(input$success, {
    sendSweetAlert(
      session = session,
      title = "Success !!",
      text = "All in order",
      type = "success"
    )
  })

  observeEvent(input$error, {
    sendSweetAlert(
      session = session,
      title = "Error !!",
      text = "It's broken...",
      type = "error"
    )
  })

  observeEvent(input$sw_html, {
    sendSweetAlert(
      session = session,
      title = NULL,
      text = tags$span(
        tags$h3("With HTML tags",
                style = "color: steelblue;"),
        "In", tags$b("bold"), "and", tags$em("italic"),
        tags$br(),
        "and",
        tags$br(),
        "line",
        tags$br(),
        "breaks",
        tags$br(),
        "and an icon", icon("thumbs-up")
      ),
      html = TRUE
    )
  })

}

shinyApp(ui, server)


# output in Sweet Alert #

library("shiny")
library("shinyWidgets")

shinyApp(
  ui = fluidPage(
    tags$h1("Click the button"),
    actionButton(
      inputId = "sw_html",
      label = "Sweet alert with plot"
    ),
    # SweetAlert width
    tags$style(".swal-modal {width: 80%;}")
  ),
  server = function(input, output, session) {
    observeEvent(input$sw_html, {
      sendSweetAlert(
        session = session,
        title = "Yay a plot!",
        text = tags$div(
          plotOutput(outputId = "plot"),
          sliderInput(
            inputId = "clusters",
            label = "Number of clusters",
            min = 2, max = 6, value = 3, width = "100%"
          )
        ),
        html = TRUE
      )
    })
    output$plot <- renderPlot({
      plot(Sepal.Width ~ Sepal.Length,
           data = iris, col = Species,
           pch = 20, cex = 2)
      points(kmeans(iris[, 1:2], input$clusters)$centers,
             pch = 4, cex = 4, lwd = 4)
    })
  }
)

}

## End(Not run)

dreamRs/shinyWidgets documentation built on June 5, 2019, 4:07 a.m.