if_branch: Perform an if/else-like branch in a pipeline

Description Usage Arguments Examples

View source: R/if_branch.R

Description

Allows the user to perform an if/else-like branch without breaking out of a pipeline. To maintain the flow of a pipeline, it is recommended to use fseq style arguments (i.e. pipelines) for fun and elsefun, however any function can be used.

Usage

1
if_branch(data, predicate, fun, elsefun = NULL)

Arguments

data

the data being passed through the pipeline

predicate

logical statement, a function or an expression that can be evaluated in the context of data to decide which branch to follow

fun

pipeline to follow if predicate evaluates as TRUE

elsefun

pipeline to follow if predicate evaluates as FALSE, or NULL if nothing is to happen to the original data.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
1 %>%
  magrittr::multiply_by(2) %>%
  if_branch(
    . %>% magrittr::equals(2),
    . %>%
      magrittr::multiply_by(3) %>%
      magrittr::add(2)
  ) %>%
  magrittr::multiply_by(2)

tibble::tibble(x = rnorm(100), y = rnorm(100)) %>%
  dplyr::mutate(z = x + y) %>%
  if_branch(
    mean(z) > 0,
    . %>%
      pipe_cat("z is high\n\n"),
    . %>%
      dplyr::select(-z) %>%
      pipe_cat("z is low, so it was dropped\n\n")
  )

MyKo101/mpipe documentation built on Feb. 6, 2021, 2:13 p.m.