R/utils_reactive_changes.R

Defines functions reactive_changes

Documented in reactive_changes

#' Reactive Changes
#'
#' Create a reactive that is only updated when the expression returns a different vector to what is currently stored
#'
#' @param expr an expression that gets the values to observe whether they are changing
#'
#' @import shiny
#' @import rlang
#'
#' @return a reactiveVal
reactive_changes <- function(expr) {
  env <- parent.frame()
  mask <- new_data_mask(env)
  expr <- enquo(expr)
  rv <- reactiveVal()
  observe({
    nv <- eval_tidy(expr, mask)
    ov <- rv()
    if (!(length(nv) == length(rv) && all(nv == ov))) {
      rv(nv)
    }
  })
  class(rv) <- c("reactive_changes", class(rv))
  invisible(rv)
}
The-Strategy-Unit/723_mh_covid_surge_modelling documentation built on April 13, 2022, 8:52 a.m.