Quantcast
Channel: R.NET
Viewing all 1634 articles
Browse latest View live

New Post: System Invalid Operation Exception - R.Net

$
0
0
Thank you for that update.

This didn't work. forgive the less than technical explanation but because the Fidelity product left the program "open" in some sense and allowed you to run it on different ticker symbols it tried to initialize the engine more than once

I got rid of the public REngine Instance above and did the following, first calling GetInstanceFromID
It was quite confusing at first. Now I'm onto trying to understand differences between NumericVector and double arrays. Always fun learning new languages.
            // Put R in the PATH
            var oldPath = System.Environment.GetEnvironmentVariable("PATH");
            var rPath = System.Environment.Is64BitProcess ? @"C:\Program Files\R\R-3.0.3\bin\x64" : @"C:\Program Files\R\R-3.0.3\bin\i386";
            var newPath = string.Format("{0}{1}{2}", rPath, System.IO.Path.PathSeparator, oldPath);
            System.Environment.SetEnvironmentVariable("PATH", newPath);

            REngine r = REngine.GetInstanceFromID("RDotNet");

            if (r == null)
            {
                r = REngine.CreateInstance("RDotNet");
                r.Initialize();
            }

Updated Wiki: User documentation for the next version

$
0
0

Introduction

Relation to F# RProvider

Getting set up

There is a page gathering Software Prerequisites listing the platforms on which R.NET is known to run.
As of version 1.6, R.NET binaries are platform independent. You might need to set up a small add-on workaround on some Linux distributions (CentOS a known one), but you can just move and use the R.NET binaries across platforms.
Assuming you have the right Software Prerequisites, you can obtain R.NET binaries from two sources
  1. The codeplex web site, by downloading a zip file
  2. The nuget.org web site

Visual Studio

NuGet is the preferred way to manage dependencies on R.NET. Consider giving it a try: this is the way of the future for .NET projects...

If you are using the binaries from the zip file distribution, unzip the file and copy the content to a location of your choice. Add project references to RDotNet.dll and RDotNet.Native.dll the "usual" way.

If you are using the NuGet packages:

You first have to install, if you have not already, the NuGet package manager via Tools - Extension and Updates:
0001.png

You can add the R.NET package as a dependency to one or more projects in your solution. For one project:
001.png

002.png

The NuGet system then adds a couple of references.
003.png

You can manage several projects in one go at the solution level:
004.png

You can find more general information about NuGet at NuGet documementation

Xamarin Studio

This section is a placeholder.

Getting started

Version 1.6 of R.NET includes significant changes notably to alleviate two stumbling blocks often dealt with: paths to the R shared library, and preventing multiple engine initializations.

The following "Hello World" sample illustrates how the new API is simpler in 90% of use cases on Windows:

staticvoid Main(string[] args)
{
	REngine.SetEnvironmentVariables(); // <-- May be omitted; the next line would call it.
	REngine engine = REngine.GetInstance();
	// A somewhat contrived but customary Hello World:
	CharacterVector charVec = engine.CreateCharacterVector(new[] { "Hello, R world!, .NET speaking" });
	engine.SetSymbol("greetings", charVec);
	engine.Evaluate("str(greetings)"); // print out in the consolestring[] a = engine.Evaluate("'Hi there .NET, from the R engine'").AsCharacter().ToArray();
	Console.WriteLine("R answered: '{0}'", a[0]);
	Console.WriteLine("Press any key to exit the program");
	Console.ReadKey();
	engine.Dispose();
}

You retrieve a single REngine object instance, after setting the necessary environmental variables. Even the call to SetEnvironmentVariables can be omitted, though we'd advise you keep it explicit. SetEnvironmentVariables looks at the Registry settings set up by the R installer on Windows. If need be, you can override the behaviours setting the environment variables and engine initialization with your own steps, detailed in the Appendix.

On Linux/MacOS, the path to libR.so (for Linux) must be in the environment variable LD_LIBRARY_PATH before the process start, otherwise the R.NET engine will not properly initialize. If this is not set up, R.NET will throw an exception with a detailed message. Read the Appendix at the end of this page if R.NET complains about your LD_LIBRARY_PATH.

Sample code

The following sample code illustrate the most use capabilities. It is extracted from the sample code 2 at https://github.com/jmp75/rdotnet-onboarding, as of 2014-04.

You usually interact with the REngine object with the methods Evaluate, GetSymbol, and SetSymbol. To create R vector and matrices, the REngine object has methods such as CreateNumericVector, CreateCharacterMatrix, etc. Finally, you can invoke R functions in a variety of ways, using Evaluate, and also more directly.

Let's re-emphasize the need to set up the environment variables, creating the engine, and also initializing it.
SetupHelper.SetupPath(); // See earlier sample code
REngine engine = REngine.CreateInstance("RDotNet")
engine.Initialize();

Basic operations with numeric vectors
var e = engine.Evaluate("x <- 3");
// You can now access x defined in the R environment
NumericVector x = engine.GetSymbol("x").AsNumeric();
engine.Evaluate("y <- 1:10");
NumericVector y = engine.GetSymbol("y").AsNumeric();

