Drawing on the discussion on conditional dplyr evaluation I would like conditionally execute a step in pipeline depending on whether the reference column exists in the passed da
With across()
in dplyr > 1.0.0 you can now use any_of
when filtering. Compare original with all columns:
mtcars %>%
filter(am == 1) %>%
filter(cyl == 4)
With cyl
removed, it throws an error:
mtcars %>%
select(!cyl) %>%
filter(am == 1) %>%
filter(cyl == 4)
Using any_of
(note you have to write "cyl"
and not cyl
):
mtcars %>%
select(!cyl) %>%
filter(am == 1) %>%
filter(across(any_of("cyl"), ~.x == 4))
#N.B. this is equivalent to just filtering by `am == 1`.