Site icon R-bloggers

When R met SAP Gateway

[This article was first published on Blag's bag of rants, 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.

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!

To leave a comment for the author, please follow the link and comment on their blog: Blag's bag of rants.

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.