How to find what went wrong during eval in R?

337 Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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) })");