Last data update: 2014.03.03

R: Evaluate an expression if its value is not already cached.
cacheR Documentation

Evaluate an expression if its value is not already cached.

Description

Cache the evaluation of an expression in the file system.

Usage

    cache(expr, dir=".", prefix="tmp_R_cache_")

Arguments

expr

An expression of the form LHS <- RHS, Where LHS is a variable name, RHS is any valid expression, and <- must be used (= will not work).

dir

A string specifying the directory into which cache files should be written (also where to go searching for an appropriate cache file).

prefix

A string giving the prefix to use when naming and searching for cache files. The default is "tmp_R_cache_"

Details

This function can be useful during the development of computationally intensive workflows, for example in vignettes or scripts. The function uses a cache file in dir which defaults to the current working directory whose name is obtained by paste(prefix, name, ".RData", sep="").

When cache is called and the cache file exists, it is loaded and the object whose name is given on the left of <- in expr is returned. In this case, expr is not evaluted.

When cache is called and the cache file does not exist, expr is evaluted, its value is saved into a cache file, and then its value is returned.

The expr argument must be of the form of someVar <- {expressions}. That is, the left hand side must be a single symbol name and the next syntactic token must be <-.

To flush the cache and force recomputation, simply remove the cache files. You can use file.remove to do this.

Value

The (cached) value of expr.

Note

The first version of this function had a slightly different interface which is no longer functional. The old version has arguments name and expr and the intended usage is: foo <- cache("foo", expr).

Author(s)

Wolfgang Huber, huber@ebi.ac.uk Seth Falcon, sfalcon@fhcrc.org

Examples

    bigCalc <- function() runif(10)
    cache(myComplicatedObject <- bigCalc())
    aCopy <- myComplicatedObject
    remove(myComplicatedObject)
    cache(myComplicatedObject <- bigCalc())
    stopifnot(all.equal(myComplicatedObject, aCopy))
    allCacheFiles <-
       list.files(".", pattern="^tmp_R_cache_.*\.RData$", full.name=TRUE)
    file.remove(allCacheFiles)

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(Biobase)
Loading required package: BiocGenerics
Loading required package: parallel

Attaching package: 'BiocGenerics'

The following objects are masked from 'package:parallel':

    clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
    clusterExport, clusterMap, parApply, parCapply, parLapply,
    parLapplyLB, parRapply, parSapply, parSapplyLB

The following objects are masked from 'package:stats':

    IQR, mad, xtabs

The following objects are masked from 'package:base':

    Filter, Find, Map, Position, Reduce, anyDuplicated, append,
    as.data.frame, cbind, colnames, do.call, duplicated, eval, evalq,
    get, grep, grepl, intersect, is.unsorted, lapply, lengths, mapply,
    match, mget, order, paste, pmax, pmax.int, pmin, pmin.int, rank,
    rbind, rownames, sapply, setdiff, sort, table, tapply, union,
    unique, unsplit

Welcome to Bioconductor

    Vignettes contain introductory material; view with
    'browseVignettes()'. To cite Bioconductor, see
    'citation("Biobase")', and for packages 'citation("pkgname")'.

> png(filename="/home/ddbj/snapshot/RGM3/R_BC/result/Biobase/cache.Rd_%03d_medium.png", width=480, height=480)
> ### Name: cache
> ### Title: Evaluate an expression if its value is not already cached.
> ### Aliases: cache
> ### Keywords: manip array
> 
> ### ** Examples
> 
>     bigCalc <- function() runif(10)
>     cache(myComplicatedObject <- bigCalc())
>     aCopy <- myComplicatedObject
>     remove(myComplicatedObject)
>     cache(myComplicatedObject <- bigCalc())
>     stopifnot(all.equal(myComplicatedObject, aCopy))
>     allCacheFiles <-
+        list.files(".", pattern="^tmp_R_cache_.*\.RData$", full.name=TRUE)
>     file.remove(allCacheFiles)
[1] TRUE
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>