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

Hint – Alternative to address slots

Оглавление

Alternatives to access slots (attributes) include the function slot(), that works like [[ for regular objects.

slot(my_acc, “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

The Big R-Book

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