| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/cache.R |
| 3 |
\name{cache_exec} |
| 4 |
\alias{cache_exec} |
| 5 |
\title{Cache the execution of an expression in memory or on disk} |
| 6 |
\usage{ |
| 7 |
cache_exec(expr, path = "cache/", id = NULL, ...) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{expr}{An R expression to be cached.} |
| 11 |
|
| 12 |
\item{path}{The path to save the cache. The special value \code{":memory:"} means |
| 13 |
in-memory caching. If it is intended to be a directory path, please make |
| 14 |
sure to add a trailing slash.} |
| 15 |
|
| 16 |
\item{id}{A stable and unique string identifier for the expression to be used |
| 17 |
to identify a unique copy of cache for the current expression from all |
| 18 |
cache files (or in-memory elements). If not provided, an MD5 digest of the |
| 19 |
\link{deparse}d expression will be used, which means if the expression does not |
| 20 |
change (changes in comments or white spaces do not matter), the \code{id} will |
| 21 |
remain the same. This may not be a good default is two identical |
| 22 |
expressions are cached under the same \code{path}, because they could overwrite |
| 23 |
each other's cache when one expression's cache is invalidated, which may or |
| 24 |
may not be what you want. If you do not want that to happen, you need to |
| 25 |
manually provide an \code{id}.} |
| 26 |
|
| 27 |
\item{...}{More arguments to control the behavior of caching (see |
| 28 |
\sQuote{Details}).} |
| 29 |
} |
| 30 |
\value{ |
| 31 |
If the cache is found, the cached value of the expression will be |
| 32 |
loaded and returned (other local variables will also be lazy-loaded into |
| 33 |
the current environment as a side-effect). If cache does not exist, the |
| 34 |
expression is executed and its value is returned. |
| 35 |
} |
| 36 |
\description{ |
| 37 |
Caching is based on the assumption that if the input does not change, the |
| 38 |
output will not change. After an expression is executed for the first time, |
| 39 |
its result will be saved (either in memory or on disk). The next run will be |
| 40 |
skipped and the previously saved result will be loaded directly if all |
| 41 |
external inputs of the expression remain the same, otherwise the cache will |
| 42 |
be invalidated and the expression will be re-executed. |
| 43 |
} |
| 44 |
\details{ |
| 45 |
Arguments supported in \code{...} include: |
| 46 |
\itemize{ |
| 47 |
\item \code{vars}: Names of local variables (which are created inside the expression). |
| 48 |
By default, local variables are automatically detected from the expression |
| 49 |
via \code{\link[=find_locals]{find_locals()}}. Locally created variables are cached along with the |
| 50 |
value of the expression. |
| 51 |
\item \code{hash} and \code{extra}: R objects to be used to determine if cache should be |
| 52 |
loaded or invalidated. If (the MD5 hash of) the objects is not changed, the |
| 53 |
cache is loaded, otherwise the cache is invalidated and rebuilt. By default, |
| 54 |
\code{hash} is a list of values of global variables in the expression (i.e., |
| 55 |
variables created outside the expression). Global variables are automatically |
| 56 |
detected by \code{\link[=find_globals]{find_globals()}}. You can provide a vector of names to override |
| 57 |
the automatic detection if you want some specific global variables to affect |
| 58 |
caching, or the automatic detection is not reliable. You can also provide |
| 59 |
additional information via the \code{extra} argument. For example, if the |
| 60 |
expression reads an external file \code{foo.csv}, and you want the cache to be |
| 61 |
invalidated after the file is modified, you may use \code{extra = file.mtime("foo.csv")}. |
| 62 |
\item \code{keep}: By default, only one copy of the cache corresponding to an \code{id} |
| 63 |
under \code{path} is kept, and all other copies for this \code{id} is automatically |
| 64 |
purged. If \code{TRUE}, all copies of the cache are kept. If \code{FALSE}, all copies |
| 65 |
are removed, which means the cache is \emph{always} invalidated, and can be useful |
| 66 |
to force re-executing the expression. |
| 67 |
\item \code{rw}: A list of functions to read/write the cache files. The list is of the |
| 68 |
form \code{list(name = 'xxx', load = function(file) {}, save = function(x, file) {})}. By default, \code{\link[=readRDS]{readRDS()}} and \code{\link[=saveRDS]{saveRDS()}} are used. This argument can |
| 69 |
also take a character string to use some built-in read/write methods. |
| 70 |
Currently available methods include \code{rds} (the default), \code{raw} (using |
| 71 |
\code{\link[=serialize]{serialize()}} and \code{\link[=unserialize]{unserialize()}}), \code{qs2} (using \code{\link[qs2:qs_read]{qs2::qs_read()}} and |
| 72 |
\code{\link[qs2:qs_save]{qs2::qs_save()}}), and \code{qdata} (using \code{\link[qs2:qd_read]{qs2::qd_read()}} and \code{\link[qs2:qd_save]{qs2::qd_save()}}). |
| 73 |
The \code{rds} and \code{raw} methods only use base R functions (the \code{rds} method |
| 74 |
generates smaller files because it uses compression, but is often slower than |
| 75 |
the \code{raw} method, which does not use compression). The \code{qs2} and \code{qdata} |
| 76 |
methods require the \pkg{qs2} package, which can be much faster than base R |
| 77 |
methods and also supports compression. |
| 78 |
} |
| 79 |
} |
| 80 |
\examples{ |
| 81 |
# the first run takes about 1 second |
| 82 |
y1 = xfun::cache_exec({ |
| 83 |
x = rnorm(1e+05) |
| 84 |
Sys.sleep(1) |
| 85 |
x |
| 86 |
}, path = ":memory:", id = "sim-norm") |
| 87 |
|
| 88 |
# the second run takes almost no time |
| 89 |
y2 = xfun::cache_exec({ |
| 90 |
# comments won't affect caching |
| 91 |
x = rnorm(1e+05) |
| 92 |
Sys.sleep(1) |
| 93 |
x |
| 94 |
}, path = ":memory:", id = "sim-norm") |
| 95 |
|
| 96 |
# y1, y2, and x should be identical |
| 97 |
stopifnot(identical(y1, y2), identical(y1, x)) |
| 98 |
} |
| 99 |
\references{ |
| 100 |
See \url{https://pkg.yihui.org/litedown/book/#sec:option-cache} for |
| 101 |
how it works and an application in \pkg{litedown}. |
| 102 |
} |