Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

RintermediatebackendAI generated

Plumber REST API for Model Predictions

Turn a machine learning model built in R into a web service that other applications can call. You'll train a classification model, for example predicting whether a passenger survived the Titanic or whether a customer will churn, save it, and serve it with plumber: a POST /predict endpoint receives the input as JSON and returns the prediction and its probability. The API validates input carefully, returns clear errors, documents itself with Swagger UI, and can be run in Docker. You'll learn to package a model for reuse, build REST endpoints in R, validate JSON input with jsonlite, write API tests, and think about what happens when bad data reaches a model.

Estimate
~12h
Steps
7
Completed by
0
Proposed by
codeseed.app

plumber · jsonlite

Project roadmap

  1. 01

    Train and save a model

    ~2h

    In train.R, load a public dataset (for example the Titanic data from the titanic package), clean it, and train a logistic regression with glm using a few features such as class, sex, age and fare. Split the data into train and test sets and print the test accuracy. Save the model together with the feature names and allowed values (factor levels, numeric ranges) in model.rds with saveRDS. Done when: running train.R prints the accuracy and creates model.rds.

  2. 02

    Create the plumber API

    ~1.5h

    Install plumber and jsonlite. Create plumber.R that loads model.rds once at startup, and add GET /health returning {"status":"ok","model":"..."} with the model name and training date. Create run.R that starts the API on port 8000. Done when: running run.R starts the server, and /health returns the model information.

  3. 03

    Add the prediction endpoint

    ~2h

    Add POST /predict that reads a JSON body with the feature values, builds a one-row data frame with the same types and factor levels used in training, and returns the predicted class and its probability with predict(type = "response"). Also accept a list of records and return one prediction per record. Done when: posting a valid passenger returns a prediction and a probability between 0 and 1.

  4. 04

    Validate input and return clear errors

    ~2h

    Before predicting, check that every required field is present, that numbers are numbers within plausible ranges, and that categorical values are among the ones seen in training. Return 400 with a JSON body listing every problem, like {"errors":["age must be between 0 and 100"]}. Return 415 when the body isn't JSON. Unexpected errors return 500 without R internals. Done when: each kind of bad input returns a clear 400 message.

  5. 05

    Document the API

    ~1h

    Add plumber annotations (#* @param, #* @serializer, descriptions and example values) so the Swagger UI at /__docs__/ explains every endpoint and field. Write a README with curl examples for /health, a single prediction and a batch prediction. Done when: the Swagger page documents the endpoints and a prediction can be sent from it.

  6. 06

    Write API tests

    ~2h

    Using testthat, test the validation function directly, then start the API in the background (for example with callr) and send real requests with httr2: a valid prediction, a batch, a missing field, an out-of-range value and an unknown category. Done when: the tests pass.

  7. 07

    Run it in Docker

    ~1.5h

    Write a Dockerfile based on the rocker/r-ver image that installs the required packages, copies plumber.R and model.rds, and starts the API. Build and run it, and test it with curl. Done when: docker run exposes a working API on port 8000.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~12h · 7 steps

Tech stack

plumberjsonlite