简体   繁体   中英

How to use observeEvent with a checkbox event Shiny

How do I trigger an action (in this case updateSelectInput) with observeEvent based on a TRUE/FALSE checkbox event?

For example I would like to update result when test is 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)

See below:

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)

I added session to the server function() , a comma after input$test , and ignoreInit = TRUE .

Update

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM