Skip to content

Instantly share code, notes, and snippets.

@mkearney
Created May 14, 2019 17:38
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
#' 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