3 lines of R code to Process a Web Service

June 9, 2010

(This article was first published on R-Chart, and kindly contributed to R-bloggers)

Ruby is well known for its terse syntax and ability to process web services. I prefer JSON (Javascript Object Notation) to XML whenever possible. For example, a script to retrieve a list of World Bank Data takes all of three lines of code (after installing the required packages):
[‘rubygems’,’JSON’,’open-uri’].each{|r|require r}
a[1].each{|x|puts x[‘value’]}
Can we do as well with R? Sure can!
doc = xmlTreeParse(‘http://open.worldbank.org/topics/’, useInternal = TRUE)
sapply(getNodeSet(doc, “//wb:value”) , function(el) xmlValue(el))
About the World Bank web service…
The World Bank opened up access to its data earlier this year. (This is a great opportunity for application developers who can use this data to produce applications that foster greater transparency on the part of national governments). The Developer API overview is the primary source for documentation. Data is categorized by topics which cover a relatively large area of human interest. Since there are only a few topics, this is a good place in the API to do some initial tests. The following R Script parses XML and returns the topics as a list.

In either case, the list of topics returned is follows:
Agriculture & Rural Development
Aid Effectiveness
Economic Policy and External Debt
Energy & Mining
Financial sector
Labor & Social Protection
Private Sector
Public Sector
Science & Technology
Social Development
Urban Development
For instance, if you wanted to alert your friends in New Zealand and Australia of the excellent opportunity they had to be first to market vs. their competition in Singapore and America who are starting businesses (based upon 2008 data):

x=lapply(countries, function(country) {

doc = xmlTreeParse(url, useInternal = TRUE)
} )

title(‘Time Required To Start a Business (Days)’)

This produces the chart listed at the top of this post.

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)