January 24, 2014

Interface between R and PHP or other languages

RServe (link1, link2) is a useful R package to interface R with other languages like java, php, etc. It is basically a TCP/IP server. It creates tcp socket connections through which other the outside world can talk to R.

The good thing is it manages multiple connections in a clean way. It creates a separate workspace and a directory for every connections. So, each connection is independent of others. Another good thing is several client-side implementations are available including C/C++, Java, PHP.

Recently, I used it with PHP. It works great. You may start RServe as a daemon (using the function Rserve()) or from your R session with your current session available to all connections (using the function run.Rserve()).

While I was working on integrating R with PHP via Rserve, I was struggling to have write permission in the Rserve folder. I found that both Rserve and Apche Server have to be run by the same user and group. Both Apache and Rserve has to be configured for this purpose.

Apache Config:
1. Edit /etc/apache2/envvars 

export APACHE_RUN_USER=ashis
export APACHE_RUN_GROUP=ashis
2. You may have to change the ownership of the /var/locks/apache2 folder.

sudo chown -R ashis:ashis /var/locks/apache2/

Rserve config:
1. Find the uid and gid of the user (here ashis:ashis)

id ashis
2. Edit /etc/Rserve.conf file
uid UID_OF_USER
gid GID_OF_USER

Finally, You have to restart both apache and Rserve.

1 comment: