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

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

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

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

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

“holder”) ## [1] “Philippe”

       slot()

      The object my_acc is actually not very useful. It is a structure that would be in common for all types of accounts (e.g. investment accounts, savings accounts and current accounts). However, no bank would just sell and empty structure account. So, let us open a current account first.

      my_curr_acc <- new(“CurrAcc”, holder = “Philippe”, interest_rate = 0.01, balance=, branch = “LDN12”, opening_date= as.Date(“2018-12-01”)) # Note that the following does not work and is bound to fail: also_an_account <- new(“CurrAcc”, holder = “Philippe”, interest_rate = 0.01, balance=, Acc=my_acc) ## Error in initialize(value, …): invalid name for slot of class “CurrAcc”: Acc

      image Question #8

      Why does the second approach fail? Would you expect it to work?

      It appears that while the object my_acc exist, it is not possible to insert it in the definition of a new object – even while this inherits from the first. This makes sense, because the object “account” is not an attribute of the object “current account,” but its attributes become directly attributes of current account.

      The object my_curr_acc is now ready to be used. For example, we can change the balance.

      my_curr_acc@balance <- 500

      my_inv_acc <- new(“InvAcc”, custodian = my_cust_bank, holder=“Philippe”, branch=“DUB01”, opening_date = as.Date(“2019-02-21”)) # note that the first slot is another S4 object: my_inv_acc ## An object of class “InvAcc” ## Slot “custodian”: ## An object of class “Bnk” ## Slot “name”: ## [1] “HSBC” ## ## Slot “phone”: ## [1] “123123123” ## ## ## Slot “holder”: ## [1] “Philippe” ## ## Slot “branch”: ## [1] “DUB01” ## ## Slot “opening_date”: ## [1] “2019-02-21”

      image Question #9

      If you look careful at the code fragment above this question, you will notice that it is possible to provide an object my_cust_bank as attribute to the object my_inv_acc. This situation is similar to the code just above previous question, but unlike in the creation of also_an_account, now it works. Why is this?

      To understand what happened here, we need to dig a little deeper.

      my_inv_acc@custodian # our custodian bank is HSBC ## An object of class “Bnk” ## Slot “name”: ## [1] “HSBC” ## ## Slot “phone”: ## [1] “123123123” my_inv_acc@custodian@name # note the cascade of @ signs ## [1] “HSBC” my_inv_acc@custodian@name <- “DB” # change it to DB my_inv_acc@custodian@name # yes, it is changed ## [1] “DB” my_cust_bank@name # but our original bank isn't ## [1] “HSBC” my_cust_bank@name <- “HSBC Custody” # try something different my_inv_acc@custodian@name # did not affect the account ## [1] “DB” my_inv_acc@custodian@name <- my_cust_bank@name # change back

      image Hint – List all slots

      getSlots(“Acc”) ## holder branch opening_date ## “character” “character” “Date” getSlots()

      6.3.3 Validation of Input

      While S3 provides no mechanism to check if all attributes are of the right type, creating an S4 object with the function new() will check if the slots are of the correct type. For example, if we try to create an account while providing a string for the balance (while a number is expected), then R will not create the new object and inform us of the mistake.

      # Note the mistake in the following code: my_curr_acc <- new(“CurrAcc”, holder = “Philippe”, interest_rate = 0.01, balance=“0”, # Here is the mistake! branch = “LDN12”, opening_date= as.Date(“2018-12-01”)) ## Error in validObject(.Object): invalid class “CurrAcc” object: invalid object for slot “balance” in class “CurrAcc”: got class “character”, should be or extend class “numeric”

      If you omit a slot, R coerces that slot to the default value.

      x_account <- new(“CurrAcc”, holder = “Philippe”, interest_rate = 0.01, #no balance provided branch = “LDN12”, opening_date= as.Date(“2018-12-01”)) x_account@balance # show what R did with it ## numeric(0)

      image Warning – Silent setting to default

      Did you notice that R is silent about the missing balance? This is something to be careful with. If you forget that a default value has been assigned then this might lead to confusing mistakes.

       prototype()

      setClass(“CurrAcc”, representation(interest_rate = “numeric”, balance = “numeric”), contains = “Acc”, prototype(holder = NA_character_, interst_rate = NA_real_, balance = 0)) x_account <- new(“CurrAcc”, # no holder # no interest rate # no balance branch = “LDN12”, opening_date= as.Date(“2018-12-01”)) x_account # show what R did: ## An object of class “CurrAcc” ## Slot “interest_rate”: ## numeric(0) ## ## Slot “balance”: ## [1] 0 ## ## Slot “holder”: ## [1] NA ## ## Slot “branch”: ## [1] “LDN12” ## ## Slot “opening_date”: ## [1] “2018-12-01”

      image Warning – Changing class definitions at runtime

      Most

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