How to use R from Xpress-Mosel

Document created by oliverbastert@fico.com Advocate on Nov 25, 2015
Version 1Show Document
  • View in full screen mode

Overview

 

The R interface for Xpress-Mosel is a tool that makes it possible to easily exchange data with R and execute R scripts, or evaluate expressions in the R language, from within a Mosel model.

 

The interface consists of a Mosel module that dynamically loads R binary libraries, from official R-project distributions, and provides access to the R workspace and execution environment via Mosel functions. The purpose of the module is to make the extensive data processing capabilities of R directly available in Mosel models.

 

.

R module quick start

 

In order to use R within a Mosel model, the R module must first be loaded via the Mosel uses directive. After that, the R environment is automatically initialized at the point where a Mosel model first uses any of the R interface functions. So we can have the following small example that just prints the R version (it prints the same output as if you typed R.version.string on an R console):

 

model "r version example"

  uses 'r';

  Rprint('R.version.string')

end-model

 

Here, Rprint is a function that takes a string argument, parses and evaluates it in the R environment and prints the result. A similar function, Reval, does the same just without printing the result.

 

Data exchange between Mosel and R is achieved through the Rset and Rget* functions. The first one assigns a Mosel value to an R variable, transferring data from Mosel to the R environment. The second one reads data from the R environment and assigns it to a Mosel variable. For example, the following code would perform k-means clustering in R on Mosel Longitude and Latitude arrays and retrieve cluster assignments back into a Mosel array named Clustering.

Rset('lon',Longitude)

Rset('lat',Latitude)

Rgetarr('kmeans(cbind(lon, lat), 1000)$cluster', Clustering)

 

The types of data that can be exchanged are the four Mosel elementary types (namely boolean, integer, real and string) plus arrays, lists and sets of these, all mapped into R atomic vectors of the corresponding type.

There is no direct mapping of more complex R types such as factors or data.frames, however these can be exchanged after conversion to basic types.

 

For example, a factor f could be loaded into a Mosel array as an array of integers, the factor’s codes, simply with Rgetarr("f", intarray) as the interface ignores the factor’s class and levels attributes; or it can be loaded as an array of strings, the mapped factor’s levels, with Rgetarr("f", strarray) as the casting to string, performed within R, automatically takes into account the levels attribute.

 

To load a data.frame into Mosel, it should be converted to a matrix (for instance using as.matrix if the column types allow that) or split into individual column vectors.

 

Full documentation can be found in Chapter 20 of the Xpress-Mosel Reference Manual.

Attachments

    Outcomes