Abstraction in R Programming (original) (raw)
Last Updated : 12 Jun, 2025
Abstraction refers to the process of simplifying complex systems by concealing their internal workings and only exposing the relevant details to the user. It helps in reducing complexity and allows the programmer to work with high-level concepts without worrying about the implementation.
In R, abstraction is typically achieved using:
- **S3 Classes
- **S4 Classes
- **Reference Classes (RC)
We will see how each of these can be used for abstraction in R.
1. Abstraction with S3 Classes
S3 classes are the most basic form of object-oriented programming in R. They are informal and dynamic, which makes them simple and easy to use for many tasks. While S3 does not enforce strict methods and classes like other OOP systems, it still allows abstraction through the use of methods and classes.
In this example, we’ve created an object person1 of class person with two attributes: name and age. The abstraction hides the internal structure and we only interact with the object using the create_person function to create the object and the print.person method to display its attributes.
R `
create_person <- function(name, age) { person_object <- list(name = name, age = age) return(person_object) class(person_object) <- "person" }
print.person <- function(x) { cat("Name:", x$name, "\n") cat("Age:", x$age, "\n") }
person1 <- create_person("John", 30)
print.person(person1)
`
**Output:
Name: John
Age: 30
2. Abstraction with S4 Classes
S4 classes are more formal and robust compared to S3. They allow you to define strict structures and provide a more powerful way to implement abstraction.
In this example, the abstraction is even more explicit: the Person class has defined slots (name and age) and we use the show method to print the object details, hiding the implementation details of the class.
R `
setClass("Person", slots = list(name = "character", age = "numeric"))
setMethod("show", "Person", function(object) { cat("Name:", object@name, "\n") cat("Age:", object@age, "\n") })
p <- new("Person", name = "Bob", age = 40)
show(p)
`
**Output:
Name: Bob
Age: 40
3. Abstraction with Reference Classes
Reference classes (RC) allow for mutable objects in R, which is different from the traditional object-oriented systems like S3 and S4. Reference classes are more advanced and can be used for greater abstraction in situations requiring more control over data.
In this example, the Person class defines fields (name and age) and methods (inc_age and dec_age) that operate on these fields. The internal data is abstracted and we interact with the Person object only through its methods.
R `
Person <- setRefClass("Person", fields = list(name = "character", age = "numeric"), methods = list( inc_age = function(x) { age <<- age + x }, dec_age = function(x) { age <<- age - x } ))
p <- Person$new(name = "Charlie", age = 25)
p$inc_age(5) cat("New age:", p$age, "\n")
`
**Output:
New age: 30
Advantages of Abstraction in R
- **Simplicity: Abstraction allows us to deal with high-level objects without getting bogged down by the complexity of the underlying implementation.
- **Maintainability: By hiding the internal details, abstraction makes it easier to update and maintain the code. Changes to internal workings do not affect the way we interact with the object.
- **Code Reusability: Abstraction facilitates code reuse by encapsulating the complexity of the object into a well-defined interface, which can be easily reused across different projects.
- **Improved Readability: By hiding unnecessary details, abstraction makes the code cleaner and easier to read.
In this article, we explored how abstraction works in R, focusing on S3, S4 and Reference classes and demonstrate how to implement abstraction in these class systems.