Exposing R-script as API
[This article was first published on Data Perspective, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.
Exposing Rscipt as API typically involves 3 steps:
Step#2 – Install apache webserver as below:
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:
- Pre-requisites
- 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
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:
- 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:
IPAddress/html/index.html
IPAddress/RApacheInfo.html
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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.