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

时间:2015-03-10 17:32:51

标签: r shiny

My Shiny应用程序使用来自鸟类地图集的开放数据,包括物种的纬度/经度坐标。鸟类名称有不同的语言,加上作为首字母缩略词。

这个想法是用户首先选择语言(或首字母缩略词)。根据选择,Shiny呈现了一个独特鸟类名称的选择性输入列表。然后,当选择一个物种时,生成小叶图。

我已经完成了几个Shiny应用程序,但这次我想念一些明显的东西。当应用程序启动时,一切都很好。但是,选择新语言时,不会重新呈现selectizeInput列表。

所有带有一些示例数据的现有代码都在这里作为GitHub Gist https://gist.github.com/tts/924b764e7607db5d0a57

如果有人能指出我的问题,我会感激不尽。

1 个答案:

答案 0 :(得分:6)

问题是renderUIbirds反应块都依赖于input$lan输入。

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

尝试在bird表达式的isolate周围添加input$lan,以便它仅依赖于input$birds

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

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

您还可以使用renderUI替换selectizeInputui.R创建uiOutput,而不是使用selectizeInput( inputId = "birds", label = "Select species", multiple = F, choices = unique(data[["englanti"]]) )

server.R

observe({ updateSelectizeInput(session, 'birds', choices = unique(data[[input$lan]])) }) 中,使用以下内容进行更新:

{{1}}