参差不齐的列表或数据框到JSON [英] Ragged list or data frame to JSON

查看:93
本文介绍了参差不齐的列表或数据框到JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在R中创建一个参差不齐的列表,该列表对应于

I am trying to create a ragged list in R that corresponds to the D3 tree structure of flare.json. My data is in a data.frame:

path <- data.frame(P1=c("direct","direct","organic","direct"),
P2=c("direct","direct","end","end"),
P3=c("direct","organic","",""),
P4=c("end","end","",""), size=c(5,12,23,45))

path
       P1     P2      P3  P4 size
1  direct direct  direct end    5
2  direct direct organic end   12
3 organic    end               23
4  direct    end               45

,但也可以是列表,也可以根据需要进行调整:

but it could also be a list or reshaped if necessary:

path <- list()
path[[1]] <- list(name=c("direct","direct","direct","end"),size=5)
path[[2]] <- list(name=c("direct","direct","organic","end"), size=12)
path[[3]] <- list(name=c("organic", "end"), size=23)
path[[4]] <- list(name=c("direct", "end"), size=45)

所需的输出是:

rl <- list()
rl <- list(name="root", children=list())
rl$children[1] <- list(list(name="direct", children=list()))
rl$children[[1]]$children[1] <- list(list(name="direct", children=list()))
rl$children[[1]]$children[[1]]$children[1] <- list(list(name="direct", children=list()))
rl$children[[1]]$children[[1]]$children[[1]]$children[1] <- list(list(name="end", size=5))

rl$children[[1]]$children[[1]]$children[2] <- list(list(name="organic", children=list()))
rl$children[[1]]$children[[1]]$children[[2]]$children[1] <- list(list(name="end",    size=12))

rl$children[[1]]$children[2] <- list(list(name="end", size=23))

rl$children[2] = list(list(name="organic", children=list()))
rl$children[[2]]$children[1] <- list(list(name="end", size=45))

所以当我打印到json时:

So when I print to json it's:

require(RJSONIO)
cat(toJSON(rl, pretty=T))

 {
"name" : "root",
"children" : [
    {
        "name" : "direct",
        "children" : [
            {
                "name" : "direct",
                "children" : [
                    {
                        "name" : "direct",
                        "children" : [
                            {
                                "name" : "end",
                                "size" : 5
                            }
                        ]
                    },
                    {
                        "name" : "organic",
                        "children" : [
                            {
                                "name" : "end",
                                "size" : 12
                            }
                        ]
                    }
                ]
            },
            {
                "name" : "end",
                "size" : 23
            }
        ]
    },
    {
        "name" : "organic",
        "children" : [
            {
                "name" : "end",
                "size" : 45
            }
        ]
    }
]
}

我很难集中精力在R中创建此列表结构所必需的递归步骤.在 JS 中,我可以轻松地在节点周围移动,并在每个节点处确定是否添加新节点或根据需要使用 push 继续向下移动树,例如:new = {"name": node, "children": []};new = {"name": node, "size": size};,如此示例:

I am having a hard time wrapping my head around the recursive steps that are necessary to create this list structure in R. In JS I can pretty easily move around the nodes and at each node determine whether to add a new node or keep moving down the tree by using push as needed, eg: new = {"name": node, "children": []}; or new = {"name": node, "size": size}; as in this example. I tried to split the data.frame as in this example:

 makeList<-function(x){
   if(ncol(x)>2){
      listSplit<-split(x,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],size=x[,2][y])})
   }
 }

 jsonOut<-toJSON(list(name="root",children=makeList(path)))

但这给我一个错误

 Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
 Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?

推荐答案

链接的问答集中给出的功能本质上是您所需要的,但是由于后面几列中某些行的空值,它在数据集上失败了.您不仅需要盲目地重复递归直到您用完所有列,还需要检查"end"值,然后使用该值来切换到创建叶子:

The function given in the linked Q&A is essentially what you need, however it was failing on your data set because of the null values for some rows in the later columns. Instead of just blindly repeating the recursion until you run out of columns, you need to check for your "end" value, and use that to switch to making leaves:

makeList<-function(x){
    listSplit<-split(x[-1],x[1], drop=TRUE);
    lapply(names(listSplit),function(y){
        if (y == "end") { 
            l <- list();
            rows = listSplit[[y]];
            for(i in 1:nrow(rows) ) {
               l <- c(l, list(name=y, size=rows[i,"size"] ) );
            }
            l;

       }
        else {
             list(name=y,children=makeList(listSplit[[y]]))
        }
    });
}

这篇关于参差不齐的列表或数据框到JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