2

我正在尝试使用 tidy 评估,如dplyr 0.7.0.

但是,在内部的函数调用期间,mutate()我遇到了错误。似乎没有像我预期的那样评估变量。

library(dplyr)
library(tibble)
library(tidyr)

myCor <- function(col1, col2) {
  col1 <- enquo(col1)
  col2 <- enquo(col2)

  mtcars %>%
    rownames_to_column("vehicle") %>%
    select(vehicle, !!col1, !!col2) %>%
    mutate(correlation=cor(!!col1, !!col2))
}

myCor("mpg", "disp")
# Error in mutate_impl(.data, dots) : 
#   Evaluation error: 'x' must be numeric.

相反,我必须使用这种不整洁的 eval 语法来获得所需的输出。

myCor <- function(col1, col2) {
  col1_tidy <- enquo(col1)
  col2_tidy <- enquo(col2)

  mtcars %>%
    rownames_to_column("vehicle") %>%
    select(vehicle, !!col1_tidy, !!col2_tidy) %>%
    mutate(correlation=cor(eval(parse(text=col1)), eval(parse(text=col2))))
}

myCor("mpg", "disp")
# vehicle  mpg  disp correlation
# 1            Mazda RX4 21.0 160.0  -0.8475514
# 2        Mazda RX4 Wag 21.0 160.0  -0.8475514
# 3           Datsun 710 22.8 108.0  -0.8475514
# 4       Hornet 4 Drive 21.4 258.0  -0.8475514
# ...

有没有办法在这个例子中使用整洁的评估?

4

2 回答 2

1

来自dplyr 的小插图编程

大多数 dplyr 参数不是引用透明的。这意味着您不能用您在别处定义的看似等效的对象替换值。

因此,您需要将未加引号的列名传递给函数,因为这样enquo会正确捕获环境并按预期!!返回未加引号的列名。mutate

要将正常的mutate-call 转换为具有非标准评估的函数,这样开始可能更直观。
首先像没有函数一样写下调用:

mtcars %>%
    rownames_to_column("vehicle") %>%
    select(vehicle, mpg, disp) %>%
    mutate(correlation = cor(mpg, disp))

这有效(并且会引发带引号的错误mpgdisp)。
现在拉变量,你想在调用前改变并替换它们:

col1 <- quo(mpg)
col2 <- quo(disp)

mtcars %>%
  rownames_to_column("vehicle") %>%
  select(vehicle, !!col1, !!col2) %>%
  mutate(correlation=cor(!!col1, !!col2))

由于这是我们必须在quo这里使用的函数之外,但在最后一步中,当我们将它包装在我们使用的函数中时enquo

myCor <- function(var1, var2) {
  col1 <- enquo(var1)
  col2 <- enquo(var2)

  mtcars %>%
    rownames_to_column("vehicle") %>%
    select(vehicle, !!col1, !!col2) %>%
    mutate(correlation=cor(!!col1, !!col2))
}

我为函数参数使用了不同的名称,然后使用了“引用”对象(with enquo)以使区别更清晰,但当然它也可以与col1and一起使用col2

于 2018-05-11T21:08:37.680 回答
1

如果您想将字符串作为参数传递,请使用ensym而不是...enquo

library(dplyr)
library(rlang)
library(tibble)

myCor <- function(col1, col2) {
  col1 <- ensym(col1)
  col2 <- ensym(col2)

  mtcars %>%
    rownames_to_column("vehicle") %>%
    select(vehicle, !!col1, !!col2) %>%
    mutate(correlation=cor(!!col1, !!col2))
}

# both of these will work now
myCor("mpg", "disp")
myCor(mpg, disp)
于 2018-05-12T11:49:41.713 回答