如何将observeEvent与复选框事件Shiny一起使用

时间:2019-08-19 09:23:53

标签: r checkbox shiny

我如何基于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)

1 个答案:

答案 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$testignoreInit = 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)