Shiny - ObserveEvent/UpdateSelectizeInput, box not empty when 0 choice defined

Hi everyone,

Here is a small example reproducing my problem :
I would like to show an empty box in the selectizeInput box when nothing is chosen by user in the checkboxgroupinput

I made the connection with observeEvent (objections are welcomed, using loop/"for" probably not the best way), and it works but when 0 group are chosen, it remains on the previous choice. And of course, i would like nothing appears.

library(shiny)

# define dataset
list_group <- c("Group1","Group2","Group3")

liste_ind_group <- list(Group1=c("Monica","Therese"),
                       Group2=c("Jose"),
                       Group3=c("Andre","Martina","Pete","Roger"))

liste_id <- sort(c(liste_ind_group[["Group1"]],liste_ind_group[["Group2"]],liste_ind_group[["Group3"]]))


# Define UI for application
ui <- fluidPage(

   # Application title
   titlePanel("Test ObserveEvent / UpdateSelectizeInput"),

   sidebarLayout(
       sidebarPanel(
               checkboxGroupInput("type_group", "Groupe Choice", inline=T,
                                  choices = list_group, selected=list_group),
               selectizeInput("id_type_group", "Name", choices = liste_id, selected=liste_id, multiple = T),
               h6("____________________"),
               checkboxInput('selectall_id_type_group', 'Tout / Aucun', T)
       ),

       mainPanel(
       )
   )
)

# Define server
server <- function(input, output,session) {
   
   observeEvent(input$type_group,{
       
       val <- c()
       for(i in 1:length(input$type_group)){
           val <- c(val, liste_ind_group[[which(input$type_group[[i]] == list_group)]])
       }
       
       # if(!input$type_group) { val <- character(0) } # nothing changes
       
       updateSelectizeInput(
           session, 'id_type_group',
           choices = val,
           selected = val
       )
   })
   
   observeEvent(input$selectall_id_type_group,{
       val <- liste_id
       if(!input$selectall_id_type_group)
           val <- character(0)
       
       updateSelectizeInput(
           session, 'id_type_group',
           selected = val
       )
   })

}

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

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.