我已经通读了Programming with dplyr并理解了这一点rename()
并select()
使用了整洁的选择。我正在尝试将其与胶合语法结合使用新的双花括号语法(rlang v0.4.0)创建自定义函数,但是我得到了额外的引号:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
sel_var = "homeworld"
# Attempt at using (newer) double curly syntax:
starwars %>%
select("{{sel_var}}_old" := {{ sel_var }})
#> # A tibble: 87 x 1
#> `"homeworld"_old`
#> <chr>
#> 1 Tatooine
#> # ... with 77 more rows
# Working, but uglier (and older) bang bang syntax:
starwars %>%
select(!!sym(paste0(sel_var, "_old")) := {{ sel_var }})
#> # A tibble: 87 x 1
#> homeworld_old
#> <chr>
#> 1 Tatooine
#> # ... with 77 more rows
由reprex 包(v0.3.0)于 2021-02-16 创建
`"homeworld"_old`
在使用双花{{ }}
胶和胶水:=
语法时,如何避免多余的引号?这显示在此处summarise("mean_{{expr}}" := mean({{ expr }}), ...)
的函数中起作用。