Very Nice! My C# program calls R Code through the R.NET package. Dancing in the streets!
Whoa! I finally got this working with R.NET. I can get my C# appliction call directly R code which is very nice. There is a nasty bug on the search path of R so note at the beginning of the code as well. Get more info about R.Net package from http://rdotnet.codeplex.com/. Here is the C# code but don’t foret to add the.NET.DLL DLL reference in your Visual Studio:
using System;
using System.Linq;
using RDotNet;
class Program
{
static void Main(string[] args)
{
//code solution from http://stackoverflow.com/questions/7960738/importing-mgcv-fails-because-rlapack-dll-cannot-be-found
string rhome = System.Environment.GetEnvironmentVariable(“R_HOME”);
if (string.IsNullOrEmpty(rhome))
rhome = @”C:Program FilesRR-2.15.0″;
System.Environment.SetEnvironmentVariable(“R_HOME”, rhome);
System.Environment.SetEnvironmentVariable(“PATH”, System.Environment.GetEnvironmentVariable(“PATH”) + “;” + rhome + @”bini386″);
// Set the folder in which R.dll locates.
//REngine.SetDllDirectory(@”C:Program FilesRR-2.12.0bini386″);
REngine.SetDllDirectory(@”C:Program FilesRR-2.15.0bini386″);
using (REngine engine = REngine.CreateInstance(“RDotNet”, new[] { “-q” })) // quiet mode
{
foreach (string path in engine.EagerEvaluate(“.libPaths()”).AsCharacter())
{
Console.WriteLine(path);
}
//engine.EagerEvaluate(“.libPaths(“C:/Program Files/R/R-2.15.0/library”);
// .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.EagerEvaluate(“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.EagerEvaluate(“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 can also run complete R scipts from within C# using R.NET. Here is an C# example:
using System;
using System.Linq;
using RDotNet;
class Program
{
static void Main(string[] args)
{
//code solution from http://stackoverflow.com/questions/7960738/importing-mgcv-fails-because-rlapack-dll-cannot-be-found
string rhome = System.Environment.GetEnvironmentVariable(“R_HOME”);
if (string.IsNullOrEmpty(rhome))
rhome = @”C:Program FilesRR-2.15.0″;
System.Environment.SetEnvironmentVariable(“R_HOME”, rhome);
System.Environment.SetEnvironmentVariable(“PATH”, System.Environment.GetEnvironmentVariable(“PATH”) + “;” + rhome + @”bini386″);
// Set the folder in which R.dll locates.
//REngine.SetDllDirectory(@”C:Program FilesRR-2.12.0bini386″);
REngine.SetDllDirectory(@”C:Program FilesRR-2.15.0bini386″);
using (REngine engine = REngine.CreateInstance(“RDotNet”, new[] { “-q” })) // quiet mode
{
foreach (string path in engine.EagerEvaluate(“.libPaths()”).AsCharacter())
{
Console.WriteLine(path);
}
engine.EagerEvaluate(“.libPaths(“C:/Program Files/R/R-2.15.0/library”);
// .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.EagerEvaluate(“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.EagerEvaluate(“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);
//example to call complete R script from http://rdotnet.codeplex.com/discussions/262426
//REngine R = REngine.GetInstanceFromID(“RDotNet”);
//R.EagerEvaluate(“source(“MyRscript.r”)”);
engine.EagerEvaluate(“source(“test.r”)”);
Console.ReadLine();
}
}
}
Here is R script test.r which resides in the same Release directory as the C# program. I am sure this can be elsewhere but just want to test running the R script.
G<-c(1,2,3)
G
cat(‘hello from r’, G)