首页 > 解决方案 > updateSelectizeInput 不起作用,导致服务器行为异常

问题描述

我正在尝试使用盆地,然后更新该盆地内子盆地的可能选择。但是,我的代码不起作用。我不能使它既不使用observe,也不使用reactive,或者使用observeEvent或不使用所有这些。

ui这边是:

selectInput(inputId = 'countyType_id',
            label = '1. Select a basin',
            choices = all_basins
            ),

selectizeInput(inputId = 'subbasins_id',
               label = '2. Select subbasins', 
               choices = subbasins, 
               selected = head(subbasins, 1),
               multiple = TRUE)

服务器端看起来像:

observe({
    #
    # from 
    # https://shiny.rstudio.com/reference/shiny/latest/updateSelectInput.html
    #
    subbasins <- sort(unique(curr_spatial$subbasin))

    # Can also set the label and select items
    updateSelectizeInput(session, 
                         server = FALSE, 
                         "subbasins_id",
                         label = "2. Select subbasins",
                         choices = subbasins,
                         selected = head(subbasins, 1)
                         )
    #  It seems the followin has no effect:
    # and when it is outside observe, it produces errors!
    curr_spatial <- curr_spatial %>% 
                    filter(subbasin %in% input$subbasins_id) %>% 
                    data.table()
  })

有输入吗?请。我确实将数据和整个代码放在了谷歌驱动器中: https ://drive.google.com/file/d/1qaZG6-VmBhIgMsxs5dffX9PmagkMhuB8/view?usp=sharing

标签: rshinyselectinput

解决方案


第二个 selectInput 应该从服务器而不是从 UI 呈现以进行交互。

用户界面

selectInput(inputId = 'countyType_id',
            label = '1. Select a basin',
            choices = all_basins
            ),

uiOutput('subbasins_id')

服务器.R



output$subbasins_id <- renderUI({

## add some code to filter subbasin based on the selected basin, i.e. input$countyType_id

curr_spatial <- curr_spatial %>% 
                    filter(subbasin %in% input$subbasins_id) %>% 
                    data.table()
subbasins <- sort(unique(curr_spatial$subbasin))

selectizeInput(inputId = 'subbasins_id',
               label = '2. Select subbasins', 
               choices = subbasins, 
               selected = head(subbasins, 1),
               multiple = TRUE)

}) 



推荐阅读