Search code examples
rtidyversemagrittr

magrittr tee pipe %T>% equivalent


I'm looking at the example of the magrittr tee pipe %T>% in the documentation, but I'm not immediately grasping what it's doing. Can someone show what the equivalent code is without the tee pipe?

rnorm(200) %>%
matrix(ncol = 2) %T>%
plot %>% # plot usually does not return anything. 
colSums

Solution

  • %T>% is presumably named after the T-shaped pipe splitter used in plumbing.

    T splitter

    We can replace %T>% with %>% if we replace the plot line in the question with the plot line shown below (and marked with a double hash). This plots its input and then forwards the input to the output so that the pipeline can be continued.

    rnorm(200) %>%
      matrix(ncol = 2) %>%
      { plot(.); . } %>%    ##
      colSums
    

    Thus the processing follows this graph:

    rnorm --> matrix --T--> colSums
                       |
                       v
                      plot