Evaluate point
so filter
can tell the difference between the argument and the data's column
aosmith has a good idea, so I'm putting it in answer form, along with a reproducible example:
f <- function(dat, point) {
if (!(point %in% c("NW", "SE", "SW")))
stop("point must be one of 'SW', 'SE', 'NW'")
filter(dat, point == !!point)
}
tbl <- tibble(point = c('SE', 'NW'))
f(tbl, 'SE')
f(tbl, 'XX')
If you're passing point
in as a string, you only need to differentiate the argument point
( = "SE", in this case) and the column dat$point
(or tbl$point
, depending if you're inside or outside the function). From the dplyr programming doc:
In dplyr (and in tidyeval in general) you use !! to say that you want to unquote an input so that it’s evaluated, not quoted.
You want to evaluate the argument point
so that "SE" is passed to filter
, that way filter
can tell the difference between the column point
and the argument SE
.
(I also tried using filter(dat, .data$point == point)
, but the RHS point
still refers to the column, not the f
argument.)
I hope this example helps with your real code.