Home > Enterprise >  I'm getting some unexpected behaviour with a checkboxGroupInput in a Shiny app
I'm getting some unexpected behaviour with a checkboxGroupInput in a Shiny app

Time:08-18

I'm new to Shiny - a lot I don't fully understand yet. I'm working on a Shiny app to display some data. I want to allow for optional filtering of the data before being plotted. In order to construct my criteria, the UI will supply some selectInputs. My plan is that these are initially disabled. I could add, to the UI, buttons to activate each selectInput independently. But I wanted to try and use a checkboxGroupInput for this task. To begin developing this, I tried working on just one of the checkboxGroupInput values enabling/disabling just one of the selectInputs. It works perfectly except for one specific case. If I select the targeted checkboxGroupInput, the targeted selectInput gets enabled. But if I deselect that targeted checkboxGroupInput, the targeted selectInput does not get disabled. However, this behavior only occurs if no other checkboxGroupInput selection is currently selected. If any, or multiple, other checkboxGroupInputs are selected, the target will enable and disable in exactly the way I want and expect based on my (limited) understanding of the code.

Below is (hopefully) a clear and simple piece of code to demonstrate this behavior. I have a checkboxGroupInput with four items, and 4 selectInputs. Checking the third checkboxGroupInput item is supposed to enable the third selectInput. Unchecking (deselecting) the third checkboxGroupInput item is supposed to disable the third selectInput. Again, it behaves in precisely this way - if at least one other checkboxGroupInput is selected. The third selectInput will always be enabled by selecting the third checkboxGroupInput, but deselecting the third checkboxGroupInput item will not disable the third selectInput unless at least one other item is currently selected in the checkboxGroupInput.

I added output of the currently selected checkboxGroupInput contents to try and understand what was happening.

Last thing before the code -- I also first constructed the checkboxGroupInput using choices instead of choiceNames and choiceValues; didn't seem to matter. Also, my first try with the conditional test in the 'if' block was to use is.element instead of %in%; again, no difference in behavior.


library(shiny)
library(shinyjs)

# Let's make lists for the drop boxes

list_1 <- list("a", "b", "c")
list_2 <- list("d", "e", "f")
list_3 <- list("g", "h", "i")
list_4 <- list("j", "k", "l")

# Define UI for application 
ui <- fluidPage(
    useShinyjs(),  # Set up shinyjs

    # Application title
    titlePanel("What's wrong with this??"),

    # Sidebar
    sidebarLayout(
        sidebarPanel(
          checkboxGroupInput("enabled", "Search Filters to Enable:",
                             choiceNames = list("List_1", "List_2", "List_3", "List_4"),
                             choiceValues = list("List_1_chosen", "List_2_chosen", "List_3_chosen", "List_4_chosen")),
                             
        # Input: Select from the following lists - default behavior is they are all disabled to start
        disabled(selectInput("List_1_choice", "Choose from List 1:",
                    choices = list_1)),
        disabled(selectInput("List_2_choice", "Choose from List 2:",
                    choices = list_2)),
        disabled(selectInput("List_3_choice", "Choose from List 3:",
                    choices = list_3)),
        disabled(selectInput("List_4_choice", "Choose from List 4:",
                    choices = list_4)),
        verbatimTextOutput("text_choice")),
    
        # Show a plot
        mainPanel(
               # empty
        )
    )
 )

# Define server logic
server <- function(input, output) {

# This output is so I can see what's selected in the checkboxGroupInput    
    output$text_choice <- renderPrint({
      return(paste0(input$enabled))})
    
    observeEvent(input$enabled, {

    # Here's the problem (I think) -- this 'if' block is not working the way I expect
      if("List_3_chosen" %in% input$enabled){
        enable("List_3_choice")
        }else{
        disable("List_3_choice")
        }
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

I have lot's of workarounds to complete this project, but I would very much like to understand what I'm doing wrong here.

Help! And thanks for your time and attention.

CodePudding user response:

When nothing is selected, checkboxGroupInput returns NULL. You need to explicitly tell the observeEvent to not ignore it:

  observeEvent(input$enabled, {
    # Here's the problem (I think) -- this 'if' block is not working the way I expect
    if("List_3_chosen" %in% input$enabled){
      enable("List_3_choice")
    }else{
      disable("List_3_choice")
    }
  }, ignoreNULL = FALSE)
  • Related