[R] reading json tables

Michael Friendly friendly at yorku.ca
Sat Dec 1 21:47:08 CET 2012


I'm trying to read two data sets in json format from a single .js file. 
I've tried fromJSON()
in both RJSONIOIO and RJSON packages, but they require that the lines be
pre-parsed somehow in ways I don't understand.  Can someone help?

 > wheat <- readLines("http://mbostock.github.com/protovis/ex/wheat.js")
 > str(wheat)
  chr [1:70] "var wheat = [" "  { year: 1565, wheat: 41, wages: 5 }," ...
 >

The wheat.js file looks like this and defines two tables:  wheat and 
monarch:

var wheat = [
   { year: 1565, wheat: 41, wages: 5 },
   { year: 1570, wheat: 45, wages: 5.05 },
   { year: 1575, wheat: 42, wages: 5.08 },
   { year: 1580, wheat: 49, wages: 5.12 },
   { year: 1585, wheat: 41.5, wages: 5.15 },
   { year: 1590, wheat: 47, wages: 5.25 },
   { year: 1595, wheat: 64, wages: 5.54 },
   { year: 1600, wheat: 27, wages: 5.61 },
   { year: 1605, wheat: 33, wages: 5.69 },
   { year: 1610, wheat: 32, wages: 5.78 },
   { year: 1615, wheat: 33, wages: 5.94 },
   { year: 1620, wheat: 35, wages: 6.01 },
...
   { year: 1800, wheat: 79, wages: 28.5 },
   { year: 1805, wheat: 81, wages: 29.5 },
   { year: 1810, wheat: 99, wages: 30 },
   { year: 1815, wheat: 78 }, // TODO
   { year: 1820, wheat: 54 },
   { year: 1821, wheat: 54 }
];

var monarch = [
   { name: "Elizabeth", start: 1565, end: 1603 },
   { name: "James I", start: 1603, end: 1625 },
   { name: "Charles I", start: 1625, end: 1649 },
   { name: "Cromwell", start: 1649, end: 1660, commonwealth: true },
   { name: "Charles II", start: 1660, end: 1685 },
   { name: "James II", start: 1685, end: 1689 },
   { name: "W&M", start: 1689, end: 1702 },
   { name: "Anne", start: 1702, end: 1714 },
   { name: "George I", start: 1714, end: 1727 },
   { name: "George II", start: 1727, end: 1760 },
   { name: "George III", start: 1760, end: 1820 },
   { name: "George IV", start: 1820, end: 1821 }
];

-- 
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University      Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA



More information about the R-help mailing list