R Shiny watchEvent继续触发

时间:2018-07-05 23:25:07

标签: r shiny

我正在努力使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函数,但是没有运气。感谢您的建议。

2 个答案:

答案 0 :(得分:2)

您可能将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))

答案 1 :(得分:0)

或者,您可以将反应性值带入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))