While you may evaluate function calls by generating a string and call the Evaluate method, this is quickly unwieldy for cases where you pass large amounts of data. The following demonstrates how you may call a functions, a bit like how you would invoke a function reflectively in .NET. Note that the sample is designed for R.NET 1.5.5; there are syntactic improvements under development that should be available in the next release.
// Invoking functions// invoking expand.grid directly would not work as of R.NET 1.5.5, // because it has a '...' pairlist argument. We need a wrapper function.var expandGrid = engine.Evaluate("function(x, y) { expand.grid(x=x, y=y) }").AsFunction();
var v1 = engine.CreateIntegerVector(new[] { 1, 2, 3 });
var v2 = engine.CreateCharacterVector(new[] { "a", "b", "c" });
var df = expandGrid.Invoke(new SymbolicExpression[] { v1, v2 }).AsDataFrame();

Continuing with the results of our use of expand.grid, the following code illustrate that while R.NET tries to emulate the coercion behavior of R, you should be circumspect with it, notably when you know you have factors as columns in your data frame.
engine.SetSymbol("cases", df);
// Not correct: the 'y' column is a "factor". This returns "1", "1", "1", "2", "2", etc. var letterCases = engine.Evaluate("cases[,'y']").AsCharacter().ToArray();
// This returns something more intuitive for C#  Returns 'a','a','a','b','b','b','c' etc.
letterCases = engine.Evaluate("as.character(cases[,'y'])").AsCharacter().ToArray();
// In the manner of R.NET, try
letterCases = engine.Evaluate("cases[,'y']").AsFactor().GetFactors();

To reuse whole scripts, the simplest method is to use the 'source' function in R
engine.Evaluate("source('c:/src/path/to/myscript.r')");

Data Types

All expressions in R are represented as SymbolicExpression objects in R.NET. For data access, the following special classes are defined. Note that there is no direct equivalent in .NET for 'NA' values in R. Special values are used for some types but pay attention to the behaviour, so as not to risk incorrect calculations.

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. Missing values are int.MinValue
real vector RDotNet.NumericVector System.Double[] Missing values are represented as double.NaN
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. And from version 1.5.3, DataFrameRowAttribute and DataFrameColumnAttribute are available for data mapping.
function RDotNet.Function From version 1.4. Including closure, built-in function, and special function.
factor RDotNet.Factor System.Int32[] From version 1.5.2.
S4 RDotNet.S4Object Not Available Yet. See S4 branch in the source control.

Acknowledgements

  • Daniel Collins found the workaround for the native library "libdl" loader that was not working on at least some CentOS Linux distributions.
  • evolvedmicrobe contributed to several features to run on MacOS and Linux, and initiated the changes to make R.NET platform independent.
  • Kosei initiated R.NET
  • gchapman

Appendices

Updating environment variables on Linux (MacOS?)

The path to libR.so (for Linux) must be in the environment variable LDLIBRARYPATH before the process start, otherwise the R.NET engine will not properly initialize. If this is not set up, R.NET will throw an exception with a detailed message.

What you will need to do there depends on the Linux (MacOS?) machine you are.
Let's say you needed to compile your own R from source, to get a shared R library:

LOCAL_DIR=/home/username/local
JAVAHOME=/apps/java/jdk1.7.0_25
cd ~src
cd R/
tar zxpvf R-3.0.2.tar.gz
cd R-3.0.2
./configure --prefix=$LOCAL_DIR --enable-R-shlib  CFLAGS="-g"
make
make install

Then prior to running a project with R.NET, you may need to update your LDLIBRARYPATH, and quite possibly PATH (though the latter can be done at runtime too).

LOCAL_DIR=/home/username/local
if [ "${LD_LIBRARY_PATH}" != "" ]
then
    export LD_LIBRARY_PATH=$LOCAL_DIR/lib:$LOCAL_DIR/lib64/R/lib:/usr/local/lib64:${LD_LIBRARY_PATH}
else
    export LD_LIBRARY_PATH=$LOCAL_DIR/lib:$LOCAL_DIR/lib64/R/lib:/usr/local/lib64
fi
# You may as well update the PATH environment variable, though R.NET does update it if need be.
export PATH=$LOCAL_DIR/bin:$LOCAL_DIR/lib64/R/lib:${PATH}

Updated Wiki: User documentation for the next version

$
0
0

Introduction

Relation to F# RProvider

Getting set up

There is a page gathering Software Prerequisites listing the platforms on which R.NET is known to run.
As of version 1.6, R.NET binaries are platform independent. You might need to set up a small add-on workaround on some Linux distributions (CentOS a known one), but you can just move and use the R.NET binaries across platforms.
Assuming you have the right Software Prerequisites, you can obtain R.NET binaries from two sources
  1. The codeplex web site, by downloading a zip file
  2. The nuget.org web site

Visual Studio

NuGet is the preferred way to manage dependencies on R.NET. Consider giving it a try: this is the way of the future for .NET projects...

If you are using the binaries from the zip file distribution, unzip the file and copy the content to a location of your choice. Add project references to RDotNet.dll and RDotNet.Native.dll the "usual" way.

If you are using the NuGet packages:

You first have to install, if you have not already, the NuGet package manager via Tools - Extension and Updates:
0001.png

You can add the R.NET package as a dependency to one or more projects in your solution. For one project:
001.png

002.png

The NuGet system then adds a couple of references.
003.png

You can manage several projects in one go at the solution level:
004.png

You can find more general information about NuGet at NuGet documentation

Xamarin Studio

This section is a placeholder.

Getting started

Version 1.6 of R.NET includes significant changes notably to alleviate two stumbling blocks often dealt with: paths to the R shared library, and preventing multiple engine initializations.

