Hi,
I've tried working with Rstudio 1.2 and enjoyed D3.js integration in R notebooks. There is however a behavior of R to JSON conversion performed by r2d3 package that I'd like to discuss.
in pure D3 I'd read data from *.csv file with d3.csv()
function and the resulting JSON object would look like:
[ { V1: value-1, V2: value-1, V3: value-1, ... Vn: value-1 },
{ V1: value-2, V2: value-2, V3: value-2, ... Vn: value-2 },
.......................................................... ]
Which is an array of objects where each object holds a row of original csv table. This is a format expected by D3.
When I read csv file in R and then pass R object to D3 chunk in R notebooks:
dataset <- read.csv(file)
{ d3 data=dataset }
#markdown with R object passed to D3 chunk
It is converted to JSON as presented above. However, if I have several csv files:
dataset <- list()
dataset[[file1]] <- read.csv(file1)
dataset[[file2]] <- read.csv(file2)
dataset[[file3]] <- read.csv(file3)
this list of data frames will be converted in a different JSON layout:
{ file1 : {
V1 : [value1, value2 ... valueN],
V2 : [value1, value2 ... valueN],
.....
Vn : [value1, value2 ... valueN]
},
file2 : {
V1 : [value1, value2 ... valueN],
V2 : [value1, value2 ... valueN],
...
Vn : [value1, value2 ... valueN]
}, ...
}
Therefore if I take a subset data['file1']
it will not have the json representation expected by D3. Interestingly, R package jsonlite converts list of dataframe properly dataset <- toJSON(dataset)
{ file1: [
{ V1: value-1, V2: value-1, V3: value-1, ... Vn: value-1 },
{ V1: value-2, V2: value-2, V3: value-2, ... Vn: value-2 },
........................................................
],
file2: [
{ V1: value-1, V2: value-1, V3: value-1, ... Vn: value-1 },
{ V1: value-2, V2: value-2, V3: value-2, ... Vn: value-2 },
........................................................
]
}
Subsetting data['file1'] gives the same result as reading a single csv file and converting it to JSON.
Does r2d3 uses its own R to JSON conversion engine? What are other differences between r2d3 and jsonlite?
Thanks,