env_cache()
is a wrapper around env_get()
and env_poke()
designed to retrieve a cached value from env
.
If the
nm
binding exists, it returns its value.Otherwise, it stores the default value in
env
and returns that.
Arguments
- env
An environment.
- nm
Name of binding, a string.
- default
The default value to store in
env
ifnm
does not exist yet.
Examples
e <- env(a = "foo")
# Returns existing binding
env_cache(e, "a", "default")
#> [1] "foo"
# Creates a `b` binding and returns its default value
env_cache(e, "b", "default")
#> [1] "default"
# Now `b` is defined
e$b
#> [1] "default"