我是Shiny的新手,對SO等類似問題的回答正在幫助我。在下拉菜單中閃亮的顯示只有一個選擇(在幾個之間)
我想在下拉菜單上顯示動態選項,具體取決於用戶在單選按鈕上選擇的內容。但下面的代碼片段只能顯示一個亞種的選擇,一旦用戶選擇,首先#1動物類型,然後#2全部與亞種的選擇。
我有三個變量:#1。動物類型(獅子vs老虎); #2。整體與亞種(單選按鈕); #3。亞種(如果用戶在#2中選擇「全部」,那麼亞種應該等於「不適用」)。
供參考:
虎亞種= {紅,西伯利亞}
獅子亞種= {北非,西南非洲人,德蘭士瓦}
任何幫助理解。謝謝。
library(shiny)
if (interactive()) {
ui <- fluidPage(
selectizeInput("VarAnimal",
label = "Animal",
choices = c("Tiger", "Lion"),
selected = "Tiger"),
radioButtons("VarWholeOrSub",
"Whole or Sub",
choices = c("Whole species", "Subspecies"),
selected = "Whole species"),
selectizeInput("VarSubspecies",
label = "Subspecies",
choices = c("Not Applicable", "Bengal", "Siberian", "Barbary", "Southwest African", "Transvaal"),
selected = "")
)
server <- function(input, output, session) {
observe({
x <- input$VarWholeOrSub
if (input$VarWholeOrSub == "Whole species"){
x <- c("Not Applicable")} else{
x <- ifelse(input$VarAnimal == "Tiger", c("Bengal", "Siberian"), c("Barbary", "Southwest African", "Transvaal"))
}
updateSelectizeInput(session,
"VarSubspecies",
choices = x)
})
}
shinyApp(ui, server)
}
謝謝,它的工作! – David