C# - R interface

I need to interface R to some C# application. I installed rscproxy_1.3 and R_Scilab_DCOM3.0-1B5 added COM references to the STATCONNECTORCLNTLib, StatConnectorCommonLib and STATCONNECTORSRVLib but I still cannot get it working.

When I run following test program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//COM references
using STATCONNECTORCLNTLib;
using StatConnectorCommonLib;
using STATCONNECTORSRVLib;

namespace R_TESTING
{
    class Program
    {
        static void Main(string[] args)
        {
            StatConnector sc1 = new STATCONNECTORSRVLib.StatConnectorClass();         
            sc1.Init("R");
        }
    }
}

I get this exception:

Unhandled Exception: System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x80040013
   at STATCONNECTORSRVLib.StatConnectorClass.Init(String bstrConnectorName)

Thanks in advance.

UPDATE: Ok, still no luck. I will try to explain what I did so far.

Installed R-2.12.2-win.exe from rproject to the C:\Program Files\R\R-2.12.2

Downloaded rscproxy_1.3-1.zip and copy/pasted it to the C:\Program Files\R\R-2.12.2\library

Installed R_Scilab_DCOM3.0-1B5.exe to the C:\Program Files (x86)\R\(D)COM Server

With Scilab comes a basic test. I tried to run it but I got following error:

Loading StatConnector Server... Done Initializing R...Function call failed Code: -2147221485 Text: installation problem: unable to load connector Releasing StatConnector Server...Done

Than I looked in the PATH/System Variables and found no path/R_HOME/R_USER info. Also, I couldn't find anything R related in the registry.

I guess I am doing something terribly wrong, so I desperately need help from you guys.


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);
        }
    }
}