cache {depcache}R Documentation

Evaluate an expression and cache its results

Description

This function extracts all dependencies of an R expression, hashes them together with the expression itself and either loads the already-existing file, or evaluates the expression and stores the result in that file.

Usage

  cache(expr, extra = NULL, ...)

Arguments

expr

An expression to evaluate or load from cache, unquoted.

extra

Any R value that should be considered part of the state deciding whether the expression should be re-computed. For example, if expr reads a file, consider using file.mtime or md5sum to check for changes in it.

...

Additional options, see depcache.options.

Details

Currently, the hash is obtained by means of serialisation. In order to make semantically same values have same hashes on a wide range of R versions, the following steps were taken:

The exact algorithm used and the way hash is obtained are implementation details and may eventually change, though not without a good reason.

Other aspects of R data structures are currently not handled:

Value

The result of evaluating expr, either directly, or loaded from cache.

See Also

setCached

Examples

  
  a <- 1
  # will evaluate the expression the first time
  cache({ message('evaluating expression'); a + 1 }) # 2
  # saved value of the expression will be used
  cache({
    message('evaluating expression')
    # even if written a bit differently
    a + 1
  }) # 2
  a <- -1
  # expression evaluated again because dependencies changed
  cache({ message('evaluating expression'); a + 1 }) # 0
  

[Package depcache version 0.1-2 Index]