I heard standard evaluation is not recommended in dplyr, and we can do similar thing with enquo()
and quo()
.
My original code (simplified) is
my_function <- function(data, x="OriginalX", y="OriginalY"){
data %>%
mutate_(CopyX = x, CopyY = y)
}
and it works.
I tried following code
my_function <- function(data, x="OriginalX", y="OriginalY"){
qx <- enquo(x)
qy <- enquo(y)
data %>%
mutate(CopyX = (!!qx), CopyY = (!!qy))
}
Why it does not work? And should we keep using standard evaluation?
The idea behind tidyeval is specifically that you don't need to put your column name between
""
. So this should work:Note that the
!!qx
and!!qy
don't need to be between parenthesisIf you need to use strings in the function parameters, you can use the ensym function to convert them: