0
votes

I still have updated problem as described at How to listen for more than one event expression within a Shiny eventReactive handler.

There were two separate actionButton responding to different observeEvent. The values from observeEvent will be sent to UI.

Although i try the above method, there were still many errors. The two actionButton could not be run independently.

For example:

#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

2 Answers

0
votes

I am currently working on Shiny too, and it appears that best practices are to build modules for your server and ui elements. Let me re-write (and comment) your server part:

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)

}
0
votes

Thanks Elie. But there are still problems, as following,

  #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)