3.11 Pipeline Identity Operator

REVIEW A handy trick when building a pipeline is to use what is effectively the identity operator. An identity operator simply takes the data being communicated through the pipeline, and without changing it passes it on to the next operator in the pipeline. An effective identity operator is constructed with the syntax {.}. In R terms this is a compound statement containing just the period whereby the period represents the data. Effectively this is an operator that passes the data through without processing it—an identity operator.

Why is this useful? Whilst we are building our pipeline, one line at a time, we will be wanting to put a pipe at the end of each line, but of cause we can not do so if there is no following operator. Also, whilst debugging a pipeline, we may want to execute only a part of it, and so the identity operator is handy there too.

As a typical scenario we might be in the process of building a pipeline as here and find that including the %>% at the end of the line of the dplyr::select() operation:

ds %>%
  select(rainfall, min_temp, max_temp, sunshine) %>%
  {.}
## # A tibble: 275,410 × 4
##    rainfall min_temp max_temp sunshine
##       <dbl>    <dbl>    <dbl>    <dbl>
##  1      0.6     13.4     22.9       NA
##  2      0        7.4     25.1       NA
##  3      0       12.9     25.7       NA
##  4      0        9.2     28         NA
##  5      1       17.5     32.3       NA
##  6      0.2     14.6     29.7       NA
##  7      0       14.3     25         NA
##  8      0        7.7     26.7       NA
##  9      0        9.7     31.9       NA
## 10      1.4     13.1     30.1       NA
## # ℹ 275,400 more rows

We then add the next operation into the pipeline without having to modify any of the code already present:

ds %>%
  select(rainfall, min_temp, max_temp, sunshine) %>%
  summary() %>%
  {.}
##     rainfall          min_temp       max_temp        sunshine     
##  Min.   :  0.000   Min.   :-8.7   Min.   :-4.10   Min.   : 0.000  
##  1st Qu.:  0.000   1st Qu.: 7.5   1st Qu.:18.00   1st Qu.: 4.900  
##  Median :  0.000   Median :11.9   Median :22.70   Median : 8.500  
##  Mean   :  2.339   Mean   :12.1   Mean   :23.26   Mean   : 7.629  
##  3rd Qu.:  0.600   3rd Qu.:16.8   3rd Qu.:28.20   3rd Qu.:10.600  
##  Max.   :474.000   Max.   :33.9   Max.   :49.60   Max.   :14.500  
##  NAs    :8094      NAs    :4678   NAs    :4631    NAs    :171126

And so on. Whilst it appears quite a minor convenience, over time as we build more pipelines, this becomes quite a handy trick.



If you find this curated material useful then you can consider a donation to support it's ongoing availability and give you access to the PDF version of this book. The material has been scoped up by Generative AI without permission or any kind of recompense so do consider a donation if you can afford it. Unlike Generative AI your access to this materials is freely given. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Togaware has a 30 year tradition of making popular open source software which includes sold privacy preserving productivity apps, rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0