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 54.662 60.2020 62.1860 64.2050 1160.546 100
## r0() 53.347 58.5245 60.9265 63.1895 280.850 100
## r1() 53.264 59.1895 61.5585 64.4085 142.030 100
## r2() 53.019 58.5310 61.4965 64.5650 820.949 100
## vc$x <- vc$x + 1 144.999 158.4605 203.0445 258.5035 325.054 100
## f_rc1() 367.687 408.9990 456.9355 496.5965 1686.687 100
## f_rc2() 518.295 571.9160 600.6425 646.6310 2089.693 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.9006
##
## loaded via a namespace (and not attached):
## [1] digest_0.6.4 evaluate_0.5.5 formatR_1.0 htmltools_0.2.6
## [5] httpuv_1.3.0 knitr_1.6.12 R6_2.0.0.9000 Rcpp_0.11.2
## [9] RJSONIO_1.3-0 rmarkdown_0.2.54 stringr_0.6.2 tools_3.1.1
## [13] xtable_1.7-3 yaml_2.1.13