Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access JRI exception or trace from Java #11

Open
kenahoo opened this issue Jul 28, 2016 · 0 comments
Open

Access JRI exception or trace from Java #11

kenahoo opened this issue Jul 28, 2016 · 0 comments

Comments

@kenahoo
Copy link

kenahoo commented Jul 28, 2016

If we call REngine.eval(REXP what, REXP where, boolean resolve) in Java, and the R command throws an exception (e.g. an explicit stop() or some other unanticipated condition), we currently get an REngineEvalException with the message "error during evaluation". How can we access the error message that R generates?

One technique I've been using goes something like this:

    REXP result = null;
    try {
        result = rengine.parseAndEval(expression);
    } catch (REngineException e) {
        // The REngineException for a syntax error typically only says "Parse error", so we include the code for context
        if (e.getMessage().equals("Parse error"))
            throw new REngineException(rengine, "Parse error in R code '" + expression + "'", e);
        if (e.getMessage().equals("error during evaluation")) {
            List<String> traceback = new ArrayList<>();
            for (Object o : rengine.parseAndEval("traceback()").asList()) {
                traceback.addAll(java.util.Arrays.asList(((REXPString) o).asStrings()));
            }
            throw new REngineException(rengine, "error during evaluation of R code:\n" + StringUtils.join(traceback, "\n"), e);
        }

        throw e;
    }

This is fairly brittle, because it depends on the specific error text, etc. Is similar information available somehow from the REngine, or if not, perhaps it could be added?

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant