Skip to content

Instantly share code, notes, and snippets.

@benjaminguinaudeau
Created May 3, 2019 16:30
Show Gist options
  • Save benjaminguinaudeau/33c74659a049a1b4ad1012d2c46dcb57 to your computer and use it in GitHub Desktop.
Save benjaminguinaudeau/33c74659a049a1b4ad1012d2c46dcb57 to your computer and use it in GitHub Desktop.
library(dplyr)
do_if <- function(.data, condition, call){
if(condition){
.x <- .data
call_str <- call %>%
as.character %>%
.[2]
out <- eval(rlang::parse_expr(call_str))
return(out)
} else {
return(.data)
}
}
any_condition <- T
mtcars %>%
do_if(any_condition, ~{
.x %>%
filter(cyl == 6) %>%
mutate(x = disp > 170)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment