gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:18:38 27 4
gpt4 key购买 nike

如何基于 TRUE/FALSE 复选框事件使用 observeEvent 触发操作(在本例中为 updateSelectInput)?

例如我想在测试为真时更新结果:

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)

最佳答案

见下文:

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)

我在服务器 function() 中添加了 session,在 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)

关于r - 如何将 observeEvent 与复选框事件一起使用 Shiny,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57553852/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com