| RDict {R6DS} | R Documentation |
The RDict reference class
Description
The RDict reference class implements the data structure dictionary.
Usage
RDict
Format
An object of class R6ClassGenerator of length 24.
Details
A dictionary is a collection of (key, value) pairs as its elements such that each possible key appears at most once in the collection. The dictionary data structure does not care the order of the elements.
The keys of the elements in the dictionary are stored as strings. The values in the dictionary are not necessarily to be of the same type, and they can be any R objects.
References
For the details about the dictionary data structure, see Dictionary at Wikipedia.
Immutable Methods
The immutable methods do not change the instance.
has(key)-
The method
hasreturns a boolean indicating if the dictionary contains the element with the key "key".Both of the following two sentences are equivalent:
instance$has("keyname")instance$has(keyname) get(key)-
The method
getreturns the value of the element whose key is"key". It returnsNULLif no element is found. keys-
The method
keysreturns a vector of the keys in the dictionary. values-
The method
valuesreturns a list of the values in the dictionary (unnamed list).
Mutable Methods
The mutable methods change the instance.
add(key, val)-
The method
addadds a new element (the pair key and val) into the dictionary. It will not add element with the key which exists already in the dictionary. It returns a boolean showing if the adding is successful.Note that any element with the key
""(empty string) will not be added. add_multiple(..., collapse=NULL)-
The method
add_multipleadds new elements into the dictionary. It will not add element with the key which exists already in the dictionary.The argument
...stands for any input with the formkeyname1 = value2, keyname2 = value2, ...Therefor, the input can take the form
instance$add(key1=1, key2="hello", key3=list(1))and the keys of the elements will be strings like
"key1","key2", and"key3", respectively.If the
keynameis missing, thevaluewill not be added. delete(key)-
The method
deleteremoves the element with the keykeyin the dictionary.Suppose that the key name of the element that you want to remove is "keyname". Both of the following two sentences are valid:
instance$delete("keyname")instance$delete(keyname)It returns a boolean showing if the element is found and deleted.
Author(s)
Yukai Yang, yukai.yang@statistik.uu.se
See Also
R6DS for the introduction of the reference class and some common methods
Examples
### create a new instance
# to create a new instance of the class
dict <- RDict$new()
# of course you can start to add elements when creating the instance
dict <- RDict$new(id0001=1, id0002=2, collapse=list(id0003=3, id0004=4))
# the following sentence is equivalent to the above
dict <- RDict$new(id0001=1, id0002=2, id0003=3, id0004=4)
# where the three lists are inserted into the dictionary
### immutable methods
dict$keys
dict$values
dict$has(id0001)
dict$has("id0005")
# TRUE as it has the key attribute
dict$get(id0006)
dict$get("id0002")
### mutable methods
dict$add(id0005, 5)
dict$add(key="id0006", val=6)
dict$delete(id0001)