R/map_if.R

Defines functions map_if_false map_if_true map_if_condition

Documented in map_if_condition map_if_false map_if_true

#' Apply functions depending on a condition
#' @param .x An object
#' @param condition TRUE or FALSE
#' @param .f_if_true Function to apply when `condition` is `TRUE`
#' @param .f_if_false Function to apply when `condition` is not `TRUE`
#' @param .f Function to apply
#' @return `.x` or a function applied to `.x`, depending on `condition`
#' @name mutate-if

NULL

#' @export
#' @rdname mutate-if
map_if_condition = function(.x, condition, .f_if_true = identity, .f_if_false = identity) {
  if (condition %in% TRUE) {
    .f_if_true(.x)
  } else {
    .f_if_false(.x)
  }
}

#' @export
#' @rdname mutate-if
map_if_true = function(.x, condition, .f = identity) {
  if (condition %in% TRUE) {
    .f(.x)
  } else {
    .x
  }
}

#' @export
#' @rdname mutate-if
map_if_false = function(.x, condition, .f = identity) {
  if (condition %in% TRUE) {
    .x
  } else {
    .f(.x)
  }
}
vituri/vituripackage documentation built on Jan. 19, 2024, 9:08 a.m.