1  Installing energyRt

energyRt formulates an optimization model once and hands it to one of several mathematical-programming backends. You only need one of them to get started; you can add more later and cross-check results between them.

Important

Every command below is shown for you to copy into your own R session. This page renders without executing anything — no packages are installed while the book builds.

1.1 Prerequisites

Install R (≥ 4.3) and, recommended, RStudio as the IDE for the training.

Download and run the installer from CRAN.

Install the .pkg from CRAN, or with Homebrew:

brew install --cask r

Install a current R from the CRAN apt repository (see the CRAN Ubuntu guide):

sudo apt update
sudo apt install --no-install-recommends r-base

1.3 Step-by-step install (if the quick install fails)

Prefer to install by hand, or need to debug a failure? Install pak first — the installer used throughout this guide:

install.packages("pak")

1.3.1 System libraries (Linux)

On Linux, energyRt’s dependencies compile from source and need a few system libraries. pak lists exactly which ones — install the reported apt packages first so the R installs don’t fail midway:

pak::pkg_sysreqs("optimal2050/energyRt")   # lists the apt packages
# typical set on Debian/Ubuntu:
sudo apt install libcurl4-openssl-dev libssl-dev libxml2-dev

1.3.2 R package dependencies

Install energyRt’s CRAN imports up front — one by one, so any failure is isolated and reported by name. Doing this first makes the energyRt install itself quick and reliable, and avoids the Linux error dependencies '...' are not available for package 'energyRt'.

# energyRt's direct CRAN imports (base packages are omitted):
deps <- c(
  "generics", "data.table", "DBI", "RSQLite", "tibble", "tidyr", "dplyr",
  "rlang", "stringr", "lubridate", "purrr", "arrow", "progressr", "tictoc",
  "cli", "zoo", "registry", "options", "glue", "plyr",
  # suggested -- plots and reports (optional but recommended):
  "ggplot2", "patchwork", "knitr", "rmarkdown", "tinytex", "sf",
  # optional -- the energy-rhapsody finale (plus MuseScore, see below):
  "gm"
)

failed <- character(0)
for (pkg in deps) {
  ok <- tryCatch(
    {
      pak::pkg_install(pkg, ask = FALSE)
      message("  [ok]   ", pkg)
      TRUE
    },
    error = function(e) {
      message("  [FAIL] ", pkg, ": ", conditionMessage(e))
      FALSE
    }
  )
  if (!ok) failed <- c(failed, pkg)
}

if (length(failed) == 0) {
  message("All dependencies installed — you're ready to install energyRt.")
} else {
  message("Failed packages: ", paste(failed, collapse = ", "))
  message("Install their system libraries (above), then re-run.")
}

1.3.3 Install energyRt

With the dependencies in place, installing energyRt is a quick one-liner:

pak::pkg_install("optimal2050/energyRt")

# or, with remotes:
# install.packages("remotes")
# remotes::install_github("optimal2050/energyRt")

1.4 Check what you already have

library(energyRt)
en_check_dependencies()   # solver backends: GLPK / Julia / Python / GAMS / GDX
en_check_packages()       # R packages, training extras, LaTeX engine, MuseScore

en_check_dependencies() prints a status table — for each backend: installed?, version, path, and a hint for what to do next — and tells you whether at least one solver is ready. Each backend also has its own detector, e.g. en_check_glpk(), en_check_julia(), en_check_python(), en_check_pyomo(), en_check_gams().

en_check_packages() covers the rest of the course toolkit: energyRt’s own R dependencies, the plotting/reporting extras (ggplot2, patchwork, knitr, rmarkdown, tinytex, sf, gm), and the external tools they need — a LaTeX engine for PDF reports and MuseScore for the gm music output. (en_setup() runs both checks for you.)

1.5 Choose a backend

Backend Software to install License Notes
GLPK glpsol executable open-source Easiest to install; slow on very large models. Used in this training.
Julia / JuMP Julia + JuMP, HiGHS open-source Fast (HiGHS barrier); recommended for large models.
Python / Pyomo Python + pyomo + a solver (CBC) open-source Convenient if you already use conda.
GAMS GAMS distribution proprietary Needs a license; also enables GDX I/O.

For the training we use GLPK — it is open-source and the quickest to set up. The other three backends are optional; each is covered below when enabled.

1.7 Install the library layer

Once your runtime(s) are present, install the packages each backend needs in one call:

en_install_deps()   # detects runtimes, then installs the safe library layer

en_install_deps() runs a dependency check, installs only the library layer for the runtimes it finds (skipping — with a warning — any that are missing), and re-checks at the end.

en_install_deps() bundles the R, Julia, and Python layers — run them by hand:

R layer — the GAMS GDX bridge (not on CRAN) plus optional I/O helpers:

pak::pkg_install("lolow/gdxtools")   # or remotes::install_github("lolow/gdxtools")
install.packages(c("jsonlite", "readxl", "openxlsx"))

Julia and Python layers — use the manual blocks in the Julia / JuMP and Python / Pyomo sections above (enable them with the show-julia / show-python header params).

1.8 Verify end-to-end

Re-check (everything you installed should now be green), choose a default solver, and solve a tiny model:

en_check_dependencies(solver_pkgs = TRUE)
set_default_solver(solver_options$glpk)   # or julia_highs_barrier, pyomo_cbc, ...

# a minimal model solve confirms the full toolchain works
# (see the modeling chapters for assembling a model)

If two or more backends are installed, solving the same scenario with each and comparing objectives is the strongest check that your setup is correct.

1.9 Optional: the energy-rhapsody finale 🎵

The closing chapter (@sec-rhapsody) sonifies scenarios — it turns a day of dispatch into music. It needs two optional pieces; everything else in the workshop runs without them.

# 1. the gm package (composes the score)
install.packages("gm")

# 2. MuseScore (renders score + audio) — download from https://musescore.org
#    then, if gm cannot find it automatically, point gm at the executable:
# options(gm.musescore_path = "C:/Program Files/MuseScore 4/bin/MuseScore4.exe")

Without MuseScore you can still compose (energy_rhapsody(scen, play = FALSE) returns the score object) — you just can’t hear it.