-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add equivalent of ape::drop.tip #164
Comments
@gluc This is my solution:
Is there a better way of pruning to certain lineages versus this convoluted method that I have devised? |
Just re-run into this issue with a new project: data(acme)
Prune(acme, function(node) node$name %in% c("New Labs"))
acme Results in
|
My updated approach: #' Determine whether a child node is in the target lineage
child_in_target_lineage = function(child_node, targets){
any(child_node$name %in% targets) || any(child_node$TARGET == TRUE)
}
#' Determine whether a node is in the target lineage
node_in_target_lineage = function(node, targets){
if(node$isRoot || child_in_target_lineage(node, targets)){
return(TRUE)
} else {
has_target_child = any(
sapply(node$children, child_in_target_lineage, targets=targets)
)
if(has_target_child){
return(TRUE)
}
}
return(FALSE)
}
#' Prune to target node names
datatree_prune = function(dtree, target_ids){
# check input
if(is.null(target_ids) || length(target_ids) == 0) return(dtree)
# Assign TRUE to any lineages containing the targets
dtree$Do(
function(node){
node$TARGET = node_in_target_lineage(node, target_ids)
},
traversal = "post-order"
)
# Prune based on the lineage assignments
Prune(dtree, function(node) node$TARGET == TRUE)
# Remove the lineage assignments
dtree$Do(function(node) node$TARGET = NULL)
}
data(acme)
datatree_prune(acme, c("New Labs"))
acme Result:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be helpful to include an example in the docs of how to prune a
data.tree
object in the same manner asape::drop.tip
The text was updated successfully, but these errors were encountered: