This project is read-only.

R.NET errors logging

Jul 17, 2014 at 11:54 PM

Pls, I need to log any information about errors when I call ".Evaluate()" method. What's the way to catch information from R stderr stream in my code?

Thank you/
Jul 29, 2014 at 2:23 PM
From my experience, errors are thrown in C# (try running rEngine.Evaluate("x = (-1)!")) . It is more problematic with warnings. I found a little hacky way of catching those, though I would also appreciate some advice in how to do this better:
            REngine rEngine = REngine.GetInstance();
            const string __LR_WARNINGS = "warnings";
            const string __WARNINGS_FILE = "warningsFile.txt";

            rEngine.Evaluate(__LR_WARNINGS + " <- file(\"" + __WARNINGS_FILE + "\", open = \"wt\")");
            rEngine.Evaluate("sink(" + __LR_WARNINGS + ", type = \"message\")");

            rEngine.Evaluate("cor( c( 1 , 1 ), c( 2 , 3 ) )");
            try { rEngine.Evaluate("close(" + __LR_WARNINGS + ")"); }
            catch (Exception) { }
            rEngine.Evaluate("sink(type = \"message\")");
            rEngine.Evaluate("close(" + __LR_WARNINGS + ")");
            String warOrErrs = File.ReadAllText(__WARNINGS_FILE);
The try-catch around the first close is somehow necessary, otherwise the resulting file won't contain anything.
Sep 15, 2014 at 9:15 PM
I think the errors you are seeing are actually parse exceptions (ParseException) and evaluation exceptions (EvaluationException).

In the example of x = (-1)! a ParseException is thrown with:

````"Line: 1 Error: unexpected '!'"``` which is fully expected.

In the full example, I believe that your pure R code looks something like this:
 warnings <- file('warningsFile.txt', open = 'wt')
sink(warnings, type ='message')
cor(c(1,1), c(2,3))
sink(type ='message')
In this case, for me, an EvaluationException is thrown with the message.

"""Error in close.connection(warnings) : cannot close 'message' sink connection"""

Those are actual R errors being converted to exceptions by the REngine and are appropriate. There may be an issue with how the embedded R instance deals with sink, but I think that's outside the scope of those exceptions.