cacheAssign: Lazy cache assignment operator

Description Usage Arguments Details Value Note Author(s) Examples

Description

This is very experimental. Alternative assignment operator to <-

Usage

1
2
3
4
assignCache(x, y, lazyDir = NULL, notOlderThan = NULL,
  envir = sys.frame(0))

x %<% y

Arguments

x

the left hand side objectName to assign to, analogous to <-.

y

the right hand side. Anything that R understands,

lazyDir

the lazyDir to use

notOlderThan

see cache

envir

The environment to be passed to substitute internally. This may help with programming control.

Details

This does three things in this order:

1. Take a digest::digest of the right hand side function arguments 2. Similar to cache in archivist package, it compares this digest to the lazyDir database. If it that exact digest exists already, then it will lazyLoad2 it from the lazyDir. There are 2 differences from the cache function. First, the object name that is the "assigment" recipient is included in the caching and it is saved to a lazy load database on disk. 3. Assigns in memory the result of the call (y) to x as in the normal <- operator.

Known features: caching is based on both the left side (the object name assigned to) and the the right hand side operator (the call itself). This may or may not be the best behaviour and will be revisited with usage.

Known limitations: currently, the right hand side must be a simple function call, and can't include a magrittr pipe operator.

If writing and reading from disk the outputs and inputs, respectively, takes longer than evaluating the expression, then there is no point in caching.

Likewise, if the function returns a result that is stochastic (i.e., will be different each time the function is called, even with the same input arguments), caching may not give the desired behaviour. It will always return the same result from a output

Value

Evaluation or lazy load of the right hand side (y), assigned to the objectName (x) on the left hand side.

Note

Because this works as an assignment operator, any arguments other than the x and y are not changeable unless it is used as a function call using back ticks assignCache(x, y, notOlderThan = Sys.time())

Author(s)

Eliot McIntire

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
lazyDir(tempdir(), create=TRUE)
# First time will evaluate it, create a hash, save to lazy database, return result. Will be slow
system.time(a%<%seq(1,1e6))

# Second time will return the value in the lazy load database because arguments are identical
system.time(a%<%seq(1,1e6))

# Third time - but force re-evaluation
system.time(assignCache("a", seq(1,1e6), notOlderThan = Sys.time()))

# For comparison, normal assignment may be faster if it is a fast function in R
system.time(a<-seq(1,1e6))

lazyRm("a")

PredictiveEcology/lazyR documentation built on May 8, 2019, 3:10 p.m.