nuget的Rnet官方介绍文档
http://rdotnet.codeplex.com/documentation?referringTitle=Home
本页基于Rnet1.5.13版本。其实1.5.12也一样的。
Rnet使得net环境可以用R语言交互。Rnet需要net4和R环境的Rdlls。随便使用哪种语言吧。研究本文档之前,几件事情:F#最好用这个:https://github.com/BlueMountainCapital/FSharpRProvider
设置
一个列表,Rnet运行的程序准备:
对于1.5.10;Rnet是独立平台的。对Linux而言,你或许需要一些额外的工作环境(比如CentOS),不过你可以移动来使用交互平台。
Visual Studio:
或许你是下载然后用的,怎么怎么用。
nuget更好点。
首先教你装nuget。
如果你安装了Rnet1.5.5或之前的,请卸载。
Rnet1.5.13用了不同的标识:RnetCommunity。请用最新的哟。
添加以后,会有2个引用。RDotNet和RDotNet.NativeLibrary。
而且你还可以一次管理多个项目!!!(要你管,死Rnet!)
Xamarin Studio:
Getting started with coding:
Rnet1.5.10和之后版本有重大的变化,显著的减少了经常用户处理的2个障碍:1.共享库路径;2.阻碍并行引擎初始化。
下面的hello world 例子阐述了新api在windows上90%的用例更简单了:
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();}
必须的环境设定以后,你可以检索一个简单的REngine对象实例。甚至设置环境可以省略,虽然建议你明确写出。设置环境,windows上看R安装时候设置的注册表设置。如果必要,你可以重写设置环境的偏好等。参见附录。
在Linux和MacOs,…………
示例代码:
你常常使用REngine实例来模式求值,GetSymbol, and SetSymbol。
为了设置R向量或矩阵,REngine有扩展方法,例如:CreateNumericVector, CreateCharacterMatrix等等。
你可以多样方式调用R语言,使用方法求值,和更直接的方法。
t检验的基础实例:
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();
调用R函数:
1.来一串字符串,调用Evaluate方法,但大量数据,就太笨了。
2.下面示例怎么调用一个函数,或者说在net中怎么调一个函数。
// 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();
Rnet1.5.10包含一些改进,只在从C#调用,少点字符,少点Evaluate
// 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();
数据使用框架:
继续我们的使用expand.grid。
下面的代码阐述了Rnet试图模仿R数据格式的偏好。Data frames是R数据结构的中心部分,所以看些例子:
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();
这行为是为了返回二维索引通常反应R中直接观察到的。
一个例外就是当行名称丢失了,R行为就有问题了。所以Rnet倾向于更严格。
// 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
调用R脚本:
用了默认最简单的:
engine.Evaluate("source('c:/src/path/to/myscript.r')");
缺测值:
NA,缺省时候的占位符(各种向量类型)。
以后你会看见啦。
更多例子:
运行期性能:
其他例子:
处理日期和时间
数据类型:
数据类型:
R中所有数据类型,Rnet用SymbolicExpression代表。使用时候,下面的是定义好的。
注意net中没有R中NA的等价物
使用但请注意偏好,从而避免错误计算的风险。
| 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. |
感谢名单:
什么什么。
附件:
更新Linux和MacOS:
什么什么。
初始化的高级选项:
好男孩上天堂,坏男孩走四方……
浙公网安备 33010602011771号