mk_lookup_utils: Create lookup utility functions

View source: R/mk_lookup_utils.R

mk_lookup_utilsR Documentation

Create lookup utility functions

Description

The mk_lookup_utils function is a closure which returns a list of lookup utility functions.

Usage

mk_lookup_utils(lookups)

Arguments

lookups

A named list of named character vectors. The names are available types and the elements (each a named character vector) are key/description pairs for the corresponding lookup type.

Value

A list of lookup utility functions with the following elements (see 'Returned Functions' for details):

get_all

Function to retrieve all available lookups

get

Function to retrieve the lookup options for the given key

descrip

Function to return the description for a key from the specified lookup

get_types

Function to retrieve the names of all available lookup types

get_choices

Function to retrieve the names of all available choices for the given type

make

Function to create a lookup function for the given type

make_all

Function to create lookup functions for all available types

add_to_env

Function to add the given lookup functions to the specified environment

Returned Functions

The returned functions are utilities for managing a lookup list. They are:

get_all()

Function to retrieve all available lookups
Value
A list of all available lookups.

get(type)

Function to retrieve the lookup options for the given key
Arguments

type

The name of the type for which to retrieve lookup options.

Value
A named character vector of lookup options for the given type. The character vector consists of key/value pairs where names are keys and values are descriptions for the keys.

descrip(key, type = NULL, lookup = NULL, tf = FALSE, strip_name = TRUE)

Function to return the description for a key from the specified lookup
Arguments

key

The key for which to retrieve a description from the specified lookup.

type

The type (as a string) for which to retrieve the lookup. If NULL, lookup must be supplied instead.

lookup

The lookup list (a named list of named character vectors). If NULL, type must be supplied instead.

tf

A logical specifying whether the first letter of the description should be in title case ("tf" stands for "titleize first"); otherwise it will be lower case.

strip_name

A logical specifying whether the name (i.e. the key) should be removed from the description before it is returned.

Value
Description of the specified key within the specified lookup.

get_types()

Function to retrieve the names of all available lookup types
Value
A character vector comprising names of all available lookup types.

get_choices(type)

Function to retrieve the names of all available choices for the given type
Arguments

type

The type (as a string) for which to retrieve all available choices.

Value
A character vector comprising names of all available choices for the given type.

make(type)

Function to create a lookup function for the given type
Arguments

type

The type (as a string) for which to create a lookup function.

Value
A lookup function for the given type.

make_all()

Function to create lookup functions for all available types
Value
A list of all available lookup functions.

add_to_env(envir = NULL, lst = NULL)

Function to add the given lookup functions to the specified environment
Arguments

envir

Environment to which the lookup functions should be added. Note that the default is to create a new environment from within this function, which would have no effect on its return; this is by design to avoid any unintended additions to another environment.

lst

A list of functions to add to the specified environment (or NULL to add all available lookup functions).

Value
NULL

Examples

stuff <- list(
  a = c(upper = "red", middle = "yellow", lower = "green"),
  b = c(first = "platinum level", second = "silver level")
)
lookup <- mk_lookup_utils(stuff)

lookup$get_all()

lookup$get("b")

lookup$descrip("lower", lookup = lookup$get("a"))
lookup$descrip("lower", "a")

lookup$descrip("lower", "a", tf = TRUE)

lookup$descrip("lower", "a", strip_name = FALSE)

lookup$get_types()

lookup$get_choices("b")

lookup_a <- lookup$make("a")
lookup_a("upper")

lookup_lst <- lookup$make_all()
lookup_lst$lookup_b("second")

an_env <- new.env(parent = emptyenv())
lookup$add_to_env(an_env)


toniprice/jute documentation built on Jan. 11, 2023, 8:23 a.m.