3

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
Arthur Yip
  • 5,810
  • 2
  • 31
  • 50

2 Answers2

14

%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
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
3

We can wrap it in a {} to get the equivalent output

library(dplyr)
rnorm(200) %>%
   matrix(ncol = 2)  %>% 
      { plot(.)
       colSums(.)
    }

NOTE: This was posted first with the {}

akrun
  • 874,273
  • 37
  • 540
  • 662