## Create a simple server toast
library(shiny)
library(shiny.semantic)
ui <- function() {
shinyUI(
semanticPage(
actionButton("show", "Show toast")
)
)
}
server = function(input, output) {
observeEvent(input$show, {
toast(
"This is an important message!"
)
})
}
if (interactive()) shinyApp(ui, server)
## Create a toast with options
ui <- semanticPage(
actionButton("show", "Show"),
)
server <- function(input, output) {
observeEvent(input$show, {
toast(
title = "Question",
"Do you want to see more?",
duration = 0,
action = list(
list(
text = "OK", class = "green", icon = "check",
click = ("(function() { $('body').toast({message:'Yes clicked'}); })")
),
list(
text = "No", class = "red", icon = "times",
click = ("(function() { $('body').toast({message:'No ticked'}); })")
)
)
)
})
}
if (interactive()) shinyApp(ui, server)
## Closing a toast
ui <- semanticPage(
action_button("show", "Show"),
action_button("remove", "Remove")
)
server <- function(input, output) {
# A queue of notification IDs
ids <- character(0)
# A counter
n <- 0
observeEvent(input$show, {
# Save the ID for removal later
id <- toast(paste("Message", n), duration = NULL)
ids <<- c(ids, id)
n <<- n + 1
})
observeEvent(input$remove, {
if (length(ids) > 0)
close_toast(ids[1])
ids <<- ids[-1]
})
}
if (interactive()) shinyApp(ui, server)
Run the code above in your browser using DataLab