Search code examples
javascriptrd3.jsrchartsdimple.js

Convert array of objects to csv-like format (d3)


I have modified a dimple JS script I found on the web to suit my particular needs, and it is currently working when I launch it as an HTML file. Here is my code:

http://pastebin.com/DDrG1w8X

Now I am trying to integrate this custom chart into R using rCharts, following this excelent tutorial by TimelyPortfolio.

My problem is the following: on the HTML version of the chart, I was loading the data I plotted using d3.csv. Now on R charts, when I pass on the data as r$set(data = item_series), where item_series is my dataframe that has the same format as the csv file, it gets transformed into an object with the following structure:

Object {scraper_name: Array[21], date: Array[21], items: Array[21]}

Since my script is made to work with the input data as d3.csv, I would like to get this object transformed back gain into a tabular format. How can I achieve this?

Hope the description is clear enough.


Solution

  • I found out that if I use:

    dataset = toJSONArray(item_series, json = F)
    

    then I can get rid of the d3.csv(...) function, and simply run the rest of the script using dataset as reference. To provide more details:

    On R:

    dataset = toJSONArray(item_series, json = F)
    r$set(data = dataset)
    

    On my html/js file:

    data = params.data;
    // Now there's no need to use d3.csv, we can process the data directly.
    // d3.csv(params.data, function (data) {
    var scraper_names = dimple.getUniqueValues(data, "scraper_name");
    // }