viernes, 6 de septiembre de 2013

Running R inside SAP HANA Studio

*DANGER* This is just a silly blog and shouldn't be taken any seriously.*DANGER*

Ok...you may wonder...why is Blag writing a silly blog? Well...I'm a Developer Evangelist...and it's my job to keep developers entertained  Also...I have so free time to spare

As you all may know...SAP HANA Studio runs on top of Eclipse...which means that if we can run R on Eclipse...then we can obviously run R on SAP HANA Studio...that's exactly what we're going to do

As easy as it gets...follows this instructions...


  • Go to Help --> Install New Software
  • Put the following repository

  • Choose Walware - StatET



  • Once installed, your SAP HANA Studio will restart and you will need to configure R.
  • Go to Windows --> Preferences.
  • StatET --> Run/Debug --> R Environments --> Add (In my picture, I'm using Edit as I already add it).



  • Either on the Command Line or in RStudio, install the following...
  • install.packages(c("rj", "rj.gd"), repos="http://download.walware.de/rj-1.1")
  • Also install RJava as well...install.packages("RJava")

 Now...you need to start the R console...so...
  • Go to the little Green Arrow button and press "R_Console".


Once that it's done, we can go ahead and create our R script...
  • Go to File --> New Project --> R Project.
  • On the created R Project, right click and select New --> R Script File.



Copy this code inside your new file...

R_from_SAP_HANA.sql
result<-read.table('clipboard',header=TRUE,sep=';')
result<-subset(result, !duplicated(DISTANCE))
flights<-data.frame(DISTANCE=as.numeric(result$DISTANCE))
row.names(flights)<-result$FLIGHT
d<-dist(as.matrix(flights))
hc<-hclust(d)
plot(hc)

Now...go to the "Modeler" perspective...open a SQL Console and write the following...

SAP_HANA_Select.sql
SELECT DISTINCT CITYFROM || '/' || CITYTO "FLIGHT", DISTANCE
FROM SFLIGHT.SPFLI
WHERE DISTANCE > 0
  AND CARRID = 'SQ'

Run it...and here's come the funny part...go to the Result tab and select all records...then press Copy Rows, so the selection gets copied into the Clipboard.


Go back to the StatET perspective and click the "Run file in R via command" button...



When you run the code...you're not going to see any response, unless there's an error...so just go to the R Graphics tab which is at the bottom where the Console window seats...expand it...and you will see a Cluster Dendogram graphic generated -;)


I told you...this was a very silly post...but at least...we never left the SAP HANA Studio -:P

See you next time!

Blag.
Developer Experience.

2 comentarios:

Unknown dijo...

Hi,
I have a query on the connectivity to R from Statet-
StatET --> Run/Debug --> R Environments --> Add (In my picture, I'm using Edit as I already add it).==>
here is it possible to connect to a R server using remote access?

I have a hana server with Rintegration setup successfully.
Is it possible to run .R file from HANA directly?
may be a dumb question to be asked as I am just starting with how R could be made use of in Hana environment?

Kindly advise.
Regards,
Savvy

Alvaro "Blag" Tejada Galindo dijo...

Dear Savvy:

Please read my post "When SAP HANA met R - First kiss" http://blagrants.blogspot.com/2012/05/when-sap-hana-met-r-first-kiss.html

When you do an SAP HANA --> R Server connection using the RServe package...you actually do R coding inside SAP HANA Studio...those commands will go to the R Server via RServe, where they will be processed and then send back to SAP HANA Studio for further interaction...

Greetings,

Blag.
Development Culture.