The Big R-Book. Philippe J. S. De Brouwer

Чтение книги онлайн.

Читать онлайн книгу The Big R-Book - Philippe J. S. De Brouwer страница 54

The Big R-Book - Philippe J. S. De Brouwer

Скачать книгу

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.

      1 1 Object oriented programming refers to the programming style that provides a methodology that enables a logical system (real life concept, such as for example “student”)) to be modelled an “objects.” In further code it will then be possible to address this object via its methods and attributes. For example, the object student can have a method “age” that returns the age of the student based in its attribute birth-date.

      2 2 The reader that has knowledge of C, might want to know that this is the object-like functionality that is provided by the struct keyword in C. R is written in C and to program base-R one used indeed those structures.

      3 3 Experienced C-users might want to think of this as something like the statement switch(TYPEOF(x)) in C.

      4 4 A good way to see a generic function is as an overloaded function with a twist.

      5 5 Sometimes generic functions are also referred to as “generics.”generic

      6 a How to measure and improve speed is described in Chapter 40 “The Need for Speed” on page 793.

      7 6 Notice that there are no objects of these names in base R, but for example you will find some in the methods package. This package provides formally defined methods and objects for R.methods

      8 a UppercamelCase is easy to understand when comparing to lowerCamelCase, the dot.separator and snake_case. It refers to the way long names (of objects, variables and functions) are kept readable in code. They are all good alternatives, and each programmer has his/her preference, though in many communities, there are some unwritten rules. These rules are best followed because that makes your code much easier to read.

      9 7 R5 has recently been added to R (2015). It responds to the need formutable objects and as such makes packages such as R.oo, proto and mutatr obsolete.

      10 a The development of R6 can be followed up here: https://www.r-project.org/nosvn/pandoc/R6.html

      7.1. The Philosophy of the Tidyverse

      Most of those packages will require one or more other packages to be loaded first. These packages will in their turn also have dependencies on yet other (or the same) packages. These dependenciesmight require a certain version of the upstreampackage. This package maintenance problem used to be known as the “dependency hell.” The package manager of R does, however, a good job and it usually will work as expected.

      Using the same code again after a few years, is usually more challenging. In the meanwhile you might have updated R to a newer version and most packages will be updated too. It might happen that some packages have become obsolete and are not maintained anymore and therefore, the new version is not available. This can cause some other packages to fail.

      Maintaining code is not a big challenge if you just write a project for a course at the university and will never use it again. Code maintenance becomes an issue when you want to use the code later …but it becomes a serious problem if other colleagues need to review your work, expand it and change it later (while you might not be available).

      Another issue is that because of this flexibility, core R is not very consistent (though people will argue that while Linux does even a worse job here and still is the best OS).

       OS

       operating system

      Consistency does matter and it follows from a the choice of a programming philosophy. For example, R is a software to do things with data, so each function should have a first argument that refers to the data. Many functions will follow this rule, but not all. Similar issues exist for arguments to functions, names of objects and classes (e.g. there is vector and Date, etc.)

       Use existing and common data

Скачать книгу