3.15 Pipes: Tee Pipe Intermediate Results

20210103 A use of the tee-pipe that can come in handy is to base::assign() intermediate results into a variable without breaking the main flow of the pipeline. The variable tmp in the following tee-pipe is assigned to within the base::globalenv() so that we can access it from anywhere in the R session.

ds %>%
  select(rainfall, min_temp, max_temp, sunshine) %>%
  filter(rainfall==0) %T>%
  {assign("tmp", ., globalenv())}  %>%
  select(min_temp, max_temp, sunshine) %>%
  summary()
##     min_temp       max_temp       sunshine    
##  Min.   :-8.7   Min.   :-2.1   Min.   : 0.00  
##  1st Qu.: 7.4   1st Qu.:19.7   1st Qu.: 6.90  
##  Median :12.1   Median :24.5   Median : 9.70  
##  Mean   :12.1   Mean   :24.9   Mean   : 8.72  
##  3rd Qu.:16.9   3rd Qu.:29.9   3rd Qu.:11.10  
##  Max.   :33.9   Max.   :48.9   Max.   :14.50  
##  NA's   :567    NA's   :614    NA's   :66012

We can now access the variable tmp:

print(tmp)
## # A tibble: 121,167 x 4
##    rainfall min_temp max_temp sunshine
##       <dbl>    <dbl>    <dbl>    <dbl>
##  1        0      7.4     25.1       NA
##  2        0     12.9     25.7       NA
##  3        0      9.2     28         NA
##  4        0     14.3     25         NA
##  5        0      7.7     26.7       NA
##  6        0      9.7     31.9       NA
##  7        0     13.4     30.4       NA
##  8        0      8.4     24.6       NA
##  9        0     14.1     20.9       NA
## 10        0      9.8     25.6       NA
## # … with 121,157 more rows

Notice the use of the curly braces for the first path of the tee-pipe. This allows us to reference the data from the pipeline as the period (.).



Your donation will support ongoing development and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 1995-2021 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0.