The following "Hello World" sample illustrates how the new API is simpler in 90% of use cases on Windows:

staticvoid Main(string[] args)
{
	REngine.SetEnvironmentVariables(); // <-- May be omitted; the next line would call it.
	REngine engine = REngine.GetInstance();
	// A somewhat contrived but customary Hello World:
	CharacterVector charVec = engine.CreateCharacterVector(new[] { "Hello, R world!, .NET speaking" });
	engine.SetSymbol("greetings", charVec);
	engine.Evaluate("str(greetings)"); // print out in the consolestring[] a = engine.Evaluate("'Hi there .NET, from the R engine'").AsCharacter().ToArray();
	Console.WriteLine("R answered: '{0}'", a[0]);
	Console.WriteLine("Press any key to exit the program");
	Console.ReadKey();
	engine.Dispose();
}

You retrieve a single REngine object instance, after setting the necessary environmental variables. Even the call to SetEnvironmentVariables can be omitted, though we'd advise you keep it explicit. SetEnvironmentVariables looks at the Registry settings set up by the R installer on Windows. If need be, you can override the behaviours setting the environment variables and engine initialization with your own steps, detailed in the Appendix.

On Linux/MacOS, the path to libR.so (for Linux) must be in the environment variable LD_LIBRARY_PATH before the process start, otherwise the R.NET engine will not properly initialize. If this is not set up, R.NET will throw an exception with a detailed message. Read the Appendix at the end of this page if R.NET complains about your LD_LIBRARY_PATH.

Sample code

LOOK AT THE MATERIAL IN THE UNIT TESTS
Dealing with missing values
Illustrate the speed of data transfer

You usually interact with the REngine object with the methods Evaluate, GetSymbol, and SetSymbol. To create R vector and matrices, the REngine object has methods such as CreateNumericVector, CreateCharacterMatrix, etc. Finally, you can invoke R functions in a variety of ways, using the method Evaluate of the REngine object, and also more directly.

Sample 1

It is available from the sample code 1 at https://github.com/jmp75/rdotnet-onboarding, as of 2014-04.

staticvoid Main(string[] args)
  {
	 REngine.SetEnvironmentVariables();
	 REngine engine = REngine.GetInstance();
	 // REngine requires explicit initialization.// You can set some parameters.
	 engine.Initialize();

	 // .NET Framework array to R vector.
	 NumericVector group1 = engine.CreateNumericVector(newdouble[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99 });
	 engine.SetSymbol("group1", group1);
	 // Direct parsing from R script.
	 NumericVector group2 = engine.Evaluate("group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)").AsNumeric();

	 // Test difference of mean and get the P-value.
	 GenericVector testResult = engine.Evaluate("t.test(group1, group2)").AsList();
	 double p = testResult["p.value"].AsNumeric().First();

	 Console.WriteLine("Group1: [{0}]", string.Join(", ", group1));
	 Console.WriteLine("Group2: [{0}]", string.Join(", ", group2));
	 Console.WriteLine("P-value = {0:0.000}", p);

	 // you should always dispose of the REngine properly.// After disposing of the engine, you cannot reinitialize nor reuse it
	 engine.Dispose();
  }

The following sample code illustrate the most used capabilities. It is extracted from the sample code 2 at https://github.com/jmp75/rdotnet-onboarding, as of 2014-04.

This illustrate basic operations with numeric vectors

var e = engine.Evaluate("x <- 3");
// You can now access x defined in the R environment
NumericVector x = engine.GetSymbol("x").AsNumeric();
engine.Evaluate("y <- 1:10");
NumericVector y = engine.GetSymbol("y").AsNumeric();

While you may evaluate function calls by generating a string and call the Evaluate method, this can be unwieldy for cases where you pass large amounts of data. The following demonstrates how you may call a function, a bit like how you would invoke a function reflectively in .NET.
// Invoking functions; Previously you may have needed custom function definitionsvar myFunc = engine.Evaluate("function(x, y) { expand.grid(x=x, y=y) }").AsFunction();
var v1 = engine.CreateIntegerVector(new[] { 1, 2, 3 });
var v2 = engine.CreateCharacterVector(new[] { "a", "b", "c" });
var df = myFunc.Invoke(new SymbolicExpression[] { v1, v2 }).AsDataFrame();

R.NET 1.6 includes many improvements to support function calls.

// As of R.NET 1.6, more function call syntaxes are supported.var expandGrid = engine.Evaluate("expand.grid").AsFunction();
var d = new Dictionary<string, SymbolicExpression>();
d["x"] = v1;
d["y"] = v2;
df = expandGrid.Invoke(d).AsDataFrame();

Continuing with the results of our use of expand.grid, the following code illustrate that while R.NET tries to emulate the coercion behavior of R, you should be circumspect with it, notably when you know you have factors as columns in your data frame.
engine.SetSymbol("cases", df);
// Not correct: the 'y' column is a "factor". This returns "1", "1", "1", "2", "2", etc. var letterCases = engine.Evaluate("cases[,'y']").AsCharacter().ToArray();
// This returns something more intuitive for C#  Returns 'a','a','a','b','b','b','c' etc.
letterCases = engine.Evaluate("as.character(cases[,'y'])").AsCharacter().ToArray();
// In the manner of R.NET, try
letterCases = engine.Evaluate("cases[,'y']").AsFactor().GetFactors();

