根据闪亮 R 中的另一个选择输入值更改默认值

  • 本文关键字:默认值 选择 另一个 r shiny
  • 更新时间 :
  • 英文 :


我想要两个闪亮的selectInput。第一个有一个默认为"A"的选择。我希望第二个输入值根据第一个输入值的值默认。韦普如果第一个选择是A,则第二个选择的默认值为"LOW",如果第一个选择是C或D,则第二个选择的默认值为"HIGH"。我需要将第二个输入更改为任何内容的选项。

正如我的代码所示,我目前正在使用带有 uiOutput 的 selectInput 将其链接在一起。目前,第二个值的默认值始终为"LOW",即使我选择 C 或 D 也是如此。我希望能够选择 C,并将第二个选项默认为"HIGH">

我的代码:

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)

我找到了一个名为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)

最新更新