Blog posts on Data Science, Machine Learning, Data Mining, Artificial Intelligence, Spark Machine Learning

Thursday, April 9, 2015

Exposing R-script as API

R is getting popular programming language in the area of Data Science. Integrating Rscript with web UI pages is a challenge which many application developers are facing. In this blog post I will explain how we can expose R script as an API, using rApache and Apache webserver.
rApache is a project supporting web application development using the R statistical language and environmentand the Apache web server.





Exposing Rscipt as API typically involves 3 steps:
  1. Pre-requisites
  2. Installing rApache
  3. Configuring rApache
Step #1 - Pre_requisites :
  • Linux environment - ubuntu
  • latest version of R - R- 3.1.0

Step#2 - Install apache webserver as below:
apt-get install r-base-dev apache2-mpm-prefork apache2-prefork-dev
wget http://www.rapache.net/rapache-1.2.3.tar.gz
 tarxzvf rapache-1.2.3.tar.gz
cd rapache-1.2.3
 ./configure
 make
make install
Step#3: Configuring rApache.
  • Once rApache is installed, start the rApache server as below:
  • service apache2 start
  • Since we are installing for the first time let us test if the setup is installed properly. Do the below steps.
  • sudo vim /etc/apache2/apache2.conf #Added the following:

  • Once you have done the above step, Now access the below pages:
  • IPAddress/html/index.html

    IPAddress/RApacheInfo.html

  • Before getting into expose R-script as API let us understand the rApache configuration.
    Once the rApache is installed the folder structure of rApache would be as below:

  • In apache2.conf file, we need to configure all the sites/r-scripts that we need to expose as api.. Below we have set a directory at path “/var/www/RProject“ in directory tags in apache2.conf file.


  • Now place the below R-script (test.r) generating a random normal distribution in the RProject folder specified above,in the code belowGET$p is input parameter we pass from URL.


  • The api is ready for testing.We can access the api using IP.XXX.XXX.XXX/RProject/test.r?q=10 And the results would be as below:
  • Detailed documentation of Installation is found here

7 comments:

  1. When you wonder on How to Get More Sales In Singapore, you should look for Media One. They would offer you with the best services to market your website in the best manner possible. They have been providing for your specific marketing needs in the right manner.

    ReplyDelete
  2. Something I wanted to try. Thank you, you don't believe how important it is. Make sure to check the link below:
    buy a logo

    ReplyDelete
  3. Hey there! I could have sworn I've been to this website before but after checking through some of the post I realized it's new to me. Anyways, I'm definitely glad I found it and I'll be book-marking and checking back frequently! best florist singapore

    ReplyDelete
  4. Hello there! This is my first visit to your blog! We are a team of volunteers and starting a new project in a community in the same niche. Your blog provided us beneficial information to work on. You have done a marvellous job! relex smile singapore

    ReplyDelete
  5. If you have begun noticing symptoms such as diminishing sex drive, fatigue, depression, mood swings, unexplained weight gain or hair loss, you do not have to simply accept these issues as a part of aging. Testosterone replacement therapy in men can put the spring back into your step and help you feel invigorated and confident once more.testosterone replacement therapy in men

    ReplyDelete
  6. I do agree with all the ideas you have presented in your post. They're really convincing and will definitely work. Still, the posts are very short for beginners. Could you please extend them a bit from next time? Thanks for the post.dragon tiger game

    ReplyDelete