Search in sources :

Example 1 with SEXP

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

the class RenjinSession method asMatrix.

@Override
public double[][] asMatrix(Object o) throws ClassCastException {
    if (o == null) {
        return null;
    }
    if (o instanceof double[][]) {
        return (double[][]) o;
    }
    if (o instanceof double[]) {
        return t(new double[][] { (double[]) o });
    }
    if (o instanceof Double) {
        return new double[][] { { (double) o } };
    }
    if (!(o instanceof SEXP)) {
        throw new IllegalArgumentException("[asMatrix] Not an SEXP object: " + o);
    }
    if (!(o instanceof DoubleVector)) {
        throw new IllegalArgumentException("[asMatrix] Not a DoubleVector object: " + o);
    }
    try {
        Matrix m = new Matrix((DoubleVector) o);
        double[][] mm = new double[m.getNumRows()][m.getNumCols()];
        for (int i = 0; i < mm.length; i++) {
            for (int j = 0; j < mm[i].length; j++) {
                mm[i][j] = m.getElementAsDouble(i, j);
            }
        }
        return mm;
    } catch (Exception ex) {
        throw new ClassCastException("[asMatrix] Cannot cast to double[][] " + o);
    }
}
Also used : Matrix(org.renjin.primitives.matrix.Matrix) DoubleVector(org.renjin.sexp.DoubleVector) SEXP(org.renjin.sexp.SEXP) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Example 2 with SEXP

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

the class RenjinPlotTest method eval.

protected SEXP eval(String source) {
    SEXP result = evaluate(source);
    printWarnings();
    return result;
}
Also used : SEXP(org.renjin.sexp.SEXP)

Example 3 with SEXP

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

the class RenjinPlotTest method printWarnings.

private void printWarnings() {
    SEXP warnings = topLevelContext.getBaseEnvironment().getVariable(Warning.LAST_WARNING);
    if (warnings != Symbol.UNBOUND_VALUE) {
        topLevelContext.evaluate(FunctionCall.newCall(Symbol.get("print.warnings"), warnings), topLevelContext.getBaseEnvironment());
        System.out.println();
    }
}
Also used : SEXP(org.renjin.sexp.SEXP)

Example 4 with SEXP

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

the class RenjinSessionTest method testErrorNOSink.

@Test
public void testErrorNOSink() throws Exception {
    s.voidEval(f);
    s.SINK_OUTPUT = false;
    SEXP maxsin = (SEXP) s.rawEval("f()");
    assert maxsin != null : "Null 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) Test(org.junit.Test)

Example 5 with SEXP

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

the class RenjinSessionTest method testDefaultSink.

@Test
public void testDefaultSink() throws Exception {
    s.voidEval(f);
    // without sink: SIGPIPE error
    SEXP maxsin = (SEXP) s.rawEval("f()");
    // assert Arrays.asList((s.asStrings(s.rawEval("readLines('"+s.SINK_FILE+".m')")))).size() > 0 : "Empty message sinked";
    assert maxsin != null : s.getLastLogEntry();
    assert s.asDouble(maxsin) == 0 : "Wrong eval";
    assert s.getLastOutput().equals("cat") : "Wrong LastOutput: " + s.getLastOutput();
    assert s.getLastError().equals("message") : "Wrong LastError: " + s.getLastError();
    assert s.getLastLogEntry().contains("0.0") : "Wrong LastLogEntry: " + s.getLastLogEntry();
    SEXP test = (SEXP) s.rawEval("1+pi");
    assert s.asDouble(test) > 4 : "Failed next eval";
}
Also used : SEXP(org.renjin.sexp.SEXP) Test(org.junit.Test)

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