其他分享
首页 > 其他分享> > Why Tee Pipe Is Bad Idea And How to Replace It

Why Tee Pipe Is Bad Idea And How to Replace It

作者:互联网

1. What is tee pipe

Tee pipe is an optional pipe tool from package magrittr. It solves a kind of problems that some manipulations, like plotting, printing, saving data to disk, don't return anything so they will eventually terminate the pipe.

Sometimes people want to keep the pipe going on, so tee pipe (%T>%) is invented. 

%T>% works like %>% except that it returns the left-hand side instead of the right-hand side. Because of this reason, even if a terminating manipulation exists in our pipe, our pipe will not stop.

It sounds like a good idea. For example:

library(tidyverse)
library(magrittr)  # to use tee pipe, we need to explicitly import magrittr
library(lubridate) # for date manipulation

 

# the data
flights <- nycflights13::flights

flights %>%
  mutate(date=make_date(year, month, day)) %>%
  group_by(date) %>%
  summarise(delay=mean(dep_delay, na.rm=TRUE)) %T>%
  plot(type="l") %>%
  mutate(dow=week(date))

 Results:

# A tibble: 365 x 3
   date       delay   dow
   <date>     <dbl> <dbl>
 1 2013-01-01 11.5      1
 2 2013-01-02 13.9      1
 3 2013-01-03 11.0      1
 4 2013-01-04  8.95     1
 5 2013-01-05  5.73     1
 6 2013-01-06  7.15     1
 7 2013-01-07  5.42     1
 8 2013-01-08  2.55     2
 9 2013-01-09  2.28     2
10 2013-01-10  2.84     2
# ... with 355 more rows

 

As we can see, thanks to the %T%, even if a terminating function plot() in the pipeline, the whole process will not stop and still goes on to next step.

 

2. However, ggplot() cannot work. Hadley Wickham answered.

The same process if we replace plot() with ggplot(), we will find it errors.

# Use english error message:
# Sys.setenv(LANGUAGE = "en")

# not working
flights %>%
  mutate(date=make_date(year, month, day)) %>%
  group_by(date) %>%
  summarise(delay=mean(dep_delay, na.rm=TRUE)) %T>%
  ggplot() +
  geom_line(aes(x=date, y=delay))

The error message suggests that the "+" is misunderstood by R. But even if we do the change, it still not works well. It only pass data, but no plot is showing.

# only data appears, no plot
flights %>%
  mutate(date=make_date(year, month, day)) %>%
  group_by(date) %>%
  summarise(delay=mean(dep_delay, na.rm=TRUE)) %T>%
  {ggplot() +
  geom_line(aes(x=date, y=delay))}

According to Hadley Wickham answered at stackoverflow, ggplot() actually returns the plot, not as other side-effect functions not return anything.

So %T>%, which return left-hand but not return right-hand result, will prevent ggplot() works properly.

There are some discussion on github magrittr page about people want Hadley change code in ggplot but he refused.

The discussion will go on, but I myselft don't think ggplot is the main problem here of tee pipe.

 

3. Not only ggplot() problem, tee pipe is actually against the rule of pipeline  

3.1 Two rules

There are two rules of pipe I think is good suggested:
(More: https://r4ds.had.co.nz/pipes.html#when-not-to-use-the-pipe)

- Your pipes should no longer than (say) ten steps.
- Your pipes should not have multiple inputs or outputs.

Pipes work well with liner manipulation, but expressing complex relationships will typically yield confusing code.
Tee pipes, however, return more than one output at a time and make the process no longer a liner.
That is the real reason why "tee pipe is a bad idea".

3.2 Easily replace tee pipe

The abuse of pipe creates not only one but many many tee pipe style problems.

Replace tee pipe with a liner workflow is more wisdom choice.

# the data
flights <- nycflights13::flights

flights <- flights %>%
  mutate(date=make_date(year, month, day)) %>%
  select(date, everything())
flights

# do some summary 
delay <- flights %>%
  group_by(date) %>%
  summarise(delay=mean(dep_delay, na.rm=TRUE))
delay

# do some plot
delay %>%
  ggplot() +
  geom_line(aes(x=date, y=delay)) 

# use original data furthermore
delay <- delay %>%
  mutate(dow=week(date))
delay

3.3 If you have to use tee pipe

Actually we can use {} function to do the same thing without tee pipe %T>%.

{} works like an anonymous function but without the function(x){...} syntax.

As below example, in the first line we use print() to explicitly print out the plot created by ggplot(). So that even if the plot is not the last return object we still can see it.

The second line(the last line) means return like a normal R function does. The "." stands for "current data" so it returns current data and the pipe will pass it into next step.

# the data
flights <- nycflights13::flights

# use {} instead
flights %>%
  mutate(date=make_date(year, month, day)) %>%
  group_by(date) %>%
  summarise(delay=mean(dep_delay, na.rm=TRUE)) %>%
  {print(ggplot(.) + geom_line(aes(x=date, y=delay))); .} %>%
  mutate(dow=week(date))

Results:

# A tibble: 365 x 3
   date       delay   dow
   <date>     <dbl> <dbl>
 1 2013-01-01 11.5      1
 2 2013-01-02 13.9      1
 3 2013-01-03 11.0      1
 4 2013-01-04  8.95     1
 5 2013-01-05  5.73     1
 6 2013-01-06  7.15     1
 7 2013-01-07  5.42     1
 8 2013-01-08  2.55     2
 9 2013-01-09  2.28     2
10 2013-01-10  2.84     2
# ... with 355 more rows

  

  

  

 

 

 

 

 

 

 

  

 

 

 

 

 

 

 

  

 

标签:delay,01,pipe,ggplot,Idea,Tee,Pipe,date,2013
来源: https://www.cnblogs.com/drvongoosewing/p/14317112.html