我如何基于TRUE / FALSE复选框事件使用observeEvent触发操作(在本例中为updateSelectInput)?
例如,我想在test为TRUE时更新结果:
library(shiny)
ui<-fluidPage(
checkboxInput("test","Test",value=FALSE),
selectInput("result","Result",choices=c("1","2","3"),selected="1")
)
server<-function(input, output){
observeEvent(input$test{
updateSelectInput(session,"result",choices=c("1","2","3","4","5"),selected="1")
})
}
shinyApp(ui=ui,server=server)
答案 0 :(得分:1)
参见下文:
library(shiny)
ui<-fluidPage(
checkboxInput("test","Test",value=FALSE),
selectInput("result","Result",choices=c("1","2","3"),selected="1")
)
server<-function(input, output, session){
observeEvent(input$test,{
updateSelectInput(session,"result",choices=c("1","2","3","4","5"),selected="1")
}, ignoreInit = TRUE)
}
shinyApp(ui=ui,server=server)
我将session
添加到服务器function()
,在input$test
和ignoreInit = TRUE
之后加了一个逗号。
更新
library(shiny)
ui<-fluidPage(
checkboxInput("test","Test",value=FALSE),
selectInput("result","Result",choices=c("1","2","3"),selected="1")
)
server<-function(input, output, session){
observeEvent(input$test,{
updateSelectInput(session,"result",choices=if(input$test == FALSE){c("1","2","3")}else{c("1","2","3","4","5")},selected="1")
}, ignoreInit = TRUE)
}
shinyApp(ui=ui,server=server)