I am thinking about building a RESTful API using the programming language R, mainly to expose my machine learning model to the user in an API format. I know there are some options like export to PMML, PFA and use other languages to take care of the API part. However, I want to stick to the same programming language and was wondering if there is anything like Flask/Django/Springbook framework in R?
I took a look at servr/shiny but I really don't think RESTful is what they are designed for. Is there any better solution within R that is more easy to use?
I have two options for you:
plumber
A small example file:
From the R command line:
With this you would get results like this:
Jug
It very easy to learn and has a nice vignette.
An Hello-World-example:
This is for those who want to have a comparion of API development with R - plumber, Rserve and rApache.
Basically concurrent requests are queued by httpuv in plumber so that it is not performant by itself. The author recommends multiple docker containers but it can be complicated as well as response-demanding.
There are other tech eg Rserve and rApache. Rserve forks prosesses and it is possible to configure rApache to pre-fork so as to handle concurrent requests.
See the following posts for comparison
https://www.linkedin.com/pulse/api-development-r-part-i-jaehyeon-kim/ https://www.linkedin.com/pulse/api-development-r-part-ii-jaehyeon-kim/
Adding opencpu to this list of the answers:
Do check out OpenCPU by Jeroen Ooms.
Benefits:
Simple and straightforward: Any R package installed on the opencpu server is callable via http.
Just focus on creating the R package and opencpu will take care of the rest.
You can return a relational table of results, a plot, single value or even a pointer (aka a temporary session key) to an R object [imagine a huge object/dataset that you can process/manipulate from other more limited platform ;) ]
CI/CD with your package hosted on Github.
If you are using the server version, opencpu is concurrent and async by design through leveraging of Nginx for caching and load balancing.
Use AppArmor to enforce security on Ubuntu. Or if you use fedora, you can set up public-private certificate authentication, thanks to Apache server at the backend. Thanks to rApache!
The above are too complicated: You can also start a single user session on your local machine using
opencpu::ocpu_start_app()
and serve your functions (downside is security)Need an user interface? Simply create an UI using javascript, store it in the www folder of the R package and user can open it on their web browser and use your functions.
This post doesn't do opencpu justice. I would really recommend that you read his links at the top of OpenCPU
Have a playaround with https://cloud.opencpu.org/ocpu/test or https://www.opencpu.org/apps.html