r - Workaround for lazy evaluation with date variable -
i trying use lazyeval
package , run problem. when use variable names (e.g. date, mean) exist functions in r in data.frame
, lazy
no longer works. had fall using substitute
instead. there better workaround?
functions
# function has problems variables f_lazy <- function(data, variable){ variable <- lazyeval::lazy(variable) lazyeval::lazy_eval(variable, data) } # these functions work f_lazydots <- function(data, ...){ variable <- lazyeval::lazy_dots(...)[[1]] lazyeval::lazy_eval(variable, data) } f_substitute <- function(data, variable){ variable <- substitute(variable) eval(variable, data) }
sample data
data <- data.frame(x = 1, y = "a", date = sys.date(), mean = 1)
examples
f_lazy(data, x) ## [1] 1 f_lazy(data, y) ## [1] ## levels: f_lazy(data, date) ## function () ## .internal(date()) ## <bytecode: 0x000000000cee1980> ## <environment: namespace:base> f_lazy(data, mean) ## function (x, ...) ## usemethod("mean") ## <bytecode: 0x000000000bc87820> ## <environment: namespace:base> f_lazydots(data, date) ## [1] "2015-05-12" f_lazydots(data, mean) ## [1] 1 f_substitute(data, date) ## [1] "2015-05-12" f_substitute(data, mean) ## [1] 1
Comments
Post a Comment