forked from lgatto/roo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRef.Rnw
138 lines (118 loc) · 4.07 KB
/
Ref.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
<<knitr, echo=FALSE>>=
library("knitr")
opts_chunk$set(tidy.opts =
list(width.cutoff = 50,
tidy = FALSE),
fig.align = 'center',
stop_on_error = 1L,
comment = NA,
prompt = TRUE)
options(width = 60)
@
\section{S4 Reference Classes}
\begin{frame}[fragile]{Reference classes}
\begin{block}{Reference classes}
\begin{itemize}
\item This paradigm uses \textbf{pass-by-reference} semantics:
invoking a method may modify the content of the fields.
\item Methods in this paradigm are associated with the object (rather than to generics)
\item Java-like logic.
\item See \texttt{?ReferenceClasses} for all the details.
\end{itemize}
\end{block}
\begin{example}
\begin{verbatim}
## here, you would have
> a$seq() ## equivalent of seq(a)
[1] "AGCATG"
> a$comp()
> a$seq()
[1] "TCGTAC"
\end{verbatim}
\end{example}
\end{frame}
\begin{frame}[fragile]{Reference classes - objects and fields}
\begin{block}{Defining a reference class}
Slots $\rightarrow$ a list of \textbf{fields}
\end{block}
\bigskip
<<RefClass-1,tidy=FALSE>>=
Seq <- setRefClass("Seq",
fields = list(
id = "character",
alphabet = "character",
sequence = "character"))
@
\begin{block}{Generator objects}
The return value of \code{setRefClass} is a \textbf{generator object} that is used
to construct new object of given class.
\end{block}
\end{frame}
\begin{frame}[fragile]{Reference classes - methods}
\begin{block}{Defining a reference class}
Methods $\rightarrow$ a list of \textbf{functions}
\end{block}
\begin{scriptsize}
<<RefClass-2, tidy = FALSE>>=
Seq <- setRefClass("Seq",
fields = list(
id = "character",
alphabet = "character",
sequence = "character"),
methods = list(
comp = function() {
"Complements the (DNA) sequence" ## inline docs
sequence <<- chartr("ACGT","TGCA",.self$sequence)
id <<- paste(.self$id, "-- complemented")
}
## there can be more, of course
))
@
\end{scriptsize}
\begin{block}{}
Methods can be added either directly in class definition or later by calling
\code{Seq\$methods(functionName = function() \{ ...code... \})}.
\end{block}
\end{frame}
\begin{frame}[fragile]{Using the reference classes}
\begin{block}{You also need to know that...}
\begin{itemize}
\item accessing fields and calling methods is done with the \code{\$} operator.
\item the current object can be referred to in a method by the reserved name \code{.self}.
\item Changing fields of an object within methods needs to be done with the \code{{<}{<}-} operator.
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{scriptsize}
<<RefClass-example,echo=TRUE, tidy=FALSE>>=
s <- Seq$new(id="foo", sequence="GATCATCA")
s
s$sequence
s$comp()
s$sequence
@
%$
\end{scriptsize}
\end{frame}
\begin{frame}[fragile]{Reference classes}
\begin{block}{Suitable for...}
Reference classes are suitable for objects that are \textit{dynamically tracked} by all the code:
GUI components, read-only access to files (streams, data bases), internet resources,
editing facilities, ...
\end{block}
\end{frame}
%% Final Reference class exercises
\begin{frame}[fragile,t]{S4 introspection and methods exercises}
\begin{block}{\exercise (code: \code{05_seqRefClass.R})}
We implemented some more methods using Reference Classes. Read through the methods
and make sure you understand how they work. Then try out the test code in
\code{05_seqRefClass.R}. What happens when we assign one Reference Class object
to another?
\end{block}
%% \begin{block}{\exercise}
%% What is the result of \code{isS4(Seq)}? Can you find out more about the implementation of
%% Reference Classes? \\
%% {\small Tip: try \code{ls(envir=....)}}
%% \end{block}
\end{frame}