我是Shiny的新手,我在SO上的类似问题的答案正在帮助我。
我想在下拉菜单中显示动态选项,具体取决于用户在单选按钮上选择的内容。但是下面的片段只能在用户选择时显示亚种的一种选择,首先是#1动物类型,然后是#2整体与亚种选择。
我有三个变量:#1。动物类型(狮子与老虎); #2。整体与亚种(radiobutton); #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)
}
答案 0 :(得分:2)
问题来自使用ifelse(test, yes, no)
当是或否有不同的测试时间时:
> ifelse("a"=="a", c("a","b"), c("b","a"))
[1] "a"
> ifelse(rep("a"=="a",2), c("a","b"), c("b","a"))
[1] "a" "b"
在您的情况下 - 有3种不同的长度 - 您应该使用if
代替:
if (input$VarWholeOrSub == "Whole species"){
x <- c("Not Applicable")
} else {
if(input$VarAnimal == "Tiger")
x <- c("Bengal", "Siberian")
else
x <- c("Barbary", "Southwest African", "Transvaal")
}
答案 1 :(得分:1)
ifelse的文档说明:
ifelse返回一个与test相同形状的值,该值填充了从yes或no中选择的元素,具体取决于test元素是TRUE还是FALSE。
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({
if (input$VarWholeOrSub == "Whole species"){
updateSelectizeInput(session, 'VarSubspecies', choices = c("Not Applicable"))
} else{
if(input$VarAnimal == "Tiger"){
updateSelectizeInput(session, 'VarSubspecies', choices = c("Bengal", "Siberian"))
} else if (input$VarAnimal == "Lion"){
updateSelectizeInput(session, 'VarSubspecies', choices = c("Barbary", "Southwest African", "Transvaal"))
}
}
})
}
shinyApp(ui, server)
}
答案 2 :(得分:1)
几点:
(1)如前文所述,ifelse
是罪魁祸首。
?ifelse
说
ifelse(测试,是,否)
ifelse返回一个相同形状的值作为test
在您的情况下,您应该按照建议使用if-else块。
(2)您可能希望在代码中使用较少的硬编码,通过分离数据(可能是全局的,以便以后您不需要更改R代码来更改数据)和获取摆脱一些冗余代码:
df <- data.frame(Animals = c("Tiger", "Lion"),
WholeSpecies=rep("Not Applicable", 2),
SubSpecies=c("Bengal, Siberian", "Barbary, Southwest African, Transvaal"),
stringsAsFactors = FALSE)
head(df)
# Animals WholeSpecies SubSpecies
#1 Tiger Not Applicable Bengal, Siberian
#2 Lion Not Applicable Barbary, Southwest African, Transvaal
library(shiny)
if (interactive()) {
ui <- fluidPage(
selectizeInput("VarAnimal",
label = "Animal",
choices = df$Animals,
selected = df$Animals[1]),
radioButtons("VarWholeOrSub",
"Whole or Sub",
choices = c("Whole species", "Subspecies"),
selected = "Whole species"),
selectizeInput("VarSubspecies",
label = "Subspecies",
choices = c(unique(df$WholeSpecies), unlist(strsplit(unique(df$SubSpecies), split=','))),
selected = "")
)
server <- function(input, output, session) {
observe({
x <- df[df$Animals==input$VarAnimal,]$WholeSpecies
if (input$VarWholeOrSub == "Subspecies"){
x <- unlist(strsplit(df[df$Animals==input$VarAnimal,]$SubSpecies, split=','))
}
updateSelectizeInput(session,
"VarSubspecies",
choices = x)
})
}
shinyApp(ui, server)
}