Suppose I want to filter the starwars data frame programmatically. Here is a simple example that allows me to filter based on my native world and views:
library(tidyverse) # a function that allows the user to supply filters filter_starwars <- function(filters) { for (filter in filters) { starwars = filter_at(starwars, filter$var, all_vars(. %in% filter$values)) } return(starwars) } # filter Star Wars characters that are human, and from either Tatooine or Alderaan filter_starwars(filters = list( list(var = "homeworld", values = c("Tatooine", "Alderaan")), list(var = "species", values = "Human") ))
But this does not allow me to specify, say, a height filter, because I hardcoded the %in% operator in .vars_predicate of .vars_predicate filter_at() , and the height filter would use one of > , >= , < , <= or ==
What is the best way to write the filter_starwars() function so that the user can provide filters that are common enough to filter along any column and use any operator?
NB, using the now obsolete filter_() method, I can pass the line:
filter_(starwars, "species == 'Human' & homeworld %in% c('Tatooine', 'Alderaan') & height > 175")
But then again, it was out of date.
source share