How long does it take to call reactives and access reactiveValues?
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)
))
## Unit: microseconds
## expr min lq median uq max neval
## v$x 147.579 155.7295 161.7945 171.0895 1543.066 100
## r0() 313.085 336.3540 350.3150 391.8150 2690.881 100
## r1() 313.028 327.2900 343.7225 364.4280 1797.432 100
## r2() 315.150 335.6495 349.1265 375.7345 4704.337 100
## vc$x <- vc$x + 1 332.735 399.1075 684.4725 1142.8200 2653.368 100
## f_rc1() 2223.533 2523.4550 2929.7770 3378.7445 5135.573 100
## f_rc2() 3809.190 4202.9795 4632.9100 5245.2045 44846.423 100
sessionInfo()
## R version 3.1.1 (2014-07-10)
## Platform: x86_64-apple-darwin13.1.0 (64-bit)
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] microbenchmark_1.3-0 shiny_0.10.1.9004
##
## loaded via a namespace (and not attached):
## [1] bitops_1.0-6 caTools_1.17 digest_0.6.4 evaluate_0.5.5
## [5] formatR_1.0 htmltools_0.2.6 httpuv_1.3.0 knitr_1.6.12
## [9] Rcpp_0.11.2 RJSONIO_1.3-0 rmarkdown_0.2.54 stringr_0.6.2
## [13] tools_3.1.1 xtable_1.7-3 yaml_2.1.13