mardi 14 juillet 2020

R Shiny : None Button inside CheckBox --> Deselect all responses selected

I hope you are well.

I would add a "None" Button inside the checkboxGroupButtons. So, if I explain that by words, I would add a button "None" (with others buttons in my example --> "Nuclear", "Hydro", "Solar", "Wind") and when you click to this button "None", all others buttons are deselected.

Here is an example with a "None" Button, but not inside the checkboxGroupButtons.

I try to do a similar thing but I wrong. In my shiny app, when you click on the button "None", there no response selected (that's right). But, if I click before on others buttons ("Nuclear" for exemple) - and then on button "None" - all response are kept (that's wrong). I think the problem is here : "if (input$btn == 5)". I don't know the logical's operator "At least 5" and not "==5".

Thanks in advance, Aurélien.


library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  checkboxGroupButtons(
    inputId = "btn", label = "Power :",
    choices = c("Nuclear" = 1, "Hydro" = 2, "Solar" = 3, "Wind" = 4, "None" = 5),
    selected = ""),
  
  verbatimTextOutput(outputId = "res"))

server <- function(input,output, session){
  
  observeEvent(input$btn, {
    if (input$btn == 5) {
          updateCheckboxGroupButtons(session, "btn", selected = character(0))
            } else {
          updateCheckboxGroupButtons(session, "btn", selected = input$btn)
            }
  }, ignoreInit = TRUE)
  

  output$res <- renderPrint({
    
    input$btn
    
  })
}

shinyApp(ui = ui, server = server)



Aucun commentaire:

Enregistrer un commentaire