while_pipe: Perform an while-like loop in a pipeline

Description Usage Arguments Examples

View source: R/while_pipe.R

Description

Allows the user to perform a while-like loop 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 the fun argument.

Within the while_pipe(), users can use the .counter variable to reference how many iterations have been performed so far.

Usage

1
while_pipe(data, cond, fun)

Arguments

data

the data being passed through the pipeline.

cond

an expression to be evaluated in the context of data to decide whether to perform fun. Should evaluate to a single logical value or be coercible to one.

fun

pipeline to perform until cond is FALSE.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
sample(100, 1) %>%
  runif() %>%
  pipe_cat("Current length: ", length(.), "\n") %>%
  while_pipe(
    length(.) > 1,
    . %>%
      diff() %>%
      magrittr::divide_by(2)
  )


tibble::tibble(x = runif(5)) %>%
  while_pipe(
    .counter <= 5,
    . %>%
      dplyr::mutate(!!paste0("x_", .counter) := x - x[.counter])
  )

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