Search in sources :

Example 11 with SEXP

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

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