R: Cache a value in an environment

env_cache {rlang} | R Documentation

## Cache a value in an environment

### Description

`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.

### Usage

    env_cache(env, nm, default)

### Arguments

`env` | An environment.
`nm` | Names of bindings. `nm` must be a single string.
`default` | The default value to store in `env` if `nm` does not exist yet.

### Value

Either the value of `nm` or `default` if it did not exist yet.

### Examples

    e <- env(a = "foo")

    # Returns existing binding
    env_cache(e, "a", "default")

    # Creates a `b` binding and returns its default value
    env_cache(e, "b", "default")

    # Now `b` is defined
    e$b

[Package rlang version 1.0.2 Index]