viernes, 22 de junio de 2012

Analytics with SAP and R (Windows version)


My good friend and programming guru Piers Harding wrote a blog called Analytics with SAP and R where he showed us how to link the wonderful worlds of R and SAP. Yes...SAP...not SAP HANA...but the good old NetWeaver...

Piers build the RSAP extension using Linux...but I'm a Windows user...so we start the discussion and collaboration on how to build the extension for Windows 64 bits.

I gotta say...it wasn't easy...Piers doesn't have a Windows machine...so I needed to test everything...but I don't have previous R extension building experience...so after a lot of hard work from both sides, I'm happy to say that we make it work

Here are the steps to follow...


  • Install the following packages on RStudio: yaml, reshape and RUnit.
  • Go to this thread http://scn.sap.com/thread/950318 and download the latest NWRFCSDK library.
  • Download the RSAP source code from Pier's Github https://github.com/piersharding/RSAP
  • Download RTools for your R installation version
  • Inside the /src folder copy all the content from the following folders:
    • /include from your R installation
    • /include from your nwrfcsdk folder
    • /lib from your nwrfcsdk folder
  • Change the file Makevars.win (located in RSAP/src) and use this line:
    • PKG_LIBS=sapnwrfc.dll libsapucum.dll

Make sure that you're using the right R for the NWRFCSDK folder...R 64bit for NWRFCSDK 64bits. You can check this is you go to Environment Variables --> Path and look for something like this C:\Program Files\R\R-2.15.0\bin\x64 (x32 or x64)

With that ready, we can open a CMD session and write the following:

C:\> R CMD INSTALL --build --preclean --clean --no-multiarch -l C:/RSAP RSAP

You're going to sure to receive some warnings, but no worries...it should be fine as long as you see these lines...


installing to C:/RSAP/RSAP/libs/x64
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* MD5 sums
packaged installation of 'RSAP' as RSAP_0.03.zip


* DONE (RSAP)

I'm not really sure, if that's enough to have RSAP installed and ready to work...so for the sake of completion you can do this...


  • Go to your RSAP folder...and .zip the RSAP folder that it's inside.
  • Go to your RStudio and select Tools --> Install Packages --> Choose (.zip) --> Browse your .zip and press Install.

Now, we're ready to rock...just like Piers did, I'm going to use a .yml file to host my connection parameters:

ashost: "X.X.X.X"
sysnr: "00"
client: "520"
user: "idadmin"
passwd: "XXXXXXXX"
lang: EN
trace: 1
lcheck: 1
loglevel: warn

And here's the source code to our example...(For this example you need the libraries: wordcloud and tm).

library("RSAP")
library("tm")
library("wordcloud")
setwd("C:/Blag/R_Scripts")
 
 
conn = RSAPConnect("sap.yml")
parms<-list('DELIMITER' = ';',
               'FIELDS' = list(FIELDNAME = list('CARRNAME', 'FORCURAM')),
               'QUERY_TABLE' = 'ZSBOOK')
res<-RSAPInvoke(conn, "RFC_READ_TABLE", parms)
RSAPClose(conn)
 
 
sbook<-res$DATA
flds<-sub("\\s+$", "", res$FIELDS$FIELDNAME)
sbook<-data.frame(colsplit(sbook$WA,";", names=flds))
sbook_agg=aggregate(FORCURAM ~ CARRNAME, data=sbook, FUN=sum)
vect<-cbind(sbook_agg$CARRNAME,sbook_agg$FORCURAM)
new_vect<-vect[order(vect[,2]),]
new_vect<-c(new_vect[,1])
n<-length(new_vect)
new_array<-c(n,1)
carrname<-levels(sbook_agg$CARRNAME)
for(i in 1:n) new_array[i]<-carrname[new_vect[i]]
d<-data.frame(word=new_array,freq=new_vect)

To make the graphic easier to read, I add the PDF option, so a PDF would get generated.


Hope you liked this blog...Piers and I put a big effort and the rewards are awesome...now Linux and Windows users can enjoy RSAP! -:D