Insert buttons into sendSweetAlert message in shiny

爷,独闯天下 提交于 2020-06-16 03:34:00

问题


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!


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!