ExplaineR is an open-source R package for explaining classification and regression models with SHAP (Shapley additive explanations). Use it to see which features contribute to individual predictions, explore subgroups with similar explanations, and evaluate how a model performs. The tutorials show how to use ExplaineR with models trained through mlr3.
Choose how to start
| What you want to do | Where to start |
|---|---|
| Install ExplaineR for your own data and models | Install from CRAN |
| Explain a classifier and evaluate it on held-out data | Binary classification tutorial |
| Explain predictions for each class in a multiclass model | Multiclass classification tutorial |
| Explain predictions from a regression model | Regression tutorial |
| Try a runnable example in the browser | Kaggle notebook with an interactive SHAP plot |
| Explore a SHAP figure without running code | Interactive Hugging Face showcase |
The Kaggle example trains a random forest on the original cell characteristics in the Wisconsin breast cytology dataset. It reports held-out performance and displays the widget returned by explainer::eSHAP_plot(). Hover over a point to inspect the feature contribution and whether the prediction was correct.
Installation
You can install the released version of the package from CRAN:
install.packages("explainer")To install the development version from GitHub:
# install.packages("devtools")
devtools::install_github("PERSIMUNE/explainer")Usage
Preparing your own data
DAIMS—Datasheets for AI and Medical Datasets provides a checklist, automated data checks, and templates for dataset documentation and data dictionaries. It can help identify preparation issues before model training and interpretation with ExplaineR. DAIMS is optional; passing its checks does not establish that a dataset, study design, or trained model is suitable for a particular analysis.
Example
This example classifies breast cytology samples as benign or malignant using the original predictors in mlbench::BreastCancer. It does not predict future breast-cancer risk. The ID column is excluded and rows with missing values are omitted. No synthetic demographic features are added. See articles for more examples.
# Load necessary packages
library("explainer")
# Set seed for reproducibility
seed <- 246
set.seed(seed)
# load necessary packages
library(mlbench)
library(mlr3learners)
library(ranger)
# Load BreastCancer dataset
utils::data("BreastCancer", package = "mlbench")
# Specify target column and positive class
target_col <- "Class"
positive_class <- "malignant"
# Extract relevant columns from dataset
mydata <- BreastCancer[, -1]
mydata <- na.omit(mydata)
# Create a classification task
maintask <- mlr3::TaskClassif$new(
id = "my_classification_task",
backend = mydata,
target = target_col,
positive = positive_class
)
# Split the dataset for training
splits <- mlr3::partition(maintask)
# Create a ranger learner for classification
mylrn <- mlr3::lrn("classif.ranger", predict_type = "prob")
# Train the learner on the training set
mylrn$train(maintask, splits$train)
# Generate SHAP values and plot
SHAP_output <- eSHAP_plot(
task = maintask,
trained_model = mylrn,
splits = splits,
sample.size = 30,
seed = seed,
subset = 0.8
)
shap_Mean_wide <- SHAP_output[[2]]
shap_Mean_long <- SHAP_output[[3]]
# Generate SHAP clusters and plot
SHAP_plot_clusters <- SHAPclust(
task = maintask,
trained_model = mylrn,
splits = splits,
shap_Mean_wide = shap_Mean_wide,
shap_Mean_long = shap_Mean_long,
num_of_clusters = 3,
seed = seed,
subset = 0.8
)
# Please note that the colors on the legend on SHAP cluster plot is not the same as in the SHAP summary plot, but the markers are the same.
# The markers indicate prediction correctness not the colors (so the colors could be manually changed to black/white on the legend when reporting)Research citing ExplaineR
Cited by 14 papers on Google Scholar · As of 3 November 2026.
ExplaineR is cited across medical research, food science, engineering, sustainability, and machine learning. All 14 citing works are listed below, grouped by research area.
Used ExplaineR in your research? Share your work to be featured here.
Citation
ExplaineR is developed by Ramtin Zargari Marandi, PhD.
If you use this package in your research, please consider citing it:
Zargari Marandi, Ramtin. “ExplaineR: an R package to explain machine learning models.” Bioinformatics Advances 4, no. 1 (2024): vbae049.doi:10.1093/bioadv/vbae049
Contributing
If you want to contribute to the development of this package, please read CONTRIBUTING.md for guidelines.
Issues
If you encounter any issues or have suggestions, please open an issue.
Major updates
23-09-2024: a new function for multi-class classification is added, check out the tutorial for multi-class classification on how to use the function (eSHAP_plot_multiclass.R).
License
This package is released under the MIT License.
References
Zargari Marandi R, Leung P, Sigera C, Murray DD, Weeratunga P, Fernando D, Rodrigo C, Rajapakse S, MacPherson CR, (2023). Development of a machine learning model for early prediction of plasma leakage in suspected dengue patients. PLoS Negl Trop Dis 17(3): e0010758. doi:10.1371/journal.pntd.0010758
Lang M, Binder M, Richter J, Schratz P, Pfisterer F, Coors S, Au Q, Casalicchio G, Kotthoff L, Bischl B, (2019). mlr3: A modern object-oriented machine learning framework in R. Journal of Open Source Software. doi:10.21105/joss.01903
Molnar C, Bischl B, Casalicchio G, (2018). iml: An R package for Interpretable Machine Learning.JOSS, 3(26), 786. doi:10.21105/joss.00786
Lundberg SM and Lee SI, (2017). A unified approach to interpreting model predictions. Advances in neural information processing systems.arXiv:1705.07874
Ludvig Renbo Olsen and Hugh Benjamin Zachariae, (2022). cvms: Cross-Validation for Model Selection. R package version 1.3.4. https://CRAN.R-project.org/package=cvms
R Core Team (2021). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. URL https://www.R-project.org/.
