R S4 Class - r - learn r - r programming
- In this article, you'll learn everything about S4 classes in R; how to define them, create them, access their slots, and use them efficiently in your program.
- 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.
How to define S4 Class?
- S4 class is defined using the setClass() function.
- In R terminology, member variables are called slots.
- While defining a class, we need to set the name and the slots (along with class of the slot) it is going to have.
Sample class object image in C
Example 1: Definition of S4 class
- In the above example, we defined a new class called student along with three slots it's going to have name, age and GPA.
- There are other optional arguments of setClass() which you can explore in the help section with ?setClass.
How to create S4 objects?
- S4 objects are created using the new() function.
Example 2: Creation of S4 object
- We can check if an object is an S4 object through the function isS4().
- The function setClass() returns a generator function.
- This generator function (usually having same name as the class) can be used to create new objects.
- It acts as a constructor.
- Now we can use this constructor function to create new objects.
- Note above that our constructor in turn uses the new() function to create objects. It is just a wrap around.
Example 3: Creation of S4 objects using generator function
How to access and modify slot?
- Just as components of a list are accessed using $, slot of an object are accessed using @.
Modifying slot directly
- A slot can be modified through reassignment.
Modifying slots using slot() function
- Similarly, slots can be access or modified using the slot() function.
Methods and Generic Functions
- As in the case of S3 class, methods for S4 class also belong to generic functions rather than the class itself.
- Working with S4 generics is pretty much similar to S3 generics.
- You can list all the S4 generic functions and methods available, using the function showMethods().
Example 4: List all generic functions
- Writing the name of the object in interactive mode prints it.
- This is done using the S4 generic function show().
- You can see this function in the above list.
- This function is the S4 analogy of the S3 print()function.
Example 5: Check if a function is a generic function
- We can list all the methods of show generic function using showMethods(show).
Example 6: List all methods of a generic function
How to write your own method?
- We can write our own method using setMethod() helper function.
- For example, we can implement our class method for the show() generic as follows.
- Now, if we write out the name of the object in interactive mode as before, the above code is executed.
- In this way we can write our own S4 class methods for generic functions.