From hb at stat.berkeley.edu  Thu Dec 14 08:02:28 2006
From: hb at stat.berkeley.edu (Henrik Bengtsson)
Date: Thu, 14 Dec 2006 18:02:28 +1100
Subject: [Rd] caching frequently used values
In-Reply-To: <20061213231840.GA13160@pu100877.student.princeton.edu>
References: <20061213211729.GA9737@pu100877.student.princeton.edu>
	<45807694.9060707@fhcrc.org>
	<20061213225632.GA12440@pu100877.student.princeton.edu>
	<4580874A.3000108@fhcrc.org>
	<20061213231840.GA13160@pu100877.student.princeton.edu>
Message-ID: <59d7961d0612132302o167f63a6r40deff0f437a45c7@mail.gmail.com>

I use the R.oo Object class for what has been suggested previously.
The Object class can be thought of as utility wrapper class for
environments (actually environments gained much of its behavior some
time ago when "$" etc was being mapped to get() calls).

For caching to file, take a look at the R.cache package.  From ?loadCache:

 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

>From the 'key' list, loadCache()/saveCache() will calculate a 32-char
hexadecimal hashcode (using the digest package), which is used as a
filename in the .Rcache/ directory.  Thus, you don't have to worry
about coming up with unique cache names; the chances for a false hit
using the digest algorithms are very small. As long as the 'key'
object is not too large, this procedure is very fast.

Then, combining both in-memory and on-file caching you can start doing
clever this.  In my already too long wish list, I have some ideas on a
special class for in-memory/on-file caching which can be used
globally. For instance, when we run out of memory, in-memory cached
values can be stored to file or discarded and so on.  Now probably
someone says this is just about reinventing the swap of already well
developed OSes; I would say we can do much better than a generic swap
since we know what the data is and how long it takes to recalculate it
etc.

Hope this helps

Henrik



Combining the k

On 12/14/06, Tamas K Papp <tpapp at princeton.edu> wrote:
> On Wed, Dec 13, 2006 at 03:05:46PM -0800, Robert Gentleman wrote:
>
> > e1 = new.env(hash=TRUE)
> >
> > e1[["1"]] = whateveryouwant
> >
> > ie. just transform to characters, but I don't see why you want to do
> > that - surely there are more informative names to be used -
>
> Because they are derivatives, and best indexed by numbers.  I wrote an
> example to demonstrate what I think the solution is (for memoizing
> powers of numbers).  It works, but I am not an experienced programmer:
> can you please look at it to check that I do things right and do not
> abuse any feature of R?
>
> ## memoize powers of integers
>
> createpowerlist <- function(n) {
>   list(n=n,env=new.env(hash=TRUE))
> }
>
> getpower <- function(powerlist,i) {
>   iname <- as.character(i)
>   if (exists(iname,powerlist$env))
>     get(iname,powerlist$env)
>   else {
>     res <- i^powerlist$n                # result
>     assign(iname,res,powerlist$env)
>     res
>   }
> }
>
> cubelist <- createpowerlist(3)
> exists("12",cubelist$env)               # FALSE
> getpower(cubelist,12)                   # 1728
> exists("12",cubelist$env)               # TRUE
>
>
> Thanks,
>
> Tamas
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>



[Rd] caching frequently used values
Henrik Bengtsson