Skip to content

Instantly share code, notes, and snippets.

@wch
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wch/12dcf7164619baca2d72 to your computer and use it in GitHub Desktop.
Save wch/12dcf7164619baca2d72 to your computer and use it in GitHub Desktop.
Time tests for reactives
library(shiny)
# Reactives with no changes (after first)
v <- reactiveValues(x = 0)
r0 <- reactive(1) # 0 levels of dependence
r1 <- reactive(v$x) # 1 level
r2 <- reactive(r1()) # 2 levels
# Reactives with change
vc <- reactiveValues(x = 0)
rc1 <- reactive(vc$x)
f_rc1 <- function() {
vc$x <- vc$x + 1
rc1()
}
rc2 <- reactive(rc1())
f_rc2 <- function() {
vc$x <- vc$x + 1
rc2()
}
library(microbenchmark)
isolate(microbenchmark(
v$x, # Use reactive value directly
r0(), # reactive with 0 levels
r1(), # reactive with 1 level
r2(), # reactive with 2 levels
vc$x <- vc$x + 1, # Change reactive value
f_rc1(), # Change reactive value, then call a dependent reactive (1 level)
f_rc2() # Change reactive value, then call a dependent reactive (2 levels)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment