Search code examples
javascriptrgisarcgis-js-api

Convert R data.frame to Javascript array


I want to save some columns of my data frame into a specific format (JavaScript format). I've tried to use toJSON() from rjson package but that doesn't work.

My result should looks like : http://leaflet.github.io/Leaflet.markercluster/example/realworld.388.js


Solution

  • The fastest way to do it is going to use one of the apply functions. It just so happens the best one I found was apply itself.

    # this will go row by row
    apply(allTheData, 1, function(x){
        print(x["COL_NAME"])
    })
    

    You can't use x$COL_NAME in apply so you have to use the way I did above.

    You could use other apply functions, but to go row by row I found this one the easiest to apply.