Debian Codegraph @

man/showNotification.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/notifications.R
3 \name{showNotification}
4 \alias{showNotification}
5 \alias{removeNotification}
6 \title{Show or remove a notification}
7 \usage{
8 showNotification(
9 ui,
10 action = NULL,
11 duration = 5,
12 closeButton = TRUE,
13 id = NULL,
14 type = c("default", "message", "warning", "error"),
15 session = getDefaultReactiveDomain()
16 )
17
18 removeNotification(id, session = getDefaultReactiveDomain())
19 }
20 \arguments{
21 \item{ui}{Content of message.}
22
23 \item{action}{Message content that represents an action. For example, this
24 could be a link that the user can click on. This is separate from \code{ui}
25 so customized layouts can handle the main notification content separately
26 from action content.}
27
28 \item{duration}{Number of seconds to display the message before it
29 disappears. Use \code{NULL} to make the message not automatically
30 disappear.}
31
32 \item{closeButton}{If \code{TRUE}, display a button which will make the
33 notification disappear when clicked. If \code{FALSE} do not display.}
34
35 \item{id}{A unique identifier for the notification.
36
37 \code{id} is optional for \code{showNotification()}: Shiny will automatically create
38 one if needed. If you do supply it, Shiny will update an existing
39 notification if it exists, otherwise it will create a new one.
40
41 \code{id} is required for \code{removeNotification()}.}
42
43 \item{type}{A string which controls the color of the notification. One of
44 "default" (gray), "message" (blue), "warning" (yellow), or "error" (red).}
45
46 \item{session}{Session object to send notification to.}
47 }
48 \value{
49 An ID for the notification.
50 }
51 \description{
52 These functions show and remove notifications in a Shiny application.
53 }
54 \examples{
55 ## Only run examples in interactive R sessions
56 if (interactive()) {
57 # Show a message when button is clicked
58 shinyApp(
59 ui = fluidPage(
60 actionButton("show", "Show")
61 ),
62 server = function(input, output) {
63 observeEvent(input$show, {
64 showNotification("Message text",
65 action = a(href = "javascript:location.reload();", "Reload page")
66 )
67 })
68 }
69 )
70
71 # App with show and remove buttons
72 shinyApp(
73 ui = fluidPage(
74 actionButton("show", "Show"),
75 actionButton("remove", "Remove")
76 ),
77 server = function(input, output) {
78 # A queue of notification IDs
79 ids <- character(0)
80 # A counter
81 n <- 0
82
83 observeEvent(input$show, {
84 # Save the ID for removal later
85 id <- showNotification(paste("Message", n), duration = NULL)
86 ids <<- c(ids, id)
87 n <<- n + 1
88 })
89
90 observeEvent(input$remove, {
91 if (length(ids) > 0)
92 removeNotification(ids[1])
93 ids <<- ids[-1]
94 })
95 }
96 )
97 }
98 }