R Language S4 OOP Object Initialization

I have a love-hate-love relationship with the R language. I like the huge R set of libraries (packages). But. R language syntax is very different from other languages so there’s a huge time cost to switch context from any language to R.

As a case in point, I was looking at object initialization for R S4 OOP. Weirdly, and not in a good way at all, R has three built-in ways to do OOP and all three are completely different. The three OOP modeling approaches are called S3, S4, and RC (reference classes).

So, I was reading the R documentation so I could do a little demo that defines and creates an S4 object. How hard could it be? Well, I found the official documentation to be very poor — it explained all the deep nuances but it didn’t explain how to do basic things very well at all. And the examples I found online were often flat-out incorrect.


As best I can summarize, to define an S4 class you use setClass with the “slots” keyword to define fields. The “contains” keyword is optional and is used for inheritance (which is really weird). The “representation” and “prototype” keywords are deprecated.

In order to assign initial values to an object you can define a “initialize” function using setMethod and “.Object”. To invoke initialize, you don’t call it explicitly, you can use one of two syntaxes to create an object. In order to define a class method, you use setGeneric with setMethod. To access a field, you use the “@” operator.

If all this sounds wacky, well, yes it is. It’s almost impossible to understand S4 without seeing examples. I’m very, very experienced with OOP in several languages and it took me several hours to fully figure out R OOP using S4. Compared to modern OOP with languages like Python and C#, R OOP with S4 is really poorly designed, quirky, and awkward. But it does work.

This entry was posted in R Language. Bookmark the permalink.