Book Image

R Object-oriented Programming

By : Kelly Black
Book Image

R Object-oriented Programming

By: Kelly Black

Overview of this book

Table of Contents (19 chapters)
R Object-oriented Programming
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
4
Calculating Probabilities and Random Numbers
Package Management
Index

Redefining existing functions


As mentioned previously, the summary, show, and plot commands are extended to react in an appropriate way when passed a NumericGrade or LetterGrade object. Extending these functions is done by simply defining a new method for these functions using the setMethod command. Each of these commands already exist, so it is not necessary to reserve the names using the setGeneric command. That is, we simply define the method to associate with the command when passed an object that is a member of the NumericGrade or LetterGrade class.

We extend the summary command in the first example. In this case, the function should have a different behavior if the object passed to it is NumericGrade versus LetterGrade. For an object that is LetterGrade, the summary command retrieves the grades and prints out a frequency table for the grades:

setMethod(f="summary",
          signature="LetterGrade",
          definition=function(object,...)
          {
              # Get the letter...