【问题标题】:How to use the two actionButton with two separate shiny observeEvent?如何将两个 actionButton 与两个单独的闪亮观察事件一起使用?
【发布时间】:2019-05-17 04:09:49
【问题描述】:

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

有两个单独的 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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

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

    【讨论】:

    • 您好 Elie,如果我单击交替的 actionButton,它效果不佳。是否可以在每个 observeEvent 之后清除 input$test 值?
    • 哦,那么您可以使用toggle(),如下所示:stackoverflow.com/questions/44586505/…
    【解决方案2】:

    谢谢艾莉。但是还是有问题,如下,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      相关资源
      最近更新 更多