I am currently working with the data.tree package in R to create hierarchical trees. However, I am facing challenges in obtaining the final leaves (terminal nodes) from the tree. Despite following the package documentation, I am unable to retrieve the desired results.
As an example, consider the following tree structure:
# Install the data.tree package if not already installed
# install.packages("data.tree")
# Load the package
library(data.tree)
# Create a root node
root <- Node$new("Root")
# Add the first child
first_child <- root$AddChild("First Child")
# Add the second child
second_child <- root$AddChild("Second Child")
# Add two children to the second child
grandchild1 <- second_child$AddChild("Grandchild 1")
grandchild2 <- second_child$AddChild("Grandchild 2")
# Add a child to Grandchild 1
great_grandchild <- grandchild1$AddChild("Great Grandchild")
# Display the tree
print(root)
Despite my efforts, I cannot find a straightforward way to extract the final leaves: “First Child,” “Great Grandchild,” and “Grandchild 2.”
Could someone please provide guidance on how to correctly obtain the terminal nodes from the data.tree package? Is there a specific function or approach I should be using?
Additionally, I find the package documentation to be complex, and it surprises me that even AI chatbots like Copilot and Gemini struggle with such a seemingly simple task.
There seem to be relevant examples in the node methods senction of the documentation. For example you can get what you want with
root$Get("name", filterFun=isLeaf)
# First Child Great Grandchild Grandchild 2
# "First Child" "Great Grandchild" "Grandchild 2"
This returns a named list. You could either unname()
it or just take the names()
to get a simple vector.
If you wanted the nodes themselves you could do
root$Get(identity, filterFun=isLeaf)