rguides

R for Machine Learning in 2026: A Complete Guide

In 2026, 73% of data science teams report using R alongside Python for machine learning workloads. R has transformed from a statistical computing environment into a serious ML platform with the tidysystem of packages providing a unified interface to hundreds of models. The combination of tidyverse-integrated workflows, powerful modeling packages, and excellent reproducibility tools makes R an excellent choice for data science teams.

TL;DR: Use tidymodels as your primary ML framework for its consistent API across algorithms. For gradient boosting on tabular data, choose xgboost or lightgbm. For deep learning, torch provides a native R interface to PyTorch. Deploy models with plumber APIs or Shiny dashboards. The R ML ecosystem has consolidated around these tools, and learning them covers the vast majority of production use cases.

This guide walks you through the machine learning landscape in R, from your first model to production deployment.

The R ML ecosystem in 2026

The R machine learning ecosystem has consolidated around a few key frameworks. Each solves a different class of problem, and together they cover the full spectrum from classical statistics to deep learning. Here is what each framework is best suited for:

  • tidymodels, The modern, tidyverse-aligned framework for modeling
  • caret, Still widely used, especially for classical methods
  • xgboost and lightgbm, Gradient boosting implementations
  • torch, Deep learning for R (interface to PyTorch)
  • keras, High-level neural networks (interface to TensorFlow)

The biggest shift in recent years has been the maturation of tidymodels, which now provides a unified interface for most modeling tasks.

R’s machine learning ecosystem has matured significantly around the tidymodels framework, which provides a unified interface to hundreds of models through consistent APIs. The workflow, recipe() for feature engineering, parsnip::set_engine() for model specification, workflow() to combine them, and tune() for hyperparameter optimization, covers the full supervised learning lifecycle from feature engineering to model selection.

For deep learning, torch provides a native R interface to libtorch, allowing users to define neural network architectures, write custom training loops, and run on GPU without leaving R. The learning curve is steeper than keras, but the control is greater. keras3 provides the keras interface with TensorFlow or JAX backends, which is more accessible for users who know the keras API from Python.

Getting started: your first ML project in R

If you are new to machine learning in R, start with the tidymodels framework. It provides a consistent API for data splitting, model specification, fitting, and evaluation. The workflow below works the same whether you are fitting a linear regression or a random forest:

# Install and load tidymodels
install.packages("tidymodels")
library(tidymodels)

# Load your data
data <- readRDS("your_data.rds")

# Split into training and testing
set.seed(123)
split <- initial_split(data, prop = 0.8)
train_data <- training(split)
test_data <- testing(split)

# Define a model
lm_model <- linear_reg() %>%
  set_engine("lm")

# Fit the model
lm_fit <- lm_model %>%
  fit(formula = response ~ predictor1 + predictor2, data = train_data)

# Evaluate
predictions <- predict(lm_fit, test_data)
rmse(test_data$response, predictions$.pred)

This workflow scales from simple linear regression to complex ensembles.

tidymodels: the modern standard

tidymodels is now the recommended approach for most ML tasks in R. Rather than learning a different interface for every algorithm, you use one consistent set of functions across all models. The framework is built from five packages that each handle one part of the modeling pipeline:

  • parsnip, A unified interface to dozens of modeling packages
  • recipes, Preprocessing pipelines for feature engineering
  • workflows, Combining models and preprocessing
  • tune, Hyperparameter optimization
  • yardstick, Model evaluation metrics
# A complete tidymodels workflow
model_spec <- rand_forest(mtry = tune(), trees = tune()) %>%
  set_mode("classification")

recipe_spec <- recipe(response ~ ., data = train_data) %>%
  step_normalize(all_numeric_predictors()) %>%
  step_impute_median(all_numeric_predictors())

workflow() %>%
  add_recipe(recipe_spec) %>%
  add_model(model_spec) %>%
  tune_grid(resamples = bootstraps(train_data, times = 5),
            metrics = metric_set(accuracy, roc_auc)) %>%
  show_best("roc_auc")

The tidy model fitting API means you spend less time learning package-specific quirks and more time solving problems. Once your preprocessing and model specification are defined, switching from a random forest to an XGBoost model is a one-line change to set_engine(). This consistency is what separates modern ML workflows from the older pattern of learning a separate API per algorithm.

Gradient boosting: xgboost and lightgbm

For tabular data, gradient boosting methods dominate. R interfaces to the best implementations:

# Using xgboost through tidymodels
xgb_spec <- boost_tree(
  trees = 100,
  tree_depth = 6,
  learning_rate = 0.1,
  engine = "xgboost"
)

xgb_fit <- xgb_spec %>%
  fit(response ~ ., data = train_data)

Key advantages of using xgboost or lightgbm through R:

  • Native handling of missing values
  • Built-in regularization
  • Excellent performance on structured/tabular data
  • Easy integration with tidymodels

Deep learning with torch

For neural networks, the torch package provides a native R interface to PyTorch:

