Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

S4 Classes: Multiple types per slot

Tags:

Is it possible to create an S4 class, where one or more of the slots can be of multiple classes? For example. Let's say that you had a situation where data could be either a vector, or a data.frame.

exampleClass <- setClass("exampleClass",     representation(raw=c("data.frame","numeric","character"),     anotherSlot=c("data.frame","numeric"))  

Or, is this the type of situation where defining a sub-class / super-class becomes necessary?

PS: Searching for a useful tutorial on S4 classes produces limited results. Links to a good tutorial on S4 class creation/usage/documentation would be greatly appreciated.

like image 789
Brandon Bertelsen Avatar asked Oct 21 '12 21:10

Brandon Bertelsen


People also ask

What are S4 classes?

▶ The S4 class system is a set of facilities provided in R for OO programming. ▶ R also supports an older class system: the S3 class system. like in other OO programming languages.

Why do we need S4 classes?

Unlike S3 classes and objects which lacks formal definition, we look at S4 class which is stricter in the sense that it has a formal definition and a uniform way to create objects. This adds safety to our code and prevents us from accidentally making naive mistakes.

What are S3 and S4 classes?

There are mainly two major systems of OOP, which are described below: S3 Classes: These let you overload the functions. S4 Classes: These let you limit the data as it is quite difficult to debug the program.

What is a slot in R?

A slot name can be any non-empty string, but if the name is not made up of letters, numbers, and . , it needs to be quoted (by backticks or single or double quotes). In the case of the slot function, name can be any expression that evaluates to a valid slot in the class definition.


1 Answers

R has 'class unions', so

setOldClass("data.frame") setClassUnion("data.frameORvector", c("data.frame", "vector")) 

The class data.frameORvector is virtual, so can't be instantiated but can be used in other slots (representation=), as a contained class (contains=), and for dispatch

A = setClass("A",          representation=representation(x="data.frameORvector"))   > A(x=1:3) An object of class "A" Slot "x": [1] 1 2 3  > A(x=data.frame(x=1:3, y=3:1)) An object of class "A" Slot "x":   x y 1 1 3 2 2 2 3 3 1 

Methods can be tricky to implement because all you know is that the slot contains one of the parent types of the class union.

setGeneric("hasa", function(object) standardGeneric("hasa")) setMethod("hasa", "data.frameORvector", function(object) typeof(object@x))  > hasa(A(x=1:5)) [1] "integer" > hasa(A(x=data.frame(y=1:5))) [1] "list" 

I actually find the documentation on ?Classes, ?Methods, ?setClass, and friends helpful. Hadley Wickham has a tutorial (the example on this page isn't that strong, it instantiates Person, whereas conceptually one would write a People to exploit R's vectorization strengths) and there is a section in this recent Bioconductor course. I don't think either goes in to detail about class unions.

like image 93
Martin Morgan Avatar answered Sep 29 '22 04:09

Martin Morgan