将toJson R对象转换为适合d3.js树形布局的格式 [英] Converting toJson R object into a format that fits d3.js tree layout
问题描述
这是来自以下问题的后续问题:转换ctree输出为JSON格式(用于D3树布局)
This is a followup question from : Converting ctree output into JSON Format (for D3 tree layout)
我有以下代码,是使用ctree
生成的(来自Party
,请参见下面的代码),我的意图是创建一个递归函数,以转换以下Json输出:
I have the following code, produced using ctree
(from Party
, see bellow the code that produced it), my intention is to create a recursive function that transforms the following Json output:
{"nodeID":[1],"left":
{"nodeID":[2],"weights":[50],"prediction":[1,0,0]},
"right":
{"nodeID":[3],"left":{
"nodeID":[4],"left":
{"nodeID":[5],"weights":[46],"prediction":[0,0.9783,0.0217]},"right":
{"nodeID":[6],"weights":[8],"prediction":[0,0.5,0.5]}},"right":
{"nodeID":[7],"weights":[46],"prediction":[0,0.0217,0.9783]}}}
分为以下内容:
{ "name": "1", "children": [
{
"name": "3","children": [
{
"name": "4","children":[
{"name":"5 weights:[46] prediction:[0,0.9783,0.0217]"},
{"name":"6 weights:[8] prediction[0,0.5,0.5]}"
]
}
{{"nodeID":"7 weights:[46]prediction[0,0.0217,0.9783]"}
}
]
},
{
"name": "2 weights:[50] prediction[1,0,0]",
}
]
}
这让我创建了以下出色的d3.js
输出:
which lets me create the following wonderful d3.js
output:
背景:
我有以下代码可以创建Json输出,但不能退出我需要的代码:
I have the following code which creates a Json output, but not quit the code i need:
library(party)
irisct <- ctree(Species ~ .,data = iris)
plot(irisct)
get_ctree_parts <- function(x, ...)
{
UseMethod("get_ctree_parts")
}
get_ctree_parts.BinaryTree <- function(x, ...)
{
get_ctree_parts(attr(x, "tree"))
}
get_ctree_parts.SplittingNode <- function(x, ...)
{
with(
x,
list(
nodeID = nodeID,
left = get_ctree_parts(x$left),
right = get_ctree_parts(x$right)
)
)
}
get_ctree_parts.TerminalNode <- function(x, ...)
{
with(
x,
list(
nodeID = nodeID,
weights = sum(weights),
prediction = prediction
)
)
}
toJSON(get_ctree_parts(irisct))
我试图对其进行改造,但似乎我面临一些挑战,但我无法解决: 为了获得一个通用的解决方案,我需要一些递归函数,只要有一个分割(孩子),它就会创建一个嵌套的Json对象,而且,ctree具有与d3.js导航约定不同的导航约定,在ctree中的导航使用向右"和向左"产生,其中d3.js需要子级"导航"
I tried to transform it, but it seems like i have a few challenges, i just cant solve: in order to get a general solution, i need some recursive function, that creates a nested Json object, whenever there is a split (children), also, ctree have different navigation conventions from the d3.js navigation convention, where in ctree the navigation accrues using "right" and "left", where d3.js requires "children" navigation"
任何对此的帮助都将是很棒的,我在此停留了一段时间:)
Any help on this would be wonderful, i'm stuck on this for quit a while :)
推荐答案
我刚刚看完文章后才开始R编码.
hi i just started R coding after seeing post this is i achieved so far ..
library(party)
irisct <- ctree(Species ~ .,data = iris)
plot(irisct)
get_ctree_parts <- function(x, ...)
{
UseMethod("get_ctree_parts")
}
get_ctree_parts.BinaryTree <- function(x, ...)
{
get_ctree_parts(attr(x, "tree"))
}
get_ctree_parts.SplittingNode <- function(x, ...)
{
with(
x,
list(
name = toString(nodeID),
children = list(get_ctree_parts(x$left),get_ctree_parts(x$right))
)
)
}
get_ctree_parts.TerminalNode <- function(x, ...)
{
with(
x,
list(
name = paste(nodeID,"weights",sum(weights),"prediaction",toString(paste("[",toString(prediction),"]",sep=" ")),sep = " ")
)
)
}
toJSON(get_ctree_parts(irisct))
Json输出:
{"name":["1"],"children":[
{"name":["2 weights 50 prediaction [ 1, 0, 0 ]"]},
{"name":["3"],"children":[
{"name":["4"],"children":[
{"name":["5 weights 46 prediaction [ 0, 0.978260869565217, 0.0217391304347826 ]"]},
{"name":["6 weights 8 prediaction [ 0, 0.5, 0.5 ]"]}]
},
{"name":["7 weights 46 prediaction [ 0, 0.0217391304347826, 0.978260869565217 ]"]
}]
}]
}
如果您有任何问题评论,我会尽力而为.
If you have any problem comment i will try my best.
这篇关于将toJson R对象转换为适合d3.js树形布局的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!