Description Usage Arguments Value See Also Examples
Creates a dict
object from the supplied arguments.
A dict
can be create from named arguments, named lists, environments,
or vectors of keys
and values
.
1 |
... |
Objects, possibly named. |
Object of class dict
.
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 | # Basic use:
d <- dict(a = 1, b = 2)
d$size()
d$has_key("a")
d$has_key("c")
# From a named list
d <- dict(list(chr = "name", bool = TRUE))
d$get("chr")
d$get("char", default = "missing")
d$set("char", "now i exist")
d$get("char", default = "missing")
# Updating dict
d <- dict(one = 1L)
other <- dict(two = 2L, PI = 3.14)
d$update(other)
d
# Creating from keys and values vectors
# These can be named explicitly, otherwise the first one
# is considered keys and the second, values
d <- dict(letters, seq_along(letters))
# Iterating through dict (will print the key with value 10, i.e. "j")
for (item in d$items()) {
if (item$value == 10)
print(item$key)
}
# Extracting values
d <- dict(x = 1, y = 2)
d["x"]
d["z"] <- 3
# Vectorized assignments
new_keys <- c("one", "two")
new_vals <- c(1, 2)
d[new_keys] <- new_vals
# Vectorized extractions
keys_to_extract <- c("x", "two")
d[keys_to_extract]
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.