Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

R Exception Handling in Java

DZone's Guide to

R Exception Handling in Java

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Once you have plugged the statistical software R (www.r-project.org) in to your Java application (see blog entry about how to setup a connection from Java to R) you probably want to make sure that R does what you plugged it in for – not more and not less.

Our use case is to call an R script from the Java application in order to analyse application data. In particular, the Java application writes data to a csv file that is read into R where it is analysed and visualised. The visualisations are saved into pdf files which are the desired R script output. However, as soon as anything changed with the csv such that the R script bugged, the pdfs were not created, but the Java application kept on running without any notice.

A typical data scientist will agree that one usually writes an analytic R script in a way that it fits the one data set you have (basically without any error handling because you know the form of intermediate in- and output). If you have more than one data set you might write more generic analytic code, but anyway you don’t try to catch any possible exception simply because it’s impossible to write clear analytic R code while handling all possible exceptions.

For our use case we opted for a relatively simple R error handling strategy: The typical (rather non-error proof) analytic R script is called by another R script that wraps the source() command into the tryCatch.W.E() error function of R and saves the result into a list. The resulting list is then searched for errors.

scriptResult <- tryCatch.W.E( source(analysisScript) )
isError <- grep("error",tolower(is(scriptResult$value)))

In the Java application you only call the R wrapper script and its result. If the result indicates an error the Java application throws an exception or warning in order to indicate that the R script did not run as expected:

int isError = (int) rengine.eval("as.double(isError)").asDouble();
if (isError == 1) {
   String error = rengine.eval("paste(scriptResult$value)").getContent().toString();
   String msg = "script 'analysis.R': %s";
   LOGGER.error(String.format(msg, error));
 }

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Comsysto Gmbh, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}