Mohammad
Mohammad

Reputation: 589

Link between R and C# using R.NET

I want to use R software from visual studio C# 2012. I noticed somebody did this with R.NET. I Install R.2.15.2 (version I find). I noticed R.NET just work with R (32bit). I have installed 32 and 64 already but I link to 32bit. The program can't find the DLL and throw DLL not find exception. I noticed the envPath is empty and can't find variable. I wonder if somebody can help me.

Thank you.

static void Main(string[] args)
{
    string PATH = @"c:\Users\Documents\Visual Studio 2012\Projects\ConsoleApplication1\packages\R.NET.1.5.0\lib\net40";
    // Set the folder in which R.dll locates.
    var envPath = Environment.GetEnvironmentVariable(PATH);
    var rBinPath = @"C:\Program Files\R\R-2.15.2\bin\i386";
    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);
    }
}

Upvotes: 0

Views: 1805

Answers (1)

Austin Salonen
Austin Salonen

Reputation: 50215

var currentPath = Environment.GetEnvironmentVariable("path");
var pathWithR = string.Join(";", currentPath, rPath1, rPath2, ...);
Environment.SetEnvironmentVariable("path", pathWithR);

MSDN Environment.GetEnvironmentVariable
MSDN Environment.SetEnvironmentVariable

Upvotes: 1

Related Questions