Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find what went wrong during eval in R?

Tags:

java

r

eval

Part of the code:

Rengine re = getRengine();
re.eval("library(quantmod)");
re.eval("library(PerformanceAnalytics)");
re.eval("library(tseries)");
re.eval("library(FinTS)");
re.eval("library(rugarch)");
re.eval("library(robustbase)");

re.assign("arLagNum", new double[]{1});
re.assign("maLagNum", new double[]{1});
re.assign("archLagNum", new double[]{1});
re.assign("garchLagNum", new double[]{1});

re.eval("garchSpec <- ugarchspec(variance.model = list(model=\"iGARCH\", garchOrder=c(archLagNum,garchLagNum)), mean.model = list(armaOrder=c(arLagNum,maLagNum)), distribution.model=\"std\")");

re.assign("transformedTsValueData", new double[]{getSomeDoubles()};
re.eval("estimates <- ugarchfit(spec = garchSpec, data = transformedTsValueData, solver.control = list(trace = 1))");
re.eval("estimates");

The last line returns null. The API documentation says: "the eval method returns null if something went wrong". How do I find out what went wrong?

like image 584
László Halász Avatar asked Jun 24 '15 07:06

László Halász


People also ask

How to check for errors in R?

In R, there are three tools for handling conditions (including errors) programmatically: try() gives you the ability to continue execution even when an error occurs. tryCatch() lets you specify handler functions that control what happens when a condition is signalled.

How do I turn off error messages in R?

8.3 Ignoring conditions The simplest way of handling conditions in R is to simply ignore them: Ignore errors with try() . Ignore warnings with suppressWarnings() . Ignore messages with suppressMessages() .

How do I throw an exception in R?

Throws an exception by calling stop(). Note that throw() can be defined for specific classes, which can then be caught (or not) using tryCatch (). This default function will be overridden by ditto in the R.


1 Answers

Granted it's not the most elegant, but you could try getting some information if you put your command in a try catch:

re.eval("estimates <-tryCatch(suppressWarnings(ugarchfit(spec = garchSpec, data = transformedTsValueData, solver.control = list(trace = 1))), error = function(e) { paste(\"e: \",e$message) }, warning = function(w) { paste(\"w: \", w$message) })");

you can then evaluate the response by inspecting the first few 3 characters. If you don't want to do this for every call, you could repeat your last command if your response is null when you're not expecting it (and repeating something that went wrong usually doesn't take too long).

Edit: Come to think of it, if the error occurs only when you evaluate "estimates", it may be better to wrap that last one in the try catch:

re.eval("tryCatch(suppressWarnings(estimates), error = function(e) { paste(\"e: \",e$message) }, warning = function(w) { paste(\"w: \", w$message) })");
like image 143
Kwantize Avatar answered Oct 07 '22 05:10

Kwantize