标签:
http://rdotnet.codeplex.com/documentation?referringTitle=Home
static void Main(string[] args){REngine.SetEnvironmentVariables(); // <-- 可以省略,下一行会调用它REngine engine = REngine.GetInstance();// 一个几分人为但习惯的hello world:CharacterVector charVec = engine.CreateCharacterVector(new[] { "Hello, R world!, .NET speaking" });engine.SetSymbol("greetings", charVec);engine.Evaluate("str(greetings)"); // 在控制台打印string[] a = engine.Evaluate("‘Hi there .NET, from the R engine‘").AsCharacter().ToArray();Console.WriteLine("R answered: ‘{0}‘", a[0]);Console.WriteLine("Press any key to exit the program");Console.ReadKey();engine.Dispose();}
static void Main(string[] args){REngine.SetEnvironmentVariables();REngine engine = REngine.GetInstance();// REngine需要明确的初始化,// 你可以设置一些参数。engine.Initialize();// net向量转R向量NumericVector group1 = engine.CreateNumericVector(new double[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99 });engine.SetSymbol("group1", group1);// 直接R脚本解析NumericVector group2 = engine.Evaluate("group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)").AsNumeric();// 测试不同并获取PvalueGenericVector 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);// 你要明确释放REngine.// 释放以后,你不可以再初始化或在使用它engine.Dispose();}
var e = engine.Evaluate("x <- 3");// You can now access x defined in the R environmentNumericVector x = engine.GetSymbol("x").AsNumeric();engine.Evaluate("y <- 1:10");NumericVector y = engine.GetSymbol("y").AsNumeric();
// Invoking functions; Previously you may have needed custom function definitionsvar myFunc = engine.Evaluate("function(x, y) { expand.grid(x=x, y=y) }").AsFunction();var v1 = engine.CreateIntegerVector(new[] { 1, 2, 3 });var v2 = engine.CreateCharacterVector(new[] { "a", "b", "c" });var df = myFunc.Invoke(new SymbolicExpression[] { v1, v2 }).AsDataFrame();
// As of R.NET 1.5.10, more function call syntaxes are supported.var expandGrid = engine.Evaluate("expand.grid").AsFunction();var d = new Dictionary<string, SymbolicExpression>();d["x"] = v1;d["y"] = v2;df = expandGrid.Invoke(d).AsDataFrame();
engine.SetSymbol("cases", df);// As of R.NET 1.5.10, factor to character expressions work consistently with Rvar letterCases = engine.Evaluate("cases[,‘y‘]").AsCharacter().ToArray();// "a","a","a","b","b","b", etc. Same as as.character(cases[,‘y‘]) in R// Note that this used to return "1", "1", "1", "2", "2", etc. with R.NET 1.5.5
// Equivalent:letterCases = df[1].AsCharacter().ToArray();letterCases = df["y"].AsCharacter().ToArray();
// Accessing items by two dimensional indexingstring s = (string)df[1, 1]; // "a"s = (string)df[3, 1]; // "a"s = (string)df[3, "y"]; // "b"// s = (string)df["4", "y"]; // fails because there are no row namesdf[3, "y"] = "a";s = (string)df[3, "y"]; // "a"df[3, "y"] = "d";s = (string)df[3, "y"]; // null, because we have an <NA> string in R
engine.Evaluate("source(‘c:/src/path/to/myscript.r‘)");
| R | R.NET | .NET Framework | Note |
|---|---|---|---|
| character vector | RDotNet.CharacterVector | System.String[] | |
| integer vector | RDotNet.IntegerVector | System.Int32[] | The minimum value in R is -2^31+1 while that of .NET Framework is -2^31. Missing values are int.MinValue |
| real vector | RDotNet.NumericVector | System.Double[] | Missing values are represented as double.NaN |
| complex vector | RDotNet.ComplexVector | System.Numerics.Complex[] | System.Numerics assembly is required for .NET Framework 4. |
| raw vector | RDotNet.RawVector | System.Byte[] | |
| logical vector | RDotNet.LogicalVector | System.Boolean[] | |
| character matrix | RDotNet.CharacterMatrix | System.String[, ] | |
| integer matrix | RDotNet.IntegerMatrix | System.Int32[, ] | The minimum value in R is -2^31+1 while that of .NET Framework is -2^31. |
| real matrix | RDotNet.NumericMatrix | System.Double[, ] | |
| complex matrix | RDotNet.ComplexMatrix | System.Numerics.Complex[, ] | Reference to System.Numerics assembly is required. |
| raw matrix | RDotNet.RawMatrix | System.Byte[, ] | |
| logical matrix | RDotNet.LogicalMatrix | System.Boolean[, ] | |
| list | RDotNet.GenericVector | From version 1.1. | |
| data frame | RDotNet.GenericVector | From version 1.1. RDotNet.DataFrame class is also available (below). | |
| data frame | RDotNet.DataFrame | From version 1.3. And from version 1.5.3, DataFrameRowAttribute and DataFrameColumnAttribute are available for data mapping. | |
| function | RDotNet.Function | From version 1.4. Including closure, built-in function, and special function. | |
| factor | RDotNet.Factor | System.Int32[] | From version 1.5.2. |
| S4 | RDotNet.S4Object | Not Available Yet. See S4 branch in the source control. |
标签:
原文地址:http://www.cnblogs.com/cxwcdxw/p/4700523.html