Skip to content

Instantly share code, notes, and snippets.

@cdiener
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save cdiener/5abd63400761393d6bf9 to your computer and use it in GitHub Desktop.

Select an option

Save cdiener/5abd63400761393d6bf9 to your computer and use it in GitHub Desktop.
cache operator for R
# Caching operator executes command if there is no saved version of the data
# just delete the saved file and code will be run again
'%c%' = function(ex, file) if(file.exists(file), env=parent.frame(4)) load(file) else evalq(ex))
# Example
# first execution
{ x <- rnorm(1e6); save(x, file="cache.Rd") } %c% "cache.Rd" # executes the sampling
# second execution ("cache.Rd" exists now)
{ x <- rnorm(1e6); save(x, file="cache.Rd") } %c% "cache.Rd" # does not execute but reads from cache.Rd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment