代码之家  ›  专栏  ›  技术社区  ›  geotheory

R闪亮的观察事件继续触发

  •  1
  • geotheory  · 技术社区  · 8 年前

    我正在努力让一个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 不走运地工作。感谢你的建议。

    1 回复  |  直到 8 年前
        1
  •  2
  •   Chris    8 年前

    你可能有 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))