Suppose that I wanted to prune a tree consisting of a hierarchy of nested lists in R, based on some particular criterion. I can do this "easily" enough using lapply
:
# Based an example from the NetworkD3 documentation
# https://christophergandrud.github.io/networkD3/
URL <- paste0(
"https://cdn.rawgit.com/christophergandrud/networkD3/",
"master/JSONdata//flare.json")
flare <- jsonlite::fromJSON(URL, simplifyDataFrame = FALSE)
# Leaf nodes have a "size" attribute. Let's say we want to
# prune all the nodes with size < 5000.
prune <- function(tree) {
if ("children" %in% names(tree)) {
p <- lapply(tree$children, prune)
pp <- p[!unlist(lapply(p, is.null))]
copied_tree = list()
copied_tree$name = tree$name
copied_tree$children = pp
return(copied_tree)
} else if (tree$size < 5000) {
return(NULL)
}
return(tree)
}
pruned <- prune(flare)
In R for Data Science, Hadley Wickham discusses a number of scenarios in which purrr
can replace the apply
family of functions for handling hierarchical data. However, these examples seem to deal either with singly nested lists, or with specific nodes of deeply nested lists.
Is there a way to use purrr
to accomplish recursive tasks such as the one discussed above?