Search in sources :

Example 1 with DoubleVector

use of org.renjin.sexp.DoubleVector 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 DoubleVector

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

the class RenjinSession method set.

@Override
public boolean set(String varname, double[][] data, String... names) {
    if (data == null) {
        if (names == null) {
            return false;
        }
        List<SEXP> nulls = new LinkedList<>();
        for (int i = 0; i < names.length; i++) {
            nulls.add(Null.INSTANCE.clone());
        }
        ListVector l = new ListVector(nulls);
        synchronized (R) {
            R.put(varname, l);
            R.put(varname + ".names", new StringArrayVector(names));
            try {
                R.eval("names(" + varname + ") <- " + varname + ".names");
            // R.eval(varname + " <- data.frame(" + varname + ")");
            } catch (ScriptException ex) {
                ex.printStackTrace();
                return false;
            }
        }
        return true;
    } else {
        DoubleVector[] d = new DoubleVector[data[0].length];
        for (int i = 0; i < d.length; i++) {
            d[i] = new DoubleArrayVector(DoubleArray.getColumnCopy(data, i));
        }
        ListVector l = new ListVector(d);
        // l.setAttribute(Symbols.NAMES, new StringArrayVector(names));
        synchronized (R) {
            R.put(varname, l);
            // R.put("names("+varname+")",new StringArrayVector(names));
            R.put(varname + ".names", new StringArrayVector(names));
            try {
                R.eval("names(" + varname + ") <- " + varname + ".names");
                R.eval(varname + " <- data.frame(" + varname + ")");
            } catch (ScriptException ex) {
                ex.printStackTrace();
                return false;
            }
        }
        return true;
    }
}
Also used : DoubleArrayVector(org.renjin.sexp.DoubleArrayVector) ScriptException(javax.script.ScriptException) ListVector(org.renjin.sexp.ListVector) StringArrayVector(org.renjin.sexp.StringArrayVector) DoubleVector(org.renjin.sexp.DoubleVector) SEXP(org.renjin.sexp.SEXP) LinkedList(java.util.LinkedList)

Aggregations

ScriptException (javax.script.ScriptException)2 DoubleVector (org.renjin.sexp.DoubleVector)2 SEXP (org.renjin.sexp.SEXP)2 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 Matrix (org.renjin.primitives.matrix.Matrix)1 DoubleArrayVector (org.renjin.sexp.DoubleArrayVector)1 ListVector (org.renjin.sexp.ListVector)1 StringArrayVector (org.renjin.sexp.StringArrayVector)1