Object Oriented Programming with R: My notebook
In the following post, I describe how I've used the OOP features of R to create and use the following class hierarchy:
First the class Person is defined. It contains four fields : firstName, lastName, birthDate and birthPlace.
representation(
firstName="character",
lastName="character",
birthDate="Date",
birthPlace="character"
))
A kind of 'constructor' function can be called for Person to check that both firtsName and lastName are not empty:
function(object)
{
length(object@firstName)>0 &&
length(object@lastName)>0
}
)
representation(
deathDate="Date",
deathPlace="character"
),
contains="Person"
)
representation(
knownFor="character"
),
contains="Person"
)
contains=c("Scientist","DeceasedPerson")
)
{
as.integer((Sys.Date()-individual@birthDate)/365)
}
setGeneric("age")
{
as.integer((individual@deathDate-individual@birthDate)/365)
}
setMethod(age,signature=c("DeceasedPerson"),definition=age.of.death)
"Scientist",
firstName="Craig",
lastName="Venter",
birthPlace="Salt Lake City",
birthDate=as.Date("1946-10-14", "%Y-%m-%d"),
knownFor=c("The Institute for Genomic Research","J. Craig Venter Institute")
)
"DeceasedScientist",
firstName="Charles",
lastName="Darwin",
birthDate=as.Date("1809-02-12", "%Y-%m-%d"),
deathDate=as.Date("1882-04-19", "%Y-%m-%d"),
knownFor=c("Natural Selection","The Voyage of the Beagle")
)
Error in validObject(.Object) : invalid class "Person" object: FALSE
[1] TRUE
An object of class “DeceasedScientist”
Slot "knownFor":
[1] "Natural Selection" "The Voyage of the Beagle"
Slot "firstName":
[1] "Charles"
Slot "lastName":
[1] "Darwin"
Slot "birthDate":
[1] "1809-02-12"
Slot "birthPlace":
[1] "Shrewsbury"
Slot "deathDate":
[1] "1882-04-19"
Slot "deathPlace":
character(0)
> craigVenter
An object of class “Scientist”
Slot "knownFor":
[1] "The Institute for Genomic Research" "J. Craig Venter Institute"
Slot "firstName":
[1] "Craig"
Slot "lastName":
[1] "Venter"
Slot "birthDate":
[1] "1946-10-14"
Slot "birthPlace":
[1] "Salt Lake City"
[1] TRUE
> is(craigVenter,"DeceasedScientist")
[1] FALSE
> is(charlesDarwin,"DeceasedScientist")
[1] TRUE
[1] 73 #age.of.death was called
> age(craigVenter)
[1] 63 #generic "age'
Full source code
representation(
firstName="character",
lastName="character",
birthDate="Date",
birthPlace="character"
))
setValidity("Person",
function(object)
{
length(object@firstName)>0 &&
length(object@lastName)>0
}
)
setClass("DeceasedPerson",
representation(
deathDate="Date",
deathPlace="character"
),
contains="Person"
)
setClass("Scientist",
representation(
knownFor="character"
),
contains="Person"
)
age <- function(individual)
{
as.integer((Sys.Date()-individual@birthDate)/365)
}
setGeneric("age")
age.of.death <- function(individual)
{
as.integer((individual@deathDate-individual@birthDate)/365)
}
setClass("DeceasedScientist",
contains=c("Scientist","DeceasedPerson")
)
setMethod(age,signature=c("DeceasedPerson"),definition=age.of.death)
craigVenter <-new(
"Scientist",
firstName="Craig",
lastName="Venter",
birthPlace="Salt Lake City",
birthDate=as.Date("1946-10-14", "%Y-%m-%d"),
knownFor=c("The Institute for Genomic Research","J. Craig Venter Institute")
)
charlesDarwin <-new(
"DeceasedScientist",
firstName="Charles",
lastName="Darwin",
birthDate=as.Date("1809-02-12", "%Y-%m-%d"),
deathDate=as.Date("1882-04-19", "%Y-%m-%d"),
knownFor=c("Natural Selection","The Voyage of the Beagle")
)
try(new("Person",lastName="Darwin",birthDate=as.Date("1809-02-12", "%Y-%m-%d")),FALSE)
charlesDarwin@birthPlace="Shrewsbury"
validObject(charlesDarwin)
charlesDarwin
craigVenter
is(craigVenter,"Person")
is(craigVenter,"DeceasedScientist")
is(charlesDarwin,"DeceasedScientist")
age(charlesDarwin)
age(craigVenter)
That's it !
Pierre
