Retrieve result from R in C#

2019-07-29 06:13发布

问题:

I used R.NET to perform Change Point Detection as following

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();

The Type of result is shown as RDotNet.SymbolicExpression and class of chpoints on RGui is shown as cpt

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

I need to get the result back in C# to print values, to draw a graph, to identify what change points have been identified, etc. Any suggestions please.

回答1:

This property/key of the S4 object is returning an array which contains change points.

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


标签: c# r r.net