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;
}
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;
}
}
Aggregations