[英]Passing R Shiny reactive SelectInput value to selectizeInput
我的Shiny應用程序使用來自鳥類地圖集的開放數據,包括物種的緯度/經度坐標。 鳥類名稱有不同的語言,加上作為首字母縮略詞。
想法是用戶首先選擇語言(或首字母縮略詞)。 根據選擇,Shiny呈現了一個獨特鳥類名稱的選擇性輸入列表。 然后,當選擇一個物種時,生成小葉圖。
我做了幾個Shiny應用程序,但這次我錯過了一些明顯的東西。 當應用程序啟動時,一切都很好。 但是,選擇新語言時,不會重新呈現selectizeInput列表。
所有帶有一些示例數據的當前代碼都在這里作為GitHub Gist https://gist.github.com/tts/924b764e7607db5d0a57
如果有人能指出我的問題,我將不勝感激。
問題是renderUI
和birds
反應塊都依賴於input$lan
輸入。
如果在birds
塊中添加print(input$birds)
,您將看到它在使用renderUI
之前有機會更新它們以適應新語言時使用鳥的名稱。 然后傳遞leaflet
圖的data
為空。
嘗試在birds表達式的input$lan
周圍添加isolate
,這樣它只依賴於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
,使用以下server.R
更新它:
observe({
updateSelectizeInput(session, 'birds', choices = unique(data[[input$lan]]))
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.