[R] reading json tables

Duncan Temple Lang dtemplelang at ucdavis.edu
Sun Dec 2 15:36:58 CET 2012


Hi Michael

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

 do.call(rbind, data[[1]])

will do the job, but there are elements in each of data[[1]] and data[[2]]
that are incomplete and which need to be filled in with NAs before rbinding.

Best,
  D.

On 12/2/12 6:26 AM, Michael Friendly wrote:
> 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
>




More information about the R-help mailing list