Get attribute values from the children of a node.

children(
  attribute,
  node = NULL,
  scale = NULL,
  symbol = NULL,
  link = NULL,
  filter_fun = NULL,
  continue = TRUE
)

Arguments

attribute

Any node attribute (as a character)

node

The MTG node

scale

An integer vector for filtering the .scale of the children (i.e. the SCALE column from the MTG classes).

symbol

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

link

A character vector for filtering the .link with the children

filter_fun

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

continue

If a child is not of the right scale, continue until the scale required is met if TRUE, or returns NA if FALSE.

Value

The attribute values from the children of the node

Details

This function is mainly intended to be used with mutate_mtg(). In this case, the node argument can be left empty (or you can put 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 one child: children("Length", node = extract_node(MTG, "node_6"))
#> node_7 #> 12
# Using node 4 as reference now: node_4 = extract_node(MTG, "node_4") # node_4 has two children, returns two values: children("Length", node = node_4)
#> node_5 node_6 #> 10 6
# To get the names of those children: children("name", node = node_4)
#> node_5 node_6 #> "node_5" "node_6"
# The width is not available for one child ("node_6"): children("Width", node = node_4)
#> node_5 node_6 #> 6 NA
# We can filter by scale if we need to return the values for some scales only: children("Width", node = node_4, symbol = "Leaf")
#> node_5 node_7 #> 6 7
# Here we get the value of node_7 also, because its parent "node_6" is not of scale # "Leaf", so it was filtered out. It you need the values for one scale, but not # making a recursive search from one scale to another until finding the required scale, # you can put the `continue` argument to `FALSE`: children("Width", node = node_4, symbol = "Leaf", continue = FALSE)
#> node_5 #> 6
# To get the values of the children of each node: mutate_mtg(MTG, children_width = children("Width")) print(MTG, "Width", "children_width")
#> levelName Width children_width #> 1 node_1 NA #> 2 °--node_2 NA #> 3 °--node_3 NA 1 #> 4 °--node_4 1 6, NA #> 5 ¦--node_5 6 #> 6 °--node_6 NA 7 #> 7 °--node_7 7
# And using only the bodes with symbol "Leaf": mutate_mtg(MTG, children_width2 = children("Width", symbol = "Leaf", continue = FALSE)) print(MTG, "Width", "children_width2")
#> levelName Width children_width2 #> 1 node_1 NA NA #> 2 °--node_2 NA NA #> 3 °--node_3 NA NA #> 4 °--node_4 1 6 #> 5 ¦--node_5 6 NA #> 6 °--node_6 NA 7 #> 7 °--node_7 7 NA