Deploying a CARET Machine Learning model as an API with Plumber
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I have deployed many models in my time working as a Senior Data Scientist and as a Principal Analyst at Nottingham University Hospitals. I have deployed into SQL, on client sites, in Node.JS and other types of deployment. This was the first time I used the Plumber library, and I loved it.
How to deploy your model as an API
My new YouTube page is up and it takes you through the steps of how you can deploy your machine learning model as an API:
Training a supervised machine learning model
In this section I will use a number of packages to train a R model. The packages include:
- Caret to train a bagged tree – ensemble model;
- NHSRDatasets – this is a package created by the NHS-R community. We will be using the stranded patient data example for this package;
- ConfusionTableR – this is a package to flatten outputs of the Caret’s confusion matrix to be used to output to spreadsheets, or store in databases;
- A number of other R normal files.
Creating the Plumber files
This has two main functions – a GET request which shows if the website is running and a POST request which is used to pass JSON data from the API, to the model and then the predictions are returned to the API. The R documentation on Plumber is extensive and definitely worth checking out.
Where to get the supporting files?
The supporting GitHub repository contains everything you need to get going. Hereunder are the important files:
- ML Model Training app – the R script that performs the model training and saving
- Plumber App – the Plumber API R file
- YAML file for Swagger – the YAML file to jazz this up for Swagger
Where to go next
To deploy your API you could use a number of different methods:
- Creation of a Dockerfile
- Upload to R Studio Connect
- Upload to Digital Ocean
- Deploy in your web servers
The full list is in the Plumber official hosting guide.
Coming soon…
I will write a how to deploy on docker as a component to this, but until then, I hope you find the tutorials useful, and remember:
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.