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