Nginx RApi
22 Apr 2015
I put Lua Rclient Library for Nginx and complete setup at GitHub repository for your convenience
I want to share a very simple and light way to setup custom api to R functions using Nginx server
There are of course more complete solutions available, for example using:
You will need Nginx for Windows from http://nginx-win.ecsds.eu/
This is a compiled version of Nginx server that includes support for Lua
I.e. OpenResty
I used the Nginx image processing server with OpenResty and Lua
as the guide to make this setup.
I want to design following API:
http://localhost:8080/rapi/calc?1+2+3
will return 6=1+2+3
http://localhost:8080/rapi/D?x*x
will return derivative of x*x = x + x
First, let’s create a Nginx configuration in conf\nginx.conf
file:
Next we need to process http://localhost:8080/rapi
R API requests in the lualibs/serve_rapi.lua
file:
Now it is time to run a first test.
Please start Rserve, following the example from Lua Rclient Library
I usually start Rserve with a batch file:
Rterm.exe -e "library('Rserve');Rserve(args='--no-save')"
Next, start Nginx server and try following URL in your browser:
http://localhost:8080/rapi/calc?1+2+3
you should return 6
http://localhost:8080/rapi/D?x*x
you should return x + x
Now we can setup a simple html page to access above RAPI:
This page can be accessed by going to http://localhost/
I put Lua Rclient Library for Nginx and complete setup at GitHub repository for your convenience
It adds support for native OpenResty sockets (i.e.
cosocket api)
instead of using LuaSocket
following steps done at pgmoon is a PostgreSQL client library
Aside:
Lua Rclient Library at GitHub:
- https://github.com/stepelu/lua-rclient
- https://github.com/jucor/rclient/
To Debug Lua script, i highly recommend getting ZeroBrane Studio
Following are sample tutorials:
For example you can try following script
You might also want to try LuaJIT:
Interesting Lua project castl
I also recommend installing Gow - The lightweight alternative to Cygwin
to have easy access to curl and other useful utilities.