-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
magrittr.Rmd
119 lines (82 loc) · 2.63 KB
/
magrittr.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
---
title: "Magrittr"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(magrittr) # %>%
```
# The Pipe %>%
```{r normalPipe}
# General operation moving forward
# CTRL-SHIFT-M
c(5,4,2,6,3,4,3,4,6) %>%
mean()
mean(c(5,4,2,6,3,4,3,4,6))
# The pipe is like the unix command line pipe,
# take output of left and pipe into the right
# The pipe in R differs in that you can break lines
# and it doesnt need to be STDIN or STDOUT
#------------------------------------------------#
# Taking a forward moving operation and assigning it to a variable
# read this as:
# "X receives the value of the vector going through the mean function"
x <- c(5,4,2,6,3,4,3,4,6) %>%
mean()
cat("the value of x is: ", x)
```
# The Compound Pipe %<>%
```{r compoundPipe}
# Normal pipe pushes results forward through processes
# the compound pipe pushes the operation forward but also assigns the result
# back to the initial object, basically it becomes '<-'
# first y contains this vector of numbers
y <- c(5,4,2,6,3,4,3,4,6)
# Then we compound pipe that vector into the mean function and the result of
# the mean function is then re-assigned to y, akin to:
# y = mean(y)
y %<>% mean()
cat("the value of y is: ", y)
```
# The Exposition Pipe %$%
```{r espositionPipe}
# When piping this tribble forward it assumes i want to
# calculate the mean of both vectors
tibble::tribble(
~name, ~age,
"john", 26,
"karen", 54,
"susan", 45,
"joe", 16
) %>%
mean(age) # this does not work
#------------------------------------------------#
# when using the exposition pipe %$% it allows you to use the names of the
# content on the 'left hand side' of the piping operation and therefore
# reference only the numeric vector in the mean function.
# so what appears in the mean function is equivalently exampleTribble$age
tibble::tribble(
~name, ~age,
"john", 26,
"karen", 54,
"susan", 45,
"joe", 16
) %$%
mean(age) # and so this works
```
# the Tee pipe
```{r teePipe}
# pipe a a value forward but return the original value
# value -------------> into a function (This is not returned)
# |
# |
# |
# |
# |
# V
# Returned Result
# value is piped into the mean function but only vector is returned
c(5,4,2,6,3,4,3,4,6) %T>% mean() # Shows only the vector
# This is useful when an expression is used for its side-effect, say plotting or printing.
c(5,4,2,6,3,4,3,4,6) %T>% plot() # shows the vector and the plot
```