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.
The output depends on the method:
$new()
returns a Storage
object.
$add()
, $remove()
, and $print()
invisibly return the
Storage
object (to allow for method chaining)
$get()
returns the requested element(s)
$number()
returns an integer
$indices()
return an integer
vector
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).
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.
identifier
a character
vector
, the identifiers used
confirm
setting the default value for confirmations (either
TRUE
or FALSE
)
missing_identifier
setting the default value for not specified
identifiers (either TRUE
, FALSE
, or NA
)
hide_warnings
either TRUE
to hide warnings (for example
if unknown identifiers are selected) or FALSE
(default), else
new()
initializing a Storage
object
Storage$new()
a new Storage
object
add()
adding an element
Storage$add(
x,
identifier,
confirm = interactive() & self$confirm,
missing_identifier = self$missing_identifier
)
x
any object to be saved
identifier
a character
vector
with one or more identifiers (the identifier
"all"
is reserved to select all elements)
confirm
either TRUE
to be prompted for confirmation, or FALSE
else
missing_identifier
the logical
value for not specified identifiers (either NA
,
TRUE
, or FALSE
)
invisibly the Storage
object
get()
getting elements
Storage$get(
identifier = character(),
ids = integer(),
logical = "and",
confirm = interactive() & self$confirm,
missing_identifier = self$missing_identifier,
id_names = FALSE
)
identifier
a character
vector
with one or more identifiers (the identifier
"all"
is reserved to select all elements)
ids
an integer
vector
of one or more ids
logical
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 be true)
"or"
: the identifiers are combined with logical or (at least one
identifier must be true)
confirm
either TRUE
to be prompted for confirmation, or FALSE
else
missing_identifier
the logical
value for not specified identifiers (either NA
,
TRUE
, or FALSE
)
id_names
either TRUE
to name the elements according to their ids or
FALSE
if not
the selected object(s)
remove()
removing elements
Storage$remove(
identifier = character(),
ids = integer(),
logical = "and",
confirm = interactive() & self$confirm,
missing_identifier = self$missing_identifier,
shift_ids = TRUE
)
identifier
a character
vector
with one or more identifiers (the identifier
"all"
is reserved to select all elements)
ids
an integer
vector
of one or more ids
logical
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 be true)
"or"
: the identifiers are combined with logical or (at least one
identifier must be true)
confirm
either TRUE
to be prompted for confirmation, or FALSE
else
missing_identifier
the logical
value for not specified identifiers (either NA
,
TRUE
, or FALSE
)
shift_ids
either TRUE
to shift ids when in-between elements are removed,
or TRUE
to keep the ids
invisibly the Storage
object
number()
computing the number of identified elements
Storage$number(
identifier = "all",
missing_identifier = self$missing_identifier,
logical = "and",
confirm = FALSE
)
identifier
a character
vector
with one or more identifiers (the identifier
"all"
is reserved to select all elements)
missing_identifier
the logical
value for not specified identifiers (either NA
,
TRUE
, or FALSE
)
logical
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 be true)
"or"
: the identifiers are combined with logical or (at least one
identifier must be true)
confirm
either TRUE
to be prompted for confirmation, or FALSE
else
an integer
indices()
returning indices based on defined identifiers
Storage$indices(
identifier = "all",
logical = "and",
confirm = interactive() & self$confirm
)
identifier
a character
vector
with one or more identifiers (the identifier
"all"
is reserved to select all elements)
logical
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 be true)
"or"
: the identifiers are combined with logical or (at least one
identifier must be true)
confirm
either TRUE
to be prompted for confirmation, or FALSE
else
an integer
vector
...
currently not used
invisibly the Storage
object
### 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)
# 4. Extract elements based on identifiers:
my_storage$get("rational")
my_storage$get("!rational")
my_storage$get(c("text", "!rational"))
my_storage$get("all") # get all elements
my_storage$get(c("text", "!text"))
my_storage$get(c("text", "!text"), logical = "or")
# 5. Extract elements based on ids:
my_storage$get(ids = 4:5)
my_storage$get(ids = 4:5, id_names = TRUE) # add the ids as names
Run the code above in your browser using DataLab