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 55.963 59.7105 61.8370 66.5945 1208.742 100
## r0() 72.584 76.8505 79.8975 88.1165 1382.514 100
## r1() 70.755 77.7110 80.3300 86.1960 1498.122 100
## r2() 71.089 77.1470 80.2465 86.3725 1308.765 100
## vc$x <- vc$x + 1 142.197 160.7380 211.4525 286.8470 879.705 100
## f_rc1() 383.490 446.5920 503.7500 557.8810 1732.827 100
## f_rc2() 541.795 607.6010 651.7015 712.5045 1895.144 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