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
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.
On 12/1/12 12:47 PM, Michael Friendly wrote:
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?
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
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