Abstract
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.
This is a preview of subscription content, log in via an institution.
Buying options
Tax calculation will be finalised at checkout
Purchases are for personal use only
Learn about institutional subscriptionsAuthor information
Authors and Affiliations
Rights and permissions
Copyright information
© 2016 Matt Wiley and Joshua F. Wiley
About this chapter
Cite this chapter
Wiley, M., Wiley, J.F. (2016). Writing Classes and Methods. In: Advanced R. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-2077-1_5
Download citation
DOI: https://doi.org/10.1007/978-1-4842-2077-1_5
Published:
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4842-2076-4
Online ISBN: 978-1-4842-2077-1
eBook Packages: Professional and Applied ComputingApress Access BooksProfessional and Applied Computing (R0)