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 we start the discussion and collaboration on how to build the extension for Windows 64 bits.

I gotta wasn't easy...Piers doesn't have a Windows I needed to test everything...but I don't have previous R extension building 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 and download the latest NWRFCSDK library.
  • Download the RSAP source code from Pier's Github
  • 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 (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 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


I'm not really sure, if that's enough to have RSAP installed and ready to 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).

conn = RSAPConnect("sap.yml")
parms<-list('DELIMITER' = ';',
               'FIELDS' = list(FIELDNAME = list('CARRNAME', 'FORCURAM')),
               'QUERY_TABLE' = 'ZSBOOK')
res<-RSAPInvoke(conn, "RFC_READ_TABLE", parms)
flds<-sub("\\s+$", "", res$FIELDS$FIELDNAME)
sbook<-data.frame(colsplit(sbook$WA,";", names=flds))
sbook_agg=aggregate(FORCURAM ~ CARRNAME, data=sbook, FUN=sum)
for(i in 1:n) new_array[i]<-carrname[new_vect[i]]

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 Linux and Windows users can enjoy RSAP! -:D