R如何包装使用TidyeVal的函数?

发布于 2025-01-19 18:22:51 字数 807 浏览 3 评论 0原文

我有一个函数,它使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

像极了他 2025-01-26 18:22:51

如果您更改 select_this() 以支持定额,则可以使用 {{ 与其交互。在这种情况下,您可以使用 as_name() 将潜在的 quosured 符号转换为字符串:

z <- c("a", "b")
df <- data.frame(Z = 1:3, a = 1:3, b = 1:3)

select_this <- function(df, x) {
  x_upper <- toupper(rlang::as_name(enquo(x)))

  df %>%
    select(all_of(x_upper), all_of(x))
}

df %>% select_this(z)
#>   Z a b
#> 1 1 1 1
#> 2 2 2 2
#> 3 3 3 3

然后使用 {{ 与它交互:

wrapper_select_this <- function(df, x){
  df %>% select_this({{ x }})
}

df %>% wrapper_select_this(z)
#>   Z a b
#> 1 1 1 1
#> 2 2 2 2
#> 3 3 3 3

我不得不说我发现它是有点令人困惑的是,您通过解散名称和同名环境变量中包含的列名称来选择列。

If you change select_this() to support quosures, you can then use {{ to interface with it. In this case you can use as_name() to transform a potentially quosured symbol to a string:

z <- c("a", "b")
df <- data.frame(Z = 1:3, a = 1:3, b = 1:3)

select_this <- function(df, x) {
  x_upper <- toupper(rlang::as_name(enquo(x)))

  df %>%
    select(all_of(x_upper), all_of(x))
}

df %>% select_this(z)
#>   Z a b
#> 1 1 1 1
#> 2 2 2 2
#> 3 3 3 3

Then use {{ to interface with it:

wrapper_select_this <- function(df, x){
  df %>% select_this({{ x }})
}

df %>% wrapper_select_this(z)
#>   Z a b
#> 1 1 1 1
#> 2 2 2 2
#> 3 3 3 3

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文