在 C# 中从 R 检索结果

Retrieve result from R in C#

我使用 R.NET 执行以下变化点检测

REngine.SetEnvironmentVariables();
REngine engine = REngine.GetInstance();
double[] data = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
NumericVector vector = engine.CreateNumericVector(data);
engine.Evaluate("library(changepoint)");
engine.SetSymbol("values", vector);
engine.Evaluate("values.ts = ts(values, frequency = 12, start = c(2017, 1))");
engine.Evaluate("chpoints = cpt.mean(values.ts, method=\"BinSeg\")");
var result = engine.GetSymbol("chpoints");
engine.Dispose();

result 的类型显示为 RDotNet.SymbolicExpressionchpointsclassRGui 上显示为 cpt

> class(chpoints)
[1] "cpt" 
attr(,"package") 
[1] "changepoint"

我需要在 C# 中取回结果以打印值、绘制图表、确定已识别的变化点等。请提出任何建议。

S4 对象的这个 property/key 正在返回一个包含变化点的数组。

GenericVector gvec = s4["param.est"].AsList();
NumericVector nvec = gvec[0].AsNumeric();