R Interface

R is a popular statistical and graphics language (see: https://www.r-project.org). LINGO's R interface allows you to access the many features of R directly from within your LINGO models. The R interface is straightforward in that it consists of just two functions; these functions are @R for transferring values to and from R, and @RCMD for passing a command to R for execution. These two functions are available within calc sections of a LINGO model and are discussed below.

Note:Currently, the @R and @RCMD functions are only available on Windows versions of LINGO.

In order to link to R from LINGO, you will need to do the following set up steps:

Install a version of R that has the same bit level (32 or 64 bits) as the copy of LINGO you are using.
The R bin folder must be on the system path. Typically, the R bin folder will have a name like C:\R\R-<version number>\bin\x64 when running a 64-bit version of R on Windows. R most likely installed this folder name in your system path during installation, but, if not, you will need to modify the path to include this folder.
The R_HOME environment variable must point to the main R folder. For instance, on Windows, if you enter the SET command in a command window, you should see something like the following: R_HOME=C:/R/R-<version>, where <version> is the version number of R.
Finally, you must install the RInside library in your copy of R. You may do this by running R and then entering the command: install.packages("RInside"). You can verify the installation of RInside by entering the R command: library("RInside").

@R( OBJECT_NAME);

This function may be used for transferring values to and from R. @R supports the transfer of sets, attributes and scalar variables to and from R. Some examples follows:

Example 1:         @R( 'r_x') = X;

passes the LINGO attribute X to R, placing it in the R object called r_x.

Example 2:         MYSET = @R( 'r_set');

loads the set MYSET LINGO from a R text object called r_set.

Note:Currently, @R supports the transfer of one item at a time. To pass multiple items, use multiple @R statements.

@RCMD( 'COMMAND_STRING(S)');

This function may be used to pass one, or more, command strings to R. These commands need to be written in the syntax of R's command language. Some examples follow:

Example 1:         @RCMD( 'sink( /myfolder/rsink.txt'););

opens an output sink for R in /myfolder/rsink.txt.

Example 2:         @RCMD( 'cat("x=\n"); print( x);'););

writes out the string x= and then prints the object x.

Note:For two examples of interfacing with R, refer to the CUBS and MTCARS examples in you LINGO Samples folder. CUBS illustrates forecasting the Cubs win/loss percentage using the forecasting tools of R and MTCARS shows how to retrieve the MTCARS predefined data set from R into LINGO.