To reuse whole scripts, the simplest method is to use the 'source' function in R
engine.Evaluate("source('c:/src/path/to/myscript.r')");

Data Types

All expressions in R are represented as SymbolicExpression objects in R.NET. For data access, the following special classes are defined. Note that there is no direct equivalent in .NET for 'NA' values in R. Special values are used for some types but pay attention to the behaviour, so as not to risk incorrect calculations.

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. Missing values are int.MinValue
real vector RDotNet.NumericVector System.Double[] Missing values are represented as double.NaN
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. And from version 1.5.3, DataFrameRowAttribute and DataFrameColumnAttribute are available for data mapping.
function RDotNet.Function From version 1.4. Including closure, built-in function, and special function.
factor RDotNet.Factor System.Int32[] From version 1.5.2.
S4 RDotNet.S4Object Not Available Yet. See S4 branch in the source control.

Acknowledgements

  • Daniel Collins found the workaround for the native library "libdl" loader that was not working on at least some CentOS Linux distributions.
  • evolvedmicrobe contributed to several features to run on MacOS and Linux, and initiated the changes to make R.NET platform independent.
  • Kosei initiated R.NET
  • gchapman

Appendices

Updating environment variables on Linux (MacOS?)

The path to libR.so (for Linux) must be in the environment variable LD_LIBRARY_PATH before the process start, otherwise the R.NET engine will not properly initialize. If this is not set up, R.NET will throw an exception with a detailed message.

What you will need to do there depends on the Linux (MacOS?) machine you are.
Let's say you needed to compile your own R from source, to get a shared R library:

LOCAL_DIR=/home/username/local
JAVAHOME=/apps/java/jdk1.7.0_25
cd ~src
cd R/
tar zxpvf R-3.0.2.tar.gz
cd R-3.0.2
./configure --prefix=$LOCAL_DIR --enable-R-shlib  CFLAGS="-g"
make
make install

Then prior to running a project with R.NET, you may need to update your LD_LIBRARY_PATH, and quite possibly PATH (though the latter can be done at runtime too).

LOCAL_DIR=/home/username/local
if [ "${LD_LIBRARY_PATH}" != "" ]
then
    export LD_LIBRARY_PATH=$LOCAL_DIR/lib:$LOCAL_DIR/lib64/R/lib:/usr/local/lib64:${LD_LIBRARY_PATH}
else
    export LD_LIBRARY_PATH=$LOCAL_DIR/lib:$LOCAL_DIR/lib64/R/lib:/usr/local/lib64
fi
# You may as well update the PATH environment variable, though R.NET does update it if need be.
export PATH=$LOCAL_DIR/bin:$LOCAL_DIR/lib64/R/lib:${PATH}

Advanced options for the R engine initialization

custom characterconsole
Multiple app domains

Edited Feature: ClearGlobalEnvironment [105]

$
0
0
The ClearGlobalEnvironment is doing the following:
this.Evaluate("rm(list=ls())");

This doesn't remove hidden objects.
It should instead do:
this.Evaluate("rm(list=ls(all=TRUE))");

In addition, it might be useful to add the detach() command (to unload all packages), but this should be optional (by an argument to the function) as sometimes, between requests, it is good to leave loaded packages.

And one last thing, it might be recommended to perform the CLR garbage collection twice:
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

This is because the finalizers execute after the first collection and you want to collect whatever they left behind.

Source code checked in, #d3939fd13eb2

$
0
0
Retrieve characters for data frame indexing on string as factors columns

Source code checked in, #10ae385dbc47

Source code checked in, #e3d77416d5e0

$
0
0
Add cleaning options of the R engine; https://rdotnet.codeplex.com/workitem/105

Commented Feature: ClearGlobalEnvironment [105]

$
0
0
The ClearGlobalEnvironment is doing the following:
this.Evaluate("rm(list=ls())");

This doesn't remove hidden objects.
It should instead do:
this.Evaluate("rm(list=ls(all=TRUE))");

In addition, it might be useful to add the detach() command (to unload all packages), but this should be optional (by an argument to the function) as sometimes, between requests, it is good to leave loaded packages.

And one last thing, it might be recommended to perform the CLR garbage collection twice:
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

This is because the finalizers execute after the first collection and you want to collect whatever they left behind.
Comments: Improvements with commit: Revision: 276 Changeset: e3d77416d5e04bef0ad9389bcb8faf06ca56bbc2 [e3d77416d5e0] Parents: 275 Branch: jperraud

Updated Wiki: User documentation for the next version

$
0
0

Introduction

Relation to F# RProvider

Getting set up

There is a page gathering Software Prerequisites listing the platforms on which R.NET is known to run.
As of version 1.6, R.NET binaries are platform independent. You might need to set up a small add-on workaround on some Linux distributions (CentOS a known one), but you can just move and use the R.NET binaries across platforms.
Assuming you have the right Software Prerequisites, you can obtain R.NET binaries from two sources
  1. The codeplex web site, by downloading a zip file
  2. The nuget.org web site

Visual Studio

NuGet is the preferred way to manage dependencies on R.NET. Consider giving it a try: this is the way of the future for .NET projects...

If you are using the binaries from the zip file distribution, unzip the file and copy the content to a location of your choice. Add project references to RDotNet.dll and RDotNet.Native.dll the "usual" way.

If you are using the NuGet packages:

You first have to install, if you have not already, the NuGet package manager via Tools - Extension and Updates:
0001.png

