Skip to content

Instantly share code, notes, and snippets.

@favstats
Forked from benjaminguinaudeau/do_if.R
Created May 3, 2019 16:32
Show Gist options
  • Save favstats/2a7ea96bb6f9e60de8afe52889d1ea05 to your computer and use it in GitHub Desktop.
Save favstats/2a7ea96bb6f9e60de8afe52889d1ea05 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