Skip to content

Instantly share code, notes, and snippets.

@mkearney
Created May 14, 2019 17:38
Show Gist options
  • Save mkearney/bb2ce47eb635c14d5f99151636e26b21 to your computer and use it in GitHub Desktop.
Save mkearney/bb2ce47eb635c14d5f99151636e26b21 to your computer and use it in GitHub Desktop.
#' Conditionally apply expressions on a data object
#'
#' @param .data Input data
#' @param condition A logical value to determine whether to use .if or .else
#' @param .if Formula or function to apply to intput data when condition is TRUE
#' @param .else Formula or function to apply to intput data when condition is FALSE
#' @return Output of appropriate .if/.else call
#' @export
#' @importFrom rlang as_closure
do_if_else <- function(.data, condition, .if, .else = identity) {
if (condition) {
call <- rlang::as_closure(.if)
} else {
call <- rlang::as_closure(.else)
}
do.call(call, list(.data))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment