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

cra*_*lly 8 r dplyr nse rlang

使用旧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
Run Code Online (Sandbox Code Playgroud)

我尝试使用quosures和拼接做类似的事情 - 选择列效果很好,但是矢量的名称(因此同时重命名列)似乎被忽略了.以下两个都返回数据框,列中包含名称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))
Run Code Online (Sandbox Code Playgroud)

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

ali*_*ire 11

quo(或quos多个)用于不带引号的变量名,而不是字符串.要将字符串转换为quosures,请使用sym(或syms),并使用!!!!!根据需要使用unquote或unquote-splice:

library(dplyr)

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

对于字符串,

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
Run Code Online (Sandbox Code Playgroud)

对于不带引号的变量名,

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
Run Code Online (Sandbox Code Playgroud)