39289

Insert buttons into sendSweetAlert message in shiny

<h3>Question</h3>

Friends in the code below show a message alert, if the option "Exclude farms" was selected. However, I would like to know if it is possible to insert two buttons (Yes and NO) in this alert? The functionality of these buttons is still under development. The executable code is below.

library(shinyWidgets) library(shiny) ui <- fluidPage( titlePanel("Old Faithful Geyser Data"), sidebarLayout( sidebarPanel( radioButtons( "filter1", h3("Select properties"), choiceValues = c(1, 2), choiceNames = list( tagList( tags$span("All properties"), tags$span(icon("info-circle"), id = "icon1", style = "color: blue;") ), tagList( tags$span("Exclude properties"), tags$span(icon("info-circle"), id = "icon2", style = "color: blue;") ) ), selected = 1 ), selectInput("filter2", h3("Select farms"), choices = list("All farms" = 1, "Exclude farms" = 2), selected = 1), ), mainPanel( ) ) ) server <- function(input, output, session) { observe({ if(input$filter2 == 2){ sendSweetAlert( session = session, title = "Warning!", text = "Change filter options selected above", type = "warning" ) } }) } shinyApp(ui = ui, server = server)

Thank you very much!


<h3>Answer1:</h3>

We can add additional HTML elements with the text argument in sendSweetAlert by using a tags$div. See more options/info in the examples with ?sendSweetAlert (you can put widgets and plot/table outputs too).

The simpler way for this situation is to simply pass a character vector of length 2 to the btn_labels argument depending on what you are wanting to do. Here is the app with both solutions combined in to one alert so it has two sets of yes and no buttons. Remove the “text” argument in the sendSweetAlert function to just have one set of the default yes/no

library(shinyWidgets) library(shiny) ui <- fluidPage( titlePanel("Old Faithful Geyser Data"), sidebarLayout( sidebarPanel( radioButtons( "filter1", h3("Select properties"), choiceValues = c(1, 2), choiceNames = list( tagList( tags$span("All properties"), tags$span(icon("info-circle"), id = "icon1", style = "color: blue;") ), tagList( tags$span("Exclude properties"), tags$span(icon("info-circle"), id = "icon2", style = "color: blue;") ) ), selected = 1 ), selectInput("filter2", h3("Select farms"), choices = list("All farms" = 1, "Exclude farms" = 2), selected = 1), ), mainPanel( ) ) ) server <- function(input, output, session) { observe({ if(input$filter2 == 2){ sendSweetAlert( session = session, title = "Warning!", btn_labels = c("Yes", "No"), text = tags$div(h5("Change filter options selected above"), actionButton("yes", "YES"), actionButton("no", "NO") ), type = "warning" ) } }) }

来源:https://stackoverflow.com/questions/61937337/insert-buttons-into-sendsweetalert-message-in-shiny

Recommend