Click here to Skip to main content
11,926,805 members (54,509 online)
Click here to Skip to main content
Add your own
alternative version


92 bookmarked

The R Statistical Language and C#.NET: Foundations

, 14 May 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Use the R Language in C#.NET applications.


Over a decade ago, my colleagues and I wrote two books on using different tests for examining the assumptions of time series analysis in both the univariate and multivariate contexts. Back then, the programs to conduct these tests were a mixture of Basic, C, and the use of some batch programs in commercial packages such as RATS, SHAZAM, and TSP.

  • Cromwell, J.B., M.J. Hannan, W.C. Labys, and M. Terraza. 1994. Multivariate Testing for Time Series Models. New York: Sage Publication.
  • Cromwell, J.B., W.C. Labys and M. Terraza. 1994. Univariate Testing for Time Series Models. New York: Sage Publications.

Today, other packages such as SAS, SPSS, Ox, Matlab, and S-Plus can be used to do similar work. However, a favorite open source statistical package of mine was and continues to be developed by researchers at Carnegie Mellon University, known as the statistical language R. The R language can be downloaded from here. Once installed, go to the Package menu in R and select all the packages, and hit Enter. This will install all the libraries so that the baseline Splus and R are the same. R uses the S language, and Fox (2002) and Farnsworth (2006) provide excellent references for doing the time series analysis mentioned in my two books. In a series of papers, I plan to demonstrate using R and C#.NET for a wide variety of statistical and analytical applications that can be built in a typical 3-4 hours of work.


Besides using the GUI shown in Figure 1, I wanted to be able to use R as an intelligence component in my research applications. At the University, publication for applied work is always going to need the latest statistical techniques, and for this, a programming language. Because of my background in both .NET and Java, I chose to use C#.NET for the interface language to build these middleware pieces. I downloaded Microsoft Visual C# 2008 Express Edition, and wanted to attach the references for the R .NET component to begin the work of this middleware application. Since there was not a .NET component bundled with R, I used the R-(D) COM interface for the automation.

Figure 1. The GUI interface to R

R in .NET - The R(D) COM interface

The R-(D) COM interface is a COM server that can connect to applications such as Microsoft Excel to take advantage of the statistical techniques that are constantly being added to the R releases. In addition, there are ActiveX controls that can be included in the application. Thus, data can be transferred in and out of R, and Visual Basic, C#, and Perl can serve as COM clients that enable one to use R as a computational engine that also can render graphics. The COM server can be downloaded and installed by following these steps:

  1. Go to
  2. Choose "Other" item from the Software menu
  3. Choose R-(D) COM Interface (for Windows)
  4. Choose RServer250.exe
  5. Install the application

Once installed, you can add the following COM references to your C#.NET project, as well as these namespaces to your class:

// COM References
using StatConnectorCommonLib;

Each of these type libraries uses the .NET interoperability feature for operation. Use the using statements in the code.

The code to use the .NET component as an interface to R is quite simple, and has only a few statements that can execute data in, process it, and then extract it. For example, to generate twenty random normal numbers, one uses:

object o1;
int n=20;
StatConnector sc1 = new StatConnectorSRVLib.StatConnectorClass();
Xrnd=(double) o1;

Once we instantiate the class with sc1 and initialize R, we can then generate data with a declaration of n1 as the object type in R and n to be the integer type - the number of normal random numbers we want to generate. The SetSymbol() function is used cast the C# type to an R type. Then, we can use the GetSymbol() function to get the variable, and then cast it to an object for processing. For more understanding of the R data types, examine the help documentation that comes with R.

Importing the data

While we can generate data within R, it would also be instructive to import data into R. For example, you can read data from more than one data format. The most common format is a text file, with data separated into columns, with a header for each column. I created a list of numbers, one in each line, in Notepad, to import into R. This chooses the file through a dialog.

>mydata = read.table( file.chooseO, skip = 1 ) 

The file.choose() function provides the dialog to choose the text file, and the skip=1 parameter says to skip the first line for the Header value. The R variable is mydata and the name of values is V1. To see the data:


In the dialog window, the values are shown for the variable V1. Suppose, I would like to read in these data values into a variable that represents a yearly time series that represents a stock’s values:

>zl <-ts(mydata,start=c(1992,8), 1) 

I can use the following ts() function that starts with the year 1999, with the frequency being yearly. Of course, I can plot these numbers to verify as well as look at the values of z1. Now that we can do this in the R application, how do we do this in C#.NET and not use the R Windows interface? Importing can be problematic when taking the expressions directly over to C#, so I use a traditional text file or database work to populate the variables and then use the SetSymbol() function to import the values into R and then do transformations. However, we can use the EvaluateNoReturn() function to read in the data with:


The difference between Evaluate() and EvaluateNoReturn() is that the former returns the reference to an object, which is necessary when casting and extracting data from the R environment. As a rule, this is how I prototype and test my statistical components, write them for execution in the R Windows interface, save them to text, and then read the file one line at a time with one evaluate function. Then, you can modify the text files, or combine multiple text files for statistical processing. This provides for a very creative modeling and analytical environment. Besides the computational aspects mentioned, one can also render graphics through the using statement:


The R graphics device

The code to generate a histogram plot of twenty normal random variables is provided by:

StatConnector test1= new STATCONNECTORSRVLib.StatConnectorClass(); 

The R graphics device populates its own dialog window with the histogram results. This nice feature automatically provides additional graphical resources and libraries for doing 2D and 3D charting. The dialog window permits the ability to save in a variety of graphics format, print, and save to the clipboard. Furthermore, you can examine the history and resize options. Again, refer to the R documentation for the necessary expressions and packages.

Figure 2. Histogram of normal random numbers

As one can see, an understanding of the R language and the use of the bundled packages permit the opportunity to build a business forecasting or analytical application quickly. Download some stock data from Yahoo!, and throw it into SQL Express and use LINQ for the OEM, and reference the R COM component, download some packages, and forecast some stocks using the latest methods in time series analysis. This is the subject of Part II of the series.


Using the open source R statistical language is a good way to incorporate the latest statistical research into your business intelligence prototypes and applications, as it presents a low cost alternative for the development and implementation of statistical algorithms for predictive analytics not available in standard commercial packages. I have used R in this fashion for doing wavelet analysis for price analytics, watershed modeling for acid mine drainage, and for stochastic volatility modeling of stocks using Bayesian Markov Chain Monte Carlo (MCMC) techniques. In future articles, I plan to show how to build these types of applications in 3-4 hours of programming work.


  • Farnsworth, G.V. (2006), Econometrics in R.
  • Fox, J. (2002), An R and S-Plus Companion to Applied Regression, Thousand Oaks, CA: Sage Publications.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jeff B. Cromwell
CEO The Cromwell Workshop
United States United States
Dr. Jeff B. Cromwell is the CEO/Neuroeconomist at The Cromwell Workshop.

Scholar Site:
Web Site:

You may also be interested in...

Comments and Discussions

QuestionR language - Graphics Device Pin
SOHAM_GANDHI27-Dec-14 22:15
professionalSOHAM_GANDHI27-Dec-14 22:15 
GeneralMy vote of 1 Pin
Member 1102168219-Aug-14 1:36
memberMember 1102168219-Aug-14 1:36 
QuestionException from HRESULT: 0x80040013 Pin
yuqiyang10-Jul-14 0:20
memberyuqiyang10-Jul-14 0:20 
Questionthank you Pin
kooshaqaf4-Mar-13 21:03
memberkooshaqaf4-Mar-13 21:03 
QuestionThread on StackOverflow with more information. Pin
smt525-Feb-13 1:24
membersmt525-Feb-13 1:24 

This link has more hints on how to pull graphs from R into .NET:[^]
SuggestionAternatives to StatConnector Pin
Adrian Olszewski6-Nov-12 1:42
memberAdrian Olszewski6-Nov-12 1:42 
GeneralRe: Aternatives to StatConnector Pin
Adrian Olszewski6-Nov-12 1:44
memberAdrian Olszewski6-Nov-12 1:44 
QuestionRServer250.exe is missing Pin
vidushiSrivastava7-Sep-12 3:30
membervidushiSrivastava7-Sep-12 3:30 
QuestionSeeking a word of advice on persisting an instance of StatConnector Pin
abfabsabs27-Jul-12 14:20
memberabfabsabs27-Jul-12 14:20 
NewsHow I was able to get this thing working! Pin
GR3YF0X7-Feb-12 19:35
memberGR3YF0X7-Feb-12 19:35 
QuestionR(D)COM server outdated Pin
neuwirthe30-Nov-11 3:08
memberneuwirthe30-Nov-11 3:08 
AnswerRe: R(D)COM server outdated Pin
Jeff B. Cromwell30-Nov-11 15:36
memberJeff B. Cromwell30-Nov-11 15:36 
QuestionThis can not work well with 64bit visual studio. Pin
Fan_Wang2-Nov-11 7:31
memberFan_Wang2-Nov-11 7:31 
AnswerRe: This can not work well with 64bit visual studio. Pin
Fan_Wang2-Nov-11 7:44
memberFan_Wang2-Nov-11 7:44 
AnswerRe: This can not work well with 64bit visual studio. Pin
neuwirthe30-Nov-11 3:39
memberneuwirthe30-Nov-11 3:39 
AnswerRe: This can not work well with 64bit visual studio. Pin
PvdG8-Apr-12 11:03
memberPvdG8-Apr-12 11:03 
QuestionUse the interface instead? Pin
rkb28-Jun-11 9:03
memberrkb28-Jun-11 9:03 
AnswerRe: Use the interface instead? Pin
Jim Gaull9-Sep-11 9:02
memberJim Gaull9-Sep-11 9:02 
AnswerRe: Use the interface instead? Pin
Jaqinetik29-Sep-11 9:03
memberJaqinetik29-Sep-11 9:03 
GeneralR connection Pin
codennr23-May-11 0:41
membercodennr23-May-11 0:41 
QuestionHow to get p-values and other regression attributes from R to C#? Pin
Member 793032617-May-11 0:48
memberMember 793032617-May-11 0:48 
GeneralCOM error Pin
shekarchee27-Mar-11 20:57
membershekarchee27-Mar-11 20:57 
GeneralR with C# Pin
shekarchee27-Mar-11 20:22
membershekarchee27-Mar-11 20:22 
GeneralMy vote of 5 Pin
catmeo13-Mar-11 18:57
membercatmeo13-Mar-11 18:57 
QuestionArticle Viewer Pin
Mohammad Afrashteh9-Feb-11 22:36
memberMohammad Afrashteh9-Feb-11 22:36 
GeneralLIBRARYNOTFOUND -2147221485 0x80040013 Pin
agoncalves26-Jan-11 8:04
memberagoncalves26-Jan-11 8:04 
GeneralException from HRESULT: 0x80040013 Pin
caoboidibo4-Oct-10 19:07
membercaoboidibo4-Oct-10 19:07 
GeneralRe: Exception from HRESULT: 0x80040013 Pin
maestroagressor5-Oct-10 5:40
membermaestroagressor5-Oct-10 5:40 
GeneralRe: Exception from HRESULT: 0x80040013 Pin
Asad Abbasi7-Apr-11 6:58
memberAsad Abbasi7-Apr-11 6:58 
GeneralRe: Exception from HRESULT: 0x80040013 Pin
andra.lia6-May-11 0:33
memberandra.lia6-May-11 0:33 
GeneralRe: Exception from HRESULT: 0x80040013 Pin
Andra Lia22-May-11 2:21
memberAndra Lia22-May-11 2:21 
GeneralRe: Exception from HRESULT: 0x80040013 Pin
rkb28-Jun-11 9:11
memberrkb28-Jun-11 9:11 
GeneralRe: Exception from HRESULT: 0x80040013 Pin
Andra Lia29-Jun-11 12:58
memberAndra Lia29-Jun-11 12:58 
GeneralRe: Exception from HRESULT: 0x80040013 Pin
rkb29-Jun-11 18:44
memberrkb29-Jun-11 18:44 
GeneralRe: Exception from HRESULT: 0x80040013 Pin
Andra Lia29-Jun-11 23:49
memberAndra Lia29-Jun-11 23:49 
GeneralRe: Exception from HRESULT: 0x80040013 Pin
rkb30-Jun-11 2:52
memberrkb30-Jun-11 2:52 
GeneralRe: Exception from HRESULT: 0x80040013 Pin
Will Beasley30-Jun-11 5:57
memberWill Beasley30-Jun-11 5:57 
GeneralRe: Exception from HRESULT: 0x80040013 Pin
rkb30-Jun-11 11:41
memberrkb30-Jun-11 11:41 
GeneralRe: Exception from HRESULT: 0x80040013 Pin
TheronK8-Sep-11 18:26
memberTheronK8-Sep-11 18:26 
GeneralMy vote of 5 Pin
Gmust14-Sep-10 11:15
memberGmust14-Sep-10 11:15 
GeneralR program Pin
senthilnathan thandapani27-Jun-10 19:25
membersenthilnathan thandapani27-Jun-10 19:25 
GeneralLM and GLM function Pin
Member 30695511-May-10 10:24
memberMember 30695511-May-10 10:24 
GeneralRe: LM and GLM function Pin
michaelnye26-May-10 15:41
membermichaelnye26-May-10 15:41 
GeneralRe: LM and GLM function Pin
Member 477242813-Aug-10 2:55
memberMember 477242813-Aug-10 2:55 
GeneralEmbed R plot in C# windows form Pin
vats8512-Jun-09 11:01
membervats8512-Jun-09 11:01 
GeneralRe: Embed R plot in C# windows form Pin
hhhaui17-Jul-09 3:13
memberhhhaui17-Jul-09 3:13 
GeneralRe: Embed R plot in C# windows form Pin
retool21-Nov-09 9:07
memberretool21-Nov-09 9:07 
Newsrscproxy package Pin
Will Beasley23-May-09 18:15
memberWill Beasley23-May-09 18:15 
GeneralPassing data from C# into R Pin
Tony49666-Apr-09 1:10
memberTony49666-Apr-09 1:10 
GeneralRe: Passing data from C# into R Pin
Member 477242813-Aug-10 2:57
memberMember 477242813-Aug-10 2:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 14 May 2008
Article Copyright 2008 by Jeff B. Cromwell
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid