Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

code A guest post by Tom Barker, a software engineer, an engineering manager, a professor and an author. Currently he is the Senior Manager of Web Development at Comcast, and an Adjunct Professor at Philadelphia University. He has authored Pro JavaScript Performance: Monitoring and Visualization, Pro Data Visualization with R and JavaScript, and Technical Management: A Primer, and can be reached at @tomjbarker.

In this two part series we will be looking at object-oriented programming in R. R supports two different systems for creating objects: the S3 and S4 methods. S3 is the default way that objects are handled in R. In any of my previous posts, and most likely in examples that you may have seen elsewhere, S3 objects are what is being used. S4 is a newer way to create classes in R that has more built in validation, but it also has more overhead. In this post we will look at creating S3 objects.

Just to make sure that we are on the same page – objects are data structures that are metaphors for real business logic. They contain properties (variables) and methods (functions) that describe what the idea they represent is, and what it can do. They encapsulate everything you would need to know about a given concept.

S3 Classes

S3, so called because it was first implemented in version 3 of the S language (the predecessor to the R language), uses a concept called generic functions. Basically everything in R is an object, and each object has a string property called class that signifies what the object is. There is no validation around this, and we can overwrite the class property in an ad hoc fashion. That’s the main problem with S3, the lack of validation.

See below where we create a matrix and change its class to be a vector:

Generic functions are objects that check the class property of objects passed into them and exhibit different behavior based on that attribute. It’s a nice way to implement polymorphism. We can see the methods that a generic function uses by passing the generic function to the methods() function. Here are the methods of the plot() function:

Non-visible functions are asterisked.

Okay, so we’ve seen how S3 object-oriented concepts work in R, so let’s see how we can create our own custom S3 objects and generic functions.

An S3 class is a list of properties and functions, with an attribute named class. The class attribute tells generic functions how to treat objects that implement a particular class. Let’s create an example.

We’ll create an object named tom that contains properties named userid, password, and playlist. We will set the class property to the string user. This should be the equivalent of creating a class named user and instantiating an object from that class in pure classical object-oriented programming.

We can inspect our new object by using the attributes function. This will tell us the properties that the object has, as well as its class.

Now it is time to create generic functions that we can use with our new class. We will start by creating a function that will handle only our user object, then after that generalize it so any class can use it. It will be the createPlaylist() function and it will accept the user to perform the operation on and a playlist to set. The syntax for this is [function name].[class name]. Note that we access the properties of S3 objects using the dollar sign.

Let’s test this to make sure it works as desired. It should set the playlist property of the passed in object to the vector that is passed in.

Excellent! Now let’s generalize the createPlaylist() function to be a generic function. To do this we just create a function named createPlaylist, and have it accept an object and a value. Within our function we use the UseMethod function to delegate functionality to our class specific createPlaylist function, createPlaylist.user.

The UseMethod function is the core of generic functions. It performs evaluation of the object, determines its class, and dispatches to the correct class specific function.

Now let’s try it out to see if it worked.

But now here’s the thing with S3 classes. There is no strict validation that an object holds the structure that its class name implies. Say we create an object named foo that holds only a string, but assign the string “user” to foo’s class attribute.

Now our object foo does not contain a userid, password, or playlist. But if we call createPlaylist() with foo, the createPlaylist function will add a playlist attribute to foo.

It is this loose adherence to structure that some developers find attractive. Google in fact recommends in their R style guide, located here, for developers to only use S3 objects because of their interactivity and flexibility, unless they have a very strong reason not to use S3 objects.

But it is just this lack of structure and explicit validation that the S4 object system sought to rectify, as we will see in my next article.

For more details about R, see the resources below from Safari Books Online.

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

The Art of R Programming is both broad in its coverage of various language constructs and data structures, and deep and co mprehensive in explaining them. It provides working examples, and iluminates the R philosophy: a clean functional language with strong vector operation support, and a “do more with less typing” foundation that can make programs an order of magnitude smaller and expressive.
Pro Data Visualization using R and JavaScript by Tom Barker, makes the R language approachable, and promotes the idea of data gathering and analysis. You’ll see how to use R to interrogate and analyze your data, and then use the D3 JavaScript library to format and display that data in an elegant, informative, and interactive way. You will learn how to gather data effectively, and also how to understand the philosophy and implementation of each type of chart, so as to be able to represent the results visually.
Pro JavaScript Performance: Monitoring and Visualization by Tom Barker, gives you the tools to observe and track the performance of your web applications over time from multiple perspectives, so that you are always aware of, and can fix, all aspects of your performance.
Learning R will help you learn how to perform data analysis with the R language and software environment, even if you have little or no programming experience. With the tutorials in this hands-on guide, you’ll learn how to use the essential R tools you need to know to analyze data, including data types and programming concepts.

Tags: object oriented, R programming, S3 objects, S4 objects, Tom Barker,

Comments are closed.