dplyr: Standard evaluation and enquo()

浪尽此生 提交于 2019-12-01 01:14:38
Colin FAY

The idea behind tidyeval is specifically that you don't need to put your column name between "". So this should work:

my_function <- function(data, x= OriginalX , y= OriginalY ){
  qx <- enquo(x)
  qy <- enquo(y)
  data %>%
    mutate(CopyX = !!qx,
           CopyY = !!qy)
}

Note that the !!qx and !!qy don't need to be between parenthesis

my_function(iris, Sepal.Length, Species) %>%
  head()
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species CopyX  CopyY
1          5.1         3.5          1.4         0.2  setosa   5.1 setosa
2          4.9         3.0          1.4         0.2  setosa   4.9 setosa
3          4.7         3.2          1.3         0.2  setosa   4.7 setosa
4          4.6         3.1          1.5         0.2  setosa   4.6 setosa
5          5.0         3.6          1.4         0.2  setosa   5.0 setosa
6          5.4         3.9          1.7         0.4  setosa   5.4 setosa

If you need to use strings in the function parameters, you can use the ensym function to convert them:

my_function <- function(data, x= "OriginalX" , y= "OriginalY" ){
  qx <- ensym(x)
  qy <- ensym(y)
  data %>%
    mutate(CopyX = !!qx,
           CopyY = !!qy)
}

my_function(iris, "Sepal.Length", "Species") %>%
  head()
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species CopyX  CopyY
1          5.1         3.5          1.4         0.2  setosa   5.1 setosa
2          4.9         3.0          1.4         0.2  setosa   4.9 setosa
3          4.7         3.2          1.3         0.2  setosa   4.7 setosa
4          4.6         3.1          1.5         0.2  setosa   4.6 setosa
5          5.0         3.6          1.4         0.2  setosa   5.0 setosa
6          5.4         3.9          1.7         0.4  setosa   5.4 setosa
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!