Post on 16-Oct-2021
transcript
What you can do with R OO-programming
β’ You can easily define complex data structures,starting from simpler ones
β’ You can define methods that behave differentlyaccording to the objects they are applied to
β’ Modelling real problems is simpler
Object
β’ β a thing that has state, behaviour, andidentity
β state : all of the properties of the object
β behaviour : how an object acts and reacts fromthe change of state and interactions with otherobjects
β identity :unique property of an object
Classes
β’ In R all software entities are ππππππ‘π
β’ Each ππππππ‘ belong to a ππππ π
β’ A ππππ π is a general scheme for ππππππ‘π :
β From a general standpoint it represents an "ππππβ (i.e general structure of a given object)
β’ ππππππ‘π are realizations or instances of a ππππ π
Classes in R
β’ S3 ππππ π β almost in list structureFor defining new class :
class(obj) <- "class.name"
β’ S4 ππππ π β higher programming
For defining new class :setClass("class.name",
representation(x="type"),prototype(x="..."))
Example 1
β’ Create an ππππππ‘ of ππππ π βhumanβ thatconsist the height, weight, & name.
β Height : 2.54 Γ 12 Γ 6/100
β Weight : 180/2.2
β Name : James
Constructing new S3 classes
Answer 1
jim <- list(height = 2.54 * 12 * 6/100, weight =
180/2.2,name = "James")
class(jim)="human"
class(jim)
jim
back
Example 2
β’ Create an ππππππ‘ of ππππ π βcolourβ that consistthe Cyan Component, Magenta Component,Yellow Component, Black Component, Name, &Type of colour.β CyanComp : 0
β MagentaComp : 0
β YellowComp : 0
β BlackComp :100
β Name : Black
β Type :CMYK
Answer 2
col<- list (CyanComp=0, MagentaComp=0, YellowComp=0,
BlackComp=100, Colour="Black",
Type="CMYK")
class(col)="colour"
col
back
Example 3
β’ A ππππ π representing codons (triplets of nucleotides)setClass("triplets", representation(x="character"),
prototype(x="UAG"))
β’ Construction of objects of the βtripletsβ classseq0 <- new("triplets"); # prototype is called
seq <- new("triplets",
x = c("AUG","CCA","CCA","GAA","UGA","CCA"));
typeof(seq)
Constructing new S4 classes
back
Example 4
β’ Assume we have a simple ππππ π with two slots belowtrack <- setClass("track",slots = c(x="numeric",
y="numeric"))
with an ππππππ‘ from the ππππ π t1 <- track(x = 1:10, y = 1:10 + rnorm(10))
Create a new ππππ π "trackCurveβ by adding one more slot from
the ππππ π before
Answer 4
trackCurve <- setClass("trackCurve", slots =
c(smooth = "numeric"), contains = "track")
t1s <- trackCurve(t1, smooth = 1:10)
Methods
β’ We can make general functions, that behavedifferently with different ππππππ‘π
β’ In R we need:
1. A generic function: what we want to do ?
2. A method: how we do it with an ππππππ‘ of a specific class ?
Methods (2)
β’ Method in S3 ππππ π β almost in printing method,summarizing method, and plotting method
For defining method in S3 ππππ π
β making function function(...){...}
β’ Method in S4 ππππ π
For defining method in S3 ππππ π
β setMethod("method","class.name",
function(...){...})
Example 5
β’ From Example 1, difine a πππ‘βππ for printing the βhumanβ ππππ π
Example 1
Answer 5
jim <- list(height = 2.54 * 12 * 6/100, weight =
180/2.2,name = "James")
class(jim)="human"
print.human <- function(x, ...) {
cat("name:", x$name, "\n")
cat("height:", x$height, "meters", "\n")
cat("weight:", x$weight, "kilograms", "\n")
}
print(jim)
Example 6
β’ From Example 2, difine a πππ‘βππ for printing the characteristics of βcolourβ ππππ π
Example 2
Answer 6
col<- list (CyanComp=0, MagentaComp=0, YellowComp=0,
BlackComp=100, Colour="Black",
Type="CMYK")
class(col)="colour"
print.colour <- function(obj, ...) {
cat("Colour Type:", obj$Type, "\n")
cat("Colour Name:", obj$Colour, "\n")
matrix(c(obj$CyanComp, obj$MagentaComp, obj$YellowComp,
obj$BlackComp),nc=1,
dimnames=list(c("C","M","Y","K"),c("Value")))
}
print(col)
Example 7
β’ From Example 3, we need a method to compute thenumber of codons that are present in a given objectof ππππ π βtripletsβ.
Example 3
Answer 7
setMethod("length", "triplets",
function(object) {
l <- length(object@x);
return(l);
}
)
seq <- new("triplets",
x = c("AUG","CCA","GGA","UGA","CCA"));
length(seq)