3 lines of R code to Process a Web Service

June 9, 2010
By

(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=JSON.parse(open('http://open.worldbank.org/topics?format=JSON').readlines.join)
a[1].each{|x|puts x['value']}

Can we do as well with R? Sure can!

library('XML')
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
Education
Energy & Mining
Environment
Financial sector
Health
Infrastructure
Labor & Social Protection
Poverty
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):

countries=c('NZL','AUS','SGP','USA')

x=lapply(countries, function(country) {
url=paste('http://open.worldbank.org/countries/',
country,
'/indicators/IC.REG.DURS?date=2008:2008',sep=''
)

doc = xmlTreeParse(url, useInternal = TRUE)
c(as.numeric(xmlValue(getNodeSet(doc,"//wb:value")[[1]])))
} )

pie(unlist(x),labels=countries)
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 his blog: R-Chart.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: 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: , ,

One Response to 3 lines of R code to Process a Web Service

  1. Rbloggers (R bloggers website) on June 9, 2010 at 8:25 pm

    Twitter Comment


    3 lines of R code to Process a Web Service: Ruby is well known for its terse syntax… [link to post] #rstats

    Posted using Chat Catcher