loadCache | R Documentation |
Loads data from file cache, which is unique for an optional key object.
## Default S3 method:
loadCache(key=NULL, sources=NULL, suffix=".Rcache", removeOldCache=TRUE, pathname=NULL,
dirs=NULL, ..., onError=c("warning", "error", "message", "quiet", "print"))
key |
An optional object from which a hexadecimal hash code will be generated and appended to the filename. |
sources |
Optional source objects. If the cache object has a timestamp older than one of the source objects, it will be ignored and removed. |
suffix |
A |
removeOldCache |
If |
pathname |
The pathname to the cache file. If specified,
arguments |
dirs |
A |
... |
Not used. |
onError |
A |
The hash code calculated from the key
object is a
32 characters long hexadecimal MD5 hash code.
For more details, see getChecksum
().
Returns an R object or NULL
, if cache does not exist.
Henrik Bengtsson
saveCache
().
simulate <- function(mean, sd) {
# 1. Try to load cached data, if already generated
key <- list(mean, sd)
data <- loadCache(key)
if (!is.null(data)) {
cat("Loaded cached data\n")
return(data);
}
# 2. If not available, generate it.
cat("Generating data from scratch...")
data <- rnorm(1000, mean=mean, sd=sd)
Sys.sleep(1) # Emulate slow algorithm
cat("ok\n")
saveCache(data, key=key, comment="simulate()")
data;
}
data <- simulate(2.3, 3.0)
data <- simulate(2.3, 3.5)
data <- simulate(2.3, 3.0) # Will load cached data
# Clean up
file.remove(findCache(key=list(2.3,3.0)))
file.remove(findCache(key=list(2.3,3.5)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.