updateSelectizeInput无法正常工作,导致服务器行为异常

时间:2019-11-25 23:18:31

标签: r shiny selectinput

我正在尝试使用一个盆地,然后更新该盆地内子盆地的可能选择。 但是,我的代码无法正常工作。我无法同时使用observereactiveobserveEvent或全部都不使用它。

我的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

1 个答案:

答案 0 :(得分:0)

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

ui.R

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

uiOutput('subbasins_id')

server.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)

})