[英]change default values based on another selectInput value in shiny R
I want two selectInput's in shiny. 我想要两个selectInput闪亮。 The first one has a selection with default 'A'. 第一个选择为默认值“ A”。 I want the second input value to default based on the value of the first. 我希望第二个输入值基于第一个输入的值成为默认值。 ie. 即。 If the First selection is A, then the default of the second is 'LOW', if the first selection is C or D, the default of the second is 'HIGH'. 如果第一个选择为A,则第二个的默认值为'LOW';如果第一个选择为C或D,则第二个的默认值为'HIGH'。 I need to have the option of the second input to be changed to anything. 我需要将第二个输入更改为任何内容。
I am currently using a selectInput with a uiOutput to link it together, as my code shows. 正如我的代码所示,我目前正在使用带有uiOutput的selectInput将其链接在一起。 Currently, the default of the second value is always 'LOW', even when I select C or D. I want to be able to select C, and have the second choice default to 'HIGH' 当前,即使选择C或D,第二个值的默认值始终为“低”。我希望能够选择C,并将第二个选项的默认值设置为“高”。
My code: 我的代码:
df=data.frame(expand.grid(col1=LETTERS[1:4],col2=c('LOW','MEDIUM','HIGH')))
ui = fluidPage(
column(4,
selectInput('d1','Drop 1',
choices = sort(unique(df$col1)),
selected = sort(df$col1)[1]),
uiOutput("secondSelection")
)
)
server <- function(input, output, session) {
output$secondSelection = renderUI({
selectInput("User", "Value Dependent on Drop 1:",
choices = as.character(df[df$col1==input$d1,"col2"]))
})
}
shinyApp(ui, server)
I found a function called updateselectinput, and added it in to fix the problem 我找到了一个名为updateselectinput的函数,并将其添加到其中以解决该问题。
df=data.frame(expand.grid(col1=LETTERS[1:4],col2=c('LOW','MEDIUM','HIGH')))
ui = fluidPage(
column(4,
selectInput('d1','Drop 1',
choices = sort(unique(df$col1)),
selected = sort(df$col1)[1]),
selectInput('d2','Drop 2',
choices = sort(unique(df$col2))
)
)
)
server <- function(input, output, session) {
observe({
x = input$d1
if(x=='A'){
updateSelectInput(session,'d2',
choices = sort(unique(df$col2)),
selected = 'LOW')
}else if(x=='C' || x=='D'){
updateSelectInput(session,'d2',
choices = sort(unique(df$col2)),
selected = 'HIGH')
}else{
updateSelectInput(session,'d2',
choices = sort(unique(df$col2)),
selected = 'MEDIUM')
}
})
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.