繁体   English   中英

R Shiny observeEvent 继续触发

[英]R Shiny observeEvent continues to trigger

我正在努力让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功能,但没有运气。 感谢您的建议。

您可能在renderText()而不是input$input_value周围使用了isolate()调用。 这应该为你做:

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

您可以将反应值带入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