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
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 ...

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]]

