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