[R-SIG-Finance] Cache functions

Dirk Eddelbuettel edd at debian.org
Tue Sep 20 15:07:03 CEST 2011


On 20 September 2011 at 14:44, Ulrich Staudinger wrote:
| Guys,
| 
| I have three nice functions that are particularly useful when backtesting,
| handling long processing chains, etc. I wrote them yesterday and found them
| quite beautiful and handy, a little contribution to the R-community.
| 
| The functions can be used to cache objects. They take a list() of keys
| (anything will do - i think), construct an md5 stamp of the list, save a

Yes, the digest() function from our digest package can create hash stamps for
any R object ... as we use R's own serialize() which is universal.

| to-be-cached object to disc and are able to load the object at a later time
| again.
| 
| Perfect for caching results of long computations ...

There are a couple of packages on CRAN that do that, but I haven't compared
them.  I think the CRAN Task View on Reproducible Research may have leads.

Just a thought:  Maybe you want to compare and review these, along with your
own approach, and make that a nice succinct two or three pager for the R Journal?

Dirk
 
| Example of use:
| 
| <code>
| dataPoints = -1
| if(!cacheGet(dataPoints, c(days, periodLength, iid)))
| {
|   dataPoints <- doTrendArbSplicer(dataPoints, days, iid, periodLength)
|   saveToCache(dataPoints, c(days, periodLength, iid))
| }
| </code>
| 
| 
| 
| Maybe helpful to someone, feedback welcome.
| 
| Cheers,
| Ulrich
| 
| 
| 
| 
| The functions:
| <code>
| #' private function - not exported.
| getCacheFileName <- function(keyList = list(), group="cache")
| {
|   # construct a filename from the list of parameters.
|   cacheId = paste(group, "_", digest(keyList, algo="md5"), ".dat", sep="")
|   return(cacheId)
| }
| 
| #' puts an object into the disc cache
| cachePut <- function(keyList = list(), cacheObject, group="cache")
| {
|   cacheFileName = getCacheFileName(keyList, group)
|   message("Writing to cache: ", cacheFileName)
|   save(cacheObject, file=cacheFileName)
|   message("Object cached.")
| }
| 
| #' gets an object from the disc cache. returns either true in case of
| success or false in case no cache object exists.
| #' requires a target object, where the cache content will be written into.
| cacheGet <- function(keyList = list(), targetObject, group="cache")
| {
|   cacheFileName = getCacheFileName(keyList, group)
|   message("Checking for cache file: ", cacheFileName)
|   fileInfo = file.info(cacheFileName)
|   if(!is.na(fileInfo$size))
|   {
|     message("Cache file exists.")
|     load(cacheFileName)
|     eval.parent(substitute(targetObject<-cacheObject))
|     message("Cached object loaded.")
|     # cached object will be returned.
|     return(TRUE)
|   }
|   else {
|     message("No cache exists.")
|     return(FALSE)
|   }
| }
| 
| #' removes objects from disc cache.
| cacheClear <- function(keyList = list(), group="cache")
| {
|   cacheFileName = getCacheFileName(keyList, group)
|   message("Clearing cache file: ", cacheFileName)
|   unlink(cacheFileName)
| }
| 
| 
| </code>
| 
| 
| -- 
| Ulrich Staudinger
| 
| http://www.activequant.org
| Connect online: https://www.xing.com/profile/Ulrich_Staudinger
| 
| 	[[alternative HTML version deleted]]
| 
| _______________________________________________
| R-SIG-Finance at r-project.org mailing list
| https://stat.ethz.ch/mailman/listinfo/r-sig-finance
| -- Subscriber-posting only. If you want to post, subscribe first.
| -- Also note that this is not the r-help list where general R questions should go.

-- 
New Rcpp master class for R and C++ integration is scheduled for 
San Francisco (Oct 8), more details / reg.info available at
http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php



More information about the R-SIG-Finance mailing list