Site icon R-bloggers

Github Follower Graph with R

[This article was first published on R-Chart, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Graph a github user’s followers (and follower’s followers).



Each programming language tends to develop its own idiomatic set of data structures.  In R, data frames are often the structure of choice.  JSON (a subset of Javascript) has emerged as an open standard for data interchange that has largely displaced XML for many web based APIs.   This post gives an example of how to read JSON from GitHub, manipulate the data within R and produce a graph of the results (like the one above).

A few standard R libraries are required:

In this particular API call (an example of json returned – no API key is required for the Github API), the JSON data will represent a GitHub user’s (first thirty) followers.  This implies the use of a graph to represent the data so the igraph library will be used as well. 




With the RCurl and rjson libraries available, the json results can be retrieved and converted to an R list as follows:

o <- fromJSON(getURL(‘https://api.github.com/users/EzGraphs/followers’))





You can check the class for yourself using class(o) and view the length of the list using length(o) 
   
To convert the results to the data frame where rows represent followers and columns represent attributes, unlist the results, transpose the results and cast as a data frame:

df <- as.data.frame(t(sapply(o, unlist)))



That is the basic process – the rest of the code is the details related to getting the data into the iGraph object which can then be rendered using plot, tkplot (show above) or rglplot. 

To leave a comment for the author, please follow the link and comment on their blog: R-Chart.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.