将R Shiny反应性SelectInput值传递给selectizeInput

Passing R Shiny reactive SelectInput value to selectizeInput

"我的闪亮"应用程序使用来自鸟类图集的开放数据,包括按物种划分的经/纬度坐标。鸟类物种名称以不同的语言出现,并作为首字母缩写。

这个想法是用户首先选择语言(或首字母缩写词)。根据选择,Shiny呈现唯一鸟类名称的selectizeInput列表。然后,当选择一个物种,生成小叶地图。

我已经做了几个Shiny应用程序,但是这次我错过了一些显而易见的事情。当应用启动时,一切都很好。但是,是不是在选择一种新的语言重新呈现selectizeInput列表。

带有示例数据的所有当前代码都在此处作为GitHub Gist https://gist.github.com/tts/924b764e7607db5d0a57

如果有人可以指出我的问题,我将不胜感激。


问题在于renderUIbirds反应块都取决于input$lan输入。

如果在birds块中添加print(input$birds),您会发现它在renderUI有机会更新它们以适应新语言之前使用了鸟的名称。然后通过leaflet图的data为空。

尝试在bird表达式中的input$lan周围添加isolate,使其仅依赖于input$birds

1
2
3
4
5
birds <- reactive({
    if( is.null(input$birds) )
      return()
    data[data[[isolate(input$lan)]] == input$birds, c("lon","lat","color")]
  })

更改语言时,renderUI将更改selectize,这将触发input$birds并更新数据。

除了使用renderUI,还可以使用(替换uiOutput)在ui.R中创建selectizeInput

1
2
3
4
5
6
selectizeInput(
        inputId ="birds",
        label ="Select species",
        multiple  = F,
        choices = unique(data[["englanti"]])
      )

然后在您的server.R中,使用以下命令进行更新:

1
2
3
observe({
    updateSelectizeInput(session, 'birds', choices = unique(data[[input$lan]]))
  })