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

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to