R如何包装使用TidyeVal的函数?
我有一个函数,它使用tidyeval,例如select_this
下面定义的。我有另一个函数wrapper_select_this
包装它,但是我会收到以下错误。是否有一种简单的方法来调整wrapper_select_this
进行工作(在这种情况下:输出与函数select_this
相同)?
library(tidyverse)
z <- c("a", "b")
df <- data.frame(Z = 1:3, a = 1:3, b = 1:3)
select_this <- function(df, x){
x_upper <- enexpr(x) %>% str_to_upper
df %>%
select(
all_of(x_upper), all_of(x)
)
}
wrapper_select_this <- function(df, x){
df %>% select_this(x)
}
df %>% select_this(z)
#> Z a b
#> 1 1 1 1
#> 2 2 2 2
#> 3 3 3 3
df %>% wrapper_select_this(z)
#> Error: Can't subset columns that don't exist.
#> x Column `X` doesn't exist.
由
I have a function that uses tidyeval like select_this
defined below. I have another function wrapper_select_this
wrapping it, but I get the below error. Is there a simple way to adjust the wrapper_select_this
to work (in this case: output the same as function select_this
)?
library(tidyverse)
z <- c("a", "b")
df <- data.frame(Z = 1:3, a = 1:3, b = 1:3)
select_this <- function(df, x){
x_upper <- enexpr(x) %>% str_to_upper
df %>%
select(
all_of(x_upper), all_of(x)
)
}
wrapper_select_this <- function(df, x){
df %>% select_this(x)
}
df %>% select_this(z)
#> Z a b
#> 1 1 1 1
#> 2 2 2 2
#> 3 3 3 3
df %>% wrapper_select_this(z)
#> Error: Can't subset columns that don't exist.
#> x Column `X` doesn't exist.
Created on 2022-04-07 by the reprex package (v2.0.1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您更改
select_this()
以支持定额,则可以使用{{
与其交互。在这种情况下,您可以使用as_name()
将潜在的 quosured 符号转换为字符串:然后使用
{{
与它交互:我不得不说我发现它是有点令人困惑的是,您通过解散名称和同名环境变量中包含的列名称来选择列。
If you change
select_this()
to support quosures, you can then use{{
to interface with it. In this case you can useas_name()
to transform a potentially quosured symbol to a string:Then use
{{
to interface with it:I have to say I find it a bit confusing that you're selecting columns both by defused name and by the column names contained in the env-var of the same name.