我正在尝试使用R将SQL表转换为JSON嵌套格式。我可以轻松地将表导入R。现在的问题是以JSON格式获取所有父子关系。我已经设法获得某种JSON输出,但是仅以以下形式列出了具有相应子级的所有父级个体:(我只列出表的前6行)
[
[
{
"name": ["a"],
"children": ["b"]
},
{
"name": ["b"],
"children": ["c"]
},
{
"name": ["c"],
"children": ["d"]
},
{
"name": ["b"],
"children": ["e"]
},
{
"name": ["e"],
"children": ["f"]
}
]
]
和
library(RJSONIO)
orgTable=orgTable[,c("Manager","ID")]
makeList<-function(x){
if(ncol(x)>2){
listSplit = split(x[-1],x[1],drop=T)
lapply(names(listSplit),function(y){list(name=y,children=makeList(listSplit[[y]]))})
}
else{
lapply(seq(nrow(x[1])),function(y){list(name=x[,1][y],children=x[,2][y])})
}
}
jsonOut = toJSON(list(makeList(orgTable[2:6,])),pretty=TRUE)
cat(jsonOut)
SQL表是:
Parent Children
a b
b c
c d
b e
e f
我想要获得的是这样的:
{
"name": "a",
"children": [
{
"name": "b",
"children": [
{
"name": "c",
"children": [
{
"name": "d"
}
]
},
{
"name": "e",
"children": [
{
"name": "f"
}
]
}
]
}
]
}
有人可以帮忙吗?如果可能的话,如果我也可以从第三栏中添加信息,那将是完美的。
我提供的代码来自于这篇文章,但略微调整了我的需求。我在R还是很新,所以请多多包涵。
提前致谢
我将从一个简单的递归解决方案开始,该解决方案仅获取没有其他属性的父/子。
#get some help from igraph
library(igraph)
df <- read.table(
textConnection(
'
Parent Children
a b
b c
c d
b e
e f
' )
, header = TRUE
, stringsAsFactors = FALSE
)
el_in <- get.adjlist(graph.data.frame(df),mode="in")
# fill in name/id instead of number
el_in <- lapply(
el_in,
function(x){
names(el_in)[x]
}
)
get_children <- function( adjlist, node ){
names(Filter(function(x) x==node,unlist(el_in)))
}
recurse_tree <- function( adjlist, node = NULL ){
# start at root if undefined
# root will be the node with no in
if(is.null(node)) node <- names(Filter(function(x)length(x)==0,adjlist))
children <- get_children( adjlist, node )
if(length(children)>0){
list(
name = node
,children = lapply(
children
,function(x){
recurse_tree( adjlist, x )
}
)
)
} else {
list(
name = node
)
}
}
jsonlite::toJSON(
recurse_tree( el_in ),
auto_unbox=T
)
可能不是最有效的方法,但这是使用data.tree
和igraph
构建层次结构的解决方案。
#get some help from the relatively new data.tree
#devtools::install_github("gluc/data.tree")
library(data.tree)
#get some help from igraph
library(igraph)
df <- read.table(
textConnection(
'
Parent Children
a b
b c
c d
b e
e f
' )
, header = TRUE
, stringsAsFactors = FALSE
)
#this will be our paths for data.tree
build_path <- function(df){
g <- graph.data.frame(df)
#get an adjacency list of all in
el_in <- get.adjlist( g, mode="in" )
tree <- lapply(el_in,function(x){""})
lapply(
1:length(el_in)
,function(n){
id <- names(el_in)[n]
x <- el_in[[n]]
if(length(x)>0){
tree[[id]] <<- paste0(
tree[[el_in[[id]]]],
"/",
id
)
} else {
tree[[id]] <<- id
}
}
)
return(unlist(tree))
}
tree <- as.Node(data.frame(
pathString = build_path(df),
# have the ability to specify values
# if not then just set NA
value = NA,
stringsAsFactors = F
))
jsonlite::toJSON(
as.list( tree, mode="explicit", unname = TRUE),
auto_unbox = TRUE
)
# as a test let's build a random tree with igraph
tree_grf <- graph.tree(n=10,children=3)
plot(tree_grf)
tree <- as.Node(data.frame(
pathString = build_path(
get.data.frame(tree_grf,what="edges")
),
# have the ability to specify values
# if not then just set NA
value = NA,
stringsAsFactors = F
))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句