有没有办法在R控制台中漂亮地打印JSON?
cat()
观察到了新的行字符,但没有缩进。
JSON示例:
library(tidyverse)
library(jsonlite)
require(RJSONIO)
df <- data.frame(name=c("Holdingcompany","Holdingcompany","company1","company1","company2","company2"),children=c("company1","company2","company4","company3","company5","company6"),info=c("text1","text2","text3","text5","othertext","other_text"),percentage=c("100%","100%","60%","75%","80%","70%"))
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],Percentage=x[,2][y])})
}
}
# This provides unformatted JSON
makeList(df) %>% toJSON
# This shows new lines, but not indentation
makeList(df) %>% toJSON %>% cat