An introduction to object-oriented programming in R

silkthrilledSoftware and s/w Development

Nov 18, 2013 (3 years and 8 months ago)

93 views

1
Anintroduction to object-oriented
programming in R
Giorgio Valentini
e –mail: valentini@dsi.unimi.it
DSI –Dipartimento di Scienze dell’Informazione
Universitàdegli Studi di Milano
2
WhyOO-programmingin R?
1.Modeling and programming reasons
–OO-modeling and OO-programming are close to the
problem domain
–OO-programming paradigm provides a unified view
of data structures and procedures
–It is easy to use, modifiy and extends OO sw libraries
2.Practical reasons in bioinformatics domain
–Most of Bioconductorlibraries adopt the OO-
programming paradigm
–Many useful R packages are OO
3
OO programming in R: classes and methods
Classes: “ideas”of objects
Methods: functions defined
for specific classes
Objects
4
Classes
•In R all software entities are objects
•Each objectbelong to a class
•A classis a general scheme for objects:
from a general standpoint it represents an “idea”,
i.e. the general structure of a given object
•Objectsare realizations or instances of a class.
5
Classes in R
•Two kinds of information about a
class are commonly stored in R:
1.Representation of the class
2.Relation of the class to other classes
6
Representation of the class
Classes are represented in R in two (non mutually
exclusive) ways:
1.Slots:
–a slot contains an object of another class
–complicated classes can be defined in terms of simpler
ones
2.Prototype:
•A definition of a “prototypical”object for that class
7
Example of class representation and
object generation
# Class representing codons (triplets of nucleotides)
setClass("triplets", representation(x="character"),
prototype(x="UAG"))
# Construction of objects of the “triplets”class
seq0 <-new("triplets"); # prototype is called
seq <-new("triplets",
x = c("AUG","CCA","CCA","GAA","UGA","CCA"));
8
Relations between classes and objects
Object inclusion. Objects of class B can be included
in slots of class A (relation part-of)
Class inheritance. Class B extendsclass A (relation is)
A
B
B
A
B
9
Example of class inheritance
# a class storing codons and the corresponding aa
setClass("Pairedseq",representation(y="character"),
prototype(x="CCA", y=character("Pro")),
contains=“triplets");
# construction of an object of class Pairedseq
paired <-new("Pairedseq", x=c("UUC", "CCA", "GCA"),
y=c("Phe", "Pro", "Ala"));
10
OO programming in R: classes and methods
Classes: “ideas”of objects
Methods: functions defined
for specific classes
Objects
11
Methods
Can we make general functions, that behave
differently with different objects ?
In R we need:
1.A generic function: whatwe wanto to do ?
2.A method: howwe do it with an object of a
specific class ?
12
Methods: an example
# we need a method to compute the number of
# codons that are present in a given object of
# class triplets.
> length(seq) # wrong result!
setMethod("length", "triplets",
function(object) {
l <-length(object@x);
return(l);
}
)
> seq <-new("triplets",
x = c("AUG","CCA","GGA","UGA","CCA"));
> length(seq); # correct
13
Methods in OO programming
•A generic function behaves differentlyon objects
belonging to specific classes (the corresponding method is called)
•With the inheritance mechanism, extended classes
inherit all the methodsdefined for its “parent”
classes
•Examples in the “laboratory”session …
14
What you can do with R OO-programming
•You caneasily define complex data structures, starting
from simpler ones (using the inclusion and inheritance
relations)
•You can define methods that behave differently according
to the objects they are applied to (polymorphism)
•The inheritancemechanism permits to extend to the
derived classes the methods defined for “parent”classes
(reusage of software)
•Modular programmingis easier
•Modelling real problems is simpler