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 Director of Software Engineering and 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 my previous Part 1 post, I talked about S3 objects in R. By default, R uses S3 objects for all objects created. The main argument for or against S3 objects is the lack of validation built into the system. The concept of S4 objects was introduced to R in version 1.4, via the methods package, to give developers who wanted it the structure and rigidity of a formal class system. As of version 1.7 the methods package is included in the base installation of R.

So let’s look at S4 objects. Since the main complaint of S3 is the lack of validation, S4 addresses this by having overhead built right into the class structure. Let’s take a look.

To create S4 objects, you first create a class definition with the setClass() function. The setClass() function accepts a number of parameters, most of which we can see below.

  • The first parameter is a string that signifies the name of the class that we are creating.
  • The next parameter is named representation, and it is a list of named properties that objects created from the class will contain.
  • The next parameter is usually the result of a call to the prototype() function, which consists of an object that contains default values for objects created from our class.
  • The next parameter is a reference to a superclass that our class extends. If we specify a class to extend, our class inherits all of the slots, or named properties, from the superclass.
  • Another parameter is named S3methods and accepts a Boolean value.

To start let’s first create a class called user that is structured like below:

userid Character value
password Character value
playlist List of ids

We can test this out by creating a new object from this class. We use the new() function to create a new instance of the class.

Very nice. Note that for S4 objects we use the @ symbol to reference properties of objects. So if we wanted to access the playlist in our new object we could simply do the following:

If we want to see the type information for our new object, we can pass it in to a call to the class() function. We can see here that the type “user” is returned:

Next, let’s create a generic function for this class. To do this we first call setGeneric to create a generic function signature, then we call setMethod to associate the function with our class and define the implementation of the function.

The primary signature of setGeneric() is:

We can create our generic function for createPlaylist like so:

We then use the setMethod() function to define and associate the function with the user class. We can see the primary parameters of the signature for the setMethod():

We simply pass in the function name, the class name, and then an anonymous function that will serve as the generic function:

Let’s try it out:


But wait – let’s try to fake out createPlaylist, just like we were able to do with our S3 objects. Let’s create a fake user object and try to pass that in to createPlaylist:

Our new object fakeUser is in fact just a vector with a length of 1 and not an object of type user. Let’s see what happens when we pass fakeUser into createPlaylist.

That’s great. Now let’s try and further our fraud by spoofing the class of our fakeUser object to make it appear to be an object of type user.

Now let’s pass our spoofed object into createPlaylist and see what happens.

When we call fakeUser to see what it looks like, it almost looks like it worked:

But fakeUser is not an S4 object, so if we try and reference the playlist attribute as if it were, we get the following error:


And it’s as simple as that. While some prefer the simplicity and flexibility of the S3 way, which is similar to loosely typed languages and the loose prototyping of JavaScript, some prefer the structure and rigidity of the S4 method. Ultimately the choice of S3 or S4 objects is purely one of personal preference.

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-Orient, R, S3 objects, S4 objects,

One Response to “Object Oriented Programming in R: Part 2, S4 Objects”