Get attribute values from all ancestors (basipetal).

ancestors(
  attribute,
  node = NULL,
  scale = NULL,
  symbol = NULL,
  link = NULL,
  filter_fun = NULL,
  self = FALSE,
  continue = TRUE,
  recursivity_level = NULL
)

Arguments

attribute

Any node attribute (as a character)

node

The MTG node

scale

Integer vector for filtering ancestors by their .scale (i.e. the SCALE from the MTG classes).

symbol

A character vector for filtering the ancestors by their .symbol (i.e. the SYMBOL column from the MTG classes).

link

A character vector for filtering the .link with the parent (e.g. not a branch)

filter_fun

Any filtering function taking a node as input, e.g. data.tree::isLeaf()

self

Return the value of the current node (TRUE), or the ancestors only (FALSE, the default)

continue

Boolean. If TRUE, the function returns all nodes that are not filtered. If FALSE, stop at the first node that is filtered out.

recursivity_level

The maximum number of recursions allowed (considering filters). E.g. to get the parent only: recursivity_level = 1, for parent + grand-parent: recursivity_level = 2. If NULL (the default), the function returns all values from the node to the root.

Value

The attribute values from the ancestors of the node (from first parent to farther ancestor)

Details

This function is mainly intended to be used with mutate_mtg(). In this case, the node argument can be left empty (or node = node equivalently).

Examples

filepath= system.file("extdata", "simple_plant.mtg", package = "XploRer") MTG = read_mtg(filepath)
#> Warning: the condition has length > 1 and only the first element will be used
#> Warning: the condition has length > 1 and only the first element will be used
#> Warning: the condition has length > 1 and only the first element will be used
#> Warning: the condition has length > 1 and only the first element will be used
# node_6 has four ancestors: ancestors("Length", node = extract_node(MTG, "node_6"))
#> node_4 node_3 node_2 node_1 #> 4 NA NA NA
# Two of them have no values for Length # If the value of node_6 is also needed: ancestors("Length", node = extract_node(MTG, "node_6"), self = TRUE)
#> node_6 node_4 node_3 node_2 node_1 #> 6 4 NA NA NA
# If we only need the value of the first parent: ancestors("Length", node = extract_node(MTG, "node_6"), recursivity_level = 1)
#> node_4 #> 4
# We can filter by symbol if we need to return the values for some symbols only: ancestors("Width", node = extract_node(MTG, "node_6"), symbol = "Internode")
#> node_4 #> 1
# The values are only returned for the ancestors with the required symbol # For example we know that a leaf cannot be an ancestor because it cannot bear anything: ancestors("Width", node = extract_node(MTG, "node_6"), symbol = "Leaf")
#> logical(0)
# In this case it returns a length 0 vector.