Convex Optimization in R Using CVXR
June 17, 2026 · View on GitHub
A hands-on tutorial on disciplined convex optimization in R with CVXR, prepared for useR! 2026 (Warsaw).
Authors: Anqi Fu, Balasubramanian Narasimhan, Stephen Boyd.
This is a Quarto book. It targets CVXR 1.9.1 and uses CRAN solvers only.
The earlier (2019, CVXR 1.x, bookdown) tutorial is preserved on the
useR2019branch.
For participants
-
Get the materials (this repository):
- RStudio: File → New Project → Version Control → Git, paste
https://github.com/bnaras/cvxr_tutorial.git, then opencvxr-tutorial-2026.Rproj. - Positron: Command Palette → Git: Clone → paste the same URL → open the folder.
- Terminal:
git clone https://github.com/bnaras/cvxr_tutorial.git - No Git: Code → Download ZIP on the repo page and unzip.
You also need the Quarto CLI (≥ 1.4) to render chapters or the whole book. RStudio bundles Quarto; Positron and terminal users install it once from that link. (Running individual code chunks needs no render step.)
- RStudio: File → New Project → Version Control → Git, paste
-
Install the packages (CRAN only):
# Solvers (CVXR auto-pulls clarabel, osqp, scs, highs) install.packages(c("CVXR", "scip", "Uno", "sparsediff")) # Helpers used by the examples install.packages(c("ggplot2", "tidyr", "nnls", "glmnet", "boot", "png", "bench")) -
Check your setup:
source("setup_check.R") # or: Rscript setup_check.RIt verifies your R version and packages and runs a tiny solve on each solver, reporting
PASS/FAIL. The core hands-on chapters need onlyCVXR+ the helpers;scip/Uno/sparsediffpower two end-of-book demonstrations. -
Work through the chapters by running the code chunks in RStudio.
Building the book (authors)
# install.packages("quarto") # the R wrapper, if needed
quarto::quarto_render() # or, in a terminal: quarto render
Requires the Quarto CLI (≥ 1.4). For PDF
output you also need a LaTeX install (e.g. tinytex::install_tinytex()); HTML
needs no LaTeX. Rendered output goes to _book/.
Repository layout
| Path | What |
|---|---|
index.qmd | Preface |
01-…–16-…qmd | Chapters (see _quarto.yml for the structure) |
setup_check.R | Pre-tutorial environment check |
_check_status.R | Helper sourced by example chunks |
references.bib, cvxr.bib | Bibliography |
custom.scss | Theme (shared with the CVXR website) |
figures/ | Reused figures |
License
See LICENSE.