简体   繁体   English

R Shiny observeEvent 继续触发

[英]R Shiny observeEvent continues to trigger

I'm struggling to get an observeEvent process to run only once after it's triggering event - a button click.我正在努力让observeEvent 进程在触发事件后只运行一次 - 单击按钮。 This illustrates:这说明:

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

You'll see that after the button has been clicked once, the textOutput becomes responsive to textInput changes rather than button clicks.您将看到,在单击按钮一次后,textOutput 将响应 textInput 更改而不是按钮单击。

I've tried this approach:我试过这种方法:

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

No effect.没有效果。 I've also tried employing the isolate function with no luck.我也尝试过使用isolate功能,但没有运气。 Grateful for suggestions.感谢您的建议。

You probably had your isolate() call wrapped around renderText() instead of input$input_value .您可能在renderText()而不是input$input_value周围使用了isolate()调用。 This should do it for you:这应该为你做:

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

Alliteratively you can bring the reactive values into an isolated scope of observeEvent() as below:您可以将反应值带入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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM