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()
{
rconn = new STATCONNECTORSRVLib.StatConnector();
rconn.Init("R");
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
if (rconn != null)
{
Marshal.ReleaseComObject(this.rconn);
rconn = null;
}
}
disposed = true;
}
}
.... continued
The RFacade constructor handles the creation of the COM connection and initialisation. As you can notice the above example presents also a slight improvement from the previous post when it comes to closing the connection. The RFacade class implements the IDisposable interface to ensure that all unmanaged resources are released when the wrapper is no longer used.
Setting and Getting Data from R
Although in the previous post I showed examples how this can be done, in this example I am going to provide methods in my RFacade class which further simplify the setting and getting of data from R. The two RFacade methods are show below. The first method, RGetRandomNormalVector, creates a vector on R which in return is populated by R with a set of random numbers from a Standard Normal Distribution. The second method, RSetVector, provides similar functionality but this time the data is provided by the C# front end. These two methods show how vectors can be passed easily back and forth between the two layers.
//Function to create an R vector of name [VectorName] and size [size]
//and generates random numbers from normal distribution
public double[] RGetRandomNormalVector (String VectorName, int size)
{
String RCommand = VectorName + "<-rnorm(" + size + ")";
double[] data = (double[])rconn.Evaluate(RCommand);
return data;
}
//Function to create an R vector of name [VectorName] with specific data [data]
public void RSetVector(String VectorName, double[] data)
{
int vectorSize = data.Count();
StringBuilder stringData = new StringBuilder(vectorSize);
for (int c=0; c<=(vectorSize-1); c++) {
stringData.Append(data[c]);
if (c < (vectorSize-1) ) {
stringData.Append(",");
}
}
String RCommand = VectorName + "<-c(" + stringData + ")";
rconn.EvaluateNoReturn(RCommand);
}
... continued
Exposing R functions
At this stage we have everything ready to start using some R functions. In this example I am going to provide some simple functions which provide a good indication of how one can extend this functionality.
//Function to Calculate Correlation using R cor function
public double RCorrelate(String var1, String Var2)
{
String RCommand = "cor(" + var1 + "," + Var2 + ")";
return (double)rconn.Evaluate(RCommand);
}
//Function to plot a simple XY graph using R graphics
public void RPlot(String var1, String Var2)
{
String RCommand = "plot(" + var1 + "," + Var2 + ")";
rconn.EvaluateNoReturn(RCommand.ToString());
}
//Function to Calculate Linear Regression using R lm function
//Returns a double array, [0] intercept [1] gradient
public double[] RLinearRegression(String dependentVar, String independentVar)
{
String RCommand = "fit <- lm("+ dependentVar + " ~ " + independentVar +")";
rconn.EvaluateNoReturn(RCommand);
return (double[])rconn.Evaluate("fit$coefficients");
}
Using R from C# .Net is easier...
Once we have our Facade class and also a set of common functions, making use of R becomes much easier from C#. The exerpt below provides examples of method calls from my main Form which accesses a number of exposed R functions via the RFacade object instance RInstance.
double[] year = new double[] { 2000, 2001, 2002, 2003, 2004 };
double[] rate = new double[] { 9.34, 8.50, 7.62, 6.93, 6.60 };
RInstance.RSetVector("year", year);
RInstance.RSetVector("rate", rate);
double corr = RInstance.RCorrelate("year", "rate");
RInstance.RPlot("year", "rate");
double[] regressionCoeff = RInstance.RLinearRegression("rate", "year");
double[] data = RInstance.RGetRandomNormalVector("mydata", dataSize);
This article is continued in a third and final post Part 3.
Comments
http://joachimvandenbogaert.wordpress.com/2009/02/20/rcom-and-c-safearraytypemismatchexception/
but i have found some bugs
when i tried to execute this method:
double corr = RInstance.RCorrelate("year", "rate");
i became the error that the vars should not be static.
in this case you have to add rconn.SetSymbol(...) extended
thx
Nam
I load a CVS file in R through C# (using statconn). Then, I can not get the data.frame in c#, I only get a null value.Th code:
R.EvaluateNoReturn("c<-read.csv(file=path,head=FALSE,stringsAsFactors = FALSE,sep=',')");
object[] c = R.Evaluate("c");
If I do this
object[] c2 = R.Evaluate("cierres[2,2]");
then I get the value value.
Can I have some help to get th dataframein c# please?
Thanks in advance
Rubo
Best JAVA Training in Chennai
JAVA Training
dot net course training in coimbatore
php training in coimbatore
Digital Marketing courses in Bangalore
opencu
Technology
LoadRunner Training in Chennai
Best Loadrunner training institute in chennai
Loadrunner Training in Velachery
Loadrunner Training in Anna Nagar
QTP Course in Chennai
Best QTP Training Institutes in Chennai
Best Loadrunner training institute in chennai
hp loadrunner training