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.
while_pipe(data, cond, fun)
data | the data being passed through the pipeline. |
---|---|
cond | an expression to be evaluated in the context of |
fun | pipeline to perform until |
sample(100, 1) %>% runif() %>% pipe_cat("Current length: ", length(.), "\n") %>% while_pipe( length(.) > 1, . %>% diff() %>% magrittr::divide_by(2) )#> Current length: 95#> [1] 0.009624105tibble::tibble(x = runif(5)) %>% while_pipe( .counter <= 5, . %>% dplyr::mutate(!!paste0("x_", .counter) := x - x[.counter]) )#> # A tibble: 5 x 6 #> x x_1 x_2 x_3 x_4 x_5 #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 0.803 0 0.780 0.00803 -0.0831 0.203 #> 2 0.0239 -0.780 0 -0.772 -0.863 -0.576 #> 3 0.795 -0.00803 0.772 0 -0.0912 0.195 #> 4 0.887 0.0831 0.863 0.0912 0 0.287 #> 5 0.600 -0.203 0.576 -0.195 -0.287 0