如何使用 quosures 将命名向量传递给 dplyr::select?

How to pass a named vector to dplyr::select using quosures?

使用旧的 select_() 函数,我可以将命名向量传递给 select 并立即更改位置和列名称:

my_data  <- data_frame(foo = 0:10, bar = 10:20, meh = 20:30)
my_newnames  <-  c("newbar" = "bar", "newfoo" = "foo")

move_stuff  <- function(df, newnames) {
    select_(df, .dots = newnames)
}

move_stuff(my_data,  newnames = my_newnames) )

# this is the desired output
# A tibble: 4 x 2
  newbar  newfoo
   <int>   <int>
1     10       0
2     11       1
3     12       2
4     13       3

我尝试使用 quosures 和拼接做类似的事情--selecting 列效果很好,但向量的名称(因此同时重命名列)似乎被忽略了。以下两个 return 数据框的列名为 barfoo,但不包含 newbarnewfoo

move_stuff2  <- function(df, newnames) {
  select(df, !!!newnames)
}

# returns df with columns bar and foo
move_stuff2(my_data, quo(my_newnames))
move_stuff2(my_data, quos(my_newnames))

有没有一种方法可以使用新的 NSE 方法来使用命名向量来重命名和重新排序列?

quo(或 quos 表示多个)用于不带引号的变量名,而不是字符串。要将字符串转换为 quosures,请使用 sym(或 syms),并根据需要使用 !!!!! 取消引用或取消引用拼接:

library(dplyr)

my_data  <- data_frame(foo = 0:10, bar = 10:20, meh = 20:30)
my_newnames  <-  c("newbar" = "bar", "newfoo" = "foo")

对于字符串,

move_stuff_se <- function(df, ...){
     df %>% select(!!!rlang::syms(...))
}

move_stuff_se(my_data, my_newnames)
#> # A tibble: 11 x 2
#>    newbar newfoo
#>     <int>  <int>
#>  1     10      0
#>  2     11      1
#>  3     12      2
#>  4     13      3
#>  5     14      4
#>  6     15      5
#>  7     16      6
#>  8     17      7
#>  9     18      8
#> 10     19      9
#> 11     20     10

对于不带引号的变量名,

move_stuff_nse <- function(df, ...){
    df %>% select(!!!quos(...))
}

move_stuff_nse(my_data, newbar = bar, newfoo = foo)
#> # A tibble: 11 x 2
#>    newbar newfoo
#>     <int>  <int>
#>  1     10      0
#>  2     11      1
#>  3     12      2
#>  4     13      3
#>  5     14      4
#>  6     15      5
#>  7     16      6
#>  8     17      7
#>  9     18      8
#> 10     19      9
#> 11     20     10