The plumber package for R makes it easy to expose existing R code as a webservice via an API (Trestle Technology, LLC 2017).
You take an existing R script and make it accessible with plumber by simply adding a few lines of comments. If you have worked with Roxygen before, e.g. when building a package, you will already be familiar with the core concepts. If not, here are the most important things to know:
you define the output or endpoint
you can add additional annotation to customize your input, output and other functionalities of your API
you can define every input parameter that will go into your function
every such annotation will begin with either #' or #*
With this setup, we can take a trained machine learning model and make it available as an API. With this API, other programs can access it and use it to make predictions.
What are APIs and webservices?
With plumber, we can build so called HTTP APIs. HTTP stands for Hypertext Transfer Protocol and is used to transmit information on the web; API stands for Application Programming Interface and governs the connection between some software and underlying applications. Software can then communicate via HTTP APIs. This way, our R script can be called from other software, even if the other program is not written in R and we have built a tool for machine-to-machine communication, i.e. a webservice.
How to convert your R script into an API with plumber
Training and saving a model
Let’s say we have trained a machine learning model as in this post about LIME. I loaded a data set on chronic kidney disease, did some preprocessing (converting categorical features into dummy variables, scaling and centering), split it into training and test data and trained a Random Forest model with caret. We can use this trained model to make predictions for one test case with the following code:
library(tidyverse)
# load test and train data
load(&quot;../../data/test_data.RData&quot;)
load(&quot;../../data/train_data.RData&quot;)
# load model
load(&quot;../../data/model_rf.RData&quot;)
# take first test case for prediction
input_data %
select(-class)
# predict test case using model
pred