Search code examples
rtidyrmagrittr

Split a pipe so output goes to two separate commands


I am in the process of learning the tidyverse and am loving the flow the pipe operator offers. I was wondering, is it possible to split a pipe at all so that the output from one part of the pipe can go to two separate commands? I have done a little research on this and have seen nothing about this being possible. So that instead of doing something like this where you would have to save the first step.

iris_filter <- iris %>% 
  filter(Sepal.Length <= 5.8)

iris_filter %>% 
  summarise(n= n())

iris_filter %>% 
  arrange(Sepal.Length)

Could you instead have filter passed to two separate commands and continue down two distinct pipe paths? A little image to clarify what I am curious is possible. enter image description here


Solution

  • The %T>% operator from the magrittr-package seems to be what you are looking for.

    However for that specific problem I would write a custom function which outputs the original data:

    library(tidyverse)
    
    custom.function <- function(x) {
      summarise(x, n = n()) %>%
        print()
    
      return(x)
    }
    
    iris %>% 
      filter(Sepal.Length <= 5.8) %>%
      custom.function() %>%
      arrange(Sepal.Length)
    #>    n
    #> 1 80
    #>    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
    #> 1           4.3         3.0          1.1         0.1     setosa
    #> 2           4.4         2.9          1.4         0.2     setosa
    #> 3           4.4         3.0          1.3         0.2     setosa
    #> 4           4.4         3.2          1.3         0.2     setosa
    #> 5           4.5         2.3          1.3         0.3     setosa
    #> 6           4.6         3.1          1.5         0.2     setosa
    #> 7           4.6         3.4          1.4         0.3     setosa
    #> 8           4.6         3.6          1.0         0.2     setosa
    #> 9           4.6         3.2          1.4         0.2     setosa
    #> 10          4.7         3.2          1.3         0.2     setosa
    #> 11          4.7         3.2          1.6         0.2     setosa
    #> 12          4.8         3.4          1.6         0.2     setosa
    #> 13          4.8         3.0          1.4         0.1     setosa
    #> 14          4.8         3.4          1.9         0.2     setosa
    #> 15          4.8         3.1          1.6         0.2     setosa
    #> 16          4.8         3.0          1.4         0.3     setosa
    #> 17          4.9         3.0          1.4         0.2     setosa
    #> 18          4.9         3.1          1.5         0.1     setosa
    #> 19          4.9         3.1          1.5         0.2     setosa
    #> 20          4.9         3.6          1.4         0.1     setosa
    #> 21          4.9         2.4          3.3         1.0 versicolor
    #> 22          4.9         2.5          4.5         1.7  virginica
    #> 23          5.0         3.6          1.4         0.2     setosa
    #> 24          5.0         3.4          1.5         0.2     setosa
    #> 25          5.0         3.0          1.6         0.2     setosa
    #> 26          5.0         3.4          1.6         0.4     setosa
    #> 27          5.0         3.2          1.2         0.2     setosa
    #> 28          5.0         3.5          1.3         0.3     setosa
    #> 29          5.0         3.5          1.6         0.6     setosa
    #> 30          5.0         3.3          1.4         0.2     setosa
    #> 31          5.0         2.0          3.5         1.0 versicolor
    #> 32          5.0         2.3          3.3         1.0 versicolor
    #> 33          5.1         3.5          1.4         0.2     setosa
    #> 34          5.1         3.5          1.4         0.3     setosa
    #> 35          5.1         3.8          1.5         0.3     setosa
    #> 36          5.1         3.7          1.5         0.4     setosa
    #> 37          5.1         3.3          1.7         0.5     setosa
    #> 38          5.1         3.4          1.5         0.2     setosa
    #> 39          5.1         3.8          1.9         0.4     setosa
    #> 40          5.1         3.8          1.6         0.2     setosa
    #> 41          5.1         2.5          3.0         1.1 versicolor
    #> 42          5.2         3.5          1.5         0.2     setosa
    #> 43          5.2         3.4          1.4         0.2     setosa
    #> 44          5.2         4.1          1.5         0.1     setosa
    #> 45          5.2         2.7          3.9         1.4 versicolor
    #> 46          5.3         3.7          1.5         0.2     setosa
    #> 47          5.4         3.9          1.7         0.4     setosa
    #> 48          5.4         3.7          1.5         0.2     setosa
    #> 49          5.4         3.9          1.3         0.4     setosa
    #> 50          5.4         3.4          1.7         0.2     setosa
    #> 51          5.4         3.4          1.5         0.4     setosa
    #> 52          5.4         3.0          4.5         1.5 versicolor
    #> 53          5.5         4.2          1.4         0.2     setosa
    #> 54          5.5         3.5          1.3         0.2     setosa
    #> 55          5.5         2.3          4.0         1.3 versicolor
    #> 56          5.5         2.4          3.8         1.1 versicolor
    #> 57          5.5         2.4          3.7         1.0 versicolor
    #> 58          5.5         2.5          4.0         1.3 versicolor
    #> 59          5.5         2.6          4.4         1.2 versicolor
    #> 60          5.6         2.9          3.6         1.3 versicolor
    #> 61          5.6         3.0          4.5         1.5 versicolor
    #> 62          5.6         2.5          3.9         1.1 versicolor
    #> 63          5.6         3.0          4.1         1.3 versicolor
    #> 64          5.6         2.7          4.2         1.3 versicolor
    #> 65          5.6         2.8          4.9         2.0  virginica
    #> 66          5.7         4.4          1.5         0.4     setosa
    #> 67          5.7         3.8          1.7         0.3     setosa
    #> 68          5.7         2.8          4.5         1.3 versicolor
    #> 69          5.7         2.6          3.5         1.0 versicolor
    #> 70          5.7         3.0          4.2         1.2 versicolor
    #> 71          5.7         2.9          4.2         1.3 versicolor
    #> 72          5.7         2.8          4.1         1.3 versicolor
    #> 73          5.7         2.5          5.0         2.0  virginica
    #> 74          5.8         4.0          1.2         0.2     setosa
    #> 75          5.8         2.7          4.1         1.0 versicolor
    #> 76          5.8         2.7          3.9         1.2 versicolor
    #> 77          5.8         2.6          4.0         1.2 versicolor
    #> 78          5.8         2.7          5.1         1.9  virginica
    #> 79          5.8         2.8          5.1         2.4  virginica
    #> 80          5.8         2.7          5.1         1.9  virginica
    

    Created on 2018-11-04 by the reprex package (v0.2.1)