Note: I really no longer support dot net framework array to r anymore. This means that I don’t support .NET nor Microsoft nor Windows anymore due to back door security concerns I have with their product. I would strongly Python and other open source technologies over Microsoft product Also, this R posting is here as a courtesy due to R is still popular. Also remember this link recommended from my Academy section is from 2012 so it might no longer work.
Very Nice! My C# program calls R Code through the R.NET package.
How to get R connect to NOSQL scalable database Redis with doRedis R package for parallelization
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:
Enjoy this C Sharp code
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);
}
}
}
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.
G<-c(1,2,3)
G
cat(‘hello from r’, G)
NOTE I now post my
TRADING ALERTS into my personal
FACEBOOK ACCOUNT and
TWITTER. Don't worry as I don't post stupid cat videos or what I eat!