I've read through Programming with dplyr and understand that rename()
and select()
use tidy selection. I'm trying to combine this with the glue syntax to create a custom function using the new double curly syntax (rlang v0.4.0), however I'm getting extra quotation marks:
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
Created on 2021-02-16 by the reprex package (v0.3.0)
How can I avoid the extra quotations marks in `"homeworld"_old`
using the double curly {{ }}
and glue :=
syntax? This is shown to work for summarise("mean_{{expr}}" := mean({{ expr }}), ...)
in a function here.