Model Deployment and Plumber


This resource showcases how to expose a machine learning model built in R via a Plumber API. A model is first trained and saved as an .rds file. The Plumber file loads this saved model from disk, interprets data submitted via POST request, and returns a JSON object containing predictions based on the input data. A simple Shiny app is provided to interact with the API. Click here for source code.

gif of shiny app using a plumber api deployed on Connect


Train a simple model that predicts MPG.

cars_model <- lm(mpg ~ cyl + hp, data = mtcars)
saveRDS(cars_model, here::here("R", "model-api", "cars-model.rds"))


Build an API that predicts MPG based on user inputs and the trained model.

Screenshot of the API deployed on Connect.

View the API on Connect | Dashboard view (login required)

Prediction app

Use a Shiny app to predicts MPG based from the API.

Screenshot of the Shiny app that uses the API

App [Login] to Connect.

Back to top