experimental

This pipe can only be used at any point in a pipe chain, but must be preceded by Cache(...) (which allows other Cache() %C% ... remaining pipes arguments to be passed).

This will take the input arguments of the first function immediately following the Cache() and the pipe chain until the special %C%, evaluate them both against the cacheRepo argument in Cache. If they exist, then the entire pipe chain will be skipped, and only the previous final result will be given. If there is no previous cached copy of the initial function's arguments, then all chain elements will be evaluated. The final result will be cached for future use. Therefore, the entire chain must be identical. The required usage should be straight forward to insert into existing code that uses pipes (Cache() %C% ... remaining pipes).

Still experimental and may change. This form cannot pass any arguments to ]codeCache, such as cacheRepo, thus it is of limited utility. However, it is a clean alternative for simple cases.

lhs %C% rhs

lhs %<% rhs

Arguments

lhs

A name to assign to.

rhs

A function call

Examples

library(magrittr) # standard pipe # dontrun{ # these can't be automatically run due to package conflicts with magrittr tmpdir <- file.path(tempdir(), "testCache") checkPath(tmpdir, create = TRUE)
#> [1] "C:/Users/EMCINT~1.L-V/AppData/Local/Temp/RtmpamdaBp/testCache"
a <- rnorm(10, 16) %>% mean() %>% prod(., 6) b <- Cache(cacheRepo = tmpdir) %C% # use of the %C% pipe! rnorm(10, 16) %>% # everything after here is NOT cached! mean() %>% prod(., 6)
#> Error in match.call(definition, call, expand.dots, envir): unused argument (envir = <environment>)
d <- Cache(cacheRepo = tmpdir) %C% rnorm(10, 16) %>% mean() %>% prod(., 6)
#> Error in match.call(definition, call, expand.dots, envir): unused argument (envir = <environment>)
e <- Cache(cacheRepo = tmpdir) %C% rnorm(10, 16) %>% mean() %>% prod(., 5) # changed
#> Error in match.call(definition, call, expand.dots, envir): unused argument (envir = <environment>)
all.equal(b,d) # TRUE
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'target' in selecting a method for function 'all.equal': object 'b' not found
all.equal(a,d) # different because 'a' uses a unique rnorm, 'd' uses the Cached rnorm
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'current' in selecting a method for function 'all.equal': object 'd' not found
# because the arguments to rnorm, i.e., 10 and 16, and # the subsequent functions in the chain, are identical all.equal(a,e) # different because the final function, prod, has a changed argument.
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'current' in selecting a method for function 'all.equal': object 'e' not found
########### # multiple random elements shows Cached sequence up to %C% a1 <- Cache(cacheRepo = tmpdir) %>% seq(1, 10) %>% rnorm(2, mean = .) %>% mean() %C% # Cache pipe here -- # means this pipe is the last one that is Cached rnorm(3, mean = .) %>% mean(.) %>% rnorm(4, mean = .) # Random 4 numbers, the mean is same each time
#> Error in match.call(definition, call, expand.dots, envir): unused argument (envir = <environment>)
a2 <- Cache(cacheRepo = tmpdir) %>% seq(1, 10) %>% rnorm(2, mean = .) %>% mean() %C% # Cache pipe here -- # means this pipe is the last one that is Cached rnorm(3, mean = .) %>% mean(.) %>% rnorm(4, mean = .) # Random 4 numbers, the mean is same each time
#> Error in match.call(definition, call, expand.dots, envir): unused argument (envir = <environment>)
sum(a1 - a2) # not 0 # i.e., numbers are different
#> Error in eval(expr, envir, enclos): object 'a1' not found
# NOW DO WITH CACHE AT END b1 <- Cache(cacheRepo = tmpdir) %>% seq(1, 10) %>% rnorm(2, mean = .) %>% mean() %>% # means this pipe is the last one that is Cached rnorm(3, mean = .) %>% mean(.) %C% # Cache pipe here -- rnorm(4, mean = .) # These are samethe mean is same each time
#> Error in match.call(definition, call, expand.dots, envir): unused argument (envir = <environment>)
b2 <- Cache(cacheRepo = tmpdir) %>% seq(1, 10) %>% rnorm(2, mean = .) %>% mean() %>% # means this pipe is the last one that is Cached rnorm(3, mean = .) %>% mean(.) %C% # Cache pipe here -- rnorm(4, mean = .) # These are samethe mean is same each time
#> Error in match.call(definition, call, expand.dots, envir): unused argument (envir = <environment>)
sum(b1 - b2) # 0 # i.e., numbers are same
#> Error in eval(expr, envir, enclos): object 'b1' not found
unlink(tmpdir, recursive = TRUE) #} # Equivalent a <- Cache(rnorm, 1)
#> No cacheRepo supplied and getOption('reproducible.cachePath') is inside a temporary directory; #> this will not persist across R sessions.
b %<% rnorm(1)
#> No cacheRepo supplied and getOption('reproducible.cachePath') is inside a temporary directory; #> this will not persist across R sessions.
#> ...(Object to retrieve (7072c305d8c69df0.rds))
#> loaded cached result from previous rnorm call,