Hi,
I want to write a function that is given a named list which is then passed on to mutate()
in a way that each element of the list is an argument to mutate()
. I cannot get this right, either with the new quotation/quasi-quotation syntax or with the old mutate_()
and would appreciate some help.
Small example:
foo <- function(x, args) {
args <- enquote(args)
mutate(x, UQS(args))
}
foo(mtcars, args=list(cyl2=cyl*2))
foo <- function(x, args) {
mutate_(x, .dots=args)
}
foo(mtcars, args=list(cyl2=cyl*2))
In both cases I get object 'cyl' not found
when cyl
exists in mtcars
. I suppose the expression is not evaluated in the correct environment but I am not sure why.
PS: I know that I could just use foo <- function(x, ...)
and then pass ...
to mutate()
but in the real scenario, I need to use ...
for something else.
PPS: I cannot know in advance the names and expressions that will be passed on to mutate so I cannot quote them separately (as in the examples in vignette("programming")
).
Thanks in advance!