3 lines of R code to Process a Web Service
[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.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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 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.