As some of you may have noticed, I've been playing around with JSON and R lately. I'm also trying to learn jQuery, which is a powerful JavaScript library (with its own syntax) that makes making dynamic and interactive web pages much easier to handle. In addition, it makes handling JSON data transports much easier.
In that respect, I was looking at the examples on the getJSON help page.
You can literally copy-paste the following into a notepad and save it as "whatever.html". View that web document, and it should show you a series of four images drawn from the flicker API.
Now, if you want to see the data set the JSON request is working on, you need only look at the GET request of which it consists.
As you can see, all we did was take the 3 API parameters tags, tagmode, and format, as named value equal pairs and append them to the URL with ampersand separators. It returns a big JSON string. Nicely, it comes with tab and newline formatting.
That is the JSON string that is being read by the jQuery and used in constructing the HTML document. Anyone with some HTML experience should recognize what is going on, though a little documentation about the jQuery operations might help. Basically, you're appending the content requested to the named division as image tags based on the URL provided within the JSON elements specified. It uses a "for each" loop, which is similar to a Python loop over a list or an R loop over a vector of values.
Anyway, this is where we come to "how can I get this JSON into R?"
I've been trying to make this happen. With the RCurl library, one can easily read it in directly as above
You'll see (say, write it to the "clipboard" and paste it into notepad or something) that we get the same result as the link above. Here is a snippet.
The problem now is to get this string interpreted. I can't seem to get that to work. I've tried other approaches from the RCurl library. I either get a connection problem or an empty list (if I wrap the return string in textConnection or set the parameter asText to TRUE). My aim here was merely to get it into an R list that I could then drill down and grab the URLs to the images or something. Importing this using fromJSON from the RJSONIO package has failed.
Any ideas? Anyone interested?
In that respect, I was looking at the examples on the getJSON help page.
You can literally copy-paste the following into a notepad and save it as "whatever.html". View that web document, and it should show you a series of four images drawn from the flicker API.
Code:
<!DOCTYPE html>
<html>
<head>
<style>img{ height: 100px; float: left; }</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div id="images">
</div>
<script>
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
{
tags: "cat",
tagmode: "any",
format: "json"
},
function(data) {
$.each(data.items, function(i,item){
$("<img/>").attr("src", item.media.m).appendTo("#images");
if ( i == 3 ) return false;
});
});</script>
</body>
</html>
http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json
As you can see, all we did was take the 3 API parameters tags, tagmode, and format, as named value equal pairs and append them to the URL with ampersand separators. It returns a big JSON string. Nicely, it comes with tab and newline formatting.
That is the JSON string that is being read by the jQuery and used in constructing the HTML document. Anyone with some HTML experience should recognize what is going on, though a little documentation about the jQuery operations might help. Basically, you're appending the content requested to the named division as image tags based on the URL provided within the JSON elements specified. It uses a "for each" loop, which is similar to a Python loop over a list or an R loop over a vector of values.
Anyway, this is where we come to "how can I get this JSON into R?"
I've been trying to make this happen. With the RCurl library, one can easily read it in directly as above
Code:
getURL("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?tags=cat&tagmode=any&format=json")
Code:
({
"title": "Uploads from everyone",
"link": "http://www.flickr.com/photos/",
"description": "",
"modified": "2012-01-24T23:20:03Z",
"generator": "http://www.flickr.com/",
"items": [
{
"title": "IMG_4045",
"link": "http://www.flickr.com/photos/27674175@N05/6757372941/",
"media": {"m":"http://farm8.staticflickr.com/7006/6757372941_ab7c61426e_m.jpg"},
"date_taken": "2012-01-14T15:25:31-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/27674175@N05/\">Filipe Vieira Moniz<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/27674175@N05/6757372941/\" title=\"IMG_4045\"><img src=\"http://farm8.staticflickr.com/7006/6757372941_ab7c61426e_m.jpg\" width=\"160\" height=\"240\" alt=\"IMG_4045\" /><\/a><\/p> ",
"published": "2012-01-24T23:20:03Z",
"author": "nobody@flickr.com (Filipe Vieira Moniz)",
"author_id": "27674175@N05",
"tags": ""
},
{
"title": "_MG_9870",
"link": "http://www.flickr.com/photos/talesofvesper/6757373121/",
"media": {"m":"http://farm8.staticflickr.com/7015/6757373121_f9769d6f40_m.jpg"},
"date_taken": "2012-01-24T01:02:03-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/talesofvesper/\">Mist and Lilies<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/talesofvesper/6757373121/\" title=\"_MG_9870\"><img src=\"http://farm8.staticflickr.com/7015/6757373121_f9769d6f40_m.jpg\" width=\"240\" height=\"160\" alt=\"_MG_9870\" /><\/a><\/p> ",
"published": "2012-01-24T23:20:06Z",
"author": "nobody@flickr.com (Mist and Lilies)",
"author_id": "47130918@N05",
"tags": ""
},
{
"title": "IMG_1575",
"link": "http://www.flickr.com/photos/jaimecuartas/6757373177/",
"media": {"m":"http://farm8.staticflickr.com/7014/6757373177_33b5e59a21_m.jpg"},
"date_taken": "2012-01-21T14:41:16-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/jaimecuartas/\">Jaime Cuartas<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/jaimecuartas/6757373177/\" title=\"IMG_1575\"><img src=\"http://farm8.staticflickr.com/7014/6757373177_33b5e59a21_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_1575\" /><\/a><\/p> ",
"published": "2012-01-24T23:20:07Z",
"author": "nobody@flickr.com (Jaime Cuartas)",
"author_id": "58788327@N05",
"tags": ""
},
Any ideas? Anyone interested?