Howdy!
I'm trying to write a function that will accept a list of bares.
This is easy to do with dots, but in my real use case I want to save dots for passing arguments elsewhere.
Here is a reprex showing the idea, first how this is easy to do with dots, but then how it fails with a list.
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(rlang))
# enquos on dots works easily
expr_then_dots <- function(df, q1, ...){
q1 <- enexpr(q1)
q2 <- enquos(...)
df %>%
filter(!!q1) %>%
select(!!!q2) %>%
head(3)
}
#yay!
expr_then_dots(mtcars, cyl>3, mpg, cyl)
#> mpg cyl
#> 1 21.0 6
#> 2 21.0 6
#> 3 22.8 4
#but enquos on a list does not work
expr_then_list <- function(df, q1, q2){
q1 <- enexpr(q1)
q2 <- enquos(q2)
df %>%
filter(!!q1) %>%
select(!!!q2) %>%
head(3)
}
# oh no
expr_then_list(mtcars, cyl>3, list(mpg, cyl))
#> Error in .f(.x[[i]], ...): object 'cyl' not found
Created on 2019-06-14 by the reprex package (v0.3.0)
My best guess here is I think the enquos is maybe capturing the list command? But I can't unlist bares I don't think... Any help on how to use enquos() on a list of bares in a function would be great!
Cheers,
Ben