You can add the R.NET package as a dependency to one or more projects in your solution. For one project:
001.png

002.png

The NuGet system then adds a couple of references.
003.png

You can manage several projects in one go at the solution level:
004.png

You can find more general information about NuGet at NuGet documentation

Xamarin Studio

This section is a placeholder.

Getting started with coding

Version 1.6 of R.NET includes significant changes notably to alleviate two stumbling blocks often dealt with by users: paths to the R shared library, and preventing multiple engine initializations.

The following "Hello World" sample illustrates how the new API is simpler in 90% of use cases on Windows:

staticvoid Main(string[] args)
{
	REngine.SetEnvironmentVariables(); // <-- May be omitted; the next line would call it.
	REngine engine = REngine.GetInstance();
	// A somewhat contrived but customary Hello World:
	CharacterVector charVec = engine.CreateCharacterVector(new[] { "Hello, R world!, .NET speaking" });
	engine.SetSymbol("greetings", charVec);
	engine.Evaluate("str(greetings)"); // print out in the consolestring[] a = engine.Evaluate("'Hi there .NET, from the R engine'").AsCharacter().ToArray();
	Console.WriteLine("R answered: '{0}'", a[0]);
	Console.WriteLine("Press any key to exit the program");
	Console.ReadKey();
	engine.Dispose();
}

You retrieve a single REngine object instance, after setting the necessary environmental variables. Even the call to SetEnvironmentVariables can be omitted, though we'd advise you keep it explicit. SetEnvironmentVariables looks at the Registry settings set up by the R installer on Windows. If need be, you can override the behaviours setting the environment variables and engine initialization with your own steps, detailed in the Appendix.

On Linux/MacOS, the path to libR.so (for Linux) must be in the environment variable LD_LIBRARY_PATH before the process start, otherwise the R.NET engine will not properly initialize. If this is not set up, R.NET will throw an exception with a detailed message. Read the Appendix at the end of this page if R.NET complains about your LD_LIBRARY_PATH.

Sample code

LOOK AT THE MATERIAL IN THE UNIT TESTS
Dealing with missing values
Illustrate the speed of data transfer

You usually interact with the REngine object with the methods Evaluate, GetSymbol, and SetSymbol. To create R vector and matrices, the REngine object has methods such as CreateNumericVector, CreateCharacterMatrix, etc. Finally, you can invoke R functions in a variety of ways, using the method Evaluate of the REngine object, and also more directly.

Sample 1

It is available from the sample code 1 at https://github.com/jmp75/rdotnet-onboarding, as of 2014-04.

staticvoid Main(string[] args)
  {
	 REngine.SetEnvironmentVariables();
	 REngine engine = REngine.GetInstance();
	 // REngine requires explicit initialization.// You can set some parameters.
	 engine.Initialize();

	 // .NET Framework array to R vector.
	 NumericVector group1 = engine.CreateNumericVector(newdouble[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99 });
	 engine.SetSymbol("group1", group1);
	 // Direct parsing from R script.
	 NumericVector group2 = engine.Evaluate("group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)").AsNumeric();

	 // Test difference of mean and get the P-value.
	 GenericVector testResult = engine.Evaluate("t.test(group1, group2)").AsList();
	 double p = testResult["p.value"].AsNumeric().First();

	 Console.WriteLine("Group1: [{0}]", string.Join(", ", group1));
	 Console.WriteLine("Group2: [{0}]", string.Join(", ", group2));
	 Console.WriteLine("P-value = {0:0.000}", p);

	 // you should always dispose of the REngine properly.// After disposing of the engine, you cannot reinitialize nor reuse it
	 engine.Dispose();
  }

The following sample code illustrate the most used capabilities. It is extracted from the sample code 2 at https://github.com/jmp75/rdotnet-onboarding, as of 2014-04.

This illustrate basic operations with numeric vectors

var e = engine.Evaluate("x <- 3");
// You can now access x defined in the R environment
NumericVector x = engine.GetSymbol("x").AsNumeric();
engine.Evaluate("y <- 1:10");
NumericVector y = engine.GetSymbol("y").AsNumeric();

While you may evaluate function calls by generating a string and call the Evaluate method, this can be unwieldy for cases where you pass large amounts of data. The following demonstrates how you may call a function, a bit like how you would invoke a function reflectively in .NET.
// Invoking functions; Previously you may have needed custom function definitionsvar myFunc = engine.Evaluate("function(x, y) { expand.grid(x=x, y=y) }").AsFunction();
var v1 = engine.CreateIntegerVector(new[] { 1, 2, 3 });
var v2 = engine.CreateCharacterVector(new[] { "a", "b", "c" });
var df = myFunc.Invoke(new SymbolicExpression[] { v1, v2 }).AsDataFrame();

R.NET 1.6 includes many improvements to support function calls.

// As of R.NET 1.6, more function call syntaxes are supported.var expandGrid = engine.Evaluate("expand.grid").AsFunction();
var d = new Dictionary<string, SymbolicExpression>();
d["x"] = v1;
d["y"] = v2;
df = expandGrid.Invoke(d).AsDataFrame();

Continuing with the results of our use of expand.grid, the following code illustrate that while R.NET tries to mimic the behavior of R with respect to data frames. Data frames are a central part of R data structures, so it is worth expanding with a few examples

