0

我正在使用多个独立的 selectizeInputs,它们应该提供双向过滤,但我似乎无法让它们工作。当我尝试在我的服务器函数中更新 selectizeInputs 时,它们会相互覆盖并给出不可预测的结果。例如,我希望当我向 constituency_name selectizeInput ( input$const_name ) 添加输入时, const_code selectizeInput 应该更新并包含其他代码,并且虎钳反之亦然,无论我从哪一个开始,但事实并非如此。我究竟做错了什么?另外,如果还不清楚,请告诉我,以便我解释更多。见附件的一些用户界面图片

#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)

})
}
4

0 回答 0