Decomposing R Objects

Dec 18, 2013 at 3:22 AM

Just getting started with this. Have the basics working ... can get data into and out of R and run library functions. As a test case, I'm running a VAR model using the 'vars' library. A code fragment is below.

The question is this ... having gotten back a result object in a SymbolicExpression, what's the best approach to extracting the data you're looking for? So far, I'm going one step at a time through the debugger, looking for types. For example, in the code fragment, variables v1, v2, and v3 were created on 3 successive runs.

So, am getting what I need in this case, but it seems like there ought to be an easier way. Or is this just what has to be done for each new type of object that comes back?

Any insight appreciated and thanks for a very handy tool.

NumericMatrix varMatrix = engine.CreateNumericMatrix(input);
engine.SetSymbol("varMatrix", varMatrix);

engine.Evaluate("varTest <- VAR(varMatrix, p=9, type = \"both\")");
SymbolicExpression result = engine.Evaluate("predict(varTest, n.ahead=12, ci=0.95)");
var v1 = result.AsList();
var v2 = v1[1];
double[] v3 = v2.AsNumeric().ToArray();
Dec 18, 2013 at 11:51 AM
Good question.

I don't think there is an easy way to dynamically discover in C# the type of the result returned. the R data provider that works for F# may (I have yet to explore it), but this is a feature specific to F#.

Maybe adding some infrastructure using dynamic objects for C# (dynamic keyword) may offer something, but this will not bring intellisense support. This seems more useful to access it from dynamic languages such as Python, e.g.

A more realistic scenario for C# is to add some discovery capabilities and helper functions to decompose R objects by some form of introspection, but there are higher priority issues on the todo list for now.
Dec 19, 2013 at 4:09 AM
Thanks for the quick response. Understand about the priorities.

Spent time today looking through material on dynamic objects and IronPython (looked at F# a while back). Any of these would seem to entail more of a learning curve than I can take on right now, so will probably brute force my way through.

Interestingly, in the VS debugger, typing the variable name of a SymbolicExpression (e.g. "result") followed by ", dynamic" (e.g. "result, dynamic") in the Watch list displays a sub-tree of names that make sense (e.g. 'fcast', 'model') ... but can't find a way to access them programatically.
Dec 19, 2013 at 4:21 AM
The F# type converter converts as follows:

The basic idea is that the SEXP pointers used by R come in a few easily discovered flavors (at least for primitive arrays). These can be easily converted, but the lists it uses are a bit harder to work with, but again shouldn't be too hard.