データ フレームを json 形式に変換する必要があります。データ フレームは次のようになります。
dput(head(yyy,30))
structure(list(name = c("serverA", "serverA", "serverA", "serverA",
"serverA", "serverA", "serverA", "serverA", "serverA", "serverA",
"serverA", "serverA", "serverA", "serverA", "serverA", "serverA",
"serverA", "serverA", "serverA", "serverA", "serverA", "serverA",
"serverA", "serverA", "serverA", "serverA", "serverA", "serverA",
"serverA", "serverA"), date = structure(c(1374120000, 1374120060,
1374120120, 1374120180, 1374120360, 1374120420, 1374120540, 1374120600,
1374120840, 1374120960, 1374121020, 1374121080, 1374121200, 1374121440,
1374121500, 1374121620, 1374121680, 1374122040, 1374122160, 1374122280,
1374122400, 1374122580, 1374122640, 1374122700, 1374122940, 1374123000,
1374123120, 1374123180, 1374123240, 1374123360), class = c("POSIXct",
"POSIXt"), tzone = "America/New_York"), resp = c(3644, 1067.5,
2738, 5224, 561, 723, 522, 408.5, 446, 683.75, 521, 385, 2666.5,
1268, 701, 143, 645, 474, 670.5, 549, 383, 1381, 483, 516, 467.5,
10726, 931.5, 773, 778, 323), vol = c(1L, 2L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 4L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L)), .Names = c("name", "date",
"resp", "vol"), row.names = c(1L, 3L, 7L, 8L, 13L, 17L, 21L,
25L, 33L, 39L, 42L, 44L, 48L, 59L, 63L, 68L, 71L, 83L, 88L, 91L,
98L, 105L, 109L, 113L, 122L, 123L, 129L, 132L, 135L, 140L), class = "data.frame")
私のjsonは次のようにする必要があります:
最初の部分には日付と resp が含まれ、2 番目の部分には JSON 形式の日付と vol が含まれる必要があります。
[{"name":"serverA","yAxis":1, "data":[[<date>,<resp>],[<date>,<resp>]]},{"name":"serverA","yAxis":2, "data":[[<date>,<vol>],[<date>,<vol>]]}]
json 出力は次のようにする必要があります。
[{"name":"serverA","yAxis":1, "data":[[1374105840000,27.395],[1374107640000,26.646]]},{"name":"serverA","yAxis":2, "data":[[1374105840000,25.983],[1374107640000,22.724]]}]
データ フレームに resp のみが含まれている場合は、次のように変換できます。
servers <- split(yyy, yyy$name)
dumFun <- function(x){
sData <- servers[x][[1]]
if(nrow(sData) >0){
# create appropriate list
dumList <- unname(apply(sData[,2:3], 1, function(y) unname(as.list(y))))
return(toJSON(list(name = x, data = dumList)))
}
}
jsData <- lapply(names(servers), dumFun)
jsInd <- sapply(jsData, is.null)
p<-paste0('[', paste(jsData[!jsInd], collapse = ','), ']')
Rでこれを行う簡単な方法はありますか