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

peek_at(.x, ...)

# S3 method for Container
peek_at(.x, ..., .default = NULL)

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

Arguments

.x

an R object of the respective class.

...

indices of elements to be extracted

.default

value to be returned if peeked value does not exist.

Value

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

For dict.table, returns the columns at the given indices or (if not found) columns with the given default value.

Details

peek_at tries to access specific values.

See also

at() for strict element extraction

Examples


# Container
co = container(a = 1, 2, b = 3, 4)
peek_at(co, 1)
#> [a = 1]
peek_at(co, "a")
#> [a = 1]
peek_at(co, "x")
#> []
peek_at(co, "x", .default = 0)
#> [x = 0]
peek_at(co, "a", "x", 2, 9, .default = -1)
#> [a = 1, x = -1, 2, -1]

# Dict
d = dict(a = 1, b = 1:3)
peek_at(d, "b")
#> {b = (1L 2L 3L)}
peek_at(d, "x")
#> {}
peek_at(d, "x", .default = 4:7)
#> {x = (4L 5L 6L 7L)}

# dict.table
dit = dict.table(a = 1:3, b = 4:6)
peek_at(dit, "a")
#> <dict.table> with 3 rows and 1 column
#>    a
#> 1: 1
#> 2: 2
#> 3: 3
peek_at(dit, 1)
#> <dict.table> with 3 rows and 1 column
#>    a
#> 1: 1
#> 2: 2
#> 3: 3
peek_at(dit, 3)
#> <dict.table> with 0 rows and 0 columns
#> Null data.table (0 rows and 0 cols)
peek_at(dit, "x")
#> <dict.table> with 0 rows and 0 columns
#> Null data.table (0 rows and 0 cols)
peek_at(dit, "x", .default = 0)
#> <dict.table> with 3 rows and 1 column
#>    x
#> 1: 0
#> 2: 0
#> 3: 0
peek_at(dit, "a", "x", .default = 0)
#> <dict.table> with 3 rows and 2 columns
#>    a x
#> 1: 1 0
#> 2: 2 0
#> 3: 3 0