Provides a simple indexing interface for list elements based on R6. Basically, it allows to store items in a list and to regain them based on identifiers defined by the user.
Value
The output depends on the method:
$new()
returns aStorage
object.$add()
,$remove()
, and$print()
invisibly return theStorage
object (to allow for method chaining)$get()
returns the requested element(s)$number()
returns aninteger
$indices()
return aninteger
vector
Setting identifiers
An identifier is a character
, typically a binary property. Identifiers
can be negated by placing an exclamation mark ("!"
) in front of them.
Identifiers that have been assigned to other elements previously do not need
to be specified again for new elements; instead, a default value can be used.
This default value can be defined either globally for all cases (via the
$missing_identifier
field) or separately for each specific case (via
the method argument).
User confirmation
If desired, the user can be asked for confirmation when adding, extracting,
or removing elements using identifiers. This behavior can be set globally
through the $confirm
field or customized separately for each specific
case via the method argument.
See also
Other package helpers:
Dictionary
,
identical_structure()
,
input_check_response()
,
match_arg()
,
package_logo()
,
print_data.frame()
,
print_matrix()
,
system_information()
,
unexpected_error()
,
user_confirm()
Active bindings
identifier
[
character()
]
The identifiers used.confirm
[
logical(1)
]
The default value for confirmations.missing_identifier
[
logical(1)
]
The default value for not specified identifiers.hide_warnings
[
logical(1)
]
Hide warnings (for example if unknown identifiers are selected)?
Methods
Method add()
Adding an element.
Usage
Storage$add(
x,
identifier,
confirm = interactive() & self$confirm,
missing_identifier = self$missing_identifier
)
Arguments
x
[
any()
]
An object to be saved.identifier
[
character()
]
Pne or more identifiers (the identifier"all"
is reserved to select all elements).confirm
[
logical(1)
]
Prompted for confirmation?missing_identifier
[
logical(1)
| NA]
The value for not specified identifiers.
Method get()
Getting elements.
Usage
Storage$get(
identifier = character(),
ids = integer(),
logical = "and",
confirm = interactive() & self$confirm,
missing_identifier = self$missing_identifier,
id_names = FALSE
)
Arguments
identifier
[
character()
]
Pne or more identifiers (the identifier"all"
is reserved to select all elements).ids
[
integer()
]
One or more ids.logical
[
character(1)
]
In the case that multiple identifiers are selected, how should they be combined? Options are:"and"
(the default): the identifiers are combined with logical and (all identifiers must beTRUE
)"or"
: the identifiers are combined with logical or (at least one identifier must beTRUE
)
confirm
[
logical(1)
]
Prompted for confirmation?missing_identifier
[
logical(1)
| NA]
The value for not specified identifiers.id_names
[
logical(1)
]
Name the elements according to their ids?
Method remove()
removing elements
Usage
Storage$remove(
identifier = character(),
ids = integer(),
logical = "and",
confirm = interactive() & self$confirm,
missing_identifier = self$missing_identifier,
shift_ids = TRUE
)
Arguments
identifier
[
character()
]
Pne or more identifiers (the identifier"all"
is reserved to select all elements).ids
[
integer()
]
One or more ids.logical
[
character(1)
]
In the case that multiple identifiers are selected, how should they be combined? Options are:"and"
(the default): the identifiers are combined with logical and (all identifiers must beTRUE
)"or"
: the identifiers are combined with logical or (at least one identifier must beTRUE
)
confirm
[
logical(1)
]
Prompted for confirmation?missing_identifier
[
logical(1)
| NA]
The value for not specified identifiers.shift_ids
[
logical(1)
]
Shift ids when in-between elements are removed?
Method number()
Computing the number of identified elements.
Usage
Storage$number(
identifier = "all",
missing_identifier = self$missing_identifier,
logical = "and",
confirm = FALSE
)
Arguments
identifier
[
character()
]
Pne or more identifiers (the identifier"all"
is reserved to select all elements).missing_identifier
[
logical(1)
| NA]
The value for not specified identifiers.logical
[
character(1)
]
In the case that multiple identifiers are selected, how should they be combined? Options are:"and"
(the default): the identifiers are combined with logical and (all identifiers must beTRUE
)"or"
: the identifiers are combined with logical or (at least one identifier must beTRUE
)
confirm
[
logical(1)
]
Prompted for confirmation?
Method indices()
Returning indices based on defined identifiers.
Usage
Storage$indices(
identifier = "all",
logical = "and",
confirm = interactive() & self$confirm
)
Arguments
identifier
[
character()
]
Pne or more identifiers (the identifier"all"
is reserved to select all elements).logical
[
character(1)
]
In the case that multiple identifiers are selected, how should they be combined? Options are:"and"
(the default): the identifiers are combined with logical and (all identifiers must beTRUE
)"or"
: the identifiers are combined with logical or (at least one identifier must beTRUE
)
confirm
[
logical(1)
]
Prompted for confirmation?
Method print()
Printing details of the saved elements.
Examples
### 1. Create a `Storage` object:
my_storage <- Storage$new()
# 2. Add elements along with identifiers:
my_storage$
add(42, c("number", "rational"))$
add(pi, c("number", "!rational"))$
add("fear of black cats", c("text", "!rational"))$
add("wearing a seat belt", c("text", "rational"))$
add(mean, "function")
# 3. What elements are stored?
print(my_storage)
#> number of elements: 5
#> identifiers used: number rational text function
# 4. Extract elements based on identifiers:
my_storage$get("rational")
#> [[1]]
#> [1] 42
#>
#> [[2]]
#> [1] "wearing a seat belt"
#>
my_storage$get("!rational")
#> [[1]]
#> [1] 3.141593
#>
#> [[2]]
#> [1] "fear of black cats"
#>
my_storage$get(c("text", "!rational"))
#> [[1]]
#> [1] "fear of black cats"
#>
my_storage$get("all") # get all elements
#> [[1]]
#> [1] 42
#>
#> [[2]]
#> [1] 3.141593
#>
#> [[3]]
#> [1] "fear of black cats"
#>
#> [[4]]
#> [1] "wearing a seat belt"
#>
#> [[5]]
#> function (x, ...)
#> UseMethod("mean")
#> <bytecode: 0x5611f180db30>
#> <environment: namespace:base>
#>
my_storage$get(c("text", "!text"))
#> list()
my_storage$get(c("text", "!text"), logical = "or")
#> [[1]]
#> [1] 42
#>
#> [[2]]
#> [1] 3.141593
#>
#> [[3]]
#> [1] "fear of black cats"
#>
#> [[4]]
#> [1] "wearing a seat belt"
#>
#> [[5]]
#> function (x, ...)
#> UseMethod("mean")
#> <bytecode: 0x5611f180db30>
#> <environment: namespace:base>
#>
# 5. Extract elements based on ids:
my_storage$get(ids = 4:5)
#> [[1]]
#> [1] "wearing a seat belt"
#>
#> [[2]]
#> function (x, ...)
#> UseMethod("mean")
#> <bytecode: 0x5611f180db30>
#> <environment: namespace:base>
#>
my_storage$get(ids = 4:5, id_names = TRUE) # add the ids as names
#> $`4`
#> [1] "wearing a seat belt"
#>
#> $`5`
#> function (x, ...)
#> UseMethod("mean")
#> <bytecode: 0x5611f180db30>
#> <environment: namespace:base>
#>