Search in sources :

Example 6 with SEXP

use of org.renjin.sexp.SEXP in project rsession by yannrichet.

the class RenjinSessionTest method testExplicitSink.

// @Test
public void testExplicitSink() throws Exception {
    s.SINK_OUTPUT = false;
    s.SINK_MESSAGE = false;
    s.voidEval(f);
    // without sink: SIGPIPE error
    if (new File(tmpdir, "output.txt").exists()) {
        assert new File(tmpdir, "output.txt").delete() : "Cannot delete output.txt";
    }
    if (new File(tmpdir, "message.txt").exists()) {
        assert new File(tmpdir, "message.txt").delete() : "Cannot delete message.txt";
    }
    s.voidEval("sink('" + tmpdir.getAbsolutePath() + "/output.txt',type='output')");
    s.voidEval("sink('" + tmpdir.getAbsolutePath() + "/message.txt',type='message')");
    SEXP maxsin = (SEXP) s.rawEval("f()");
    assert Arrays.asList((s.asStrings(s.rawEval("readLines('" + tmpdir.getAbsolutePath() + "/output.txt')")))).size() > 0 : "Empty output sinked";
    assert Arrays.asList((s.asStrings(s.rawEval("readLines('" + tmpdir.getAbsolutePath() + "/message.txt')")))).size() > 0 : "Empty message sinked";
    s.voidEval("sink(type='output')");
    s.voidEval("sink(type='message')");
    assert maxsin != null : s.getLastLogEntry();
    assert s.asDouble(maxsin) == 0 : "Wrong eval";
    SEXP test = (SEXP) s.rawEval("1+pi");
    assert s.asDouble(test) > 4 : "Failed next eval";
    s.SINK_OUTPUT = true;
}
Also used : SEXP(org.renjin.sexp.SEXP) File(java.io.File)

Example 7 with SEXP

use of org.renjin.sexp.SEXP in project rsession by yannrichet.

the class RenjinSessionTest method testEvalError.

@Test
public void testEvalError() throws Exception {
    String[] exprs = { "a <- 1.0.0", "f <- function(x){((}" };
    for (String expr : exprs) {
        System.err.println("trying expression " + expr);
        try {
            boolean done = s.voidEval(expr);
            assert !done : "error not found in " + expr;
        } catch (Exception e) {
            System.err.println("Well detected error in " + expr);
        // Exception well raised, everything is ok.
        }
    }
    String[] evals = { "(xsgsdfgd", "1.0.0" };
    for (String eval : evals) {
        System.err.println("trying evaluation " + eval);
        try {
            SEXP e = (SEXP) s.rawEval(eval);
            assert e == null : "error not found in " + eval;
        } catch (Exception e) {
            System.err.println("Well detected error in " + eval);
        // Exception well raised, everything is ok.
        }
    }
}
Also used : SEXP(org.renjin.sexp.SEXP) IOException(java.io.IOException) Test(org.junit.Test)

Example 8 with SEXP

use of org.renjin.sexp.SEXP in project rsession by yannrichet.

the class RenjinSession method silentlyRawEval.

@Override
public Object silentlyRawEval(String expression, boolean tryEval) {
    if (R == null) {
        log(HEAD_EXCEPTION + "R environment not initialized.", Level.ERROR);
        return null;
    }
    if (expression == null) {
        return null;
    }
    if (expression.trim().length() == 0) {
        return null;
    }
    for (EvalListener b : eval) {
        b.eval(expression);
    }
    SEXP e = null;
    synchronized (R) {
        try {
            if (SINK_OUTPUT) {
                R.eval(".f <- file('" + fixPathSeparator(SINK_FILE) + "',open='wt')");
                R.eval("sink(.f,type='output')");
            }
            if (SINK_MESSAGE) {
                R.eval(".fm <- file('" + fixPathSeparator(SINK_FILE) + ".m',open='wt')");
                R.eval("sink(.fm,type='message')");
            }
            if (tryEval) {
                e = (SEXP) (R.eval("try(eval(parse(text='" + expression.replace("'", "\\'") + "')),silent=FALSE)"));
            } else {
                e = (SEXP) (R.eval(expression));
            }
        } catch (Exception ex) {
            log(HEAD_EXCEPTION + ex.getMessage() + "\n  " + expression, Level.ERROR);
        } finally {
            if (SINK_OUTPUT) {
                try {
                    R.eval("sink(type='output')");
                    lastOuput = asString(R.eval("paste(collapse='\n',readLines('" + fixPathSeparator(SINK_FILE) + "'))"));
                    log(lastOuput, Level.OUTPUT);
                } catch (Exception ex) {
                    lastOuput = ex.getMessage();
                    log(lastOuput, Level.WARNING);
                } finally {
                    try {
                        // because Renjin.sink() do not properly close connection, so calling it explicitely
                        R.eval("close(.f)");
                        R.eval("unlink('" + fixPathSeparator(SINK_FILE) + "')");
                    } catch (Exception ex) {
                        log(HEAD_EXCEPTION + ex.getMessage(), Level.ERROR);
                    }
                }
            }
            if (SINK_MESSAGE) {
                try {
                    R.eval("sink(type='message')");
                    lastMessage = asString(R.eval("paste(collapse='\n',readLines('" + fixPathSeparator(SINK_FILE) + ".m'))"));
                    log(lastMessage, Level.INFO);
                } catch (Exception ex) {
                    lastMessage = ex.getMessage();
                    log(lastMessage, Level.WARNING);
                } finally {
                    try {
                        R.eval("close(.fm)");
                        R.eval("unlink('" + fixPathSeparator(SINK_FILE) + ".m')");
                    } catch (Exception ex) {
                        log(HEAD_EXCEPTION + ex.getMessage(), Level.ERROR);
                    }
                }
            }
        }
    }
    if (tryEval && e != null) {
        try {
            if (e.inherits("try-error")) /*e.isString() && e.asStrings().length > 0 && e.asString().toLowerCase().startsWith("error")*/
            {
                log(HEAD_EXCEPTION + e.asString() + "\n  " + expression, Level.WARNING);
                e = null;
            }
        } catch (Exception ex) {
            log(HEAD_ERROR + ex.getMessage() + "\n  " + expression, Level.ERROR);
            return null;
        }
    }
    return e;
}
Also used : SEXP(org.renjin.sexp.SEXP) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Example 9 with SEXP

