If you are programming in dplyr, you can use the selection mechanism and promises as follows:
mypull <- function(data, var) {
data |> pull({{ var }})
}
then you can call for example iris |> mypull(Sepal.Width) and it will work. You can do a similar thing with ...:
my_summarise <- function(.data, ...) {
.data |>
group_by(...) |>
summarise(
mass = mean(mass, na.rm = TRUE),
height = mean(height, na.rm = TRUE)
)
}
starwars |> my_summarise(homeworld, species)
(Example based on Programming with tidyverse).
Now my problem is this: I need to loop over the variables / promises in .... Or, more generally, I need to access arbitrary elements of .... For example, I may want to act only upon the first argument.
I tried the following and many other variations of the same, but can't seem to find the right way of doing that. {{ }} needs a symbol, so I understand why it works on var and ... in the examples above, but how do I make it work in this particular setting?
# obviously wrong
mygroup <- function(data, ...) {
vars <- rlang::list2(...)[[1]]
data |> group_by({{vars}})
}