R S4 Class

S4 class is similar to S3 but is more formal and improvement over the S3 class. There are two major differences between S3 and S4.

S4 has formal class definitions, which define the representation and inheritance for each class, and has special helper functions for defining generics and methods. S4 class also has multiple dispatches, which means that generic functions can pick methods based on the class of any number of arguments, not just one.

Defining S4 Class

S4 class is defined using the setclass() function.

In terms of R programming, member variables are also called slots. In the definition of class, we need to set the name and slots along with the class of the slot.

setClass("student", slots=list(name="character", age="numeric", GPA="numeric"))

In the above example, student is the class and name, age and GPA are the slots.

Create S4 Object

To create an S4 object we have to use the new() function.

Example:

# create an object using new()
# provide the class name and value for slots
s <- new("student",name="Deep", age=27, GPA=9.2)
s

Output:

An object of class "student"
Slot "name":
  [1] "Deep"
Slot "age":
  [1] 27
Slot "GPA":
  [1] 9.2

We can check the created object is an S4 object or not by using isS4() function:

> isS4(s)
[1] TRUE

Create an S4 Object Using Generator Function

The setClass() function returns a generator function.

Usually, this generator function has the same name as the class name. This function can be used to create new objects. It will act as a constructor.

Example:

Let's see an example to create S4 object using generator function:

> student <- setClass("student", slots=list( name="character", age="numeric", GPA="numeric" ) )
> student
> student(name="Nikita", age=24, GPA=8.5)

Output:

class generator function for class “student” from package ‘.GlobalEnv’
function (...)
  new("student", ...)
An object of class "student"
Slot "name":
  [1] "Nikita"
Slot "age":
  [1] 24
Slot "GPA":
  [1] 8.5

Access and Modify Slot

@ is used to access the slot of an object.

Example:

> s@name
[1] "Deep"
> s@age
[1] 27
> s@GPA
[1] 9.2

Modifying Slot Directly

We can modify the slot through reassignment.

Example:

> s@name <- "Nikita"
> s

Output:

An object of class "student"

Slot "name":
  [1] "Nikita"
Slot "age":
  [1] 27
Slot "GPA":
  [1] 9.2

Modifying Slots Using slot() Function

We can access or modify the slots of an object through slot() also.

> slot(s,"name")
[1] "Nikita"
> slot(s,"name") <- "Nidhi"
> s
An object of class "student"
Slot "name":
  [1] "Nidhi"
Slot "age":
  [1] 27
Slot "GPA":
  [1] 9.2

Methods and Generic Functions

Like S3 class, methods of S4 class also belong to generic functions rather than the class itself.

The showMethod() function is used to list all the generic functions and methods available in S4.

Example:

Function: - (package base)
Function: != (package base)
Function: $ (package base)
.
.
.
Function: trigamma (package base)      
Function: trunc (package base)

Like print() generic function of S3 class, S4 class has show() generic function.

List all Methods of a Generic Function

To list all methods of generic function S4 class has showMethod(show) function.

> showMethod(show)
Function: show (package methods)
object="ANY"
object="classGeneratorFunction"
object="classRepresentation"
.
.
.
object="student"
(inherited from: object="ANY")
object="traceable"

Create Our Own Method

We can create our own method using the setMethod() function.

Example:

We can create our own class method for the show() generic as follows:

setMethod("show",
          "student",
          function(object) {
            cat("Name: ", object@name, "\n")
            cat("Age: ", object@age, "years old\n")
            cat("GPA: ", object@GPA, "\n")
          }
)
s <- new("student",name="Deep", age=2, GPA=9.2)
s    # this is same as show(s)

Output:

Name:  Deep
Age:  2 years old
GPA:  9.2Reference:
http://adv-r.had.co.nz/S4.html
https://www.datamentor.io/r-programming/s4-class/