| Storage {oeli} | R Documentation |
Storage R6 Object
Description
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 aStorageobject. -
$add(),$remove(), and$print()invisibly return theStorageobject (to allow for method chaining) -
$get()returns the requested element(s) -
$number()returns aninteger -
$indices()return anintegervector
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.
Active bindings
identifiera
charactervector, the identifiers usedconfirmsetting the default value for confirmations (either
TRUEorFALSE)missing_identifiersetting the default value for not specified identifiers (either
TRUE,FALSE, orNA)hide_warningseither
TRUEto hide warnings (for example if unknown identifiers are selected) orFALSE(default), else
Methods
Public methods
Method new()
initializing a Storage object
Usage
Storage$new()
Returns
a new Storage object
Method add()
adding an element
Usage
Storage$add( x, identifier, confirm = interactive() & self$confirm, missing_identifier = self$missing_identifier )
Arguments
xany object to be saved
identifiera
charactervectorwith one or more identifiers (the identifier"all"is reserved to select all elements)confirmeither
TRUEto be prompted for confirmation, orFALSEelsemissing_identifierthe
logicalvalue for not specified identifiers (eitherNA,TRUE, orFALSE)
Returns
invisibly the Storage object
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
identifiera
charactervectorwith one or more identifiers (the identifier"all"is reserved to select all elements)idsan
integervectorof one or more idslogicalin 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)
-
confirmeither
TRUEto be prompted for confirmation, orFALSEelsemissing_identifierthe
logicalvalue for not specified identifiers (eitherNA,TRUE, orFALSE)id_nameseither
TRUEto name the elements according to their ids orFALSEif not
Returns
the selected object(s)
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
identifiera
charactervectorwith one or more identifiers (the identifier"all"is reserved to select all elements)idsan
integervectorof one or more idslogicalin 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)
-
confirmeither
TRUEto be prompted for confirmation, orFALSEelsemissing_identifierthe
logicalvalue for not specified identifiers (eitherNA,TRUE, orFALSE)shift_idseither
TRUEto shift ids when in-between elements are removed, orTRUEto keep the ids
Returns
invisibly the Storage object
Method number()
computing the number of identified elements
Usage
Storage$number( identifier = "all", missing_identifier = self$missing_identifier, logical = "and", confirm = FALSE )
Arguments
identifiera
charactervectorwith one or more identifiers (the identifier"all"is reserved to select all elements)missing_identifierthe
logicalvalue for not specified identifiers (eitherNA,TRUE, orFALSE)logicalin 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)
-
confirmeither
TRUEto be prompted for confirmation, orFALSEelse
Returns
an integer
Method indices()
returning indices based on defined identifiers
Usage
Storage$indices( identifier = "all", logical = "and", confirm = interactive() & self$confirm )
Arguments
identifiera
charactervectorwith one or more identifiers (the identifier"all"is reserved to select all elements)logicalin 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)
-
confirmeither
TRUEto be prompted for confirmation, orFALSEelse
Returns
an integer vector
Method print()
printing details of the saved elements
Usage
Storage$print(...)
Arguments
...currently not used
Returns
invisibly the Storage object
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)
# 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