mk_lookup_utils: Create lookup utility functions

Description Usage Arguments Value Returned Functions Examples

Description

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

Usage

1
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:

1
get_all()

function to retrieve all available lookups
Value
a list of all available lookups.

1

function to retrieve the lookup options for the given key
Arguments

type

character: 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.

1
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

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

type

character: type for which to retrieve the lookup. If NULL, lookup must be supplied instead.

lookup

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

tf

logical (stands for "titleize first"): should the first letter of the description be in title case or lower case?

strip_name

logical: should the name (i.e. the key) be removed from the description before it is returned?

Value
description of the specified key within the specified lookup.

1
get_types()

function to retrieve the names of all available lookup types
Value
a character vector comprising names of all available lookup types.

1
get_choices(type)

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

type

character: type for which to retrieve all available choices.

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

1
make(type)

function to create a lookup function for the given type
Arguments

type

character: type for which to create a lookup function.

Value
a lookup function for the given type.

1
make_all()

function to create lookup functions for all available types
Value
a list of all available lookup functions.

1
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 an environment.

lst

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

Value
NULL

Examples

 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
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/jutebag documentation built on May 12, 2019, 4:39 a.m.