use of org.renjin.sexp.SEXP in project rsession by yannrichet.

the class RenjinSession method asList.

@Override
public Map asList(Object o) throws ClassCastException {
    if (o == null) {
        return null;
    }
    if (o instanceof Map) {
        return (Map) o;
    }
    if (!(o instanceof SEXP)) {
        throw new IllegalArgumentException("[asList] Not an SEXP object: " + o);
    }
    if (!(o instanceof ListVector)) {
        throw new IllegalArgumentException("[asList] Not a ListVector object: " + o);
    }
    ListVector l = (ListVector) o;
    Map m = new HashMap<String, Object>();
    for (int i = 0; i < l.length(); i++) {
        m.put(l.getName(i), cast(l.get(i)));
    }
    return m;
}
Also used : HashMap(java.util.HashMap) ListVector(org.renjin.sexp.ListVector) SEXP(org.renjin.sexp.SEXP) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with SEXP

use of org.renjin.sexp.SEXP in project rsession by yannrichet.

the class RenjinSession method silentlyVoidEval.

@Override
public boolean silentlyVoidEval(String expression, boolean tryEval) {
    if (R == null) {
        log(HEAD_EXCEPTION + "R environment not initialized.", Level.ERROR);
        return false;
    }
    if (expression == null) {
        return false;
    }
    if (expression.trim().length() == 0) {
        return true;
    }
    for (EvalListener b : eval) {
        b.eval(expression);
    }
    SEXP e = null;
    synchronized (R) {
        try {
            if (SINK_OUTPUT) {
                R.eval(".f <- file('" + fixPathSeparator(SINK_FILE) + "',open='wt')");
                R.eval("sink(.f,type='output')");
            }
            if (SINK_MESSAGE) {
                R.eval(".fm <- file('" + fixPathSeparator(SINK_FILE) + ".m',open='wt')");
                R.eval("sink(.fm,type='message')");
            }
            if (tryEval) {
                e = ((SEXP) R.eval("try(eval(parse(text='" + expression.replace("'", "\\'") + "')),silent=FALSE)"));
            } else {
                e = ((SEXP) R.eval(expression));
            }
        } catch (Exception ex) {
            log(HEAD_EXCEPTION + ex.getMessage() + "\n  " + expression, Level.ERROR);
        } finally {
            if (SINK_OUTPUT) {
                try {
                    R.eval("sink(type='output')");
                    lastOuput = asString(R.eval("paste(collapse='\n',readLines('" + fixPathSeparator(SINK_FILE) + "'))"));
                    log(lastOuput, Level.OUTPUT);
                } catch (Exception ex) {
                    lastOuput = ex.getMessage();
                    log(lastOuput, Level.WARNING);
                } finally {
                    try {
                        R.eval("close(.f)");
                        R.eval("unlink('" + fixPathSeparator(SINK_FILE) + "')");
                    } catch (Exception ex) {
                        log(HEAD_EXCEPTION + ex.getMessage(), Level.ERROR);
                    }
                }
            }
            if (SINK_MESSAGE) {
                try {
                    R.eval("sink(type='message')");
                    lastMessage = asString(R.eval("paste(collapse='\n',readLines('" + fixPathSeparator(SINK_FILE) + ".m'))"));
                    log(lastMessage, Level.INFO);
                } catch (Exception ex) {
                    lastMessage = ex.getMessage();
                    log(lastMessage, Level.WARNING);
                } finally {
                    try {
                        R.eval("close(.fm)");
                        R.eval("unlink('" + fixPathSeparator(SINK_FILE) + ".m')");
                    } catch (Exception ex) {
                        log(HEAD_EXCEPTION + ex.getMessage(), Level.ERROR);
                    }
                }
            }
        }
    }
    if (tryEval && e != null) {
        try {
            if (e.inherits("try-error")) /*e.isString() && e.asStrings().length > 0 && e.asString().toLowerCase().startsWith("error")*/
            {
                log(HEAD_EXCEPTION + e.asString() + "\n  " + expression, Level.WARNING);
                return false;
            }
        } catch (Exception ex) {
            log(HEAD_ERROR + ex.getMessage() + "\n  " + expression, Level.ERROR);
            return false;
        }
    }
    return true;
}
Also used : SEXP(org.renjin.sexp.SEXP) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Aggregations

SEXP (org.renjin.sexp.SEXP)11 IOException (java.io.IOException)4 ScriptException (javax.script.ScriptException)4 Test (org.junit.Test)3 DoubleVector (org.renjin.sexp.DoubleVector)2 ListVector (org.renjin.sexp.ListVector)2 File (java.io.File)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Matrix (org.renjin.primitives.matrix.Matrix)1 DoubleArrayVector (org.renjin.sexp.DoubleArrayVector)1 StringArrayVector (org.renjin.sexp.StringArrayVector)1