salvu salvu - 1 month ago 8
R Question

Can a data.tree be built using other trees as child nodes in R?

I'm extracting comment data from a webpage's source and building trees using:

`tmpTree <- FromListExplicit(postData[[1]], nameName = "poster", childrenName = "child")`


where
postData
is a list of nodes extracted using
xml_find_all
and
postData[[1]]
is changed every time a new tree is created from the list. The functions to extract the nodes can be found in this SO question I had posted in August and was thankfully answered by the creator of RSelenium himself, jdharrison.

What I would like to ask is whether I can create a sort of tree of trees for example:

newTree <- Node$new("Tree67770)
newTree$AddChild(tmpTree)


so that I end up with one tree made up of other threes that would then become nodes in the final tree and when I plot the large tree I can see all the names (the poster's).

The above did not work and the error
cannot coerce type 'environment' to vector of type 'character'
is understandable as each
tmpTree
is not character but a list. I thought of changing each tree into a data.frame and then add everything data.frame back to build one large tree but it seemed to me that it would be too long and cumbersome. Any help would be greatly appreciated. Thanks.

Edited to add dput examples:
Example 1:

structure(list(postId = 2794984430, date = "Thursday, July 21, 2016 11:17 AM",
poster = "MMM", disqusUname = "disqus_rVXuxnq9MP", message = "\rI am against abortion but I am in favour of contraceptives. Is the MAP a (emergency) contraceptive or not? Is the MAP abortive or not? Unless there is clear unequivocal evidence about this, the circus will continue!\r",
child = list(structure(list(postId = 2795948275, date = "Thursday, July 21, 2016 9:07 PM",
poster = "David Farrugia", disqusUname = "davidfarrugia",
message = "\rIt all depends when the soul has been installed into the egg. LOL\r"), .Names = c("postId",
"date", "poster", "disqusUname", "message")))), .Names = c("postId", "date", "poster", "disqusUname", "message", "child"))


Example 2:

structure(list(postId = 2795142611, date = "Thursday, July 21, 2016 2:04 PM",
poster = "David", disqusUname = "disqus_tTjwlqxma8", message = "\rthis reminds me of the Divorce debate. the dinosaurs from church and the parliament seem to be against anything 'god' does not allow. can they accept the fact that not all of us are into religious fairy tales?\r",
child = list(structure(list(postId = 2796284665, date = "Friday, July 22, 2016 12:30 AM",
poster = "Nessy Testa", disqusUname = "NICOTI", message = "\rno they want to shove their \"morals\" down our throats.. then they go to repent their sins..\r"), .Names = c("postId",
"date", "poster", "disqusUname", "message")))), .Names = c("postId", "date", "poster", "disqusUname", "message", "child"))


Each of the above examples produce a tree with a root and a child node and where chosen for their size as some others are 6 levels deep or more.

I used
tmpTree <- FromListExplicit(postData[[Example 1 or 2]], nameName = "poster", childrenName = "child")
to extract the tree which I then tried convert into a new node using:

newTree <- Node$new("root6770")
newNode <- Node$new(tmpTree)
newTree$AddChildNode(newNode)


with the result of the
Error in as.vector(x, "character") :
cannot coerce type 'environment' to vector of type 'character'
as soon as
newNode <- Node$new(tmpTree)
was executed.

I hope that with this example I explained myself better. Thanks for your help.

Answer

Yes, that's possible. Use Node$AddChildNode instead of Node$new to add a sub-tree to an existing node:

library(data.tree)
newTree <- Node$new("roottree")
tmpTree <- Node$new("subtree")
newTree$AddChildNode(tmpTree)
Comments