Skip to contents

Safely get the attribute of an object. An error is given if the attribute does not exist (and do.stop = TRUE)

Usage

getAttribute(x, attributeName, do.stop = TRUE)

Arguments

x

object from which to take the attribute

attributeName

name of the attribute to be returned

do.stop

if TRUE (default) an error is raised if the attribute does not exist.

Examples


x <- structure(1, a = 2)
# getAttribute(x, "b") # gives a clear error message
identical(getAttribute(x, "a"), attr(x, "a")) # is TRUE
#> [1] TRUE

# Get an attribute's attribute by means of a "path" notation
y <- structure(1, a = structure(2, b = 3))
z <- structure(4, y = y)

str(y)
#>  num 1
#>  - attr(*, "a")= num 2
#>   ..- attr(*, "b")= num 3
str(z)
#>  num 4
#>  - attr(*, "y")= num 1
#>   ..- attr(*, "a")= num 2
#>   .. ..- attr(*, "b")= num 3

kwb.utils::getAttribute(y, "a/b")
#> [1] 3
kwb.utils::getAttribute(z, "y/a/b")
#> [1] 3