S4 Classes
Purpose
To code S3 and S4 classes
Set a new class called Distribution
> setClass("Distribution", representation(domain = "character")) [1] "Distribution" > myDist1 <- new("Distribution", domain = "stats") > myDist2 <- new("Distribution") |
Prototype
> setClass("Distribution", representation(domain = "character"), + prototype = list(domain = "discrete")) [1] "Distribution" > myDist2 <- new("Distribution") |
Assigning Values
> myDist2@domain <- "Rads" > print(myDist2) An object of class "Distribution" Slot "domain": [1] "Rads" |
Accessing Slot Values
> print(myDist2@domain) [1] "Rads" |
Create a class that extends distribution
> setClass("Poisson", contains = "Distribution", representation(param = "character", + val = "numeric"), prototype = list(param = "lambda", val = 0)) [1] "Poisson" > myPoisson <- new("Poisson") |
Assign to the parent class through child class
> myPoisson@domain <- "Discrete" > myPoisson An object of class "Poisson" Slot "param": [1] "lambda" Slot "val": [1] 0 Slot "domain": [1] "Discrete" |
GetClass Details
> getClass("Poisson") Class "Poisson" Slots: Name: param val domain Class: character numeric character Extends: "Distribution" |
Introspection
> getSlots("Poisson") param val domain "character" "numeric" "character" > slotNames("Poisson") [1] "param" "val" "domain" > extends("Poisson") [1] "Poisson" "Distribution" |
Get the superClassNames and subClassNames
> library(RBioinf) > superClassNames("Poisson") [1] "Distribution" > subClassNames("Poisson") character(0) |
Learnt something today about how class is represented in R
> y <- getClass("Poisson") > names(getClass("Poisson")@contains) [1] "Distribution" > str(y) Formal class 'classRepresentation' [package "methods"] with 11 slots ..@ slots :List of 3 .. ..$ param : atomic [1:1] character .. .. ..- attr(*, "package")= chr "methods" .. ..$ val : atomic [1:1] numeric .. .. ..- attr(*, "package")= chr "methods" .. ..$ domain: atomic [1:1] character .. .. ..- attr(*, "package")= chr "methods" ..@ contains :List of 1 .. ..$ Distribution:Formal class 'SClassExtension' [package "methods"] with 10 slots .. .. .. ..@ subClass : chr "Poisson" .. .. .. ..@ superClass: chr "Distribution" .. .. .. ..@ package : chr ".GlobalEnv" .. .. .. ..@ coerce :function (from, strict = TRUE) .. .. .. ..@ test :function (object) .. .. .. ..@ replace :function (from, to, value) .. .. .. ..@ simple : logi TRUE .. .. .. ..@ by : chr(0) .. .. .. ..@ dataPart : logi FALSE .. .. .. ..@ distance : num 1 ..@ virtual : logi FALSE ..@ prototype :Formal class 'S4' [package ""] with 0 slots list() ..@ validity : NULL ..@ access : list() ..@ className : atomic [1:1] Poisson .. ..- attr(*, "package")= chr ".GlobalEnv" ..@ package : chr ".GlobalEnv" ..@ subclasses: list() ..@ versionKey:<externalptr> ..@ sealed : logi FALSE |
Wow! the above class structure is awesome. It has slots, contains, virtual, prototype, validity, access, className, package, subclasses, versionKey, sealed etc.
Structure for the matrix
> getClass("matrix") Class "matrix" No Slots, prototype of class "matrix" Extends: Class "array", directly Class "structure", by class "array", distance 2 Class "vector", by class "array", distance 3, with explicit coerce Known Subclasses: Class "array", directly, with explicit test and coerce Class "mts", from data part |
Extends
> extends("matrix") [1] "matrix" "array" "structure" "vector" |
Check whether a class is S4 object or not
> isS4(myPoisson) [1] TRUE |
Experimenting with coercion
> setClass("Derivative", representation(name = "character"), prototype = list(name = "NIFTY")) [1] "Derivative" > myD <- new("Derivative") > setClass("Options", representation(type = "character"), contains = "Derivative", + prototype = list(type = "NIFTYFutures")) [1] "Options" > myOpt <- new("Options") > as(myOpt, "Derivative") An object of class "Derivative" Slot "name": [1] "NIFTY" > myD1 <- new("Derivative", name = "Benchmark") > as(myOpt, "Derivative") <- myD1 > myOpt An object of class "Options" Slot "type": [1] "NIFTYFutures" Slot "name": [1] "Benchmark" |