Advanced R pp 61-81 | Cite as

Writing Classes and Methods

  • Matt Wiley
  • Joshua F. Wiley


It is often helpful to have a function behave differently depending on the type of object passed. For example, when summarizing a variable, it makes sense to create a different summary for numeric or string data. It is possible to have a different function for every type of object, but then users would have to remember many function names, and to remain unique, function names may be longer. Object-oriented programming (OOP) is based on objects and is implemented in R (as in most programming languages) by using two concepts: classes and methods. A class defines a template, or blueprint, describing the variables and features of an object as well as determining what methods work for it. For example, a house may be defined as having a floor, four walls, a roof, and a door. Specific data represents these properties, such as the dimensions and color of each wall. The methods are behaviors or actions that can be performed on a particular object type. For instance, a house can be painted, which changes its color, but a house cannot be eaten. R has three object-oriented systems: S3, S4, and R5. This chapter covers the S3 and S4 systems, which are the most common.


System Method Data Frame Character String Validity Check Validity Function 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.

Copyright information

© Matt Wiley and Joshua F. Wiley 2016

Authors and Affiliations

  • Matt Wiley
    • 1
  • Joshua F. Wiley
    • 1
  1. 1.Elkhart Group Ltd. & Victoria CollegeColumbia CityUSA

Personalised recommendations