Setting-up Your Project
- 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.
- Now, just write out your codes in your language.
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)
// Mac OS X
// Linux (in case of libR.so exists in the directory)
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.
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.
// 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();