Home > Enterprise >  CheckBoxGroupInput reactivity
CheckBoxGroupInput reactivity

Time:12-21

I will be very appreciative if someone could help me to solve the behavior of the code below.
This is the logic: Only if A B are selected A1 is showing up.
If A1 is selected "Buttons" is showing up.
But when A and/or B are deselected, only A1 is disappearing but "Buttons" are staying visible.
Of course, if A1 is deselected "Buttons" are disappearing...
I am aware that the A1 is still being selected even if A and/or B are deselected.
Wondering if there is an easy way to deselect A1 "automatically" when A and/or B are deselected.

And apologies in advance, if this is trivial question...

library(shiny)

ui <- fluidPage(

    checkboxGroupInput("choice", 
                       "Choice",
                       choices  = c("A", "B"),
                       selected = NULL), 

    conditionalPanel(
       condition='input.choice.indexOf("A") > -1 &&
                 input.choice.indexOf("B") > -1',
       h5(tags$b("Single checkbox")),
           checkboxInput(inputId = "a1", label = "A1", value = FALSE)), 

     conditionalPanel(
       condition = "input.a1 == 1",
           radioButtons(inputId = "A11", 
                        label = "Buttons",
                        choices = list("Bla" = 2, "Ble" = 3),
                        selected = 2)))

server <- function(input, output, session) {}

shinyApp(ui = ui, server = server)

CodePudding user response:

Using an observer and an updateCheckboxInput you could do:

library(shiny)

ui <- fluidPage(
  checkboxGroupInput("choice",
    "Choice",
    choices  = c("A", "B"),
    selected = NULL
  ),
  conditionalPanel(
    condition = 'input.choice.indexOf("A") > -1 &&
                 input.choice.indexOf("B") > -1',
    h5(tags$b("Single checkbox")),
    checkboxInput(inputId = "a1", label = "A1", value = FALSE)
  ),
  conditionalPanel(
    condition = "input.a1 == 1",
    radioButtons(
      inputId = "A11",
      label = "Buttons",
      choices = list("Bla" = 2, "Ble" = 3),
      selected = 2
    )
  )
)

server <- function(input, output, session) {
  observe({
    if (!all(c("A", "B") %in% input$choice)) {
      updateCheckboxInput(inputId = "a1", value = FALSE)
    }
  })
}

shinyApp(ui = ui, server = server)
  • Related