In below example, I'm creating a nested representation of a tree. I'm expecting that children "1,a" and "1,b" to be associated with parent "1". Yet it ends up to be associated with parent "2", while it shouldn't. Do you have any ideas of what is going wrong?
library(data.tree)
data <- data.frame(
Name = c("A", "A1", "A2", "A1a", "A1b", "A2a", "A2b", "A2c","B"),
Path = c("1", "1,a", "1,b", "1,a,1", "1,a,2", "1,b,1", "1,b,2", "1,b,3","2")
)
my_tree <- data %>%
rowwise() %>%
mutate(pathString = strsplit(Path, ',') %>% unlist() %>% paste(collapse = '/')) %>%
as.Node()
treeToList <- function(node) {
list(
text = node$Name,
li_attr = list(id = node$Path),
state = list(opened = TRUE),
children = Map(
node$children,
f = treeToList
)
)
}
treeToList(my_tree)
It seems you provided a forest rather than a tree, a tree has one root. Therefore your 1 and 2 should have a common root, and this should be explicit
new code
we succesfully have both 1 and 2 under the common
treeroot