0

I am working with multiple independent selectizeInputs that should offer bidirectional filtering but I cannot seem to make them work. When I try to update the selectizeInputs in my server function they override each other and give unpredictable results.e.g I expect when I add an input to the constituency_name selectizeInput (input$const_name) the const_code selectizeInput should update and include the additional codes, and vise versa regardless of which one I start with but this is not the case. What am I doing wrong? Additionally, let me know if it is still unclear so I can explain more. See attached some pics of the UI

#My server side statements
server <- function(input, output, session) {
    observe({
    if (is.not.null(input$const_code_id)){
      #Observing events for const code first
      observe({
        dt <- voters_data$CONSTITUENCY[voters_data$CONST_CODE %in% input$const_code_id]
        updateSelectizeInput (session, "const_name_id", choices = unique(voters_data$CONSTITUENCY),selected = dt)
      })

      observe({
        dt <- voters_data$COUNTY.NAME[voters_data$CONST_CODE %in% input$const_code_id]
        updateSelectizeInput (session, "county_name_id", choices = unique(voters_data$COUNTY.NAME),selected = dt)
      })

      observe({
        dt <- voters_data$COUNTY.NAME[voters_data$CONST_CODE %in% input$const_code_id]
        updateCheckboxGroupInput (session, "county_group", choices = unique(dt),selected = dt)
      })

      observe({
        dt <- voters_data$CONSTITUENCY[voters_data$CONST_CODE %in% input$const_code_id]
        updateCheckboxGroupInput (session, "constituency_group", choices = unique(dt),selected = dt)
      })

    } else if (is.not.null(input$const_name_id)){
      #Observing events for const name first
      observe({
        dt <- voters_data$CONST_CODE[voters_data$CONSTITUENCY %in% input$const_name_id]
        updateSelectizeInput (session, "const_code_id", choices = unique(voters_data$CONST_CODE),selected = dt)
      })

      observe({
        dt <- voters_data$COUNTY.NAME[voters_data$CONSTITUENCY %in% input$const_name_id]
        updateSelectizeInput (session, "county_name_id", choices = unique(voters_data$COUNTY.NAME),selected = dt)
      })

      observe({
        dt <- voters_data$COUNTY.NAME[voters_data$CONSTITUENCY %in% input$const_name_id]
        updateCheckboxGroupInput (session, "county_group", choices = unique(dt),selected = dt)
      })

      observe({
        dt <- voters_data$CONSTITUENCY[voters_data$CONSTITUENCY %in% input$const_name_id]
        updateCheckboxGroupInput (session, "constituency_group", choices = unique(dt),selected = dt)
      })

    } else if (is.not.null(input$const_name_id)){
      #Observing events for const name first
      observe({
        dt <- voters_data$CONST_CODE[voters_data$CONSTITUENCY %in% input$const_name_id]
        updateSelectizeInput (session, "const_code_id", choices = unique(voters_data$CONST_CODE),selected = dt)
      })

      observe({
        dt <- voters_data$COUNTY.NAME[voters_data$CONSTITUENCY %in% input$const_name_id]
        updateSelectizeInput (session, "county_name_id", choices = unique(voters_data$COUNTY.NAME),selected = dt)
      })

      observe({
        dt <- voters_data$COUNTY.NAME[voters_data$CONSTITUENCY %in% input$const_name_id]
        updateCheckboxGroupInput (session, "county_group", choices = unique(dt),selected = dt)
      })

      observe({
        dt <- voters_data$CONSTITUENCY[voters_data$CONSTITUENCY %in% input$const_name_id]
        updateCheckboxGroupInput (session, "constituency_group", choices = unique(dt),selected = dt)
      })

    } else if (is.not.null(input$county_name_id) & is.null(input$const_code_id) &
               is.null(input$const_name_id) & is.null(input$county_group) & is.null(input$constituency_group)){
      #Observing events for county name ID
      observe({
        dt <- voters_data$CONST_CODE[voters_data$COUNTY.NAME %in% input$county_name_id]
        updateSelectizeInput (session, "const_code_id", choices = unique(voters_data$CONST_CODE),selected = dt)
      })

      observe({
        dt <- voters_data$CONSTITUENCY[voters_data$COUNTY.NAME %in% input$county_name_id]
        updateSelectizeInput (session, "const_name_id", choices = unique(voters_data$CONSTITUENCY),selected = dt)
      })

      observe({
        dt <- voters_data$COUNTY.NAME[voters_data$COUNTY.NAME %in% input$county_name_id]
        updateCheckboxGroupInput (session, "county_group", choices = unique(dt),selected = dt)
      })

      observe({
        dt <- voters_data$CONSTITUENCY[voters_data$COUNTY.NAME %in% input$county_name_id]
        updateCheckboxGroupInput (session, "constituency_group", choices = unique(dt),selected = dt)
      })

    } else if (is.not.null(input$county_name_id) & is.not.null(input$const_code_id)){
      #Observing events for county name ID
      observe({
        dt <- voters_data$CONST_CODE[voters_data$COUNTY.NAME %in% input$county_name_id & voters_data$CONST_CODE %in% input$const_code_id]
        updateSelectizeInput (session, "const_code_id", choices = unique(voters_data$CONST_CODE),selected = dt)
      })

      observe({
        dt <- voters_data$CONSTITUENCY[voters_data$COUNTY.NAME %in% input$county_name_id & voters_data$CONST_CODE %in% input$const_code_id]
        updateSelectizeInput (session, "const_name_id", choices = unique(voters_data$CONSTITUENCY),selected = dt)
      })

      observe({
        dt <- voters_data$COUNTY.NAME[voters_data$COUNTY.NAME %in% input$county_name_id & voters_data$CONST_CODE %in% input$const_code_id]
        updateCheckboxGroupInput (session, "county_group", choices = unique(dt),selected = dt)
      })

      observe({
        dt <- voters_data$CONSTITUENCY[voters_data$COUNTY.NAME %in% input$county_name_id & voters_data$CONST_CODE %in% input$const_code_id]
        updateCheckboxGroupInput (session, "constituency_group", choices = unique(dt),selected = dt)
      })

    } else if (is.not.null(input$county_group)){
      #Observing events for county group
      observe({
        dt <- voters_data$CONST_CODE[voters_data$COUNTY.NAME %in% input$county_group]
        updateSelectizeInput (session, "const_code_id", choices = unique(voters_data$CONST_CODE),selected = dt)
      })

      observe({
        dt <- voters_data$CONSTITUENCY[voters_data$COUNTY.NAME %in% input$county_group]
        updateSelectizeInput (session, "const_name_id", choices = unique(voters_data$CONSTITUENCY),selected = dt)
      })

      observe({
        dt <- voters_data$COUNTY.NAME[voters_data$COUNTY.NAME %in% input$county_group]
        updateSelectizeInput (session, "county_name", choices = unique(voters_data$COUNTY.NAME),selected = dt)
      })

      observe({
        dt <- voters_data$CONSTITUENCY[voters_data$COUNTY.NAME %in% input$county_group]
        updateCheckboxGroupInput (session, "constituency_group", choices = unique(dt),selected = dt)
      })

    } else if (is.not.null(input$constituency_group)){
      #Observing events for constituency group
      observe({
        dt <- voters_data$CONST_CODE[voters_data$CONSTITUENCY %in% input$constituency_group]
        updateSelectizeInput (session, "const_code_id", choices = unique(voters_data$CONST_CODE),selected = dt)
      })

      observe({
        dt <- voters_data$CONSTITUENCY[voters_data$CONSTITUENCY %in% input$constituency_group]
        updateSelectizeInput (session, "const_name_id", choices = unique(voters_data$CONSTITUENCY),selected = dt)
      })

      observe({
        dt <- voters_data$COUNTY.NAME[voters_data$CONSTITUENCY %in% input$constituency_group]
        updateSelectizeInput (session, "county_name", choices = unique(voters_data$COUNTY.NAME),selected = dt)
      })

      observe({
        dt <- voters_data$COUNTY.NAME[voters_data$CONSTITUENCY %in% input$constituency_group]
        updateCheckboxGroupInput (session, "county_group", choices = unique(dt),selected = dt)
      })
    }
  })

#Butoon for clearing all filters
reset_filters <- observeEvent(input$reset_id, {
  updateSelectizeInput (session, "const_code_id", choices = unique(voters_data$CONST_CODE),selected = NULL)

  updateSelectizeInput (session, "const_name_id", choices = unique(voters_data$CONSTITUENCY), selected = NULL)

  updateSelectizeInput (session, "county_name_id", choices = unique(voters_data$COUNTY.NAME), selected = NULL)

  updateCheckboxGroupInput (session, "county_group", choices = unique(voters_data$COUNTY.NAME), selected = NULL)

  updateCheckboxGroupInput (session, "constituency_group", choices =  unique(voters_data$CONSTITUENCY), selected = NULL)

})
}
bill
  • 21
  • 2
  • You might want to check `selectizeGroup-module` from `library(shinyWidgets)`. [Here](https://rdrr.io/cran/shinyWidgets/man/selectizeGroup-module.html) you can find an example. – ismirsehregal May 27 '20 at 12:59
  • I explored this solution but the UI is not as pleasing as the individual selectizeInputs (with some custom css). Any other option? – bill May 27 '20 at 20:19

0 Answers0