I like creating dplyr functions with character inputs, so will be very happy with new v0.6.0 coming up.
For fun and learning current dplyr version 0.5.0.9004, i tried to make a flexible function that can take a character argument as well as an expressions (NSE).
I did succeed, but can't this be done more elegantly?!
d = data.frame(v1=1:2, v2=9:8)
fixquo <- function(x){
# 'fixquo' expects a enquo-ed object, then sees if enquo-ing needs to be 'reversed'...
if(!length(tryCatch({ls(get_env(x))}, error=function(e) "empty")))
x = as.name(as.character(UQ(x))[2])
x
}
Dtest <- function(d, x="v1", res="z"){
x <- enquo(x) %>% fixquo()
d %>% mutate(!!res := UQ(x))
}
Dtest(d)
Dtest(d, "v1")
Dtest(d, v1)
Dtest(d, v2)