My Shiny应用程序使用来自鸟类地图集的开放数据,包括物种的纬度/经度坐标。鸟类名称有不同的语言,加上作为首字母缩略词。
这个想法是用户首先选择语言(或首字母缩略词)。根据选择,Shiny呈现了一个独特鸟类名称的选择性输入列表。然后,当选择一个物种时,生成小叶图。
我已经完成了几个Shiny应用程序,但这次我想念一些明显的东西。当应用程序启动时,一切都很好。但是,选择新语言时,不会重新呈现selectizeInput列表。
所有带有一些示例数据的现有代码都在这里作为GitHub Gist https://gist.github.com/tts/924b764e7607db5d0a57
如果有人能指出我的问题,我会感激不尽。
答案 0 :(得分:6)
问题是renderUI
和birds
反应块都依赖于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
替换selectizeInput
来ui.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}}