我正在努力让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))
Run Code Online (Sandbox Code Playgroud)
您将看到,单击按钮一次后,textOutput 将响应 textInput 更改而不是按钮单击。
我尝试过这种方法:
server = function(input, output, session) {
o = observeEvent(input$execute, {
output$report = renderText(input$input_value)
o$destroy
})
}
Run Code Online (Sandbox Code Playgroud)
没有效果。我也尝试过使用该isolate功能,但没有成功。感谢您的建议。
您的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))
Run Code Online (Sandbox Code Playgroud)