Skip to content

Instantly share code, notes, and snippets.

@thomas-neitmann
Created January 15, 2020 11:30
Show Gist options
  • Save thomas-neitmann/7d30592c271c369ccb9e12f779404c07 to your computer and use it in GitHub Desktop.
Save thomas-neitmann/7d30592c271c369ccb9e12f779404c07 to your computer and use it in GitHub Desktop.
library(dplyr)
data(mtcars)
filter_a <- TRUE; filter_b <- TRUE; filter_c <- TRUE
### Variant 1 ###
d <- mtcars
if (filter_a) d <- filter(d, am == 0)
if (filter_b) d <- filter(d, hp > 10)
if (filter_c) d <- filter(d, cyl == 4)
### Variant 2 ###
call <- quote(filter(mtcars))
call
#> filter(mtcars)
call[[1]]
#> filter
call[[2]]
#> mtcars
if (filter_a) call[[length(call)+1]] <- quote(am == 0)
if (filter_b) call[[length(call)+1]] <- quote(hp > 10)
if (filter_c) call[[length(call)+1]] <- quote(cyl == 4)
call
#> filter(mtcars, am == 0, hp > 10, cyl == 4)
all(eval(call) == d)
#> [1] TRUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment