As some calculation costs lots of time, it would be a good idea to cache its result for reusing later. A naive method is to “save” the object and to “load” it when needed. However it is not so convenient when there are lots of objects to save, say, 1000 matrixes generated by simulation, and lm model fits for each matrix.
The result of functions is decided by its parameters, including the implicit options such as the status of seed, variables from the global environment and the functions Options. If same hash (or digest) could be generated for each call and result, the hash value could be used as the name(or keyword) to save the data. If we meet the same call later, we can just load the cached result by checking the call-result cache dictionary. I would like to utilize the idea of makefile into R, and save the dependence relationship automatically from the R expressions.
The function “digest” can generate “hash” digests of arbitrary R objects.
Some possible issues
- Digest for environment
- Weak reference to environment in object
- Object including variables require name looking up in global environment or same other namespace. The package codeTools is helpful to check this problem
Resource
The package on https://github.com/rdpeng/ might be helpful.