1
我正在構建了stacked column chart並試圖找出改造我的CSV數據最徹底的方法:我怎樣才能把這個csv轉換成D3-ready的json?
data <- "apples oranges grapes
5 10 22
4 12 28
2 19 32
7 23 35
23 17 43"
fruits <- read.table(text=data, header=TRUE)
到多維JSON:
var dataset = [
//apples
[
{ x: 0, y: 5 },
{ x: 1, y: 4 },
{ x: 2, y: 2 },
{ x: 3, y: 7 },
{ x: 4, y: 23 }
], //oranges
[
{ x: 0, y: 10 },
{ x: 1, y: 12 },
{ x: 2, y: 19 },
{ x: 3, y: 23 },
{ x: 4, y: 17 }
], // grapes
[
{ x: 0, y: 22 },
{ x: 1, y: 28 },
{ x: 2, y: 32 },
{ x: 3, y: 35 },
{ x: 4, y: 43 }
]
];
我可以使用rson
「 s toJSON
得到一個基本的陣列:
{
"apples":[5,4,2,7,23],
"oranges":[10,12,19,23,17],
"grapes":[22,28,32,35,43]
}
但我遇到了麻煩繞包裝如何我會將其轉換爲我實際需要的格式。如果更直接,我很樂意進行轉換,但我想弄清楚如何在R中生成它。