View source: R/mk_lookup_utils.R
| mk_lookup_utils | R Documentation |
The mk_lookup_utils function is a closure which returns a list of
lookup utility functions.
mk_lookup_utils(lookups)
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. |
A list of lookup utility functions with the following elements (see 'Returned Functions' for details):
get_allFunction to retrieve all available lookups
getFunction to retrieve the lookup options for the given key
descripFunction to return the description for a key from the specified lookup
get_typesFunction to retrieve the names of all available lookup types
get_choicesFunction to retrieve the names of all available choices for the given type
makeFunction to create a lookup function for the given type
make_allFunction to create lookup functions for all available types
add_to_envFunction to add the given lookup functions to the specified environment
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
typeThe 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
keyThe key for which to retrieve a description from the specified lookup.
typeThe type (as a string) for which to retrieve the
lookup. If NULL, lookup must be supplied instead.
lookupThe lookup list (a named list of named character
vectors). If NULL, type must be supplied instead.
tfA 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_nameA 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
typeThe 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
typeThe 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
envirEnvironment 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.
lstA list of functions to add to the specified environment (or NULL to add all available lookup functions).
Value
NULL
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.