Install R Language and Integrate it With SAP HANA
We have a Chinese version of this blog.
1 R language introduce
R language is is an GNU project which based on S language, it can be treated as an implementation of S language. It is firstly developed by Ross lhaka and Robert Gentleman in the University of Auckland, New Zealand, it mainly used for statistic computing, graphing, and data mining.
Since SAP HANA SP5, it is has been enforced greatly with the integrate of memory computing and the statistic function of R language. This enables you use R as a procedure language, and call the function of R. The data exchange between SAP HANA and R is very efficient, because they all use the column storage style. The communication process between SAP HANA and R is shown below:
In order to execute R code in SAP HANA, the R code is writen as a procedure with RLANG. It is executed
by the external Rserve.For the support of R operator, The calculate engine inside SAP HANA has been extended, for the given input object, after some computing , output with a result table. Differently with the local operator, R operator can be processed by R function, when the calculate engine recognized the R operator, the R client will send a request to the Rserve, and at the same time send the parameters needed by the program, then it will begin to execute, and result data frame will be send back to the calculate engine.
currently, there are some restrictions:
1. In RLang, the input parameter can only be table types, so if you want to pass a scalar type, you need to encapsulate it into a table,.
2. the name of variable in RLang Procedure can not contain uppercase letters.
3. the RLang procedure must return at least one result ,in the form of data frame.
2 The installation of R
The installation in windows is very simple, just download the corresponding installation package and double-click the set up program and go on with next. The following will focus on the installation in Linux platform. Before installation please make sure that the related software packages exists:
xorg-x11-devel: for the support of X window
gcc-fortran: build environment
readline-devel: When using R as a standalone program.
libgfortran46: SLES 11 sp2
Then download the R language source package(R-2.15.0 has been tested), and decompress it, run the following command:
./configure –enable-R-shlib
make
make install
if the installation is successful, execute R command in the shell ,you can start the interactive interpreter of R, as shown in the following figure.
3 Integrate it with SAP HANA
(1) Install Rserve
Rserve is a server side of R based on TCP/IP. After start R, execute “install.packages(“Rserve”) “, the program will prompt you to select the image, and then it will start downloading and install it. Of course , you can also download “Rserve.tar.gz ,and execute “install.packages(“/path/to/your/Rserve.tar.gz”,repos=NULL) “, this will also lead you to install Rserve.
After the installation ,please edit “/etc/Rserve.conf”, and the following content:
maxinbuf 10000000
maxsendbuf 0
remote enable
then launch Rserve:
/usr/local/lib64/R/bin/Rserve –RS-port 30120 –no-save –RS-encoding utf8
(2) configure SAP HANA
Start SAP HANA Studio, chooseManage View, Configuration tab, navigate to indexserver.ini -> calcEngine, add the following parameters:
(4) simple test demo
The following demo will compute the square of a column of prime:
|
execute this procedure, the result is like this:
5 tips
Because currently the RLANG procedure only support table type parameter, but many times you need to input a scalar argument. When this happens, you can use “select from dummy” to generate a temp table.
CREATE PROCEDURE WAPPER_WEIBOSOHU(IN keyword NVARCHAR, INcrawltime INTEGER,OUT result WEIBOSOHU_TYPE)
LANGUAGE SQLSCRIPT
AS BEGIN
inputinfo=select :keyword AS “keyword”,:crawltime as“crawltime” from DUMMY;
CALL fetch_weibosohu(:inputinfo, :result);
END;
For example , the procedure above, the outer WAPPER_WEIBOSOHU procedure is a SQLSCRIPT procedure, the fetch_weibosohu is a RLANG procedure, we use “select from dummy ” to generate a temp table, and pass it to the inner procedure.
[Note: The test case for this article is based on SAP HANA SPS07 revision 70.00]
Hi Whisly,
Thanks for the information.
Is there any way we can get list of functions available in HANA at the moment. I do not have R server configured yet.
Regards,
Viren
for more information ,please refer to the document:
http://help.sap.com/hana/SAP_HANA_R_Integration_Guide_en.pdf
Hi Whisly,
Can I install R on Windows platform and integrate it with HANA?
Or is there any free linux cloud server available where I can install R and than use that to integrate it with HANA?
Please let me know.
Warm Regards,
Neha
technically speaking, you can install R on windows platform or any other distributed linux version such as ubuntu, even R version 3.0. Then install Rserve to integrate with SAP HANA(because the main component is Rserve in order to integrate). But these ways has not been tested by SAP, the official ways is R 2.15 and SUSE linux platform. see http://help.sap.com/hana/SAP_HANA_R_Integration_Guide_en.pdf
Whisly,
Where can I get the R libraries to download? And also is there is any sizing or PAM for R server to setup?
Hello Jawahar, you can get the R libraries in CRAN - Mirrors
But for the related linux package, as far as I know there's no PAM you have to search the packages and download them and compile them separately.
Thanks for the information. Is there any server sizing specs I need to consider of?
There's no official specification. But if you want to do the analysis you have to make sure that the data transfered to the R server can be hold in the server's memory. And secondly, you may have to consider the network speed. As you know, each time there is a data tranportation between SAP HANA and R server.
Thanks Whisly. Minimum requirements to start with please??
Memory/CPU ??
Whisly,
I am getting below error while installing RServe version 3.2.5 on Suse Linux. Am i missing anything?
install.packages('/root/RServer/R-3.2.5.tar.gz', repos = NULL)
ERROR: cannot extract package from â/root/RServer/R-3.2.5.tar.gzâ
Warning message:
In install.packages("/root/RServer/R-3.2.5.tar.gz", repos = NULL) :
installation of package â/root/RServer/R-3.2.5.tar.gzâ had non-zero exit status
I'm not sure. Please check the official document about RServe. https://rforge.net/Rserve/
Hope that help.
It is not very clear. Does SAP support will help on this? Is there any R forums, can you guide me, may be I can get some help there?
Finally i got it working. Now i have another issue while running below command. Do i need below command from root or after launching R.
/usr/local/lib64/R/bin/Rserve --RS-port 30120 --no-save --RS-encoding utf8
I don't think it needs root privilege, you can try.
I tried that with root and also user I have created. But I am getting an error "unexpected numeric constant in "Rserve --RS-port 30120"
Hi Whisly
Your Blog is very informative.Can you guide me through the steps to be followed while setting up and configuring R server on windows machine?
Regards
Ranajay
Hi, Ranajay, Personally speaking, I can give you some suggustions but this may not officially supported by SAP.
In fact , in the windows environment, the main process is the same and the key is to install Rserve, which is very simple, just execute install.packages("Rserve"); then find the location of the binary executive Rserve file, e.g. \\path_of_your_R_environment\R\win-library\3.2\Rserve\libs\x64, then you can find Rserve.exe. Execute this file and of course maybe you have to add some arguments , if there are errors(maybe report dll error), you can also call Rserve() in R environment with the arguments. If Rserve is started, you can write a small program to test it, anyway, Rserve is just a server program and can be connected by many clients.
This two documents will help you.https://rforge.net/Rserve/doc.html
https://cran.r-project.org/web/packages/Rserve/Rserve.pdf
You can make it! Have a try!
Thanks Whisly I guess I have to go my SAP standards and use Linux based environment.
Regards
Ranajay
Whisly,
I have a question, when I executed the call my_f(prime), I got the exact results what you showed in your screenshot. Does that mean R is executing from HANA right? Please confirm
I think so. congratulations!
Hi,
I am following the the following guide in order to install R server from source code on a machine, that runs SLES 12, SP1: http://help.sap.com/hana/SAP_HANA_R_Integration_Guide_en.pdf
Unfortunately, when I try to install the kernlab package, I receive the following error message:
> install.packages("kernlab_0.9-14.tar.gz", repos = NULL)
Warning in install.packages("kernlab_0.9-14.tar.gz", repos = NULL) :
'lib = "/usr/local/lib64/R/library"' is not writable
Would you like to create a personal library
~/R/x86_64-unknown-linux-gnu-library/2.15
to install packages into? (y/n) y
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
* installing *source* package 'kernlab' ...
** package 'kernlab' successfully unpacked and MD5 sums checked
** libs
g++ -I/usr/local/lib64/R/include -DNDEBUG -I/usr/local/include -fPIC -c brweight.cpp -o brweight.o
make: execvp: g++: Permission denied
/usr/local/lib64/R/etc/Makeconf:130: recipe for target 'brweight.o' failed
make: *** [brweight.o] Error 127
ERROR: compilation failed for package 'kernlab'
* removing '/home/ruser/R/x86_64-unknown-linux-gnu-library/2.15/kernlab'
Warning message:
In install.packages("kernlab_0.9-14.tar.gz", repos = NULL) :
installation of package 'kernlab_0.9-14.tar.gz' had non-zero exit status
Does anyone have any suggestions?
Regards,
Rashid