在下面的例子中,我创建了一个树的嵌套表示。我希望子节点“1,a”和“1,b”与父节点“1”相关联。但它最终与父节点“2”相关联,而它不应该。你知道哪里出了问题吗?
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)
字符串
1条答案
按热度按时间axzmvihb1#
看起来你提供了一个森林而不是一棵树,一棵树只有一个根。因此你的1和2应该有一个公共的根,这应该是显式的
字符串
新代码
我们成功地在公共
treeroot
下有1和2