I am working with a list of lists, collected from an XML chunk, that I would like to represent with object defined by the data.tree
package for R. The example below seems to work, and I can extract elements from the data.tree
representation of the list of lists. However, I cannot figure out how to use any of the text-formatting or visualization options (e.g. igraph
) due to the fact that "child" elements of each list are not uniquely labeled.
Ideally, I would like to recursively re-name "Children" with a serial number. For example, convert this:
Children
|-- RuleRule
|-- RuleRule
|-- RuleRule
To this:
Children
|-- RuleRule_01
|-- RuleRule_02
|-- RuleRule_03
Or even better, re-name the "Children" according to an attribute such as
Children
|-- RuleRule_15976
|-- RuleRule_49444
|-- RuleRule_15748
Here is a similar question that is almost what I am looking for. I am not sure if the use of data.tree
functionality would simplify the re-naming of children elements, or if this should be done before initializing the data.tree
object. The tree-traversal capabilities of data.tree
seem like the right route, especially since the types of data I will be using can have multiple set of children, at any level.
A self-contained example:
library(data.tree)
# a typical list
l <- structure(list(RuleStart = structure(list(Children = structure(list(
RuleOperator = structure(list(Children = structure(list(RuleRule = structure(list(
Children = NULL, RefId = "49446"), .Names = c("Children",
"RefId")), RuleRule = structure(list(Children = NULL, RefId = "15976"), .Names = c("Children",
"RefId")), RuleRule = structure(list(Children = NULL, RefId = "49444"), .Names = c("Children",
"RefId")), RuleRule = structure(list(Children = NULL, RefId = "15748"), .Names = c("Children",
"RefId")), RuleRule = structure(list(Children = NULL, RefId = "49440"), .Names = c("Children",
"RefId")), RuleRule = structure(list(Children = NULL, RefId = "15746"), .Names = c("Children",
"RefId")), RuleRule = structure(list(Children = NULL, RefId = "49449"), .Names = c("Children",
"RefId"))), .Names = c("RuleRule", "RuleRule", "RuleRule",
"RuleRule", "RuleRule", "RuleRule", "RuleRule")), Type = "product"), .Names = c("Children",
"Type"))), .Names = "RuleOperator")), .Names = "Children")), .Names = "RuleStart")
# convert XML list into data.tree object
n <- FromListExplicit(l$RuleStart, nameName=NULL, childrenName='Children')
# check
print(n, 'RefId')