Description Usage Arguments Value Active bindings Methods Examples
A key-value dictionary data structure based on R6 class which is designed to be similar usages with other languages dictionary (e.g. Python) with reference semantics and extendabilities by R6.
1 |
... |
Any length of key and value pairs. If you would like to use
a not valid R name as a key, you must wrap it by backquotes or convert it
using |
.class |
A character scalar of value object's class. It must be an
output from |
.overwrite |
A logical scalar whether to overwrite the value if the key is overlapped. |
A Dict
class object.
items
A tbl_df
of the dictionary items.
keys
A character vector of the dictionary keys.
values
A list of of the dictionary values.
length
A integer scalar of the items length.
class
A character scalar of value class.
overwrite
A logical scalar whether to overwrite value if key is overlapped.
new()
Construct a new Dict object.
Dict$new(..., .class = "any", .overwrite = TRUE)
...
Any length of key and value pairs. If you would like to use
a not valid R name as a key, you must wrap it by backquotes or convert it
using make.names
.
.class
A character scalar of value object's class. It must be an
output from class
. If "any"
(default), value can
contain any type of object.
.overwrite
A logical scalar whether to overwrite the value if the key is overlapped.
A Dict
class object.
ages <- Dict$new( Charlie = 40L, Alice = 30L, Bob = 25L, .class = "integer", .overwrite = TRUE )
print()
Print Dict items
which is a tbl_df-class
object by tibble package.
Dict$print(...)
...
Additional arguments passed to print.tbl
.
Dict
object by invisible(self)
.
ages$print(n = Inf)
add()
Add key-value objects to the dictionary.
Dict$add(...)
...
Any length of key and value pairs. If you would like to use
a not valid R name as a key, you must wrap it by backquotes or convert it
using make.names
.
Dict
object by invisible(self)
.
ages$add(John = 18L) ages["John"] <- 18L
has()
Check if the object contains the key.
Dict$has(key = NULL)
key
A character scalar of the dictionary key.
A logical scalar.
ages$has("Bob")
get()
Retrieves object with a key from the dictionary.
Dict$get(key = NULL, default = NULL)
key
A character scalar, integer scalar of items index or NULL. If key is NULL and items is not empty, the first value is returned.
default
A default value returned, if the key is not found. Default
is NULL
.
A object with the key.
ages$get("Bob") ages["Bob"] ages[3] # also by integer index
remove()
Removes a key-value from the dictionary by a key. If the key is a not
valid key, this function throw an error. Use self$has()
to check
key availability.
Dict$remove(key = NULL)
key
A character scalar of the dictionary key.
Dict
object by invisible(self)
.
ages$remove("Bob")
sort()
Sort dictionary by keys.
Dict$sort(desc = FALSE)
desc
A logical scalar whether to sort in descending order. Default
is FALSE
.
Dict
object by invisible(self)
.
ages$sort()
clear()
Clear dictionary.
Dict$clear()
Dict
object by invisible(self)
.
ages$clear()
clone()
The objects of this class are cloneable with this method.
Dict$clone(deep = FALSE)
deep
Whether to make a deep clone.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | ## ------------------------------------------------
## Method `Dict$new`
## ------------------------------------------------
ages <- Dict$new(
Charlie = 40L,
Alice = 30L,
Bob = 25L,
.class = "integer",
.overwrite = TRUE
)
## ------------------------------------------------
## Method `Dict$print`
## ------------------------------------------------
ages$print(n = Inf)
## ------------------------------------------------
## Method `Dict$add`
## ------------------------------------------------
ages$add(John = 18L)
ages["John"] <- 18L
## ------------------------------------------------
## Method `Dict$has`
## ------------------------------------------------
ages$has("Bob")
## ------------------------------------------------
## Method `Dict$get`
## ------------------------------------------------
ages$get("Bob")
ages["Bob"]
ages[3] # also by integer index
## ------------------------------------------------
## Method `Dict$remove`
## ------------------------------------------------
ages$remove("Bob")
## ------------------------------------------------
## Method `Dict$sort`
## ------------------------------------------------
ages$sort()
## ------------------------------------------------
## Method `Dict$clear`
## ------------------------------------------------
ages$clear()
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.