Debian Codegraph @

man/cache_rds.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/cache.R
3 \name{cache_rds}
4 \alias{cache_rds}
5 \title{Cache the value of an R expression to an RDS file}
6 \usage{
7 cache_rds(
8 expr = {
9 },
10 rerun = FALSE,
11 file = "cache.rds",
12 dir = "cache/",
13 hash = NULL,
14 clean = getOption("xfun.cache_rds.clean", TRUE),
15 ...
16 )
17 }
18 \arguments{
19 \item{expr}{An R expression.}
20
21 \item{rerun}{Whether to delete the RDS file, rerun the expression, and save
22 the result again (i.e., invalidate the cache if it exists).}
23
24 \item{file}{The \emph{base} (see Details) cache filename under the directory
25 specified by the \code{dir} argument. If not specified and this function is
26 called inside a code chunk of a \pkg{knitr} document (e.g., an R Markdown
27 document), the default is the current chunk label plus the extension
28 \file{.rds}. The file can also be other formats (see Details).}
29
30 \item{dir}{The path of the RDS file is partially determined by \code{paste0(dir, file)}. If not specified and the \pkg{knitr} package is available, the
31 default value of \code{dir} is the \pkg{knitr} chunk option \code{cache.path} (so if
32 you are compiling a \pkg{knitr} document, you do not need to provide this
33 \code{dir} argument explicitly), otherwise the default is \file{cache/}. If you
34 do not want to provide a \code{dir} but simply a valid path to the \code{file}
35 argument, you may use \code{dir = ""}.}
36
37 \item{hash}{A \code{list} object that contributes to the MD5 hash of the cache
38 filename (see Details). It can also take a special character value
39 \code{"auto"}. Other types of objects are ignored.}
40
41 \item{clean}{Whether to clean up the old cache files automatically when
42 \code{expr} has changed.}
43
44 \item{...}{Other arguments to be passed to the saving method such as
45 \code{\link[=saveRDS]{saveRDS()}}.}
46 }
47 \value{
48 If the cache file does not exist, run the expression and save the
49 result to the file, otherwise read the cache file and return the value.
50 }
51 \description{
52 Save the value of an expression to a cache file. Next time the value is
53 loaded from the file if it exists. Please consider using \code{\link[=cache_exec]{cache_exec()}}
54 instead, which is more flexible and intelligent.
55 }
56 \details{
57 Note that the \code{file} argument does not provide the full cache filename. The
58 actual name of the cache file is of the form \file{BASENAME_HASH.rds}, where
59 \file{BASENAME} is the base name provided via the \file{file} argument (e.g.,
60 if \code{file = 'foo.rds'}, \code{BASENAME} would be \file{foo}), and \file{HASH} is
61 the MD5 hash (also called the \sQuote{checksum}) calculated from the R code
62 provided to the \code{expr} argument and the value of the \code{hash} argument, which
63 means when the code or the \code{hash} argument changes, the \file{HASH} string
64 may also change, and the old cache will be invalidated (if it exists). If you
65 want to find the cache file, look for \file{.rds} files that contain 32
66 hexadecimal digits (consisting of 0-9 and a-z) at the end of the filename.
67
68 The file can be other formats such as \code{.qs2}. See the \code{rw} argument of
69 \code{\link[=cache_exec]{cache_exec()}}.
70
71 The possible ways to invalidate the cache are: 1) change the code in \code{expr}
72 argument; 2) delete the cache file manually or automatically through the
73 argument \code{rerun = TRUE}; and 3) change the value of the \code{hash} argument. The
74 first two ways should be obvious. For the third way, it makes it possible to
75 automatically invalidate the cache based on changes in certain R objects. For
76 example, when you run \code{cache_rds({ x + y })}, you may want to invalidate the
77 cache to rerun \code{{ x + y }} when the value of \code{x} or \code{y} has been changed, and
78 you can tell \code{cache_rds()} to do so by \code{cache_rds({ x + y }, hash = list(x, y))}. The value of the argument \code{hash} is expected to be a list, but it can
79 also take a special value, \code{"auto"}, which means \code{cache_rds(expr)} will try
80 to automatically figure out the global variables in \code{expr}, return a list of
81 their values, and use this list as the actual value of \code{hash}. This behavior
82 is most likely to be what you really want: if the code in \code{expr} uses an
83 external global variable, you may want to invalidate the cache if the value
84 of the global variable has changed. Here a \dQuote{global variable} means a
85 variable not created locally in \code{expr}, e.g., for \code{cache_rds({ x <- 1; x + y })}, \code{x} is a local variable, and \code{y} is (most likely to be) a global
86 variable, so changes in \code{y} should invalidate the cache. However, you know
87 your own code the best. If you want to be completely sure when to invalidate
88 the cache, you can always provide a list of objects explicitly rather than
89 relying on \code{hash = "auto"}.
90
91 By default (the argument \code{clean = TRUE}), old cache files will be
92 automatically cleaned up. Sometimes you may want to use \code{clean = FALSE} (set
93 the R global option \code{options(xfun.cache_rds.clean = FALSE)} if you want
94 \code{FALSE} to be the default). For example, you may not have decided which
95 version of code to use, and you can keep the cache of both versions with
96 \code{clean = FALSE}, so when you switch between the two versions of code, it will
97 still be fast to run the code.
98 }
99 \note{
100 Changes in the code in the \code{expr} argument do not necessarily always
101 invalidate the cache, if the changed code is \code{\link{parse}}\code{d} to the same
102 expression as the previous version of the code. For example, if you have
103 run \code{cache_rds({Sys.sleep(5);1+1})} before, running \code{cache_rds({ Sys.sleep( 5 ) ; 1 + 1 })} will use the cache, because the two expressions are
104 essentially the same (they only differ in white spaces). Usually you can
105 add/delete white spaces or comments to your code in \code{expr} without
106 invalidating the cache. See the package vignette \code{vignette('xfun', package = 'xfun')} for more examples.
107
108 When this function is called in a code chunk of a \pkg{knitr} document, you
109 may not want to provide the filename or directory of the cache file,
110 because they have reasonable defaults.
111
112 Side-effects (such as plots or printed output) will not be cached. The
113 cache only stores the last value of the expression in \code{expr}.
114
115 The hash is calculated by saving the hash object to a temporary file via
116 \code{saveRDS()} and getting the MD5 digest of the file. Since the file
117 generated by \code{saveRDS()} may be platform-dependent, the cache may not be
118 platform-agnostic. In other words, the cache saved on one platform may be
119 invalidated on another platform even if nothing in the hash object has
120 changed. By comparison, \code{\link[=cache_exec]{cache_exec()}} doesn't have this problem.
121 }
122 \examples{
123 f = tempfile() # the cache file
124 compute = function(...) {
125 res = xfun::cache_rds({
126 Sys.sleep(1)
127 1:10
128 }, file = f, dir = "", ...)
129 res
130 }
131 compute() # takes one second
132 compute() # returns 1:10 immediately
133 compute() # fast again
134 compute(rerun = TRUE) # one second to rerun
135 compute()
136 unlink(paste0(f, "_*.rds"))
137 }
138 \seealso{
139 \code{\link[=cache_exec]{cache_exec()}}, which is more flexible (e.g., it supports in-memory
140 caching and different read/write methods for cache files).
141 }
142 \keyword{internal}