
Image by Editor | Midjourney
Deploying R models makes them useful for real-world tasks. There are numerous different ways to do this, including using APIs, Shiny apps, or containers. Plumber turns R models into APIs, while Shiny creates interactive web apps. Docker helps package and run models anywhere. Cloud services like AWS, GCP, and Shinyapps.io make deployment easy and scalable. The best method depends on what you need and where you want to run your model.
This article will discuss simple ways to deploy and manage R models.
Preparing the R Model for Deployment
Before deployment, ensure that the model is trained and saved correctly.
# Load necessary libraries library(caret) library(randomForest) # Load dataset data(iris) # Train a model set.seed(42) model <- randomForest(Species ~ ., data = iris) # Save the model saveRDS(model, file = "model.rds")
Deploying Using Plumber API
Plumber is an R package that lets you to create RESTful APIs for your R models. This is useful when you want to serve predictions over HTTP requests. The process involves the following steps.
1. Install and Load Plumber
install.packages("plumber")
library(plumber)
2. Create a Plumber API Script
library(plumber)
library(randomForest)
# Load the model
model <- readRDS("model.rds")
#* @post /predict
#* @param sepal_length:double
#* @param sepal_width:double
#* @param petal_length:double
#* @param petal_width:double
#* @serializer json
predict_species <- function(sepal_length, sepal_width, petal_length, petal_width) {
new_data <- data.frame(Sepal.Length = as.numeric(sepal_length),
Sepal.Width = as.numeric(sepal_width),
Petal.Length = as.numeric(petal_length),
Petal.Width = as.numeric(petal_width))
pred <- predict(model, new_data)
return(list(prediction = as.character(pred)))
}
3. Start the Plumber API
r <- plumb("api.R")
r$run(port = 8000)
This starts a Plumber API server in R. It loads routes from api.R and the server runs on port 8000.
Deploying Using Shiny
Shiny is a framework for building interactive web applications in R. You can use it to create a simple web interface for making predictions.
Here are the steps.
1. Install and Load Shiny
install.packages("shiny")
library(shiny)
2. Create a Shiny App
# Define UI
ui <- fluidPage(
titlePanel("Iris Species Predictor"),
sidebarLayout(
sidebarPanel(
numericInput("sepal_length", "Sepal Length", 5.1),
numericInput("sepal_width", "Sepal Width", 3.5),
numericInput("petal_length", "Petal Length", 1.4),
numericInput("petal_width", "Petal Width", 0.2),
actionButton("predict", "Predict")
),
mainPanel(
textOutput("prediction")
)
)
)
# Define Server
server <- function(input, output) {
prediction <- eventReactive(input$predict, {
new_data <- data.frame(Sepal.Length = input$sepal_length,
Sepal.Width = input$sepal_width,
Petal.Length = input$petal_length,
Petal.Width = input$petal_width)
predict(model, new_data)
})
output$prediction <- renderText({
paste("Predicted Species:", prediction())
})
}
# Run the application
shinyApp(ui, server)
3. Run the Shiny App
shiny::runApp("app.R")
This will launch a web application where users can input feature values and get real-time predictions. Use rsconnect::deployApp() to host it on shinyapps.io.
Deploying with Docker
Docker packages your R model with its dependencies into a container. This makes deployment easy across different systems. Ensure Docker is installed on your system. You can download it from Docker’s official website.
Here are the steps.
1. Create a Dockerfile
FROM rocker/r-ver
RUN R -e "install.packages('plumber')"
COPY api.R /app/api.R
WORKDIR /app
CMD ["Rscript", "api.R"]
2. Build and Run the Container
docker build -t r_model_api . docker run -p 8000:8000 r_model_api
These Docker commands will build and run a containerized R API using Plumber.
Deploying on Cloud Services
For scalable solutions, cloud platforms like AWS, Google Cloud, and Azure provide different options.
- AWS Lambda: R scripts can be executed using Lambda with Docker containers
- Google Cloud Run: Deploy an R API with minimal server management
- Azure ML: Deploy and monitor R models with built-in support for R
Conclusion
Deploying R models involves data preparation, model training, and selection of deployment methods. Options include APIs, Shiny apps, and cloud services. Automate workflows with Docker and CI/CD tools. Monitor performance and retrain as needed. Choose deployment strategies based on scalability and user needs.
Keep these best practices in mind while looking to deploy your R projects.
- Use Version Control (GitHub) – Track changes and maintain different versions of the model.
- Monitor Model Performance – Regularly check prediction accuracy and retrain if needed.
- Optimize for Scalability – Use cloud services or Kubernetes to handle large requests.
- Implement Security Measures – Protect APIs with authentication and HTTPS encryption.