library(torch)
library(torchdatasets)

# Define a simple neural network
net <- nn_module(
  initialize = function(input_dim, hidden_dim) {
    self$fc1 <- nn_linear(input_dim, hidden_dim)
    self$fc2 <- nn_linear(hidden_dim, 1)
  },
  forward = function(x) {
    x %>%
      self$fc1() %>%
      nn_relu() %>%
      self$fc2()
  }
)

The torch ecosystem in R is maturing rapidly and is now viable for most deep learning tasks. For tabular data and structured problems, gradient boosting still dominates, and R’s xgboost and lightgbm interfaces are first-class. The key decision is whether your data benefits from learned representations or from ensemble methods on hand-crafted features.

Model evaluation and validation

R provides excellent tools for model evaluation:

# Cross-validation with tidymodels
cv_results <- fit_resamples(
  model_spec,
  recipe_spec,
  resamples = vfold_cv(train_data, v = 10),
  metrics = metric_set(rmse, rsq, mae)
)

# Collect metrics
collect_metrics(cv_results)

Key evaluation metrics in R:

  • Regression, RMSE, MAE, R-squared
  • Classification, Accuracy, ROC AUC, F1 score
  • Multiclass, Log loss, macro F1

Production deployment

Once your model is trained, R offers several paths to production:

  1. plumber, Turn your model into a REST API directly from R
  2. Shiny, Build interactive applications around your model
  3. quarto, Include predictions in reproducible documents
  4. arrow and duckdb, Scale inference with large datasets
# Simple plumber API for predictions
#* @post /predict
function(req) {
  new_data <- parse_json(req$postBody, simplifyVector = TRUE)
  predict(model_fit, new_data)
}

Which package should you use?

TaskRecommended Package
General MLtidymodels
Classical statsbase R, MASS
Gradient boostingxgboost, lightgbm
Deep learningtorch, keras
Time seriesfable, forecast
Model deploymentplumber, Shiny

Feature engineering with recipes

The recipes package handles preprocessing in a reproducible, pipeline-compatible way. Steps are added sequentially: step_normalize() scales numeric variables, step_dummy() creates dummy variables from factors, step_impute_median() fills missing values, step_pca() performs dimensionality reduction. The recipe learns parameters (means, standard deviations, principal components) from the training set and applies them to test and new data, preventing the data leakage that would occur from fitting preprocessing on the full dataset.

prep() fits the recipe to training data; bake() applies the transformations. In a workflow, this is automated: workflow() |> add_recipe(rec) |> add_model(mod) |> fit(training_data) handles both steps.

Cross-validation and model selection

rsample provides vfold_cv(), bootstraps(), and mc_cv() for creating resampling objects. tune::tune_grid() and tune::tune_bayes() search over hyperparameter grids using these resamples. The results are tidy data frames of performance metrics across folds and parameter values, easy to visualize with ggplot2 or rank with show_best().

yardstick provides performance metrics, accuracy(), roc_auc(), rmse(), huber_loss(), with consistent interfaces. All metrics follow the same convention: first argument is the truth column, second is the prediction column, and they work with the grouped metric_set() interface for computing multiple metrics simultaneously.

Model explainability

DALEX (Descriptive mAchine Learning EXplanations) and iml provide framework-agnostic model explainability tools. DALEX::model_profile() computes partial dependence plots; DALEX::model_parts() computes permutation feature importance. Both work with any model that has a predict method, making them compatible with tidymodels, caret, and raw model objects.

lime and shapviz (for SHAP values) provide local explanations, why the model made a specific prediction for a specific observation. For regulated industries where model decisions must be explainable, these tools are essential components of the modeling pipeline rather than afterthoughts.

Current state

R’s machine learning ecosystem has stabilized around tidymodels as the unifying framework. The older pattern of learning a separate API per algorithm (caret, e1071, randomForest) has largely given way to the tidymodels workflow: define a recipe, specify a model, bundle into a workflow, and tune with a grid. This consistency reduces the cognitive overhead of switching between algorithms. For deep learning, torch and keras provide bindings to PyTorch and Keras respectively. R is not the first choice for neural network development, but it is viable for prototyping and inference.

Why choose R for machine learning?

R offers several compelling reasons to use it for machine learning:

Statistical foundation, Unlike general-purpose languages, R was built by statisticians for statistics. This means modeling functions often have more options, better defaults, and deeper statistical support than equivalent packages in other languages.

Tidyverse integration — The data preprocessing pipeline in R is second to none. From importing with readr to transforming with dplyr to visualizing with ggplot2, the entire data science workflow lives in R.

Research compatibility — If you read academic papers, R is likely the language the authors used. Many statistical methods are first released in R before appearing elsewhere.

Shiny for deployment — Building an interactive web application around your model takes minutes in Shiny, not days.

The trade-off is that R is not as fast as Python for very large datasets, and the deep learning ecosystem is smaller. But for most business use cases, R is more than sufficient.

See also