How to use Gurobi with R


In order to use Gurobi in R, you need to install a Gurobi R package which is not publicly available.

On the ULHPC, you need to:

  • Load both the Gurobi and R modules
  • Run R and install the Gurobi R package located in the Gurobi EasyBuild root folder

How to do this on the HPC

You can do this via the following commands:

Load the modules

# Request an interactive sessions to perform the following actions
# on a worker node
# Load both modules
module load lang/R
module load math/Gurobi

Install the necessary packages in R

# Run R and install the Gurobi R package along with a mandatory dependency
install.packages(file.path(Sys.getenv("EBROOTGUROBI"), "R/gurobi_9.1-2_R_4.0.2.tar.gz"))
install.packages("slam", repos = "")

Optional: test Gurobi with a simple problem

# Load gurobi package
# Create optimization problem
model <- list()
model$obj        <- c(1, 1, 2)
model$modelsense <- "max"
model$rhs        <- c(4, 1)
model$sense      <- c("<", ">")
model$vtype      <- "B"
model$A          <- matrix(c(1, 2, 3, 1, 1, 0), nrow = 2, ncol = 3,
                           byrow = TRUE)

# Solve the optimization problem using Gurobi
result <- gurobi(model, list())