C# - R interface C# - R interface r r

C# - R interface


You can have a look at R.NET, for another approach...


Ok, I solved it finally.The problem is that R (D)Com doesn't work with current version of R. I installed 2.11.1 and it worked out of box.

Thanks a lot.


Use R.NET (I installed mine from NuGet) and the following code in a new C# console app (which was copied with minor changes from http://rdotnet.codeplex.com/).

It will work when pointed at the 32-bit version of R v2.11.1, but it will not work when pointed at the 64-bit version of R v2.11.1 (as noted in the code below).

When I installed NuGet, it automatically added references to the current project: RDotNet (RDotNet.dll) and RDotNet.NativeLIbrary (RDotNet.NativeLibrary.dll). You'll need these references in any new project.

Works under VS2012 (untested under VS2010, but will probably work).

Works when compiled for both "x32" and "All CPU" (under "Build..Configuration Manager" in VS2012).

// Call R from .NET. Advantage is that everything is in process.// Tested on VS2012, will probably work on VS2010.using System;using System.IO;using System.Linq;using RDotNet;class Program{    static void Main(string[] args)    {        // Set the folder in which R.dll locates.        var envPath = Environment.GetEnvironmentVariable("PATH");        var rBinPath = @"C:\Program Files (x86)\R\R-2.11.1\bin";        //var rBinPath = @"C:\Program Files\R\R-2.11.1-x64\bin"; // Doesn't work ("DLL was not found.")        Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);        using (REngine engine = REngine.CreateInstance("RDotNet"))        {            // Initializes settings.            engine.Initialize();            // .NET Framework array to R vector.            NumericVector group1 = engine.CreateNumericVector(new double[] { 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);        }    }}