toast: Toasts

View source: R/toast.R

toastR Documentation

Toasts

Description

Send notifications to the user. Create notification elements, toasts, with the toast() function. Display toasts with showToast() and remove all active toasts with closeToast().

Usage

toast(header, ...)

showToast(
  toast,
  duration = 4,
  action = NULL,
  session = getDefaultReactiveDomain()
)

closeToast(session = getDefaultReactiveDomain())

Arguments

header

A character string or tag element specifying a header for the toast, defaults to NULL. A close button is always included in the header.

...

Any number of character strings or tag elements to include in the body of the toast.

Any number of named arguments passed as HTML attributes to the parent element.

toast

A toast element, typically built with toast().

duration

A positive integer or NULL specifying the duration of the toast in seconds by default a toast is removed after 4 seconds. If NULL the toast is not automatically removed.

action

A character string specifying a reactive id. If specified, the hiding or closing of the toast will set the reactive id action to TRUE.

session

A reactive context, defaults to getDefaultReactiveDomain().

Showing notifications

ui <- container(
  buttonInput(
    id = "show",
    label = "Show notification"
  ) %>%
    margin(3)
)

server <- function(input, output) {
  observeEvent(input$show, {
    showToast(
      toast(
        list(
          span("Notification") %>%
            margin(right = "4"),
          span(strftime(Sys.time(), "%H:%M")) %>%
            margin(right = 1)
        ),
        "This is notification ", input$show
      ) %>%
        margin(right = 2, top = 2)
    )
  })
}

shinyApp(ui, server)

Reacting to notifications

When a notification is not automatically closed you may want to know when the notification is manually closed.

ui <- container(
  buttonInput(
    id = "show",
    label = "Show notification"
  ) %>%
    margin(3)
)

server <- function(input, output) {
  observeEvent(input$show, {
    showToast(
      action = "undo",
      duration = NULL,
      toast(
        tags$strong("Close") %>%
          margin(right = "auto"),
        "When closing this notification, see the console"
      ) %>%
        margin(right = 2, top = 2)
    )
  })

  observeEvent(input$undo, {
    print("The notification was closed")
  })
}

shinyApp(ui, server)

See Also

Other components: alert(), badge(), blockquote(), card(), collapsePane(), d1(), dropdown(), img(), jumbotron(), modal(), navContent(), popover(), pre()


nteetor/dull documentation built on June 10, 2022, 11:30 a.m.