【问题标题】:Updating a reactive 'value' using itself in R Shiny在 R Shiny 中使用自身更新反应式“值”
【发布时间】:2023-02-25 05:31:55
【问题描述】:

我正在尝试构建一个仪表板来跟踪我在 bugzilla 中的组错误。检索此数据的查询很慢,所以我只想检索已更改的错误并更新本地副本。

我有一个函数“get_bugzilla”,它返回所有内容,或者如果提供了时间戳,则在该时间戳之后所有内容都发生了变化。

我目前以反应方式使用它的最佳尝试是:

poll<-reactiveTimer(intervalMs = 10000)
ckbdata<-reactive({get_bugzilla()})
ckbdata<-reactive({
    poll()
    wip<-ckbdata()
    new<-get_bugzilla(max(wip[['last_change_time']]))
    if(length(new)>0){
        wip<-wip[!(id %in% new[['id']]),]
        wip<-rbind(wip,new)
    }
    wip
})

这会产生错误“评估嵌套太深:无限递归/选项(表达式=)?”,这是我担心的事情。但是我无法找到正确的方法。

【问题讨论】:

  • 要更新仪表板的反应部分,我们使用 reactiveValues()reactiveVal() 而不是 reactive()。为了展示如何实现这一点,我们需要一个可重现的示例(一个工作仪表板)。

标签: r shiny shiny-reactivity


【解决方案1】:

这是因为存在无限循环,即反应表达式之一正在调用另一个,反之亦然。 ckbdata 被定义为反应式表达式两次,这很可能导致无限递归。

也许这行得通

poll <- reactiveTimer(intervalMs = 10000)

ckbdata <- reactive()
  poll()
  current_data <- isolate(ckbdata())
  # retrieve all the data, if the current data is empty
  if (is.null(current_data)) {
    get_bugzilla()
  } else {
    get_bugzilla(max(current_data[['last_change_time']]))
  }
})
ckbdata_updated <- reactive({
  current_data <- ckbdata()
  # Update the local copy in case of modification or bug
  new_data <- get_bugzilla(max(current_data[['last_change_time']]))
  if (length(new_data) > 0) {
    current_data <- current_data[!(current_data$id %in% new_data[['id']]),]
    current_data <- rbind(current_data, new_data)
  }
  current_data
})

干杯

【讨论】:

    猜你喜欢
    • 2020-04-23
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 2018-11-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    相关资源
    最近更新 更多