-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSec-Programming.Rnw
170 lines (144 loc) · 3.41 KB
/
Sec-Programming.Rnw
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
\section{Programming}
\begin{frame}
\begin{block}{Flow control}
<<flowctrl, eval = FALSE, tidy = FALSE>>=
for (var in seq) expr
while (cond) expr
repeat expr
break
@
\end{block}
\end{frame}
\begin{frame}[fragile]
<<for>>=
for (i in 1:4) { ## bad
print(i^2)
}
(1:4)^2 ## good
@
\end{frame}
\begin{frame}
\begin{block}{The \Rfunction{apply} family and friends}
\begin{itemize}
\item Applies a function to each element of an input, being a list or a vector (\Rfunction{sapply}, \Rfunction{lapply}), a matrix or a data frame (\Rfunction{apply}) or an environment (\Rfunction{eapply}).
\item Same functionality than an explicit \Robject{for} loop, but often more elegant, function-centric, \textbf{not} faster.
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]
<<slapply>>=
sapply(month.name[1:2], paste0, "_2012")
lapply(month.name[1:2], paste0, "_2012")
@
\end{frame}
\begin{frame}[fragile]
<<apply>>=
M <- matrix(1:9, ncol = 3)
M
apply(M, 1, sum) ## better rowSums
apply(M, 2, sum) ## better colSums
2
@
\end{frame}
\begin{frame}[fragile]
<<replicate>>=
mean(rnorm(100))
replicate(3, mean(rnorm(100)))
replicate(2, rnorm(3))
@
\end{frame}
%% eapply
%% replicate
%% by
%% tapply
%% rapply
\begin{frame}
\begin{block}{Conditionals}
<<flow, eval = FALSE, tidy = FALSE>>=
if (cond) expr1 else expr2
ifelse(cond, expr1, expr2)
switch
@
\end{block}
\end{frame}
\begin{frame}[fragile]
<<ifelse>>=
x <- 2
if (x > 0) { ## bad
log2(x)
} else {
log2(-x)
}
log2(abs(x)) ## better
@
\end{frame}
\begin{frame}
\begin{block}{Exception handling}
\begin{description}
\item[\Rfunction{try(exprs)}] will either return the value of the expression \Robject{expr}, or an object of class \Robject{try-error}.
\item[\Rfunction{tryCatch}] provides a more configurable mechanism for condition handling and error recovery.
\end{description}
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Writing functions}
<<fun, eval=FALSE, tidy = FALSE>>=
myFun <- function(param1, param2, ...) {
## function body
## acting on copies of the params
ans <- param1 + param2
return(ans)
}
@
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Function facts}
\begin{itemize}
\item Single \Rfunction{return} value.
\item To return multiple items, use a \Robject{list} or define
your own \Robject{object} (see OO programming).
\item The return value is either the last statement, or explicit
return using \Rfunction{return} (can be called from any where in
a function)
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Function facts (cont.)}
\begin{itemize}
\item Functions act on a pass-by-copy semantic.
<<f1>>=
x <- 1
f <- function(x) { x <- x + 10; x }
f(x)
x
@
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Function facts (cont.)}
\begin{itemize}
\item Functions live/act in their own environment and
have access to \textit{global} variables.
<<f2>>=
x <- 1
f <- function() { x <- x + 10; x }
f()
x
@
\end{itemize}
\end{block}
\end{frame}
%% \begin{frame}[fragile]
%% \begin{block}{Anonymous function}
%% <<anonymfun>>=
%% M <- matrix(rnorm(50), ncol = 5)
%% M[sample(50, 10)] <- NA
%% sum(is.na(M))
%% apply(M, 1, function(x) sum(is.na(x)))
%% apply(M, 2, function(x) sum(is.na(x)))
%% @
%% \end{block}
%% \end{frame}