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

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

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

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

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

or in other words, they have reference semantics. To understand this, we shall create the current account class and the investment account class.

      custBank <- setRefClass(“custBank”, fields = list(name = “character”, phone = “character” ) ) invAccount <- setRefClass(“invAccount”, fields = list(custodian = “custBank”), contains = c(“account”) # methods go here )

      Let us now come back to the current account and define it while adding some methods (though as we will see later, it is possible to add them later too).

      image Note – Assigning in the encapsulating environment

      Notice that, the assignment operator within an object is the local assignment operator: <<-. The operator <<- will call the accessor functions if defined (via the object$accessors() function).

      We can now create accounts and use the methods supplied.

      ph_acc <- currAccount$new(ref_number = 321654987, holder = “Philippe”, branch = “LDN05”, opening_date = as.Date(Sys.Date()), account_type = “current”, interest_rate = 0.01, balance = )

      ph_acc$balance # after creating balance is 0: ## [1] 0 ph_acc$debet(200) # impossible (not enough balance) ## Error in ph_acc$debet(200): Not rich enough! ph_acc$credit(200) # add money to the account ph_acc$balance # the money arrived in our account ## [1] 200 ph_acc$debet(100) # this is possible ph_acc$balance # the money is indeed gone ## [1] 100

      image Note – Addressing attributes and methods

      The Reference Class OO implementation R5 uses the $ to access attributes and methods.

      It is also possible – though not recommendable – to create methods after creation of the class.

      alsoCurrAccount <- setRefClass(“currentAccount”, fields = list( interest_rate = “numeric”, balance = “numeric”), contains = c(“account”) ) alsoCurrAccount$methods(list( credit = function(amnt) { balance <<- balance + amnt }, debet = function(amnt) { if (amnt <= balance) { balance <<- balance - amnt } else { stop(“Not rich enough!”) } } ))

      image Note – No dynamic editing of field definitions

      In general, R is very flexible in how any of the OO systems is implemented. However, the refclasses do not allow to add fields after creating. This makes sense because it would make all existing objects invalid, since they would not have those new fields.

      6.4.2 Important Methods and Attributes

      All refclass object inherit from the same superclass envRefClass, and so they get at creation some hidden fields and methods. For example, there is .self. This variable refers to the object itself.

       RC_obj$callSuper(): This method initializes the values defined in the super-class.

       RC_obj$initFields(): This method initializes the values fields if the super-class has no initialization methods.

       RC_obj$copy(): This method creates a copy of the current object. This is necessary because Reference Classes classes don't behave like most R objects, which are copied on assignment or modification.

       RC_obj$field(): This method provides access to named fields, it is the equivalent to slots for S4. RC_obj$field(“xxx”) the same as RC_obj$xxx. RC_obj$field(“xxx”, 5), the same as assigning the value via RC_obj$xxx <- 5.

       RC_obj$import(x): This method coerces x into this object, and RC_obj$export(Class) coerces a copy of RC_obj into that class.

      Digression – R6

       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

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