Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 206

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

Now, we will create an investment account. At this point, it becomes crucial to see that the object “custodian bank” is not a parent class, but rather an attribute. This means that before we can create an investment account, we need to define at least one custodian.

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”

The Big R-Book

Подняться наверх