magrittr管道内的enquo() [英] enquo() inside a magrittr pipeline

查看:141
本文介绍了magrittr管道内的enquo()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想了解这里出了什么问题. 在第一种情况下(有效),我将enquo() -ted参数分配给变量,在第二种情况下,我直接在对mutate的调用中使用带引号的参数.

I just would like to understand what's going wrong here. In the first case (working), I assign the enquo()-ted argument to a variable, in the second case, I use the enquoted argument directly in my call to mutate.

library("dplyr")
df <- tibble(x = 1:5, y= 1:5, z = 1:5)

# works
myfun <- function(df, transformation) {
  my_transformation <- rlang::enquo(transformation)
  df %>% 
    gather("key","value", x,y,z) %>% 
    mutate(value = UQ(my_transformation))
}
myfun(df,exp(value))

# does not work
myfun_2 <- function(df, transformation) {
  df %>% 
    gather("key","value", x,y,z) %>% 
    mutate(value = UQ(rlang::enquo(transformation)))
}
myfun_2(df,exp(value))
#>Error in mutate_impl(.data, dots) : Column `value` is of unsupported type closure

修改 这是您需要考虑的其他几行:)

Edit Here are some more lines to think about :)

将调用包装到quo()中,就好像要评估的表达式正确地构建"了

Wrapping the call into quo() it looks as if the expression to evaluate is "built" correctly

# looks as if the whole thing should be working
myfun_2_1 <- function(df, transformation) {
  quo(df %>% 
    gather("key","value", x,y,z) %>% 
    mutate(value = UQ(rlang::enquo(transformation))))
}
myfun_2_1(df,exp(value))

如果您将此内容告知eval_tidy,它会起作用(没有quo()不会起作用)

If you tell this to eval_tidy, it works (it doesn't work without quo())

# works
myfun_2_2 <- function(df, transformation) {
  eval_tidy(quo(df %>% 
    gather("key","value", x,y,z) %>% 
    mutate(value = UQ(rlang::enquo(transformation)))))
}
myfun_2_2(df,exp(value))

如果您不使用管道,它也可以工作

If you don't use the pipe, it also works

# works
myfun_2_3 <- function(df, transformation) {
  mutate(gather(df,"key","value", x,y,z), value = UQ(rlang::enquo(transformation)))
}
myfun_2_3(df,exp(value))

关于错误消息,这是当尝试传递data.frames不支持的类型时得到的,例如

Regarding the error message, this is what one gets, when one tries to pass types that are not supported by data.frames, eg.

mutate(df,value = function(x)x) #mutate_impl(.data,点)中的错误:value列的闭包类型不受支持

mutate(df, value = function(x) x) # Error in mutate_impl(.data, dots) : Column value is of unsupported type closure

对我来说,好像myfun_2中的quaquo没有被mutate评估,这在某种程度上是有趣的/非直觉的行为.您认为我应该将此报告给开发人员吗?

To me it looks as if the quosure in myfun_2 isn't evaluated by mutate, which is somehow interesting/non-intuitive behaviour. Do you think I should report this to the developers?

推荐答案

此限制已在rlang 0.2.0中解决.

This limitation is solved in rlang 0.2.0.

从技术上讲:问题的核心是magrittr在当前环境的子级中评估其论点.这是包含.代词的环境.从0.2.0版本开始,使用enquo()和变体对参数进行捕获的词法范围已经确定,这意味着它将查找父级环境堆栈以查找要捕获的参数.这样就解决了magrittr问题.

Technically: The core of the issue was that magrittr evaluates its arguments in a child of the current environment. This is this environment that contains the . pronoun. As of 0.2.0, capture of arguments with enquo() and variants is now lexically scoped, which means it looks up the stack of parent environments to find the argument to capture. This solves the magrittr problem.

这篇关于magrittr管道内的enquo()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