Chapter 8. S3
You may have noticed that your slot machine results do not look the way I promised they would. I suggested that the slot machine would display its results like this:
## 0 0 DD
But the current machine displays its results in a less pretty format:
## "0" "0" "DD"
Moreover, the slot machine uses a hack to display symbols (we call
play). As a result, the symbols do not follow your prize output if you save it:
## "B" "0" "B"one_play
You can fix both of these problems with R’s S3 system.
The S3 System
S3 refers to a class system built into R. The system governs how R handles objects of different classes. Certain R functions will look up an object’s S3 class, and then behave differently in response.
But if you give that number the S3 class
POSIXct followed by
## "2001-09-08 19:46:40 CST"
If you use objects with classes—and you do—you will run into R’s S3 system. S3 behavior can seem odd at first, but is easy to predict once you are familiar with it.
R’s S3 system is built around three components: attributes (especially the
class attribute), generic functions, and methods.
In Attributes, you learned that many R objects come with attributes, pieces of extra information that are given a name and ...