Try to access element and return some default value if not found. In contrast to [at2()], this function provides a less stricter element access, that is, it remains valid even if peeked elements don't exist.

peek_at2(x, index, default = NULL)

# S3 method for Container
peek_at2(x, index, default = NULL)

# S3 method for dict.table
peek_at2(x, index, default = NULL)

Arguments

x

an R object of the respective class.

index

character name or numeric position of the sought value.

default

value to be returned if peeked value does not exist.

Value

For Container, returns the value at the given index or (if not found) the given default value.

For dict.table, returns the column named index if it exist otherwise the given default value. If the default length does not match the number of rows, it is recycled accordingly and a warning is given, unless the default value has a length of 1, in which case recycling is done silently.

See also

at2() for strict element extraction

Examples


# Container
co = container(a = 1, 2, b = 3, 4)
peek_at2(co, 1)
#> [1] 1
peek_at2(co, "a")
#> [1] 1
peek_at2(co, "x")
#> NULL
peek_at2(co, "x", default = 0)
#> [1] 0

# Dict
d = dict(a = 1, b = 1:3)
peek_at2(d, "b")
#> [1] 1 2 3
peek_at2(d, "x")
#> NULL
peek_at2(d, "x", default = 4:7)
#> [1] 4 5 6 7

# dict.table
dit = dict.table(a = 1:3, b = 4:6)
peek_at2(dit, "a")
#> [1] 1 2 3
peek_at2(dit, 1)
#> [1] 1 2 3
peek_at2(dit, 3)
#> NULL
peek_at2(dit, 3, default = 9)
#> [1] 9 9 9
peek_at2(dit, "x")
#> NULL
peek_at2(dit, "x", default = 0)
#> [1] 0 0 0