Thursday, August 12, 2010

Interfacing C# .Net and R - Integrating the best of both worlds

In specific software areas like in quantitative finance or else in other mathematical domains, data centric programming typically requires a good balance between three requirements - (1) a solid platform with rich mathematical/statistical functionality (2) having an easy to use, contemporary, programming environment which permits easy and flexible front end code development and (3) an easy to use interface between the two environments.

In this artcile I am going to explain how such a balance can be attained by using two of the best products in their specific worlds - using the rich R library as the mathematical/statistical component but then interfacing with C# for the front end application design. As an interfacing option I banked on using R (D)COM which provides an easy to use interfacing method which keeps you away from spending hours identifying interfacing problems.

The software required for this tutorial is the following:
1. R software (download from here)
2. R (D)COM Interface (download from here)
3. If you dont have a C# IDE you can download Visual Studio Express for free (download from here)

Once you installed all your software we can start focusing on the small example. Albeit a simple example, the example will show the flexibility that one can attain by interfacing R and C#. In the example we have a C# Windows Form which fires a request to R in order to generate a random data set from a standard normal distribution, calculate some basic statistics, and then display the data and results back on our form.

STEP 1 - Creating the Windows Form

In Visual Studio create a new Windows Form application.

STEP 2 - Add project reference to the R (D)COM library

From the solution explorer add reference to the R (D)COM library. In the COM components list you typically find the component name listed as "Repository for R COM Server Instances".

STEP 3 - Add library references to your form code

This is done by adding references as follows:

using StatConnectorCommonLib;

STEP 4 - Setting and Initializing Connection

Two variables were defined in the form class. The variable rconn of type StatConnector represents the connection to the R COM component. The variable dataSize is used to store the size of the data array that we are going to randomly generate from R.

private StatConnector rconn;
private int dataSize;

Both these variables are initialized in the default constructor of my form, in my case named frmMain.

public frmMain()
dataSize = 100;
rconn = new STATCONNECTORSRVLib.StatConnector();

STEP 5 - Invoking R commands and displaying results

The form that I created in my example had the following components:

1. A listbox which is used to present the random numbers generated from R
2. Two labels which present the Mean and Standard Deviation of the data calculated from R
3. A button which kicks off the process

The button clicked handler code is presented below:

private void btnGenData_Click(object sender, EventArgs e)

       //Generate Data in R
       double[] data = (double[])rconn.GetSymbol("sdata");

      //Calculate Statistics in R
     double average = (double)rconn.GetSymbol("saverage");
     double stdev = (double)rconn.GetSymbol("sstdev");

     // Display Data and Results on Windows Form
     for (int c = 0; c < dataSize; c++)
    lblAverage.Text = "Average: " + average;
    lblStdDev.Text = "Standard Deviation: " + stdev;


As it can be seen from the above code, the interfacing mechanism is quite simple and intuitive. The only important thing to note is that the COM object returns back general object references which need to be typcasted as required. In the case of the data variable which holds the random data vetor, this is typecasted into a double[] type. In the case of a single return value form average and standard deviation this is typecasted into a double.

Pretty easy right? I found this method really easy to use and keeps you focused on the problem that you wish to solve, rather than spending hours debugging code. Using C# for your front end application also provides a lot of flexibility when it comes to building world class applications and user interfaces. Testing the method with large amounts of data, running in the hundreds of thousands, also proved to be very efficient.

A continuation of this article is available in Part 2


Anonymous said...

I am getting an exception when calling Init("R");

Exception from HRESULT: 0x80040013

NB: The OS is Windows 7.

Anthony L. Burns, Esq. said...

statconnDCOM, SWord, and statconnWS currently do NOT work with R 2.12.0

zedtaha said...

Very interssent blog thank u;)
please how can i view the summary ( R laguage) in a textbox (C#)? thank u

Anonymous said...

Dim Sc1 As New StatConnectorClass()
Sc1.Evaluate(“library (RODBC)”)
Sc1.EvaluateNoReturn("x<-sqlQuery(cn, paste(""select * from Product""))")

In above, I didnt get Ans in X, I what whole output in Table/Array format…
Can you hele me in it.
Mine email id is

Anonymous said...

I am getting an exception when calling Init("R");

Exception from HRESULT: 0x80040013

NB: The OS is Windows 7.

> version
platform i386-pc-mingw32
arch i386
os mingw32
system i386, mingw32
major 2
minor 13.0
year 2011
month 04
day 13
svn rev 55427
language R
version.string R version 2.13.0 (2011-04-13)

Anonymous said...

run installconnstatDOM()
in RStudio

Anonymous said...

What's happening here?

> installconnstatDOM()
Error: could not find function "installconnstatDOM"
> run installconnstatDOM()
Error: unexpected symbol in "run installconnstatDOM"
> run installconnstatDOM()
Error: unexpected symbol in "run installconnstatDOM"
> run installconnstatDOM
Error: unexpected symbol in "run installconnstatDOM"
> installconnstatdom
Error: object 'installconnstatdom' not found
> install connstatDOM()
Error: unexpected symbol in "install connstatDOM"
> install connstatDOM
Error: unexpected symbol in "install connstatDOM"
> install connstatdom
Error: unexpected symbol in "install connstatdom"
> run installconnstatDOM()
Error: unexpected symbol in "run installconnstatDOM"

Anonymous said...

I am new to R and am trying to use the R (D)Com library with 2.15.0.

I get the exception as per the first comment. Is there any solution to this problem?

LIfe is crazyy said...

Hi. I am too using R-2.15.1 and getting the same exception as said in the above comment. Has anyone found any solution. Please reply.

SlavaNetDeveloper said...

good blog :) I also write articles about .NET developing

Anonymous said...

For those getting the error:
Exception from HRESULT: 0x80040013

try following mrnye's tip on:

Worked for me.

AnonymousByChoice said...

How can i read a double array in c# code and set a r variable so that i can apply r functions on this array?

Nikhil Rajendran said...

rconn.SetSymbol("sdataSize", dataSize);

shows the error
Can't enumerate any more, because the associated data is missing (Exception from HRESULT: 0x80040002 (OLE_E_ENUM_NOMORE))
whats this ??
please Help

Anonymous said...

0x80040002 means SCN_E_INVALIDFORMAT

Seems that you have an incompatibility between rscproxy and statconnDCOM. Very bad thing, one of the software versions seems to be 3 years old or older and the one more current.

Anand Rajan said...

how to communicate with r file and access the r function.

csata said...

I have been struggliing with Exception from HRESULT: 0x80040013 for a week... I am ashamed to confess that the solution was there on the DCM homepage, wiki, how to install:

Start R as administrator (on Windows 7 and later you need to right-click the R icon and click the corresponding item)
In R, run the following commands (you must start R as administrator to do this)
options(install.packages.check.source = "no")