engine.Evaluate in an event driven method

Feb 5, 2014 at 5:54 AM
Thanks for writing this code. I just started playing with it and look forward to seeing what can be done.

I'm sure I'm missing something simple but I'm having issues using engine.Evaluate in an event driven method call (the event is new incoming market data). Exceptions are thrown, apparently, the second time engine.Evaluate is called to update a calculation.

I can recreate similar behavior using this simple example:
engine.Evaluate("a <- a+1");
NumericVector v1 = engine.GetSymbol("a").AsNumeric();
txtBox.Text = v1[0].ToString()
But if I run this:
engine.Evaluate("a <- 1");
NumericVector v1 = engine.GetSymbol("a").AsNumeric();
txtBox.Text = v1[0].ToString()
No exceptions are thrown no matter how many times that method is called.

Am I missing something basic? Thanks.
Feb 5, 2014 at 8:51 AM
I am not sure exactly what problem you describe. I assume you mean that running your first code sample twice leads to an exception being thrown.

If the assumption is correct I cannot seem to reproduce the issue with R.NET 1..5.5 nor the most recent source code.
You can have a look at the code git repo that I use to investigate R.NET issues and questions.
         engine.Evaluate("a <- 1");
         engine.Evaluate("a <- a+1");
         NumericVector v1 = engine.GetSymbol("a").AsNumeric();
         bool eq = 2.0 == v1[0];
         engine.Evaluate("a <- a+1");
         NumericVector v2 = engine.GetSymbol("a").AsNumeric();
         eq = 3.0 == v2[0];
For a more efficient investigation of this issue uou should provide:
  • The stack trace of the exception
  • The context of your application: which versions of the runtimes, R.NET, R, etc.
  • The context of the function call: event driven how; is there cross-thread calls.
Feb 5, 2014 at 12:45 PM
Given the jperraud can't reproduce the issue, I just wanted to check, you are setting the "A" variable before updating it to "a+1" right?
Feb 5, 2014 at 11:36 PM
evolvedmicrobe's comment had me go back to the drawing board and start with a fresh console app to test some ideas. This works as expected:
                var e = engine.Evaluate("a<-0");
                NumericVector v1 = engine.GetSymbol("a").AsNumeric();
                Console.WriteLine("a = "+ v1[0].ToString());
                for (int i = 0; i < 10; i++)
                    engine.Evaluate("a <- a + 1");
                    NumericVector v2 = engine.GetSymbol("a").AsNumeric();
                    Console.WriteLine("a + 1 = " + v2[0].ToString());
                Console.WriteLine("Press any key to exit");
... and by playing around with variable scope and declaration I can recreate the exceptions I saw yesterday ( C# null refs and R.NET parse exceptions). I'll take this knowledge and now go back to the initial project and try to fix it. Thanks for the hand holding!
Feb 5, 2014 at 11:43 PM
Glad you seem to have a way forward; let us know if you can reproduce the issue and if the expected behavior of R.NET is not to fail on the test setup.
Feb 6, 2014 at 4:33 AM
Everything has been resolved. The issue of an exception being thrown the second time engine.Evaluate was called was due to a coding error on my part. Specifically, I was using a StringBuilder to build part of the R command, and did not call StringBuilder.Clear() first.