Using the isolate Function

For example, the next code creates an infinite loop because the observer will take a reactive dependency on x and count.

r <- reactiveValues(count = 0, x = 1)
observe({
  r$x
  r$count <- r$count + 1
})

As we don’t want to create dependency based on r$count we can isolate it.

r <- reactiveValues(count = 0, x = 1)

observe({
  r$x
  r$count <- isolate(r$count) + 1
})

r$x <- 1
r$x <- 2
r$count
#> [1] 2

r$x <- 3
r$count
#> [1] 3