viernes, 20 de abril de 2012

When R met SAP Gateway


A couple of days ago, I was toying with the idea of doing something with SAP Gateway...I thought of using SUP, but as I recently wrote 2 blogs about it, I decided to go back to one of my favorite programming languages...R...

So...Gateway can be consumed as ODATA and JSON (If I'm not wrong) is not fully supported, so my only option was ODATA...but R doesn't support ODATA...so I was facing a real problem...

I was wrong -:P ODATA is the protocol and JSON and XML are formats...Gateway generates XML right now and JSON is coming soon...special thanks to Wayne Brown (Solution Owner, SAP NetWeaver Gateway)

Gladly, R is one of those programming languages that comes to the rescue when you less expect them...by browsing around I found a library to do Web Scrapping...which means that basically I will read the whole XML generated by SAP Gateway, parse it to extract the information I need and the generate a nice graphic to impress someone...BTW...I'm filtering to get only the flights of 'AA', as getting the full list of 1,304 records will make it really hard for R to handle as R doesn't really shine when it comes to processing speed...also parsing that huge XML is a little stressful for anyone...

First, I create a simple SAP Gateway service based on BAPI_FLIGHT_GETLIST. Here's the resulting XML:

http://lscies1.sapdevcenter.com/sap/opu/sdata/sap/ZFLIGHT_MODEL/zflightCollection?$filter=airline_1 EQ 'QF'&sap-client=520&$format=xml


Then, I did some nice lines of code in R Studio. Basically, it looks for the line that contains the "cityto" tag and then extract everything else that is not, in order to extract only the name of the city. Then we simply, sort the values, sum them and generate the graphic.

library("plotrix")
 
web_page <- readLines("http://lscies1.sapdevcenter.com/sap/opu/sdata/sap/ZFLIGHT_MODEL/zflightCollection?$filter=airline_1 EQ 'AA'&sap-client=520&$format=xml")
 
mypattern = '([^<]*)'
datalines = grep(mypattern,web_page,value=TRUE)
getexpr = function(s,g)substring(s,g,g+attr(g,'match.length')-1)
g_list = gregexpr(mypattern,datalines)
matches = mapply(getexpr,datalines,g_list)
result = gsub(mypattern,'\\1',matches)
names(result) = NULL
airlines <- sort(table(result), decreasing = TRUE)
lbls <- names(airlines)
 
pie3D(airlines,labels=lbls,explode=0.1,
      main="American Airlines")

And here's the result...


See you next time!

No hay comentarios: