Reputation: 121
Me and a couple of colleagues are wondering why
N <- rnorm(16) %>% matrix(., ncol = 4) %>% `colnames<-`(letters[1:4]) %T>% summary()
or
N <- rnorm(16) %>% matrix(., ncol = 4) %>% `colnames<-`(letters[1:4]) %T>% summary() %>% `+`(., 0)
do not work (summary is not printed), while
N <- rnorm(16) %>% matrix(., ncol = 4) %>% `colnames<-`(letters[1:4]) %T>% {print(summary(.))}
does?
Upvotes: 0
Views: 150
Reputation: 145775
There's no reason for summary
to print its output any more than rnorm
or matrix
or any of the other functions in the pipeline. Printing is generally suppressed when there is assignment <-
.
N <- rnorm(16) %>% matrix(., ncol = 4) %>% `colnames<-`(letters[1:4]) %T>% summary()
# no printing
N <- 5
# no printing
M <- rnorm(16) %>% matrix(., ncol = 4) %>% `colnames<-`(letters[1:4])
# no printing
M %>% summary
summary(M)
# prints
sM <- M %>% summary
sM <- summary(M)
# no printing
# assignment prevents implicit printing.
An explicit print
call creates a side-effect (printing), which is what you want to happen.
The tee pipe %T>%
doesn't create any side-effects (like printing), it just returns the LHS so that if you %T>%
into a function that does create side-effects but does not return it's input (like plot()
), you can get those side effects while still piping the input to another step.
Note that print
does return its argument (invisibly), so you don't actually need the tee pipe %T>%
with print
.
Upvotes: 9