Running R-as-a-service was something I was obsessed about for a while. I managed to search quite a few websites to come up with the strategy as below:
# GET THE CODE (Please do NOT skip this step and try to install using APTITUDE)
# ----------------------------------------------------------------------------
mkdir rApache
git init
git remote add origin https://github.com/jeffreyhorner/rapache
git pull origin master
sudo apt-get install r-base-dev apache2-mpm-prefork apache2-prefork-dev
# BUILD THE CODE
# ----------------
./configure --with-apxs=/usr/bin/apxs2
sudo make clean
sudo make
sudo make install
# CREATE 2 FILES:
#-----------------------------------------------------
sudo touch /etc/apache2/mods-available/r.conf
sudo touch /etc/apache2/mods-available/r.load
# ENTER THE FOLLOWING CONTENTS INTO THE r.conf FILE
#-----------------------------------------------------
<Location /R>
ROutputErrors
SetHandler r-script
RHandler sys.source
</Location>
<Location /RApacheInfo>
SetHandler r-info
</Location>
# ENTER THE FOLLOWING CONTENTS INTO THE r.load FILE
# -----------------------------------------------------
LoadModule R_module /usr/lib/apache2/modules/mod_R.so
# ENABLE THE RAPACHE MODULE
#----------------------------
sudo a2enmod r
# RESTART WEBSERVER
#----------------------------
#/etc/init.d/apache2 restart
sudo service apache2 restart
# TEST WEBSERVER
#----------------
# ENTER THE FOLLOWING CONTENTS INTO THE /var/www/R/test FILE
# ----------------------------------------------------------
y = rnorm(100)
print(y)
# OPEN BROWSER AND VISIT THE FOLLOWING URL:
# -----------------------------------------
http://localhost/R/test
# THE OUTPUT SHOULD LOOK SOMETHING LIKE THIS:
# ----------------------------------------------------------------
# Static Graphics can be created in R and transferred on HTTP response too!
# --------------------------------------------------------------------------
# Change the contents of /var/www/R/test file above and add the following:
setContentType ("image/png")
temp <- tempfile ()
y = rnorm (100)
png (temp, type="cairo")
plot (1:100, y, t='l')
dev.off ()
sendBin (readBin (temp, 'raw', n=file.info(temp)$size))
unlink (temp)
# -----------------------------------------------------------------------
#Of course one can handle other HTTP request methods and pass JSON back and forth
# ------------------------------------------------------------------------------
# Ensure that RJSONIO package is installed in R!
# Change the contents of the /var/www/R/test file above to be:
setContentType("application/json")
library(RJSONIO)
library(utils)
print(GET$data)
#json <- fromJSON(URLdecode(GET$data))
#print (json)
#print(toJSON(rnorm(10)))
#Interactive Graphics?
#-----------------------
# You just need a new apache module that supports ggplot (more on this for later...)
REFERENCES:
https://dzone.com/articles/building-json-webservice-r
https://binfalse.de/2011/05/28/r-for-the-web/
# GET THE CODE (Please do NOT skip this step and try to install using APTITUDE)
# ----------------------------------------------------------------------------
mkdir rApache
git init
git remote add origin https://github.com/jeffreyhorner/rapache
git pull origin master
sudo apt-get install r-base-dev apache2-mpm-prefork apache2-prefork-dev
# BUILD THE CODE
# ----------------
./configure --with-apxs=/usr/bin/apxs2
sudo make clean
sudo make
sudo make install
# CREATE 2 FILES:
#-----------------------------------------------------
sudo touch /etc/apache2/mods-available/r.conf
sudo touch /etc/apache2/mods-available/r.load
# ENTER THE FOLLOWING CONTENTS INTO THE r.conf FILE
#-----------------------------------------------------
<Location /R>
ROutputErrors
SetHandler r-script
RHandler sys.source
</Location>
<Location /RApacheInfo>
SetHandler r-info
</Location>
# ENTER THE FOLLOWING CONTENTS INTO THE r.load FILE
# -----------------------------------------------------
LoadModule R_module /usr/lib/apache2/modules/mod_R.so
# ENABLE THE RAPACHE MODULE
#----------------------------
sudo a2enmod r
# RESTART WEBSERVER
#----------------------------
#/etc/init.d/apache2 restart
sudo service apache2 restart
# TEST WEBSERVER
#----------------
# ENTER THE FOLLOWING CONTENTS INTO THE /var/www/R/test FILE
# ----------------------------------------------------------
y = rnorm(100)
print(y)
# OPEN BROWSER AND VISIT THE FOLLOWING URL:
# -----------------------------------------
http://localhost/R/test
# THE OUTPUT SHOULD LOOK SOMETHING LIKE THIS:
# ----------------------------------------------------------------
[1] -1.11999180 0.77388394 0.47741262 0.59742444 -0.90835401 -0.73759768
[7] -0.98358905 0.17164534 -0.30633769 -0.26356790 -0.72520593 1.09611240
[13] -0.87002950 -0.68221878 -0.19672396 0.27719816 0.35486869 -2.62321997
[19] 0.09403267 -0.59546938 -0.71671409 -0.37427584 0.22468514 0.39514394
[25] -0.02668597 -0.04376564 -0.46238028 -0.26864170 0.55945746 0.18677297
[31] -0.13377996 -0.14030508 0.02584913 2.25083262 0.10285414 -0.58769284
[37] 0.88162455 -0.60222629 -0.19106042 -0.33075772 0.88249825 0.20592161
[43] -0.53860098 -0.29864930 -0.34677542 -1.13457872 -0.52058656 -1.05854668
[49] -0.87010347 0.47185953 -0.07035073 -0.61571393 -1.53781353 -1.48808031
[55] 1.70151404 0.55821896 -0.86341586 1.01017915 -1.11602614 1.25460339
[61] -0.07592987 -0.66240349 0.42753736 -0.87203214 1.13069688 1.10198204
[67] -0.62701037 -0.91552938 1.34857229 2.00109074 -0.34138944 1.00038689
[73] 1.94789712 -1.28643020 1.01272894 1.36189124 1.33133999 -0.37285196
[79] -1.97906684 -1.47064465 1.54959469 1.34553324 -0.59611516 0.47388310
[85] 2.42228484 0.66866070 -0.40705464 -0.48787802 -0.71283698 1.53617890
[91] 1.02509900 0.87506909 0.79658073 1.97578280 -0.31492661 0.88690593
[97] 1.02348963 1.75672929 -1.18559266 0.56992037
# Static Graphics can be created in R and transferred on HTTP response too!
# --------------------------------------------------------------------------
# Change the contents of /var/www/R/test file above and add the following:
setContentType ("image/png")
temp <- tempfile ()
y = rnorm (100)
png (temp, type="cairo")
plot (1:100, y, t='l')
dev.off ()
sendBin (readBin (temp, 'raw', n=file.info(temp)$size))
unlink (temp)
# -----------------------------------------------------------------------
#Of course one can handle other HTTP request methods and pass JSON back and forth
# ------------------------------------------------------------------------------
# Ensure that RJSONIO package is installed in R!
# Change the contents of the /var/www/R/test file above to be:
setContentType("application/json")
library(RJSONIO)
library(utils)
print(GET$data)
#json <- fromJSON(URLdecode(GET$data))
#print (json)
#print(toJSON(rnorm(10)))
#Interactive Graphics?
#-----------------------
# You just need a new apache module that supports ggplot (more on this for later...)
REFERENCES:
https://dzone.com/articles/building-json-webservice-r
https://binfalse.de/2011/05/28/r-for-the-web/