Provides a simple key-value interface based on R6.
See also
Other package helpers:
Storage,
check_missing(),
find_namespace_calls(),
identical_structure(),
input_check_response(),
match_arg(),
package_logo(),
print_data.frame(),
print_matrix(),
system_information(),
unexpected_error(),
user_confirm()
Active bindings
keys[
character()]
Available keys.alias[
list()]
Available keys per alias value.
Methods
Method new()
Initializing a new Dictionary object.
Usage
Dictionary$new(
key_name,
alias_name = NULL,
value_names = character(),
value_assert = alist(),
allow_overwrite = TRUE,
keys_reserved = character(),
alias_choices = NULL,
dictionary_name = NULL
)Arguments
key_name[
character(1)]
The name for the key variable.alias_name[
NULL|character(1)]
Optionally the name for the alias variable.value_names[
character(0)]
The names of the values connected to a key.value_assert[
alist(1)]
For each element invalue_names,values_assertcan have an identically named element of the formcheckmate::assert*(...), where...can be any argument for the assertion function except for thexargument.allow_overwrite[
logical(1)]
Allow overwriting existing keys with new values? Duplicate keys are never allowed.keys_reserved[
character()]
Names that must not be used as keys.alias_choices[
NULLorcharacter()]
Optionally possible values for the alias. Can also beNULL, then all alias values are allowed.dictionary_name[
NULLorcharacter()]
Optionally the name for the dictionary.
Method get()
Getting elements from the dictionary.
Method remove()
Removing elements from the dictionary (and associated alias, if any).
Examples
# Managing variable metadata for a dataset
meta_dict <- Dictionary$new(
key_name = "var_name",
alias_name = "category",
value_names = c("label", "type"),
value_assert = alist(
label = checkmate::assert_string(),
type = checkmate::assert_choice(choices = c("numeric", "factor", "character"))
),
allow_overwrite = FALSE,
keys_reserved = c("id"),
alias_choices = c("demographics", "outcome", "other"),
dictionary_name = "Variable Metadata"
)
# Add entries to the dictionary
meta_dict$add(
var_name = "age",
label = "Age of respondent",
type = "numeric",
category = "demographics"
)
meta_dict$add(
var_name = "gender",
label = "Gender identity",
type = "factor",
category = "demographics"
)
meta_dict$add(
var_name = "income",
label = "Annual income in USD",
type = "numeric",
category = c("demographics", "outcome")
)
# Print dictionary
meta_dict$print()
#> <Dictionary> Variable Metadata
#> Keys:
#> - age
#> - gender
#> - income
# Retrieve full metadata for a variable
meta_dict$get("income")
#> $label
#> [1] "Annual income in USD"
#>
#> $type
#> [1] "numeric"
#>
# Retrieve a specific piece of metadata
meta_dict$get("income", value = "label")
#> [1] "Annual income in USD"
# Show variables by category
meta_dict$alias
#> $demographics
#> [1] "age" "gender" "income"
#>
#> $outcome
#> [1] "income"
#>
