Home > Blog > Simple charting in R

Simple charting in R

R is a popular choice in some circles, and we’ve had several requests for examples on using R with Xignite web services. Personally, I have not used R before but this was surprisingly easy to do with just a little research. I’ll give you the code and then a brief walk through of each section:

library(RCurl)
library(XML)

#call the web service
Result <- basicTextGatherer()
curlPerform(url = "http://www.xignite.com/xGlobalHistorical.xml/GetGlobalHistoricalQuotesRange?IdentifierType=Symbol&Identifier=RBS.XLON&AdjustmentMethod=SplitOnly&StartDate=8/22/2013&EndDate=8/21/2014&Header_Username=YOUR_TOKEN", writefunction = Result$update)
ResultXML = xmlRoot(xmlTreeParse(Result$value()))

#parse the result and draw the chart
DataPoints = xmlElementsByTagName(ResultXML, "Last", TRUE)
plot(unlist(DataPoints), type="o", col = "blue", ylim=c(0, 5), ann = FALSE, axes = FALSE)
title(main = "RBS.XLON 1 Year History", col = "Red", xlab = "Date", ylab = "Price")
axis(2, at=1*0:5)
box()

The first block to the library calls loads the necessary libraries: RCurl and XML. These were libraries that I had to add to the default R implementation, and just consisted of copying the folders into my R library folder.

The second block is where the web service is called. I chose to pull the data as XML and load it up that way for easy parsing.

In the third and final block is where the chart is actually drawn. I leveraged xmlElementsByTagName to go through the XML results and extract all the last data points. I then used plot to draw the actual chart and the next few lines to pretty it up, giving me the end result:

RBS.XLON 1 Year History

Obviously this could be taken further, making the size of the chart dynamic for example. Another improvement to make would be to utilize the “fields” parameter to lessen the data that comes back, as in:

 

So there wasn’t much to using Xignite within R, even though I had never written a line of R before. In fact, I ended up spending more more time playing around with the charting and the basics of R than I did gathering the data from Xignite.

__________

For more info on how Xignite's market data cloud can improve your business processes, request a consultation with one of our data experts.

To test drive our market data APIs and experience for yourself how easy they are to integrate into your applications, request a free trial.