-1

I am calling R functions in C# using R.NET. While doing that I realized that every time I call a function I need to specif its type.

For Instance:

 engine.Evaluate("x = 3.5:10.5");

            NumericVector a1= engine.GetSymbol("x").AsNumeric();

For above code I need to specify that a1 is a numeric vector.

engine.Evaluate("x = c('a',3,7,'adad', 'xyz')");
            CharacterVector a2 = engine.GetSymbol("x").AsCharacter();

For this code I needed to specify that a2 is a character vector.

Is there any way to get to know the type of the data? or use something general which is applicable to all data types/objects?

Any help is appreciated. Thanking you in advance.

Artiga
  • 776
  • 2
  • 16
  • 37
  • You can use class(a2) to find the data type. You can also use is.character(a2) to check if it meets the requirement – Bas Oct 12 '15 at 09:48
  • Thank you for the reply @Heuer, but it is not working I don't even want to use `CharacterVector a2 = engine.GetSymbol("x").AsCharacter();` this line . As it I am already mentioning the data type. I just need to use something which will tell me what is the data type of the string used in `engine.Evaluate("x = c('a',3,7,'adad', 'xyz')");` Basically what is the type of "x"? – Artiga Oct 12 '15 at 09:55
  • `> class(c('a',3,7,'adad', 'xyz'))` `[1] "character"`it works for me in R. maybe try to split it up. `x <- c('a',3,7,'adad', 'xyz')` `engine.Evaluate(x);` – Bas Oct 12 '15 at 09:59
  • Not in R my friend. I need to do it in C#. – Artiga Oct 12 '15 at 09:59
  • Maybe [this](http://stackoverflow.com/questions/11634079/how-can-i-get-the-data-type-of-a-variable-in-c) topic will help you? – Bas Oct 12 '15 at 10:05

2 Answers2

3

Why not just save a reference to the symbol

engine.Evaluate("x = 3.5:10.5");
var a1 = engine.GetSymbol("x");
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • thank you so much for your response. `engine.Evaluate("x = c('a',3,7,'adad', 'xyz')"); CharacterVector a2 = engine.GetSymbol("x").AsCharacter(); string[] b2 = a2.ToArray(); MessageBox.Show("(" + string.Join(",", b2) + ")");` I want to display the output in the message box. When I am using `var' I am not able to convert a1 to array and hence not able to display in the message box. – Artiga Oct 12 '15 at 10:21
  • Also, I tried using a1 directly in the message box `engine.Evaluate("x = list(a=c(3.5:10.5), b='aaa')"); var a1 = engine.GetSymbol("x"); MessageBox.Show("("+ string.Join(",", a1)+ ")");` but message box shows "RDotNet.SymbolicExpression" – Artiga Oct 12 '15 at 10:26
2

As mentioned in a previous comment you can use engine.Evaluate("class(x)").AsCharacter().ToArray() to get the class(es) of the R objects.

in R.NET the parent type SymbolicExpression has some methods to query the type dynamically, but this does not cover all cases. There are things such as:

SymbolicExpression blah = engine.GetSymbol("blah");
bool isList = blah.IsList();

but no method such as IsCharacter(). I note that expanding the range of type checking methods as a feature request.

Broadly speaking, R is dynamically typed, C# is statically typed. What I understand you ask for is to project R dynamic types into C# ones; this is (practically) not possible. The F# R Provider provides some features you seem to be looking for, if you are willing to try F#.

j-m
  • 1,473
  • 1
  • 13
  • 17