Last active
August 29, 2015 14:23
-
-
Save cdiener/5abd63400761393d6bf9 to your computer and use it in GitHub Desktop.
cache operator for R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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