Setting-up Your Project

  1. Add reference of R.NET assembly to your project.
    • [Option] You also add reference of System.Numerics assembly if you like to use complex numbers; otherwise use R.NET for .NET Framework 3.5.
  2. Now, just write out your codes in your language.

Coding

First, you have to set the search path of R.dll on Windows, libR.dylib on Mac OS X, or libR.so on Linux (hereinafter all of the three are referred to as 'R library'). R.NET will throw DllNotFoundException before you call REngine.SetDllDirectory method appropriately. On Windows environment, REngine.SetDllDirectory just calls SetDllDirectory function in kernel32.dll. Read the documentation to know how REngine find R library. On Mac OS X and Linux environment, after REngine.SetDllDirectory is called, REngine uses R library in the directory you set.

Example:
// Windows x86 (note that installation hierarchy is changed from version 2.12)
REngine.SetDllDirectory(@"C:\Program Files\R\R-2.12.0\bin\i386");

// Mac OS X
REngine.SetDllDirectory("/Library/Frameworks/R.framework/Libraries");

// Linux (in case of libR.so exists in the directory)
REngine.SetDllDirectory("/usr/lib");

For your information, on Windows, you may find the directory via Windows registry.

Also, R_HOME environment variable should be set as the location where R is installed. On Windows, R can find it via Windows registry so you may do nothing.

Data Types

All expressions in R are represented in SymbolicExpression class in R.NET. For data accession, the following special classes are defined.

Table. Classes in R.NET bridges between R and .NET Framework.
R R.NET .NET Framework Note
character vector RDotNet.CharacterVector System.String[]
integer vector RDotNet.IntegerVector System.Int32[] The minimum value in R is -2^31+1 while that of .NET Framework is -2^31.
real vector RDotNet.NumericVector System.Double[]
complex vector RDotNet.ComplexVector System.Numerics.Complex[] System.Numerics assembly is required for .NET Framework 4.
raw vector RDotNet.RawVector System.Byte[]
logical vector RDotNet.LogicalVector System.Boolean[]
character matrix RDotNet.CharacterMatrix System.String[, ]
integer matrix RDotNet.IntegerMatrix System.Int32[, ] The minimum value in R is -2^31+1 while that of .NET Framework is -2^31.
real matrix RDotNet.NumericMatrix System.Double[, ]
complex matrix RDotNet.ComplexMatrix System.Numerics.Complex[, ] System.Numerics assembly is required for .NET Framework 4.
raw matrix RDotNet.RawMatrix System.Byte[, ]
logical matrix RDotNet.LogicalMatrix System.Boolean[, ]
list RDotNet.GenericVector From version 1.1.
data frame RDotNet.GenericVector From version 1.1. RDotNet.DataFrame class is also available (below).
data frame RDotNet.DataFrame From version 1.3.
function RDotNet.Function From version 1.4. Including closure, built-in function, and special function.

Parsing R Scripts

R.NET has two ways to parse R scripts: REngine.Parse method and REngine.EagerParse method. Parse method generates results for each statement; EagerParse method returns the last evaluation. Keep it in mind that Parse method does not immediately evaluate the argument and that EagerEvaluate method literally evaluates the argument as soon as you mention the statement.

Example:
// Evaluate method delays an effect on the R environment.
var e = engine.Evaluate("x <- 3");
// Error: GetSymbol method returns null at the moment.
// NumericVector x = engine.GetSymbol("x").AsNumeric();

// Evaluates the statement.
e.ToArray();
// You can now access x defined in the R environment.
NumericVector x = engine.GetSymbol("x").AsNumeric();

// EagerEvaluate method evaluates the statement as soon as you call it.
engine.EagerEvaluate("y <- 1:10");
NumericVector y = engine.GetSymbol("y").AsNumeric();

Last edited Feb 26, 2011 at 6:12 AM by kos59125, version 7

Comments

Gravitas Jan 5 at 8:48 PM 
Has anyone managed to get it to work with VS2012?

jdizzy769 Aug 16, 2012 at 7:06 PM 
I return hat in hand after my previous post. Although I have not yet tested the memory management and garbage collection, which was the cause of all of my frustrations last time, v1.5 is just plain AWESOME. I am giving it another shot. Thank you for the update!!

QuidProMS Mar 26, 2012 at 11:36 PM 
In response to jdizzy769, i think you have it wrong. This is a great adult project, albeit a work in progress with some bugs, that works quite well if the programmer avoids the buggy usages, which seem to involve multiple R instances (see below). The whole idea of anything dotnet is NOT to have to *explicitly* use COM, e.g. R(D)-COM. R.Net seems to manage the memory involved across the managed/unmanaged boundaries quite well, but more testing on my part will tell, and that makes programming a lot easier. There are some features I of R.Net I don't understand, but the idea of getting delegate for an R.dll internal, var cos = engine.GetSymbol("cos").AsFunction(), is very powerful.
In response to those who want multiple instances of the REngine, just glancing at the code, it seems like the problem is that the R.dll is LoadLibrary() loaded multiple times into the same process and upon calling yet again "setup_Rmainloop()", the application is locked. I don't know much about the R.dll code, but I can guess it is not threadsafe, and it is an error to call "setup_Rmainloop()" again before exiting the main loop. So the multiple instance issue seems to demand either one process per instance, with the concomitant added complexity to R.Net, or a revision of the R.dll to support multiple instances.

aoldevstat Oct 20, 2011 at 2:14 AM 
I've quickly become a fan of the R.NET and started using it in real solutions even though it took some additional work. This is pretty fast, much more faster than RDCOM library. Please don't leave it and develop it better and better. I agree that error handling still needs work. Why not through the ParseException? Using ICharacterDevice to do that isn't the best way in my opinion...

FrankyHollywood Oct 13, 2011 at 3:37 PM 
The idea is very good, however it keeps crashing constantly. Once a crash has occured I have to restart VS2010 again before I can send any command to the engine before it responds without error (probably the com component which need te reset or something). I'm trying to build a webservice, which needs multiple R sessions. Maybe a winforms app with just 1 engine instance works better.

conclusion, nice project, pretty simple and straightforward interface, but session management and engine response/error handling need some work. That would make it production worthy :)

tylt Aug 19, 2011 at 8:03 AM 
I think it's a worthwhile project, and I'm glad it exists. Definitely needs a lot more work, but I think it'll evolve to be more useful. COM is reliable, but the performance is pretty poor.

jdizzy769 Aug 18, 2011 at 3:31 PM 
VERY buggy and terribly supported. This is merely a hobby project, and a juvenile one at that. Use R(D)-COM if you need it to actually work.