Open In App

R – S4 Class

Last Updated : 21 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A system for object-oriented programming is the S4 system in R. Confoundingly, R supports at least three separate object-oriented programming systems S3, S4, and S5. They follow a standard process for producing objects and have a clearly defined structure. This improves the security of our code and keeps us from unintentionally committing simple errors.

Definition of S4 class in R Programming language

In R, member variables are referred to as slots. The name and the slots (along with the class of the slot) that a class will have must be defined. For instance,

setClass("Employee", slots=list(name="character", age="numeric", role="character"))

Name, age, and role are the three slots (member variables) in the Employee class that we have built.

Creating S4 Object in R

S4 objects are created using the new() function.

employee1 <- new("Employee", name = "Sanket", age = 21, role = "Software Developer")

By specifying the class name Student and values for each of the three slots within new, we have generated the object student1 in new().

Example: S4 Class and Object in R

R




# create a class "Employee" with three member variables
setClass("Employee", slots=list(name="character",
                                age="numeric",
                                role="character"))
  
# create an object of class 
emp1 <- new("Employee", name = "Sanket",
                        age = 21,
                        role = "Software Developer")
  
# call emp1 object 
emp1


Output:

An object of class "Employee"
Slot "name":
[1] "Sanket"

Slot "age":
[1] 21

Slot "role":
[1] "Software Developer"

Here, we’ve used the setClass() method to build an S4 class called Employee. The new() method was then used to create an object with the name emp1.

new("Employee", name = "Sanket", age = 21, role = "Software Developer")

Here,

  • name accepts “character” value so, we have passed “Sanket”
  • age accepts “numeric” value, so we passed numeric value 21
  • role accepts “character” value so, we have passed the “Software Developer”

Access S4 Class Slot in R

In R, we use the @ operator to access slots. For instance,

# access name slot of Employee class
emp1@name

Example: Access S4 Class slot

R




# access name slot of Employee class
emp1@name
  
# access role slot of Employee
emp1@role 


Output:

[1] "Sanket"
[1] "Software Developer"

Here,

  • Emp1 accesses Employee Info’s name slot and outputs “Sanket” 
  • Emp1 accesses Employee Info’s role slot and outputs “Software Developer”

Modify S4 Class Slot in R

In R, we may access a slot and change its value by using the @ symbol. For instance,

# access and assign new value to role slot
emp1@role <- "Web Designer"

# print new slot value
emp1@role

Here, the role slot value is changed from “Software Developer” to “Web Designer”.

Example: Modify S4 Class slot

R




emp1@name
emp1@role
  
# access and assign new value to role slot
emp1@role <- "Web Designer"
  
emp1@role


Output:

[1] "Sanket"
[1] "Software Developer"
[1] "Web Designer"

S4 Generic Function and Method in R

Similar to the S3 class, the S4 class also has methods that are part of generic functions rather than the class itself. Working with S4 generics is quite comparable to working with S3 generics. To learn more about generics, go visit S3 generics. 

When we just enter the object’s name in interactive mode after creating an object of the class, the object is printed. The S4 generic function display is used to do this. This function is displayed in the list above. The S4 equivalent of the S3 print() function is this function.

# call object without show()
emp1

# call object with show()
show(emp1)

Output:

An object of class "Employee"
Slot "name":
[1] "Sanket"

Slot "age":
[1] 21

Slot "role":
[1] "Software Developer"

Here, in both cases, the output will be the same as shown above.

Example: Check if a function is a generic function

isS4(print)
# Output: [1] FALSE

isS4(show)
# Output: [1] TRUE

Here, the isS4() function has been used to determine whether or not a function is an S4 generic function. Since the function returns FALSE for print and TRUE for the show if the latter is an S4 generic function.

Writing Own Method in R

With the help of the setMethod() function in R, we may create our own methods. For instance, we may use the following code to construct our class function for the display() generic.

R




# create own method
setMethod("show",
"Employee",
  
function(object) {
cat(object@name, "\n")
cat(object@age, "years old\n")
cat("Role:", object@role, "\n")
}
)
  
# call employee1 object 
emp1


Output:

Sanket 
21 years old
Role: Software Developer


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads