如何将两个actionButton与两个单独的闪亮的watchEvent一起使用?

时间:2019-05-17 04:09:50

标签: r shiny

我仍然有How to listen for more than one event expression within a Shiny eventReactive handler中所述的更新问题。

有两个单独的actionButton响应不同的watchEvent。来自watchEvent的值将发送到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)

2 个答案:

答案 0 :(得分:0)

我目前也在研究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)

}

答案 1 :(得分:0)

感谢Elie。但是仍然存在如下问题,

  #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) {
     Test <- eventReactive(list(input$test1,input$test2), {

    xx <- ifelse(input$test1 == 0, 0, 
                          ifelse(input$test1 == 1, "Hello World", 0))
    xx <- ifelse(input$test2 == 0, 0, 
                          ifelse(input$test2 == 1, "Word World", 0))
    return(xx)   },ignoreInit = TRUE)
     output$txfg <- renderText(Test())    })


shinyApp(ui, server)