engine.SetSymbol("cases", df);
// As of R.NET 1.6, factor to character expressions work consistently with Rvar letterCases = engine.Evaluate("cases[,'y']").AsCharacter().ToArray();
// "a","a","a","b","b","b", etc. Same as as.character(cases[,'y']) in R// Note that this used to return  "1", "1", "1", "2", "2", etc. with R.NET 1.5.5

There are other ways to extract columns from the data frame, without passing strings of R expressions:
// Equivalent:
letterCases = df[1].AsCharacter().ToArray();
letterCases = df["y"].AsCharacter().ToArray();

The behavior for what is returned by 2-dimensional indexing usually mirrors what is observed directly in R. One exception is when row names are missing; the R behavior is debatable, so R.NET prefers to be strict.
// Accessing items by two dimensional indexingstring s = (string)df[1, 1]; // "a"
s = (string)df[3, 1]; // "a"
s = (string)df[3, "y"]; // "b"// s = (string)df["4", "y"]; // fails because there are no row names
df[3, "y"] = "a";
s = (string)df[3, "y"]; // "a"
df[3, "y"] = "d";
s = (string)df[3, "y"]; // null, because we have an <NA> string in R

To reuse whole scripts, the simplest method is to use the 'source' function in R
engine.Evaluate("source('c:/src/path/to/myscript.r')");

Data Types

All expressions in R are represented as SymbolicExpression objects in R.NET. For data access, the following special classes are defined. Note that there is no direct equivalent in .NET for 'NA' values in R. Special values are used for some types but pay attention to the behaviour, so as not to risk incorrect calculations.

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. Missing values are int.MinValue
real vector RDotNet.NumericVector System.Double[] Missing values are represented as double.NaN
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. And from version 1.5.3, DataFrameRowAttribute and DataFrameColumnAttribute are available for data mapping.
function RDotNet.Function From version 1.4. Including closure, built-in function, and special function.
factor RDotNet.Factor System.Int32[] From version 1.5.2.
S4 RDotNet.S4Object Not Available Yet. See S4 branch in the source control.

Acknowledgements

  • Daniel Collins found the workaround for the native library "libdl" loader that was not working on at least some CentOS Linux distributions.
  • evolvedmicrobe contributed to several features to run on MacOS and Linux, and initiated the changes to make R.NET platform independent.
  • Kosei initiated R.NET
  • gchapman

Appendices

Updating environment variables on Linux (MacOS?)

The path to libR.so (for Linux) must be in the environment variable LD_LIBRARY_PATH before the process start, otherwise the R.NET engine will not properly initialize. If this is not set up, R.NET will throw an exception with a detailed message.

What you will need to do there depends on the Linux (MacOS?) machine you are.
Let's say you needed to compile your own R from source, to get a shared R library:

LOCAL_DIR=/home/username/local
JAVAHOME=/apps/java/jdk1.7.0_25
cd ~src
cd R/
tar zxpvf R-3.0.2.tar.gz
cd R-3.0.2
./configure --prefix=$LOCAL_DIR --enable-R-shlib  CFLAGS="-g"
make
make install

Then prior to running a project with R.NET, you may need to update your LD_LIBRARY_PATH, and quite possibly PATH (though the latter can be done at runtime too).

LOCAL_DIR=/home/username/local
if [ "${LD_LIBRARY_PATH}" != "" ]
then
    export LD_LIBRARY_PATH=$LOCAL_DIR/lib:$LOCAL_DIR/lib64/R/lib:/usr/local/lib64:${LD_LIBRARY_PATH}
else
    export LD_LIBRARY_PATH=$LOCAL_DIR/lib:$LOCAL_DIR/lib64/R/lib:/usr/local/lib64
fi
# You may as well update the PATH environment variable, though R.NET does update it if need be.
export PATH=$LOCAL_DIR/bin:$LOCAL_DIR/lib64/R/lib:${PATH}

Advanced options for the R engine initialization

custom characterconsole
Multiple app domains

Source code checked in, #507d18df9d7b

$
0
0
Add an option to implement a workaround for probably multithreading issues https://rdotnet.codeplex.com/workitem/67

Commented Issue: Frequently calling R causes failure of RDotNet.ParseException or System.AccessViolationException [67]

$
0
0
I test RDotNet in F# by using RProvider.

This is my F# code, which frequently fails for RDotNet.ParseException or sometimes System.AccessViolationException.

My environment is
Windows 8.1 Ultimate x64
Microsoft.NET Framework 4.5.1
F# 3.1
R 3.0.2 x64

The same problem also occurs in .NET 4.5, F# 3.0, and R 3.0.x x86

``` F#
module RFSharpTest.Program

open System
open RDotNet
open RProvider
open RProvider.``base``
open RProvider.stats
open RProvider.utils

[<EntryPoint>]
let main argv =
[1..1000]
|> List.iter (fun i ->
let dic = dict [("ma",R.c(0.6,0.4,0.1).Value)]
let list = R.list(dic)
let data1 = R.arima_sim(model=list,n=50)
let model = R.arima(x=data1,order=R.c(0,0,5))
printfn "test"
)
Console.Read()
0 // return an integer exit code
```

It's not rare since I tested for other more formal purposes. For example, I iterated a long list of data rows for each an unitrootTest (in R package fUnitRoots) is called to provide some test statistic. This is almost the same situation as the code presented above: both involve intensive iteration of calling RDotNet library or calling R. Very frequently, the program fails.

