您如何检查 R Shiny 中的所有后代是否干净(未失效)?

How do you check whether all descendants are clean (not invalidated) in R Shiny?

假设我在 Shiny 中有一个观察者,如下所示:

observe({
  input$obs_1
  input$obs_2
  input$obs_3

  # do "something"
  # ...
  # ...

  # If the code above finished doing "something," set a reactive value
  rv$something_is_done <- FALSE
  rv$something_is_done <- TRUE
})

上面的观察者会在每次 obs_1obs_2obs_3 更改时执行 "something"。

如何检查是否所有输入都已完成更改,并且 do something 代码块最后一次 运行 它的进程?

一旦 returns 上方的观察者变为 "idle"(返回监听三个输入),我希望最后的反应值 rv$something_is_done 触发 "another thing" 作为结果发生,可能与下面类似:

observe({
  rv$something_is_done
  if(rv$something_is_done) {
    # do "another thing"
    # ...
    # ...
  }
})

Observers一直在监听变化。鉴于您在 obs_1, obs_2,... 中有预设值,它只会关注那些。因此,它不知道或不关心您是否考虑其他 2 个输入来等待更改。如果你愿意,你可以做的是用 debounce 延迟这种反应。请注意,在设置所有参数后,它设置为等待 3 秒。我已将 3 个变量添加到列表中,以便您稍后可以将其用作 mystuff_d()

library(shiny)
library(magrittr)

ui <- fluidPage(
    sliderInput("obs_1", "obs_1", min = 0, max = 1000, value = 500),
    sliderInput("obs_2", "obs_2", min = 0, max = 1000, value = 500),
    sliderInput("obs_3", "obs_3", min = 0, max = 1000, value = 500)
)

rv <- reactiveValues(something_is_done <- FALSE)

server <- function(input,output,session){

    mystuff <- eventReactive(c(input$obs_1,input$obs_2,input$obs_3),{
        rv$something_is_done <- FALSE
        c(input$obs_1,input$obs_2,input$obs_3)
    },ignoreInit = TRUE)

    mystuff_d <- mystuff %>% debounce(3000)

    observeEvent(mystuff_d(),{
        print(mystuff_d())
        # do "something"
        # ...
        # ...
        rv$something_is_done <- TRUE
    })

    observeEvent(rv$something_is_done,{
       if(rv$something_is_done){
           # do "another thing"
           # ...
           # ...
           print("another thing")
       }
    },ignoreInit = TRUE)

}
shinyApp(ui=ui,server = server)