I am relying on https://dplyr.tidyverse.org/articles/programming.html to create interactive tidy functions. These rely on environment variables (as the article calls them) such as the following example.
var_summary <- function(data, var) {
data %>%
summarise(n = n(), min = min({{ var }}), max = max({{ var }}))
}
mtcars %>%
group_by(cyl) %>%
var_summary(mpg)
#> `summarise()` ungrouping output (override with `.groups` argument)
However, when I try a similar method using left_join()
I receive an error. Reproducible example below.
# A table
foobar <- tribble(~fooname, ~value, "setosa", 20, "versicolor", 30, "virginica", 10)
# A function
foobarjoin <- function(table, joincol){iris %>% left_join(table, by = c("Species" = {{ joincol }}))}
# When I use the function
foobarjoin(table = foobar, joincol = fooname)
#> Error in standardise_join_by(by, x_names = x_names, y_names = y_names) :
#> object 'fooname' not found
What is the correct way to use environment variables to perform a dplyr join within a custom function?
Note that this is not a duplicate question of How to join (merge) data frames (inner, outer, left, right). That question is a vanilla join question. This one is specifically about how to use environment variables within functions to achieve a join.