Skip to main content

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;
using STATCONNECTORSRVLib;

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()
{
InitializeComponent();
dataSize = 100;
rconn = new STATCONNECTORSRVLib.StatConnector();
rconn.Init("R");
}

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)
{
        rconn.SetSymbol("sdataSize",dataSize);


       //Generate Data in R
       rconn.Evaluate("sdata<-rnorm(sdataSize)");
       double[] data = (double[])rconn.GetSymbol("sdata");


      //Calculate Statistics in R
      rconn.Evaluate("saverage<-mean(sdata)");
      rconn.Evaluate("sstdev<-sd(sdata)");
     double average = (double)rconn.GetSymbol("saverage");
     double stdev = (double)rconn.GetSymbol("sstdev");




     // Display Data and Results on Windows Form
     lbDataList.Items.Clear();
     for (int c = 0; c < dataSize; c++)
    {
       lbDataList.Items.Add(data[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

Comments

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

Exception from HRESULT: 0x80040013

NB: The OS is Windows 7.
statconnDCOM, SWord, and statconnWS currently do NOT work with R 2.12.0

http://rcom.univie.ac.at/
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.Init(“R”)
Sc1.Evaluate(“library (RODBC)”)
Sc1.EvaluateNoReturn(“cn<-odbcConnect(""DSN_server"",""sa"",""password"")")
Sc1.EvaluateNoReturn("x<-sqlQuery(cn, paste(""select * from Product""))")
Sc1.Evaluate("x")

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 nadir.riyani@msbc.in
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
status
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.
Anonymous said…
For those getting the error:
Exception from HRESULT: 0x80040013

try following mrnye's tip on:
http://stackoverflow.com/questions/5076732/using-rdcom-for-integrating-r-with-c-sharp

Worked for me.
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?
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.
Season Sounds 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")
install.packages(c("rscproxy","rcom"),repos="http://rcom.univie.ac.at/download",lib=.Library,type="win.binary")
library(rcom)
comRegisterRegistry()


Anonymous said…
Very useful information. Thank you for sharing this

Dot Net training center in Chennai | Dot Net course in Chennai
blackkutty said…
Exceptionally helpful data. Much obliged to you for sharing this
Article Submission sites
Ishu Sathya said…

Thanks for taking time to discuss about this technology. I love to learn more about this topic. If possible. as you gain experience update your blog with more information? It is extremely helpful for me.
Selenium Training Chennai
software testing selenium training
sociogramics said…
The next time I read a blog, Hopefully it does not disappoint me just as much as this one. After all, Yes, it was my choice to read, however I really believed you'd have something interesting to talk about. All I hear is a bunch of crying about something you could possibly fix if you were not too busy seeking attention.
Tech info

Popular posts from this blog

Interfacing C# .Net and R - Integrating the best of both worlds (Part 2)

This post is a continuation from the previous post ( Part I ) focusing on interfacing C# with R using the R (D)COM. In this post I am going to enhance my previous exercise by creating a Facade .Net Class which facilitates access to specific functions in R. Creating the R Facade Class Creating a Facade Class (or a set of .Net classes) which acts as a .Net wrapper to R functions greatly facilitate the use of R functions and their integration within the .Net programming environment. Below I am showing an excerpt from the class RFacade that I have created in this example. using System; using System.Collections.Generic; using System.Linq; using System.Text; using StatConnectorCommonLib; using STATCONNECTORSRVLib; using System.Runtime.InteropServices; namespace R { class RFacade : IDisposable {      private StatConnector rconn;      private bool disposed = false;      public RFacade()    ...

Interfacing C# .Net and R - Integrating the best of both worlds (Part 3) - Trader Desk Example

In this third and final part of the series ( Part 1 , Part 2 ), I am going to continue shaping the examples in the previous posts by quickly building a small application - a simple Trading Desk application. In the example I will use the same C# and R interface method together with a specific Quantitative Financial Modelling R library called Quantmod . Example Objective In the example the objective is to build a C# Web Form which acts as the main application controller that captures various user option parameters with the main functions being: 1. To automatically kick off an R routine to download data from Yahoo Finance. 2. To select specific dates of interest 3. To add or udpate different charts 4. To add different chart indicators 5. To calculate Period Return Statistics Most of these functions are provided through the set of R functions exposed by the Quantmod R library. Additions under the hood Below I am showing some of the salient additions done to the code over and abov...