[R] reading json tables

Michael Friendly friendly at yorku.ca
Sun Dec 2 15:26:08 CET 2012


On 12/1/2012 4:08 PM, Duncan Temple Lang wrote:
> Hi Michael
>
>    The problem is that the content of the .js file is not JSON,
> but actual JavaScript code.
>
> You could use something like the following
>
> tt = readLines("http://mbostock.github.com/protovis/ex/wheat.js")
>
> txt = c("[", gsub(";", ",", gsub("var [a-zA-Z]+ = ", "", tt)), "]")
> tmp = paste(txt, collapse = "\n")
> tmp = gsub("([a-zA-Z]+):", '"\\1":', tmp)
> o = fromJSON(tmp)
> data = structure(o[1:2], names = c("wheat", "monarch"))
>
> Basically, this
>      removes the 'var <variable name> =' part
>      replaces the ; with a , to separate elements
>      quotes the names of the fields, e.g. year, wheat, wages
>      puts the two global data objects into a top-level array ([]) container
>
> This isn't ideal (as the regular expressions are not sufficiently specific
> and could modify the actual values incorrectly). However, it does the job
> for this particular file.

Thanks for this, Duncan

I hadn't understood that the data had to be pure JSON.

The actual result I want is two data frames, wheat and monarch, whereas 
fromJSON returns a list of lists.  I'll try to figure that part out.

-Michael

-- 
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