-
Notifications
You must be signed in to change notification settings - Fork 0
/
magrittr.tex
180 lines (148 loc) · 4.68 KB
/
magrittr.tex
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
\documentclass{beamer}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{framed}
\begin{document}
%=================================================%
\begin{frame}
\begin{figure}
\centering
\includegraphics[width=0.99\linewidth]{images/pipe}
\end{figure}
\begin{center}
{\huge THE $ \%>\% $ OPERATOR}
\end{center}
\end{frame}
\begin{frame}
\begin{figure}
\centering
\includegraphics[width=0.99\linewidth]{images/pipe2}
\end{figure}
\end{frame}
%=================================================%
\begin{frame}[fragile]
\frametitle{ The $\%>\%$ operator}
\LARGE
% - http://r2014-mtp.sciencesconf.org/file/92631
\begin{itemize}
\item From \textbf{magrittr} package.
\item Used extensively in \textbf{dplyr}.
\item $\%>\%$ is a piping operator, and can be verbalised as ``\textit{then}".
\item It takes the output of the left side, and uses it as the first
argument of the function on the right side.
\end{itemize}
\end{frame}
%================================================================= %
\begin{frame}[fragile]
\frametitle{magrittr : the $\%>\%$ operator}
\Large
\begin{framed}
\begin{verbatim}
subset(mtcars, cyl == 6, c(mpg, wt))
mtcars %>% subset(cyl == 6, c(mpg, wt))
\end{verbatim}
\end{framed}
\end{frame}
\begin{frame}
\begin{figure}
\centering
\includegraphics[width=0.99\linewidth]{images/magrittrcode01}
\end{figure}
\end{frame}
%================================================================= %
\begin{frame}[fragile]
\frametitle{magrittr : the $\%>\%$ operator}
\Large
\begin{framed}
\begin{verbatim}
summary(subset(mtcars, cyl == 6,
c(mpg, wt)), digits=2)
mtcars %>%
subset(cyl == 6, c(mpg, wt)) %>%
summary(digits=2)
\end{verbatim}
\end{framed}
\end{frame}
%================================================================= %
\begin{frame}[fragile]
\frametitle{magrittr : the $\%>\%$ operator}
\Large
\begin{framed}
\begin{verbatim}
mtcars %>%
subset(cyl == 6, c(mpg, wt)) %>%
summary(digits=2)
\end{verbatim}
\end{framed}
\begin{itemize}
\item Get the mtcars data set
\item \textbf{Then} subset it like this
\item \textbf{Then} get the summary, with this setting
\end{itemize}
\end{frame}
\begin{frame}
\begin{figure}
\centering
\includegraphics[width=0.99\linewidth]{images/magrittrcode02}
\end{figure}
\end{frame}
\end{document}
%================================================================= %
\begin{frame}
\frametitle{magrittr : the $\%>\%$ operator}
\begin{itemize}
\item You can use the $\%>\%$ operator with any \texttt{R} functions.
\item The rules are simple: the object on the left hand side is passed as the first argument to the function on the right hand side. So:
\end{itemize}
\begin{framed}
my.data $\%>\%$ \texttt{my.function} is the same as \texttt{my.function(my.data)}
my.data $\%>\%$ \texttt{my.function(arg=value)} is the same as \texttt{my.function(my.data, arg=value)}
\end{framed}
\end{frame}
%=================================================%
\begin{frame}[fragile]
\frametitle{The $ \%>\% $ Operator}
\Large
\textbf{The $ \%>\% $ Operator}
\begin{itemize}
\item ggvis makes use of the $ \%>\% $ operator from the
package magrittr
\item This allows us to layer up graphics in the same
way we would with ggplot2
\end{itemize}
\end{frame}
%=================================================%
\begin{frame}[fragile]
\frametitle{The $ \%>\% $ Operator}
\textbf{Tube Data Example }\\ (\textit{Dr. Aimee Gott, Mango Solutions})
% \begin{itemize}
% \item The $ \%>\% $ operator passes the left hand object to the
% first argument of the right hand expression
% \item We can pass data or objects to functions in this way
%
% \end{itemize}
\begin{framed}
\begin{verbatim}
> tubeData$Excess %>% tapply(tubeData$Line, mean)
# Bakerloo 5.047714
# Central 5.998667
# Circle & HamDistrict 7.166095
\end{verbatim}
\end{framed}
\end{frame}
%================================================================= %
\begin{frame}
\frametitle{magrittr : the $\%>\%$ operator}
\Large
\vspace{-1cm}
\textbf{ $ \%>\% $ in ggvis}
\begin{itemize}
\item With ggvis we pass "\texttt{ggvis}" objects
\item We create the initial object by passing data to
\texttt{ggvis()}
\item All other functions expect a ggvis object as the
first argument and return a ggvis object
\end{itemize}
\end{frame}
%============================================================================= %
\end{document}