Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R and object oriented programming

Tags:

oop

r

Object oriented programming in one way or another is very much possible in R. However, unlike for example Python, there are many ways to achieve object orientation:

  • The R.oo package
  • S3 and S4 classes
  • Reference classes
  • the proto package

My question is:

What major differences distinguish these ways of OO programming in R?

Ideally the answers here will serve as a reference for R programmers trying to decide which OO programming methods best suits their needs.

As such, I am asking for detail, presented in an objective manner, based on experience, and backed with facts and reference. Bonus points for clarifying how these methods map to standard OO practices.

like image 613
Paul Hiemstra Avatar asked Mar 01 '12 18:03

Paul Hiemstra


People also ask

What is object in R programming?

Objects are the instance of the class. Also, everything in R is an object and to know more look at Data types in R. They also can have their attributes like class, attributes,dimnnames, names, etc.

Is R functional programming?

Strictly speaking, R isn't a functional programming language because it doesn't require that you write pure functions. However, you can certainly adopt a functional style in parts of your code: you don't have to write pure functions, but you often should.

What kind of programming language is R?

R is an open source programming language and software environment for statistical computing and graphics. It is one of the primary languages used by data scientists and statisticians alike. It is supported by the R Foundation for Statistical Computing and a large community of open source developers.

Why is R programming used?

R is a programming language for statistical computing and graphics that you can use to clean, analyze, and graph your data. It is widely used by researchers from diverse disciplines to estimate and display results and by teachers of statistics and research methods.


2 Answers

S3 classes

  • Not really objects, more of a naming convention
  • Based around the . syntax: E.g. for print, print calls print.lm print.anova, etc. And if not found,print.default

S4 classes

  • Can dispatch on multiple arguments
  • More complicated to implement than S3

Reference classes

  • Primarily useful to avoid making copies of large objects (pass by reference)
  • Description of reasons to use RefClasses

proto

  • ggplot2 was originally written in proto, but will eventually be rewritten using S3.
  • Neat concept (prototypes, not classes), but seems tricky in practice
  • Next version of ggplot2 seems to be moving away from it
  • Description of the concept and implementation

R6 classes

  • By-reference
  • Does not depend on S4 classes
  • "Creating an R6 class is similar to the reference class, except that there’s no need to separate the fields and methods, and you can’t specify the types of the fields."
like image 176
Ari B. Friedman Avatar answered Oct 03 '22 19:10

Ari B. Friedman


Edit on 3/8/12: The answer below responds to a piece of the originally posted question which has since been removed. I've copied it below, to provide context for my answer:

How do the different OO methods map to the more standard OO methods used in e.g. Java or Python?


My contribution relates to your second question, about how R's OO methods map to more standard OO methods. As I've thought about this in the past, I've returned again and again to two passages, one by Friedrich Leisch, and the other by John Chambers. Both do a good job of articulating why OO-like programming in R has a different flavor than in many other languages.

First, Friedrich Leisch, from "Creating R Packages: A Tutorial" (warning: PDF):

S is rare because it is both interactive and has a system for object-orientation. Designing classes clearly is programming, yet to make S useful as an interactive data analysis environment, it makes sense that it is a functional language. In "real" object-oriented programming (OOP) languages like C++ or Java class and method definitions are tightly bound together, methods are part of classes (and hence objects). We want incremental and interactive additions like user-defined methods for pre-defined classes. These additions can be made at any point in time, even on the fly at the command line prompt while we analyze a data set. S tries to make a compromise between object orientation and interactive use, and although compromises are never optimal with respect to all goals they try to reach, they often work surprisingly well in practice.

The other passage comes from John Chambers' superb book "Software for Data Analysis". (Link to quoted passage):

The OOP programming model differs from the S language in all but the first point, even though S and some other functional languages support classes and methods. Method definitions in an OOP system are local to the class; there is no requirement that the same name for a method means the same thing for an unrelated class. In contrast, method definitions in R do not reside in a class definition; conceptually, they are associated with the generic function. Class definitions enter in determining method selection, directly or through inheritance. Programmers used to the OOP model are sometimes frustrated or confused that their programming does not transfer to R directly, but it cannot. The functional use of methods is more complicated but also more attuned to having meaningful functions, and can't be reduced to the OOP version.

like image 27
Josh O'Brien Avatar answered Oct 03 '22 19:10

Josh O'Brien