This article is archived because of a reason. The content can be outdated or in draft form. You are welcome to read it anyway.
This is the second of a series of posts related to object oriented programming in R and the package aoos. The previous introduced the first version of aoos; this introduces version 0.2.0. the next is on different representations of reference classes in R.
R has more than enough systems for object orientation and here is yet another one. S3 and S4 are the built in systems. R.oo has been developed since 2001; proto since 2005; and R6 is the newest and published to CRAN in 2014.
What I wanted to have is a system where method definitions are part of the class definition. Something which forces me to define functions belonging to one concept in one place, however, I don’t feel comfortable to define them inside lists. So here I present my solution for your consideration.
As an example for this post consider the class Directory.
defineClass is used to define a new class. If you want a field or method to be private, i.e. not easily accessible from the client, you can use the function ` private`. The class definition is a R-expression; every assignment in that expression will define a field or method.
The class Directory is basically a S4 class and inherits from environment. You can only access public member; and the return value of
defineClass is the constructor function, so you can use
Directory() to create an instance of Directory. Arguments to the constructor are passed on to the
init method if you have defined one. The class inherits from Show which means that the member function
show is used as
show-method, and Binary allows to define binary operators. On initialization a directory is created if it doesn’t exist. We start with a directory named ‘foo’.