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:
- Installing rApache
- 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
Step#3: Configuring rApache.
- Once rApache is installed, start the rApache server as below:
- Since we are installing for the first time let us test if the setup is installed properly. Do the below steps.
- Once you have done the above step, Now access the below pages:
- 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
service apache2 start
sudo vim /etc/apache2/apache2.conf #Added the following: