KentonWhite / ProjectTemplate

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

pass actual variable or character of var name to cache #96

Closed jackwasey closed 10 years ago

jackwasey commented 10 years ago

I find it better to pass an actual variable, e.g. cache(myVar) instead of a string containing the variable name. Using deparse and substitute will allow the previous behavior, but can also find the name of a variable argument. This also allows Rstudio, etc., to autocomplete the variable name.

KentonWhite commented 10 years ago

Thanks @jackwasey for the pull request. Sorry it has taken me so long to look at it -- you caught me on vacation!

This is a feature that we have discuss before in the past. Many of us agree with you that this would be welcome, but just as many see potential for problems in the larger picture. You can see some of the history in issue #44. If you are up to addressing them would really love the contribution

A comment on this particular PR. As written it breaks the way cache() worked previously -- it needs to handle both the actual variable and the string name.

The challenge that we face with this idiom (and why we have shelved this request in the past) is interpreting temporary variables.

Imagine this case:

variables_to_cache <- c('var1', 'var2')
for (var in variables_to_cache) {
    cache(var)
}

Currently, this will work as expected. If we allow variables in addition to strings, this will simply cache the temporary variable var each time. In issue #44 it was suggested to adopt an interface similar to save.

krlmlr commented 10 years ago

Closing for now.

krlmlr commented 10 years ago

See #101 for a perhaps better way.