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