Skip to content

Instantly share code, notes, and snippets.

MaksimRudnev /
Last active March 3, 2024 05:02
Branching pipes

Pipe helpers

Here are three little functions that allow for brunching logical pipes as defined in magrittr package. It is against Hadley's idea, as pipes are in principle linear, and in general I agree, but sometimes it would be comfy to ramify pipes away. It overcomes native magrittr %T>% by allowing more than one step after cutting the pipe. Imagine you need to create a list with means, correlations, and regression results. And you like to do it in one single pipe. In general, it is not possible, and you'll have to start a second pipe, probably doing some redundant computations. Here is an example that allows it:

  ramify(1) %>%
    branch(1) %>% colMeans %>%
    branch(2) %>% lm(a ~ b, .) %>% broom::tidy(.) %>%