Using CPLEX in R: Installing cplexAPI in Windows 10

I have a very large mixed integer problem to solve.   COIN‘s solver accessed through Rsymphony did a decent job, but had trouble finding feasible solutions.  I had been told that the IBM cplex  solver was the best on the market for mixed integer programming.

Installing cplex is a relatively easy task.  The difficulties began when I tried to install a link from R.  Either cplexAPI or Rcplex.  You can search and see that there is very little helpful information in the usual places.  I reached out to the developers and Jonathan of cplexAPI responded with very helpful troubleshooting and instructions.

We (honestly, all Jonathan) determined that the following sequence should correctly install cplexAPI.  First, read the directions.

After downloading, unpack the .tar.gz file someplace.  I am going to unpack mine on the desktop.

Open the newly created folder cplexAPI and you will find a directory labeled src.  Open the file within that folder.  I have Notepad++ installed, so I right click and open withNotepad++.  The instructions note that you will need to find each of these directories and type them in.  They may (and probably will if you are using any other version).

PKG_CPPFLAGS=-g -D_R_=1 -DUSE_R=1 -I"C:/Program Files/IBM/ILOG/CPLEX_Studio127/cplex/include"
PKG_LIBS=-L"C:/Program Files/IBM/ILOG/CPLEX_Studio127/cplex/bin/x64_win64" -lm -lcplex1270

After saving you will need to repackage cplexAPI.  Head over to the command line (remember I unpacked my tar.gz file on my desktop) and use the command:

R CMD build --no-build-vignettes --no-manual  --md5     C:\Users\{your name here}\Desktop\cplexAPI

This will repackage the folder, avoiding the md5 error you can find while searching for solutions.  After a successful repackage you can then install the package at the command line.

R CMD INSTALL --build --no-multiarch .\cplexAPI_1.3.3.tar.gz

At this point, you may have errors.  The key you are looking for is this output:

packaged installation of 'cplexAPI' as
DONE (cplexAPI)

Now try it out.  Here is an example from Gabriel Gelius-Dietrich’s cplexAPI – Quick Start.

> library(cplexAPI)

env prob chgProbNameCPLEX(env, prob, "sample")
[1] 0
nc nr obj rhs sense lb ub cn rn beg cnt ind val copyLpwNamesCPLEX(env, prob, nc, nr, CPX_MAX, obj, rhs, sense,
+ beg, cnt, ind, val, lb, ub, NULL, cn, rn)
[1] 0
lpoptCPLEX(env, prob)
[1] 0
solutionCPLEX(env, prob)
[1] 1
[1] 13
[1] 2 0 1
[1] 1 0 1
[1] 0 1 0
[1] 0 -3 0

I am looking forward to using cplexAPI for a larger problem!

For more R posts visit:


5 thoughts on “Using CPLEX in R: Installing cplexAPI in Windows 10

  1. Thank you Jason for a very helpful post. I have been struggling with getting cplex to work in R for a long time. Your post helped me get a bit further. I still have not been able to get it work though. I get error when try to install the package. My command line looks like this:

    R CMD INSTALL –build –no-multiarch .\cplexAPI_1.3.3.tar.gz
    * installing to library ‘C:/Users/rohatgin/Documents/R/win-library/3.3’
    * installing *source* package ‘cplexAPI’ …
    ** package ‘cplex’ successfully unpacked and MD5 sums checked
    Warning: running command ‘sh./’ had status 127
    ERROR: configuration failed for package ‘cplexAPI’
    * removing ‘C:/Users/rohatgin/Documents/R/win-library/3.3/cplexAPI’

    Could you please see if you can help me in this? Thanks lot once again! 🙂

    1. To tell the truth, the developer will be much better help than I am if he is available. Looking at your output I am wondering about your environmental variables. That is a really sticky point in this thing.

  2. ifeq “WIN8” “64”
    PKG_LIBS = -L”C:\Program Files\IBM\ILOG\CPLEX_Studio128\cplex\bin\x64_win64″ -lcplex1280 -lm
    PKG_CPPFLAGS = -D_LP64 -I”C:\Program Files\IBM\ILOG\CPLEX_Studio128\cplex\include”
    PKG_LIBS = -L”C:\Program Files\IBM\ILOG\CPLEX_Studio128\cplex\bin\x86_win32″ -lcplex1280 -lm
    PKG_CPPFLAGS = -I”C:\Program Files\IBM\ILOG\CPLEX_Studio128\cplex\include”
    This is what i type in the test file

    after that

    i try to repackage using cmd

    C:\Users\David>R CMD build –no-build-vignettes –no-manual –md5 C:\Users\David\Desktop\Rcplex
    ‘R’ is not recognized as an internal or external command,operable program or batch file.

    Is there any wrong ?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s