Exposing R-script as API

April 8, 2015
By

(This article was first published on Data Perspective, and kindly contributed to R-bloggers)

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

To leave a comment for the author, please follow the link and comment on their blog: Data Perspective.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)