【问题标题】:updateSelectizeInput not working, causing the server behave strangeupdateSelectizeInput 不起作用,导致服务器行为异常
【发布时间】:2020-03-21 07:34:48
【问题描述】:

我正在尝试使用一个盆地,然后更新该盆地内子盆地的可能选择。 但是,我的代码不起作用。我无法使其与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

【问题讨论】:

  • 您是否错过了带有花括号的 if-else 子句? subbasins 总是会从一行覆盖到另一行..
  • 你的意思是if (is.null(input$subbasins_id)) subbasins &lt;- character(0)?我从 R 文档的一些代码 (shiny.rstudio.com/reference/shiny/latest/updateSelectInput.html) 中复制了它。所以,我认为因为它是单行的,所以不需要大括号。无论如何,我摆脱了那部分。
  • 如果它确实在一行中,则不需要大括号,而不是两个。此外,如果您在之后分配另一个值,无论如何它都会被覆盖。
  • 所以,我不知道您的第一条评论是什么意思:“您是否错过了带有花括号的 if-else 子句?子流域总是会从一行到另一行被覆盖”跨度>
  • 除此之外,请尝试提供reproducible minimal example。这需要努力将代码归结为问题的本质,但它甚至可以帮助您弄清楚实际问题是什么

标签: r shiny selectinput


【解决方案1】:

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

ui.R

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)

}) 


【讨论】:

    猜你喜欢
    • 2012-09-13
    • 2013-11-11
    • 1970-01-01
    • 2012-06-13
    • 2013-03-26
    • 2016-08-27
    • 2016-03-24
    • 2018-07-13
    • 1970-01-01
    相关资源
    最近更新 更多