KentonWhite / ProjectTemplate

A template utility for R projects that provides a skeletal project.
http://projecttemplate.net
GNU General Public License v3.0
623 stars 159 forks source link

run cache() CODE chunks in a local environment #171

Closed connectedblue closed 7 years ago

connectedblue commented 7 years ago

This PR proposes a modification to the way cache() handles CODE chunk execution.

Currently, the chunk is run in global environment and the result of the execution is assigned to the variable name specified in the cache(variable_name, ...) call. This has the side effect that if some helper variables are used in the CODE chunk, these are also placed in the global environment. This is a bit messy as the CODE chunk should really only produce the single intended variable in global environment.

This fix runs the CODE chunk in a local environment and saves the result in the appropriate global environment variable. Some additional tests are included to check the behaviour.