Search in sources :

Example 1 with ListVector

use of org.renjin.sexp.ListVector 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 2 with ListVector

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

ListVector (org.renjin.sexp.ListVector)2 SEXP (org.renjin.sexp.SEXP)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 ScriptException (javax.script.ScriptException)1 DoubleArrayVector (org.renjin.sexp.DoubleArrayVector)1 DoubleVector (org.renjin.sexp.DoubleVector)1 StringArrayVector (org.renjin.sexp.StringArrayVector)1