ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Why Tee Pipe Is Bad Idea And How to Replace It

2021-01-23 12:03:57  阅读:221  来源: 互联网

标签:delay 01 pipe ggplot Idea Tee Pipe date 2013


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有