Classes Methods
Classes Methods
R was originally quite interesting because it is both interactive and has a system for object
orientation.
In R much of the code for supporting classes/methods is written by J ohn Chambers himself (the
creator of the original S language) and documented in the book Programming with Data: A Guide
to the S Language
A natural extension of Chambers idea of allowing someone to cross the user programmer
spectrum
Object oriented programming is a bit different in R than it is in most languages even if you are
familiar with the idea, you may want to pay attention to the details
Other languages which support OOP (C++, J ava, Lisp, Python, Perl) generally speaking are
not interactive languages
-
S3 classes/methods
S4 classes/methods
Included with version 3 of the S language.
Informal, a little kludgey
Sometimes called old-style classes/methods
For now (and the forseeable future), S3 classes/methods and S4 classes/methods are separate
systems (but they can be mixed to some degree).
Each system can be used fairly independently of the other.
Developers of new projects (you!) are encouraged to use the S4 style classes/methods.
But many developers still use S3 classes/methods because they are quick and dirty (and
easier).
In this lecture we will focus primarily on S4 classes/methods
The code for implementing S4 classes/methods in R is in the methods package, which is usually
loaded by default (but you can load it with l i br ar y( met hods) if for some reason it is not
loaded)
A class is a description of an thing. A class can be defined using set Cl ass( ) in the methods
package.
An object is an instance of a class. Objects can be created using new( ) .
A method is a function that only operates on a certain class of objects.
A generic function is an R function which dispatches methods. A generic function typically
encapsulates a generic concept (e.g. pl ot , mean, pr edi ct , ...)
A method is the implementation of a generic function for an object of a particular class.
The help files for the methods package are extensive do read them as they are the primary
documentation
You may want to start with ?Cl asses and ?Met hods
Check out ?set Cl ass, ?set Met hod, and ?set Gener i c
Some of it gets technical, but try your best for nowit will make sense in the future as you keep
using it.
Most of the documentation in the methods package is oriented towards developers/programmers
as these are the primary people using classes/methods
All objects in R have a class which can be determined by the class function
cl ass( 1)
## [ 1] "numer i c"
cl ass( TRUE)
## [ 1] "l ogi cal "
cl ass( r nor m( 100) )
## [ 1] "numer i c"
cl ass( NA)
## [ 1] "l ogi cal "
cl ass( "f oo")
## [ 1] "char act er "
Data classes go beyond the atomic classes
x <- r nor m( 100)
y <- x + r nor m( 100)
f i t <- l m( y ~ x) ## l i near r egr essi on model
cl ass( f i t )
## [ 1] "l m"
S4 and S3 style generic functions look different but conceptually, they are the same (they play the
same role).
When you program you can write new methods for an existing generic OR create your own
generics and associated methods.
Of course, if a data type does not exist in R that matches your needs, you can always define a
new class along with generics/methods that go with it
The pl ot function is generic and its behavior depends on the object being plotted.
set . seed( 10)
x <- r nor m( 100)
pl ot ( x)
For time series objects, pl ot connects the dots
set . seed( 10)
x <- r nor m( 100)
x <- as. t s( x) ## Conver t t o a t i me ser i es obj ect
pl ot ( x)
If you write new methods for new classes, youll probably end up writing methods for the following
generics:
There are two ways that you can extend the R system via classes/methods
print/show
summary
plot
Write a method for a new class but for an existing generic function (i.e. like pr i nt )
Write new generic functions and new methods for those generics
Creating new classes/methods is usually not something done at the console; you likely want to save
the code in a separate file
l i br ar y( met hods)
set Cl ass( "pol ygon",
r epr esent at i on( x = "numer i c",
y = "numer i c") )
The slots for this class are xand y
The slots for an S4 object can be accessed with the @operator.
A plot method can be created with the set Met hod function.
For set Met hod you need to specify a generic function (pl ot ), and a signature.
A signature is a character vector indicating the classes of objects that are accepted by the
method.
In this case, the pl ot method will take one type of object, a pol ygon object.