The Big R-Book. Philippe J. S. De Brouwer
[1] 987654321 # attr can also change the value of an attribute. attr(my_cust_bank, which=’phone’) <- ‘123123123’ # Let us verify: my_cust_bank@phone ## [1] “123123123” # It is even possible to create a new attribute or remove one. attr(my_cust_bank, ‘something’) <- ‘Philippe’ attr(my_cust_bank, ‘something’) ## [1] “Philippe” attr(my_cust_bank, ‘something’) <- NULL attr(my_cust_bank, ‘something’) ## NULL str(my_cust_bank) # the something attribute is totally gone ## Formal class ‘Bnk’ [package “.GlobalEnv”] with 2 slots ## ..@ name : chr “HSBC” ## ..@ phone: chr “123123123”
While the function attr()
allows partial matching. It is never a good idea to use partial matching in a batch environment. This can lead to hard to detect programming errors.
Some slots – like class
, comment
, dim
, dimnames
, names
, row.names
and tsp
(for time series objects) – are special: they can only take some values. This knowledge can even be used to change those attributes.
x <- 1:9 x # x is a vector ## [1] 1 2 3 4 5 6 7 8 9 class(x) ## [1] “integer” attr(x, “dim”) <- c(3,3) x # is is now a matrix! ## [,1] [,2] [,3] ## [1,] 1 4 7 ## [2,] 2 5 8 ## [3,] 3 6 9 class(x) # but R is not fooled. ## [1] “matrix”
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
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”
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
The function getSlots()
will return a description of all the slots of a class:
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