These are convenience wrappers around DBI
package functions.
They allow the user a bit of control over what is being cached.
clearCache(
x,
userTags = character(),
after = NULL,
before = NULL,
ask = getOption("reproducible.ask"),
useCloud = FALSE,
cloudFolderID = getOption("reproducible.cloudFolderID", NULL),
drv = getOption("reproducible.drv", RSQLite::SQLite()),
conn = getOption("reproducible.conn", NULL),
...
)# S4 method for ANY
clearCache(
x,
userTags = character(),
after = NULL,
before = NULL,
ask = getOption("reproducible.ask"),
useCloud = FALSE,
cloudFolderID = getOption("reproducible.cloudFolderID", NULL),
drv = getOption("reproducible.drv", RSQLite::SQLite()),
conn = getOption("reproducible.conn", NULL),
...
)
cc(secs, ...)
showCache(
x,
userTags = character(),
after = NULL,
before = NULL,
drv = getOption("reproducible.drv", RSQLite::SQLite()),
conn = getOption("reproducible.conn", NULL),
...
)
# S4 method for ANY
showCache(
x,
userTags = character(),
after = NULL,
before = NULL,
drv = getOption("reproducible.drv", RSQLite::SQLite()),
conn = getOption("reproducible.conn", NULL),
...
)
keepCache(
x,
userTags = character(),
after = NULL,
before = NULL,
ask = getOption("reproducible.ask"),
drv = getOption("reproducible.drv", RSQLite::SQLite()),
conn = getOption("reproducible.conn", NULL),
...
)
# S4 method for ANY
keepCache(
x,
userTags = character(),
after = NULL,
before = NULL,
ask = getOption("reproducible.ask"),
drv = getOption("reproducible.drv", RSQLite::SQLite()),
conn = getOption("reproducible.conn", NULL),
...
)
Will clear all objects (or those that match userTags
, or those
between after
or before
) from the repository located at
cachePath
of the sim object, if sim
is provided, or located in
cacheRepo
.
Invisibly returns a data.table
of the removed items.
A simList or a directory containing a valid Cache repository. Note:
For compatibility with Cache
argument, cacheRepo
can also be
used instead of x
, though x
will take precedence.
Character vector. If used, this will be used in place of the
after
and before
.
Specifying one or more userTag
here will clear all
objects that match those tags.
Matching is via regular expression, meaning partial matches
will work unless strict beginning (^) and end ($) of string
characters are used.
Matching will be against any of the 3 columns returned by showCache()
,
i.e., artifact
, tagValue
or tagName
.
Also, length userTags
> 1, then matching is by `and`.
For `or` matching, use |
in a single character string.
See examples.
A time (POSIX, character understandable by data.table). Objects cached after this time will be shown or deleted.
A time (POSIX, character understandable by data.table). Objects cached before this time will be shown or deleted.
Logical. If FALSE
, then it will not ask to confirm deletions using
clearCache
or keepCache
. Default is TRUE
Logical. If TRUE
, then every object that is deleted locally will
also be deleted in the cloudFolderID
, if it is non-NULL
A googledrive dribble of a folder, e.g., using drive_mkdir()
.
If left as NULL
, the function will create a cloud folder with name from last
two folder levels of the cacheRepo
path, :
paste0(basename(dirname(cacheRepo)), "_", basename(cacheRepo))
.
This cloudFolderID
will be added to options("reproducible.cloudFolderID")
,
but this will not persist across sessions. If this is a character string, it will
treat this as a folder name to create or use on GoogleDrive.
an object that inherits from DBIDriver, or an existing DBIConnection object (in order to clone an existing connection).
A DBIConnection object, as returned by
dbConnect()
.
Other arguments. Currently, regexp
, a logical, can be provided.
This must be TRUE
if the use is passing a regular expression.
Otherwise, userTags
will need to be exact matches. Default is
missing, which is the same as TRUE
. If there are errors due
to regular expression problem, try FALSE
. For cc
, it is
passed to clearCache
, e.g., ask
, userTags
Currently 3 options: the number of seconds to pass to clearCache(after = secs)
,
a POSIXct
time e.g., from Sys.time()
, or missing. If missing,
the default, then it will delete the most recent entry in the Cache.
If neither after
or before
are provided, nor userTags
,
then all objects will be removed.
If both after
and before
are specified, then all objects between
after
and before
will be deleted.
If userTags
is used, this will override after
or before
.
cc(secs)
is just a shortcut for clearCache(repo = Paths$cachePath, after = secs)
,
i.e., to remove any cache entries touched in the last secs
seconds.
clearCache
remove items from the cache based on their
userTag
or times
values.
keepCache
remove all cached items except those based on
certain userTags
or times
values.
showCache
display the contents of the cache.
mergeCache
. Many more examples
in Cache
.
library(raster)
tmpDir <- file.path(tempdir(), "reproducible_examples", "Cache")
try(clearCache(tmpDir, ask = FALSE), silent = TRUE) # just to make sure it is clear
# Basic use
ranNumsA <- Cache(rnorm, 10, 16, cacheRepo = tmpDir)
# All same
ranNumsB <- Cache(rnorm, 10, 16, cacheRepo = tmpDir) # recovers cached copy
ranNumsD <- Cache(quote(rnorm(n = 10, 16)), cacheRepo = tmpDir) # recovers cached copy
# Any minor change makes it different
ranNumsE <- Cache(rnorm, 10, 6, cacheRepo = tmpDir) # different
## Example 1: basic cache use with tags
ranNumsA <- Cache(rnorm, 4, cacheRepo = tmpDir, userTags = "objectName:a")
ranNumsB <- Cache(runif, 4, cacheRepo = tmpDir, userTags = "objectName:b")
ranNumsC <- Cache(runif, 40, cacheRepo = tmpDir, userTags = "objectName:b")
showCache(tmpDir, userTags = c("objectName"))
showCache(tmpDir, userTags = c("^a$")) # regular expression ... "a" exactly
# Fine control of cache elements -- pick out only the large runif object, and remove it
cache1 <- showCache(tmpDir, userTags = c("runif")) # show only cached objects made during runif
toRemove <- cache1[tagKey == "object.size"][as.numeric(tagValue) > 700]$cacheId
clearCache(tmpDir, userTags = toRemove, ask = FALSE)
cacheAfter <- showCache(tmpDir, userTags = c("runif")) # Only the small one is left
tmpDir <- file.path(tempdir(), "reproducible_examples", "Cache")
try(clearCache(tmpDir, ask = FALSE), silent = TRUE) # just to make sure it is clear
Cache(rnorm, 1, cacheRepo = tmpDir)
thisTime <- Sys.time()
Cache(rnorm, 2, cacheRepo = tmpDir)
Cache(rnorm, 3, cacheRepo = tmpDir)
Cache(rnorm, 4, cacheRepo = tmpDir)
showCache(x = tmpDir) # shows all 4 entries
cc(ask = FALSE, x = tmpDir)
showCache(x = tmpDir) # most recent is gone
cc(thisTime, ask = FALSE, x = tmpDir)
showCache(x = tmpDir) # all those after thisTime gone, i.e., only 1 left
cc(ask = FALSE, x = tmpDir) # Cache is
cc(ask = FALSE, x = tmpDir) # Cache is already empty
Run the code above in your browser using DataLab