A scdrake
's list with overloaded, strict access operators.
Source: R/classes.R
, R/generics.R
scdrake_list.Rd
It behaves like a normal list, but, by default, it has more strict rules for non-existing elements:
$
, [
, and [[
operators are overloaded for this class.
They try to extract element(s) from a scdrake_list
object, but throw an error when the element(s) does not exist.
This behaviour can be avoided for [
and [[
by using the check = FALSE
parameter, e.g. l[name, check = FALSE]
.
Also, in that case and for [
, a scdrake_list
with non-existing names and NULL
values is returned instead of putting
NA_character_
for them, which is the default behaviour for a base list, e.g.
> list(a = 2)["b"]
$<NA>
NULL
compared to
> scdrake_list(list(a = 2))["b", check = FALSE]
$b
NULL
attr(,"class")
[1] "scdrake_list"
However, this does not work for numeric subscripts.
Usage
scdrake_list(l = NULL)
# S3 method for scdrake_list
$(x, i)
# S3 method for scdrake_list
[(x, i, check = TRUE)
# S3 method for scdrake_list
[[(x, i, check = TRUE)
Arguments
- l
A list.
- x
An object of class
scdrake_list
.- i
A character scalar or vector: name / names of element(s) to extract.
- check
A logical scalar: if
TRUE
, throw an error when non-existing names or subscripts are present.
Examples
cfg <- scdrake_list(list(var_1 = 1, var_2 = 2))
# The standard list behavior.
cfg$var_1
#> [1] 1
cfg[["var_2"]]
#> [1] 2
cfg["var_1"]
#> $var_1
#> [1] 1
#>
#> attr(,"class")
#> [1] "scdrake_list" "list"
cfg[c("var_1", "var_2")]
#> $var_1
#> [1] 1
#>
#> $var_2
#> [1] 2
#>
#> attr(,"class")
#> [1] "scdrake_list" "list"
if (FALSE) {
# Strict rules for non-existing elements - throws an error.
cfg$var_3
cfg[["var_3"]]
cfg[c("var_1", "var_3")]
}