【问题标题】:R Shiny observeEvent continues to triggerR Shiny observeEvent 继续触发
【发布时间】:2018-12-14 12:22:36
【问题描述】:

我正在努力让 observeEvent 进程在触发事件后只运行一次 - 单击按钮。这说明:

require(shiny)

ui = fluidPage(
  textInput("input_value", '1. input a value. 2. click button. 3. input another value', ''),
  actionButton("execute", 'execute'),
  textOutput('report')
)

server = function(input, output, session) {
  observeEvent(input$execute, {
    output$report = renderText(input$input_value)
  })
}

shinyApp(ui = ui, server = server, options = list(launch.browser = T))

您会看到,在单击按钮一次后,textOutput 会响应 textInput 更改而不是按钮单击。

我试过这种方法:

server = function(input, output, session) {
  o = observeEvent(input$execute, {
    output$report = renderText(input$input_value)
    o$destroy
  })
}

没有效果。我也尝试过使用isolate 函数,但没有成功。感谢您的建议。

【问题讨论】:

  • 这是一个非常有趣的问题。我(闪亮的新手)认为你的代码应该像发布的那样工作。我在这里问了一个后续问题:community.rstudio.com/t/…

标签: r shiny


【解决方案1】:

您的isolate() 呼叫可能围绕renderText() 而不是input$input_value。这应该为你做:

require(shiny)

ui = fluidPage(
  textInput("input_value", '1. input a value. 2. click button. 3. input another value', ''),
  actionButton("execute", 'execute'),
  textOutput('report')
)

server = function(input, output, session) {
  observeEvent(input$execute, {
    output$report = renderText(isolate(input$input_value))
  })
}

shinyApp(ui = ui, server = server, options = list(launch.browser = T))

【讨论】:

    【解决方案2】:

    您可以将响应式值带入observeEvent() 的隔离范围,如下所示:

    library(shiny)
    
    ui = fluidPage(
      textInput("input_value", '1. input a value. 2. click button. 3. input another value', ''),
      actionButton("execute", 'execute'),
      textOutput('report')
    )
    
    server = function(input, output, session) {
      observeEvent(input$execute, {
    
        # bringing reactive values into an isolated scope
        inp_val <- input$input_value
    
        output$report <- renderText(inp_val)
      })
    }
    
    shinyApp(ui = ui, server = server, options = list(launch.browser = T))
    

    【讨论】:

      猜你喜欢
      • 2019-08-01
      • 2018-03-12
      • 2017-11-06
      • 2021-09-05
      • 2022-01-19
      • 2018-12-05
      • 2021-09-07
      • 2021-11-26
      • 2021-11-14
      相关资源
      最近更新 更多