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

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

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

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

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

## function (x, y) ## { ## new_bal <- x@balance + y ## new_bal ## } ## ## Signatures: ## x ## target “CurrAcc” ## defined “CurrAcc”

      There is a lot more to say about S4 inheritance and method dispatch, though it is not really necessary in the rest of the book. Therefore, we refer to other literature. For example, “Advanced R” by Wickham (2014) is a great source.

       RC

       R5

       refclass

      image Note – Recent developments

      Reference classes are reasonably new to R and therefore, will develop further after the publication of this book. So for the most up-to-date information, we refer to R itself: type ?ReferenceClasses in the command prompt to see the latests updates in your version of R.

      People with OOP background will naturally feel more comfortable with RC, it is something what people with C++, C#, PHP, Java, Python, etc., background will be familiar with: a messagepassing OO implementation. However, that sense of comfort has to be mended, in many ways, the refclass system in R is a combination of S4 and environments.

       OOP

       object oriented

       programming

      That said, the RC implementation brings R programming to a next level. This system is particularly suited for larger projects, and it will seamlessly collaborate with S4, S3 and base types. However, note that the vast majority of packages in R does not use RC, actually none of the most often used packages do. This is not only because the pre-date the refclass system but also because they do not need it (even while some are rather complex).

      Using RC in R will add some complexity to your code and many people advice to use the refclass system only where the mutable state is required. This means that even while using R5, it is still possible to keep most of the code functional.

      6.4.1 Creating RC Objects

      R provides the function setRefClass to create a new R5 class definition that can immediately be used to create an instance of the object.

       setRefClass()

      The RC object has three main components, given by three arguments to its creator function setRefClass:

      1 contains: The classes from which it inherits – note that only other refclass objects are allowed;

      2 fields: These are the attributes of the class – they are the equivalent of “slots” in S4; one can supply them via a vector of field names or a named list of field types;

      3 methods: These functions are the equivalent for for dispatched methods in S4, and they operate within the context of the object and canmodify its fields. While it is possible to add these later, this is not good programming practice, makes the code less readable and will lead to misunderstandings.

      We will illustrate this with a remake of the example about bank accounts.

      image Note

      Fields can also be supplied as a vector (we use a list in the example). Using a vector does not allow to specify a type. It looks as follows:

      setRefClass(“account”, fields = c(“ref_number”, “holder”, “branch”, “opening_date”, “account_type” ) )

      image Hint

      It is possible to leave the input type undecided by not providing a type in the list environment. This could look like this:

      setRefClass(“account”, fields = list(holder, # accepts all types branch, # accepts all types opening_date = “Date” # dates only ) )

      Let us now explore this object that was returned by the function setRefClass().

      isS4(account) ## [1] TRUE # account is S4 and it has a lot more than we have defined: account ## Generator for class “account”: ## ## Class fields: ## ## Name: ref_number holder branch ## Class: numeric character character ## ## Name: opening_date account_type ## Class: Date c haracter ## ## Class Methods: ## “field”, “trace”, “getRefClass”, “initFields”, ## “copy”, “callSuper”, “.objectPackage”, “export”, ## “untrace”, “getClass”, “show”, “usingMethods”, ## “.objectParent”, “import” ## ## Reference Superclasses: ## “envRefClass”

      The object returned by setRefClass (or retrieved later by getRefClass) is called a generator object. This object automatically gets the following methods.

       generator object

       new to create instances of the class. Its arguments are the named arguments specifying initial values for the fields;

       methods to add methods or modify existing;

       help to get help about the methods;

       fields to get a list of attributes (fields) of that class;

       lock to lock the named fields so that their value can only be set once;

       accessors sets up accessor-methods in form

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