Consuming R from SAP Mobile Platform

November 9, 2012
By

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

Early this year, in March, I was visiting my team mates in SAP Labs Palo Alto, and my good friend a team mate Rui Nogueira asked to participate in his most excellent Technology Innovation Podcast show where we spoke about R and SAP HANA. By the end of the interview I said Rui that I was going to try to connect R and SUP (Which is now called SMP)...but actually...never did because my lack of time and specially and most important...because I didn't have a clue on how to do it...

So...yesterday, while I was reading the Steve Jobs book on my Kindle, I head a voice inside my head saying..."Dude! What the SAP? Where's R and SMP?"...at that moment...I knew I had to do something about it...

Again...I didn't have a clue on how to do it or how to really start working about it...but as I have already used Rook (R WebServer) in my blog RSAP, Rook and ERP and also Sinatra (Ruby WebServer) in my blog PowerBuilder and Gateway - The Sinatra style I knew, that was the way to go.

I knew that I needed to develop a Rook application to expose the data as JSON and pass it to SMP. Well...that failed quickly, because as far as I know and also my tests failed, SMP doesn't support JSON yet.

My next thought was to make the Rook application to expose the data as XML, which of course worked fine, but without using the standard XML library from R because the response is an C object that really looks bad when converted to a string.

First thing, was to think about a good example for this blog...but as they say, you have to teach a man how to fish...so I came up with a fairly simple example. Let's say you're a professor and my wife Milly, my daughter Kiara and myself are students. You have an Excel file where you will put the names and the grades and you want a mobile application that will read the file, calculate the means and provide the final score by simply passing the name of the student. I know...Excel? Why not SAP HANA? Well...you're an old fashion teacher...SAP HANA is so fast that you cannot even see it...so you stick to the most basic tools...

Of course, in R, we prefer to work with .CSV files, so being a good teacher, you create the file and give to us, so we can play with it.

To make thing simple for myself, I used my CloudShare.com account to start the work...I create my SMP application, called the Rook WebPage and test it on my BlackBerry emulator...everything worked like a charm...but...there's always one...the real thing came when I decided to move everything to AWS...

Thing is...and I didn't realize it in time...in CloudShare.com everything worked because both the SMP Server and the Rook Server are in the same place...the same localhost environment...in AWS, things change because the SMP Server is in the cloud while my Rook Server is in my localhost (laptop)...big problem...

I stayed yesterday working until 10:00 pm trying to figure out how to solve this big problem...maybe that's why I came up with a really solution...I said...Ok...let's move the Rook Server to the cloud as well! So I logged into my SMP Server, installed R and everything and run the Rook Server...back in my laptop...of course it failed miserably...the new Rook server was localhost but for my AWS server...so no way my Android emulator was going to be able to see it...

I said...Ok...don't panic...I have an R Server on AWS...let's do it there...another fail (getting used to it)...the R Server in AWS is headless, so no browser is allowed to work...also...Rook is always localhost, so there was no way to make the call...

I started to panic...so I went to sleep...at least for a while...

Today, I woke up at 5:30 am and start browsing hoping to see the light at the end of the tunnel...and I did...thank God...I did...

An amazing fella called Noah Lorang managed to make Rook work on Heroku...everything explain on his Github account...so my life was saved (not for long, sadly)...

I have never used Heroku before...and I gotta admit...is not for newbies...it really took me a long time to make it work...but I finally did it (Obviously...otherwise I wouldn't be boring you with all my senseless ranting)...

So...here's what I did...

• I create myself a Heroku account and installed the Heroku Tool Belt.
• I create myself a public key.
• Inside the Git Bash application installed by the Heroku Tool Belt I log myself in.

Clone and create application in Heroku
git clone git://github.com/noahhl/rookonheroku.git blagcodes
#(This will clone Noah's Github and create a folder called blagcodes
#to store the codes)heroku create blagrook
#(I create an application for my Rook script)git push heroku master
#(This allow me to pass everything from my blagcodes folder
#to my Github account)

With that, I was almost ready to rock...but I needed to do something else first...pass my own Rook script...

Summarize.R
library(Rook) newapp<-function(env){  req<-Rook::Request$new(env) res<-Rook::Response$new()   name_param = req$params()$name   Grades_Source = read.csv(file="Grades.csv",header=TRUE)  Name<-Grades_Source$Name Grades<-Grades_Source$Grades  Mean<-aggregate(Grades~Name,data=Grades_Source,FUN=mean)  Mean_Result<-c(subset(Mean,Name == name_param))   res$write("<root>") res$write("<Name>")  res$write(Mean_Result$Name)  res$write("</Name>") res$write("<Final_Grade>")  res$write(as.character(Mean_Result$Grade))  res$write("</Final_Grade>") res$write("</root>")   res$finish()} server = Rhttpd$new()server$add(app = newapp, name = "summarize")server$start(listen="0.0.0.0", port=as.numeric(Sys.getenv("PORT")))  while(T) {  Sys.sleep(10000)}

Despise the name, I actually made an easier thing a just grabbed the file called demo.R and replace it with my own source code. Also, I copied the Grades.csv file to my blagcodes folder (which is located in my laptop).

The code is simple, we create a Rook application called "summarize" that will read the Grades.csv file, aggregate it using the mean function and print a basic XML structure passing the Name and Grade of the person we're passing a parameter. We need to pass the listen="0.0.0.0" and the port=as.numeric(Sys.getenv("PORT")) so Heroku knows how to call the page.

Back into the Git Bash I did the following to pass my changes back to Heroku...

Passing back to Heroku
git add .git commit -am "message"git push heroku

With this, everything was set-up and ready...so let's see how it looks...

(I'm using IE just because I wanted to show that the response from the Rook Application might look different than an XML response, but by looking at the source code you can actually see that's is an XML...also, because IE doesn't try to melt the tabs when putting them together as Chrome does).

When I start developing the SMP application I realized that calling a WebService wasn't an option...as this is of course not a WebService...so instead I used a REST Web Service...but it was asking me for XSD structures...so after another long time...I find a nice on-line tool to do that...

I simply pass an XML structure and let the tool work for me...

Of course...I have never worked with XSD before...so I didn't knew what to expect...thing is...if you use this structure...is going to fail...first because of the Final_Grade being xs:byte and second because when I was loading the parameters, Final_Grade was showing as well...and I didn't want it...so I made a copy of the file, change a bit here and there and came with Request.txt and Response.txt as you can see here...

So...as I was telling you...I create a REST WebService...

After this...came another tricky part as I wasn't sure how to make the parameter worked for me...gladly...I manage to make it work...

I load up my Request.txt and get the Root element...

Then I repeat the same for Response.txt and ended up with this...

After I create my Mobile Application, I create a Personalization Key to keep track of the parameter.

This is the look and feel of the application. Something very simple, you are requested a name, you press Get Grades and the result will be shown in a list.

Now...we're ready to test the application...

I guess...it didn't went very well for me in the exams...let's see how my daughter went...

As expected! My daughter is both smarter and beautiful than me...

Well...that's all folks...it took around 24 hours (summing up yesterday and today) to get this thing working...but I can assure Mr. Nogueira that Blag always keep his promises...I promised to have R working on SMP...and here it is...

Greetings,

Blag.