Last active
August 29, 2015 14:02
-
-
Save wch/12dcf7164619baca2d72 to your computer and use it in GitHub Desktop.
Time tests for reactives
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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