However, temporarily I find a way to avoid this: in the end of each iteration, I force GC to collect all generations (or just generation 0), and the problem does not appear again. As long as I remove this line of code, the same problem occurs.

Here's my new "safe" code:
``` F#
module RFSharpTest.Program

open System
open RDotNet
open RProvider
open RProvider.``base``
open RProvider.stats
open RProvider.utils

[<EntryPoint>]
let main argv =
[1..1000]
|> List.iter (fun i ->
let dic = dict [("ma",R.c(0.6,0.4,0.1).Value)]
let list = R.list(dic)
let data1 = R.arima_sim(model=list,n=50)
let model = R.arima(x=data1,order=R.c(0,0,5))
printfn "test"
GC.Collect()
)
Console.Read()
```

Now, I see it's unsafe to intensively call RDotNet (at least through RProvider) unless I add code to force GC collection frequently.

Is there a way to solve this problem with more reasons?
Comments: Revision: 277 Changeset: 507d18df9d7b00b2d70626201720faa728ab9edb [507d18df9d7b] Branch: jperraud Add an option to implement a workaround for probably multithreading issues https://rdotnet.codeplex.com/workitem/67

Commented Feature: ClearGlobalEnvironment [105]

$
0
0
The ClearGlobalEnvironment is doing the following:
this.Evaluate("rm(list=ls())");

This doesn't remove hidden objects.
It should instead do:
this.Evaluate("rm(list=ls(all=TRUE))");

In addition, it might be useful to add the detach() command (to unload all packages), but this should be optional (by an argument to the function) as sometimes, between requests, it is good to leave loaded packages.

And one last thing, it might be recommended to perform the CLR garbage collection twice:
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

This is because the finalizers execute after the first collection and you want to collect whatever they left behind.
Comments: Probably not the end of the story with this issue, so leaving it open, but there should be enough latitude for users to work around this. Note to self: ask a guru at MS research or the like whether SEXP as SafeHandle is OK or not.

Commented Feature: ClearGlobalEnvironment [105]

$
0
0
The ClearGlobalEnvironment is doing the following:
this.Evaluate("rm(list=ls())");

This doesn't remove hidden objects.
It should instead do:
this.Evaluate("rm(list=ls(all=TRUE))");

In addition, it might be useful to add the detach() command (to unload all packages), but this should be optional (by an argument to the function) as sometimes, between requests, it is good to leave loaded packages.

And one last thing, it might be recommended to perform the CLR garbage collection twice:
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

This is because the finalizers execute after the first collection and you want to collect whatever they left behind.
Comments: Ignore previous comment - wrong issue...

Commented Issue: Frequently calling R causes failure of RDotNet.ParseException or System.AccessViolationException [67]

$
0
0
I test RDotNet in F# by using RProvider.

This is my F# code, which frequently fails for RDotNet.ParseException or sometimes System.AccessViolationException.

My environment is
Windows 8.1 Ultimate x64
Microsoft.NET Framework 4.5.1
F# 3.1
R 3.0.2 x64

The same problem also occurs in .NET 4.5, F# 3.0, and R 3.0.x x86

``` F#
module RFSharpTest.Program

open System
open RDotNet
open RProvider
open RProvider.``base``
open RProvider.stats
open RProvider.utils

[<EntryPoint>]
let main argv =
[1..1000]
|> List.iter (fun i ->
let dic = dict [("ma",R.c(0.6,0.4,0.1).Value)]
let list = R.list(dic)
let data1 = R.arima_sim(model=list,n=50)
let model = R.arima(x=data1,order=R.c(0,0,5))
printfn "test"
)
Console.Read()
0 // return an integer exit code
```

It's not rare since I tested for other more formal purposes. For example, I iterated a long list of data rows for each an unitrootTest (in R package fUnitRoots) is called to provide some test statistic. This is almost the same situation as the code presented above: both involve intensive iteration of calling RDotNet library or calling R. Very frequently, the program fails.

However, temporarily I find a way to avoid this: in the end of each iteration, I force GC to collect all generations (or just generation 0), and the problem does not appear again. As long as I remove this line of code, the same problem occurs.

Here's my new "safe" code:
``` F#
module RFSharpTest.Program

open System
open RDotNet
open RProvider
open RProvider.``base``
open RProvider.stats
open RProvider.utils

[<EntryPoint>]
let main argv =
[1..1000]
|> List.iter (fun i ->
let dic = dict [("ma",R.c(0.6,0.4,0.1).Value)]
let list = R.list(dic)
let data1 = R.arima_sim(model=list,n=50)
let model = R.arima(x=data1,order=R.c(0,0,5))
printfn "test"
GC.Collect()
)
Console.Read()
```

Now, I see it's unsafe to intensively call RDotNet (at least through RProvider) unless I add code to force GC collection frequently.

Is there a way to solve this problem with more reasons?
Comments: Probably not the end of the story with this issue, so leaving it open, but there should be enough latitude for users to work around this. Note to self: ask a guru at MS research or the like whether SEXP as SafeHandle is OK or not.

Edited Feature: Suggest some Extension methods to allow fluent-style accessing R objects [68]

$
0
0
RDotNet provides SymbolicExpression object to get access to R objects. Most R objects are constructed as Lists.

For example, a t-test result is a list of statistics and other things, a linear model (lm) is a list of coefficients, residuals and other useful list members. However, it is quite inconvenient to extract the members from a list without chaining several methods and convert the value to appropriate types.

