Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 232
Digression – R6
ОглавлениеNote that we did not discuss the R6OOsystem. R6 is largely the same as R5 but adds some important OO features to the mix, such as private and public functions and properties. However, it is not widely used and most probably will become obsolete in further versions of R5.a
R6
Three or even four OO systems in one language is a lot, but that complexity does not stand in the way of practical applications. It seems that a little common sense allows the user to take the best of what is available, even when mixing systems.
So what system to use?
1 For a casual calculation of a few hundred lines of code, it is probably not necessary to define your own classes. You probably will use S3 implicitly in the packages that you will load, but you will not have to worry about it at all: just use plot() and print() and expect it to work.
2 If your inheritance structure is not too complex (and so you do not need multi-argument method signatures) and if your objects will not change themselves then S3 is the way to go. Actually, most packages on CRAN use S3.
3 If still your objects are static (not self-modifying), but you need multi-argument method signatures, then S4 is the way to go. You might want to opt for S4 also to use the formalism, as it can make code easier to understand for other people. However, be careful with the inheritance structure, it might get quite complex.
4 If your objects are self-modifying, then RC is the best choice. In S3 and S4, you would need to use replacement methods. For example:my_replacement_method(object) <- new_value
5 For larger projects, where many people work on the same code it makes sense to have a look at R6 (it allows private methods, for example).
In fact, the OO system implemented in R is so that it does not come in the way of what you want to do, and you can do all your analysis of great complexity and practical applicability without even knowing about the OO systems. For casual use and the novice user alike, the OO system takes much of the complexity away. For example, one can type summary(foo)
, and it will work regardless of what this “foo” is, and you will be provided with a summary that is relevant for the type of object that foo represents.