I am trying for some time to understand tidyverse design and how to program with it. I was trying to write a function that uses tidyselect semantics, and I found that tidyselect::eval_select
appends numbers to lhs expressions. This was not surprising seeing that this sematic is used for column renaming. Unfortunately, my function meant for building a data structure doesn't need this behavior, it needs the regular name provided in lhs of the expression (duplicated as many times as necessary). I haven't managed to find out where this behavior is even coming from; it seems to be a make.unique
but I can't find where it is implemented. If you know, I am quite curious to learn, if not, solving my problem shouldn't depend on it.
All I want is for the lhs names to not have appended numbers, as in the example:
library(tidyverse)
# Data
data <- mtcars[, 8:11]
# Example
data %>%
tidyselect::eval_select(rlang::expr(c(foo = 1, bar = c(2:4), foobar = c(1, "am", "gear", "carb"))), .)
#> foo bar1 bar2 bar3 foobar1 foobar2 foobar3 foobar4
#> 1 2 3 4 1 2 3 4
# Function
test <- function(.data, ...) {
loc <- tidyselect::eval_select(rlang::expr(c(...)), .data)
names <- names(.data)
list(names(loc), names[loc])
}
data %>%
test(foo = 1, bar = c(2:4), foobar = c(1, "am", "gear", "carb"))
#> [[1]]
#> [1] "foo" "bar1" "bar2" "bar3" "foobar1" "foobar2" "foobar3"
#> [8] "foobar4"
#>
#> [[2]]
#> [1] "vs" "am" "gear" "carb" "vs" "am" "gear" "carb"
Created on 2021-05-22 by the reprex package (v2.0.0)
Desired output:
#> [[1]]
#> [1] "foo" "bar" "bar" "bar" "foobar" "foobar" "foobar"
#> [8] "foobar"
#>
#> [[2]]
#> [1] "vs" "am" "gear" "carb" "vs" "am" "gear" "carb"
Any help is greatly appreciated.