Learn R Programming

reproducible (version 0.2.0)

clearCache: Examining and modifying the cache

Description

These are convenience wrappers around archivist package functions. They allow the user a bit of control over what is being cached.

Usage

clearCache(x, userTags = character(), after, before, ...)

# S4 method for ANY clearCache(x, userTags = character(), after, before, ...)

showCache(x, userTags = character(), after, before, ...)

# S4 method for ANY showCache(x, userTags = character(), after, before, ...)

keepCache(x, userTags = character(), after, before, ...)

# S4 method for ANY keepCache(x, userTags = character(), after, before, ...)

Arguments

x

A simList or a directory containing a valid archivist repository

userTags

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.

after

A time (POSIX, character understandable by data.table). Objects cached after this time will be shown or deleted.

before

A time (POSIX, character understandable by data.table). Objects cached before this time will be shown or deleted.

...

Other arguments. Currently unused.

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.

Value

Will clear all (or that match userTags, or between after or before) objects from the repository located at cachePath of the sim object, if sim is provided, or located in cacheRepo. Also returns a data.table invisibly of the removed items.

Details

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.

See Also

mergeCache, splitTagsLocal. Many more examples in Cache

Examples

Run this code
# NOT RUN {
library(raster)
try(detach("package:magrittr", unload = TRUE), silent = TRUE) # magrittr,
                                    #if loaded, gives an error below

tmpDir <- file.path(tempdir(), "reproducible_examples", "Cache")
try(clearCache(tmpDir), 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
ranNumsC <- rnorm(10, 16) %>% Cache(cacheRepo = tmpDir) # recovers cached copy
ranNumsD <- Cache(quote(rnorm(n = 10, 16)), cacheRepo = tmpDir) # recovers cached copy

# Any minor change makes it different
ranNumsE <- rnorm(10, 6) %>% Cache(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]$artifact
clearCache(tmpDir, userTags = toRemove)
cacheAfter <- showCache(tmpDir, userTags = c("runif")) # Only the small one is left


# }

Run the code above in your browser using DataLab