Setting-up Your Project
- Add reference of RDotNet and RDotNet.NativeLibrary to your project. Note that RDotNet.NativeLibrary is OS dependent. For Windows, get it from RDotNet.Windows.zip or NuGet gallery; for Mac OS and Linux, choose RDotNet.Unix.zip.
- 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:
var oldPath = System.Environment.GetEnvironmentVariable("PATH"); // Windowsvar rPath = System.Environment.Is64BitProcess ? @"C:\Program Files\R\R-3.0.1\bin\x64" : @"C:\Program Files\R\R-3.0.1\bin\i386"; // Mac OS Xvar rPath = "/Library/Frameworks/R.framework/Libraries"; // Linux (in case of libR.so exists in the directory)var rPath = "/usr/lib"; var newPath = System.String.Format("{0}{1}{2}", rPath, System.IO.PathSeparator, oldPath);
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[, ] | Reference to System.Numerics assembly is required. |
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:
// Defer method delays an effect on the R environment.var e = engine.Defer("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(); // Evaluate method evaluates the statement as soon as you call it. engine.Evaluate("y <- 1:10"); NumericVector y = engine.GetSymbol("y").AsNumeric();