In some packages, the R functions returns S4 object. For example, fUnitRoots package provides a unitrootTest function which returns S4 object that provide much richer information provided by official R stats packages. But it's a nightmare to extract wanted information from these Lists or the slots of S4 objects. R methods package provide slot() function to extract slots from S4 objects.

As a user of C# and F#, I just want to conveniently get access to the inner information of the R objects without caring too much about the nature of the R object.

Here I developed a very rough RDotNet.Extensions project using F# and RProvider to extend SymbolicExpression:
``` F#
module RDotNet.Extensions

open RDotNet
open RDotNet.Internals
open RProvider
open RProvider.``base``
open RProvider.methods

type SymbolicExpression with
/// Get the member symbolic expression of List or S4 object.
member this.Member (name: string) =
match this.Type with
| SymbolicExpressionType.List -> this.AsList().[name]
| SymbolicExpressionType.S4 -> R.slot(this,name)
| _ -> invalidOp "Unsupported operation on R object"

/// Convert the symbolic expression to a typed array.
member this.ToArray<'a> () = this.Value :?> 'a[]

/// Get the value from the typed vector by name.
member this.ValueOf<'a> (name: string) =
match this.Type with
| SymbolicExpressionType.NumericVector -> box(this.AsNumeric().[name]) :?> 'a
| SymbolicExpressionType.CharacterVector -> box(this.AsCharacter().[name]) :?> 'a
| SymbolicExpressionType.LogicalVector -> box(this.AsLogical().[name]) :?> 'a
| SymbolicExpressionType.IntegerVector -> box(this.AsInteger().[name]) :?> 'a
| SymbolicExpressionType.ComplexVector -> box(this.AsComplex().[name]) :?> 'a
| SymbolicExpressionType.RawVector -> box(this.AsRaw().[name]) :?> 'a
| _ -> invalidOp "Unsupported operation on R object"

/// Get the value from the typed vector by index.
member this.ValueAt<'a> (index: int) = box(this.ToArray<'a>().[index]) :?> 'a

/// Get the first value from the typed vector.
member this.First<'a> () = this.ValueAt<'a>(0)

/// Assign this symbolic expression to a string symbolic identifier in current R environment.
member this.AssignTo(name:string) = R.assign(name,this) |> ignore
```

This extension currently allows me to write code in a fluent style like this:
``` F#
let dic = dict [("ma",R.c(0.6,0.4,0.1).Value)]
let list = R.list(dic)
let data1 = R.arima_sim(model=list,n=500)
let model = R.arima(x=data1,order=R.c(0,0,5))
let coef:double[] = model.Member("coef").ToArray()
```

``` F#
let data = R.rnorm(1000)
let test = R.unitrootTest(data)
let pvalue:double = test.Member("test").Member("p.value").ValueOf("n") // test is S4 object
```

I suggest that RDotNet adopt some extensions to allow such fluent-style of accessing the data members of R objects.

Edited Issue: Crazy mono glibc detected error [75]

$
0
0
I am using the jperraud branch and just tried to load R on CentOS using the new initialization that passes in strings ( this was to test/avoid the whole --no-save/-vanilla stuff). When I tried this I received the following error:

*** glibc detected *** mono-sgen: double free or corruption (fasttop):

Followed by a very long debug list and signs that this occurred in the constructor/initialization call. Examining the string as argumetns being passed, I found this was being passed:

rdotnet_app --slave --interactive --no-save --no-restore-data --max-ppsize=50000

Changing this to remove the interactive bit as:

rdotnet_app --quiet --slave --no-save --no-restore-data --max-ppsize=50000

Seemed to avoid the bad memory pointer free (whch I gather is what the glibc detected thing was). I have no idea what caused this, but it seems here might be a problem with conflicting arguments or god knows what.

New Post: New to R - simple question of retrieving values

$
0
0
I am new to this and understand c# variables but perhaps not R variables

I have a vector in R
            NumericVector x = r.CreateNumericVector(_x);
            r.SetSymbol("x", x);
and I want to get the length of it to use in C#

I am trying
        var len_x = r.Evaluate("length(x)").AsNumeric();
        NumericVector len_x = r.Evaluate("length(x)").AsNumeric();
but both don't seem to retrieve the the length of this vector into a variable I can use

Likely a very dumb question but I couldn't get the answer from the web

Thanks

New Post: New to R - simple question of retrieving values

$
0
0
This looks like this should work. But the length being an integer value by nature, you'd be better off with something like
int len_x = r.Evaluate("length(x)").AsInteger()[0];

New Post: R Engine Reuse (re-initialize)

$
0
0
Hi jperraud,

I am also facing the same issue. I am trying to use in a Window service. How do I do with inter-process communication. can you send me a sample code.

Thanks

New Post: source and load direct via bytearray

$
0
0
Hello,

Interesting. I wonder whether similar questions outside of R.NET were asked. It may be worth asking on StackOverflow as a general R question.

R.NET does not offer particular facilities, but if there is a way to do this in R (in-memory byte array load from a DB) , I am not sure something more in R.NET is required. The load function in R accepts connection objects, besides file paths. So, creating from .NET a SymbolicExpression that points to a connection object, and passing it to the load function is what I would look for. I am not familiar with these R objects of class connection

I can only offer thought bubbles really, this is a rather advanced usage, that could lead you to some arcane corners of R. Then again, I may overestimate the difficulty. Let us know if you gain some insights.
Viewing all 1634 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>