Object Oriented Programming - IPB University

Post on 16-Oct-2021

0 views 0 download

transcript

Object Oriented Programming

𝜏𝜌

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

OO programming in R: classes and methods

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)

OO programming in R: classes and methods

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)

Thank you