When R met SAP Gateway

(This article was first published on Blag's bag of rants, and kindly contributed to R-bloggers)

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.

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)

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 on topics such as: Data science, Big Data, R jobs, 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...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)