首页 > 解决方案 > 如何将两个 actionButton 与两个单独的闪亮观察事件一起使用?

问题描述

我仍然有更新的问题,如如何在闪亮的 eventReactive 处理程序中侦听多个事件表达式中所述

有两个单独的 actionButton 响应不同的 observeEvent。来自 observeEvent 的值将被发送到 UI。

虽然我尝试了上述方法,但仍然有很多错误。这两个 actionButton 不能独立运行。

例如:

#rm(list = ls())
library(shiny)
ui <- shinyUI(bootstrapPage(


 column(12,align="center", div(textOutput('txfg'),style = "font-size:18px")),

 br(),
 actionButton("test1", "test1"),
 actionButton("test2", "test2"))

 )

 server <- shinyServer(function(input, output) {

           toListen <- reactive({
           list(input$test1,input$test2)
           })
     observeEvent(toListen(), {

      ################## two different observeEvent

            if(input$test1==0 && input$test2==0){
           return()
             }
           if(input$test1==1)
          { outputTest <- 'Hello World'}
           if(input$test2==1)
             { outputTest <-'World Hello'}
            })

       ################## 
             output$txfg <- renderText(outputTest)
       })

    shinyApp(ui, server)

标签: rshiny

解决方案


我目前也在研究 Shiny,看来最佳实践是为您的服务器和 ui 元素构建模块。让我重写(并评论)您的服务器部分:

server <- shinyServer(function(input, output) {

  observeEvent({input$test1: input$test2}, {
    output$Test <- ifelse(input$test1 == 0, NULL, 
                          ifelse(input$test1 == 1, "Hello World", NULL))
    output$Test <- ifelse(input$test2 == 0, NULL, 
                          ifelse(input$test2 == 1, "Hello World", NULL))
  })

  output$txfg <- renderText(output$Test)

}

推荐阅读