/** returns the content of the REXP as a matrix of doubles (2D-array: m[rows][cols]). This is the same form as used by popular math packages for Java, such as JAMA. This means that following leads to desired results:<br> <code>Matrix m=new Matrix(c.eval("matrix(c(1,2,3,4,5,6),2,3)").asDoubleMatrix());</code><br> @return 2D array of doubles in the form double[rows][cols] or <code>null</code> if the contents is no 2-dimensional matrix of doubles */ public double[][] asDoubleMatrix() throws REXPMismatchException { double[] ct = asDoubles(); REXP dim = getAttribute("dim"); if (dim == null) throw new REXPMismatchException(this, "matrix (dim attribute missing)"); int[] ds = dim.asIntegers(); if (ds.length != 2) throw new REXPMismatchException(this, "matrix (wrong dimensionality)"); int m = ds[0], n = ds[1]; double[][] r = new double[m][n]; // R stores matrices as matrix(c(1,2,3,4),2,2) = col1:(1,2), col2:(3,4) // we need to copy everything, since we create 2d array from 1d array int k = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) r[j][i] = ct[k++]; return r; }
/** determines whether this object inherits from a given class in the same fashion as the <code>inherits()</code> function in R does (i.e. ignoring S4 inheritance) * @param klass class name * @return <code>true</code> if this object is of the class <code>klass</code>, <code>false</code> otherwise */ public boolean inherits(String klass) { if (!hasAttribute("class")) return false; try { String c[] = getAttribute("class").asStrings(); if (c != null) { int i = 0; while (i < c.length) { if (c[i]!=null && c[i].equals(klass)) return true; i++; } } } catch (REXPMismatchException me) { } return false; }
public RList asList() throws REXPMismatchException { return resolve().asList(); } public RFactor asFactor() throws REXPMismatchException { return resolve().asFactor(); }
/** returns dimensions of the object (as determined by the "<code>dim</code>" attribute) * @return an array of integers with corresponding dimensions or <code>null</code> if the object has no dimension attribute */ public int[] dim() { try { return hasAttribute("dim")?_attr().asList().at("dim").asIntegers():null; } catch (REXPMismatchException me) { } return null; }
if (retVal.isInteger()) { len = retVal.length(); iAList[i] = (retVal.asIntegers()[i]); intOutput.emit(retVal.asInteger()); } else if (retVal.isNumeric()) { len = retVal.length(); dAList[i] = (retVal.asDoubles()[i]); doubleOutput.emit(retVal.asDouble()); } else if (retVal.isString()) { len = retVal.length(); strArrayOutput.emit(retVal.asStrings()); strOutput.emit(retVal.asString()); } else if (retVal.isLogical()) { len = retVal.length(); boolean[] bData = new boolean[len];
if (rExp.isNull()) { return stringNA; if (rExp.isLogical()) { return rExp.asString(); if (rExp.isInteger()) { if (rExp.asInteger() != 0) { return stringTrue; if (rExp.isNumeric()) { if (Math.abs(rExp.asDouble()) > 0.00001) { return stringTrue; if (rExp.isString()) { if (rExp.asString().equalsIgnoreCase("true")) { return stringTrue; if (rExp.isNull()) { return boolResult; if (rExp.isLogical()) { for (String s : rExp.asStrings()) { boolResult.add(s); if (rExp.isInteger()) { for (int i : rExp.asIntegers()) { boolResult.add (i != 0 ? stringTrue : stringFalse);
log.debug("Converting : " + rexp.toString()); if (rexp.isInteger()) { int[] array = rexp.asIntegers(); result = array; } else if (rexp.isFactor()) { result = rexp.asFactor(); } else if (rexp.isNumeric()) { double[] doubleArray = rexp.asDoubles(); result = doubleArray; } else if (rexp.isString()) { result = rexp.asStrings(); String[] stringArray = (String[]) result; } else if (rexp.isLogical()) { result = rexp.asStrings(); String[] strings = ((String[]) result); } else if (rexp.isNull()) { } else if (rexp.inherits(RInstructions.CLASS_DATAFRAME)) { result = convertToRDataFrame(rexp);
if (type==XT_VECTOR_STR) rxt=XT_ARRAY_STR; // VECTOR_STR is broken right now if (type==XT_LIST || type==XT_LIST_TAG || type==XT_LIST_NOTAG) rxt=(cont.asList()!=null && cont.asList().isNamed())?XT_LIST_TAG:XT_LIST_NOTAG; case XT_NULL: break; case XT_INT: RTalk.setInt(cont.asInteger(),buf,off); break; case XT_DOUBLE: RTalk.setLong(Double.doubleToRawLongBits(cont.asDouble()),buf,off); break; case XT_ARRAY_INT: int ia[]=cont.asIntegers(); int i=0, io=off; while(i<ia.length) { byte ba[] = cont.asBytes(); int io = off; RTalk.setInt(ba.length, buf, io); double da[]=cont.asDoubles(); int i=0, io=off; while(i<da.length) { byte by[] = cont.asBytes(); RTalk.setInt(by.length, buf, off); off+=4; System.arraycopy(by, 0, buf, off, by.length); String sa[] = cont.asStrings(); int i = 0, io = off; while (i < sa.length) {
int rxt = type; if (type==XT_LIST || type==XT_LIST_TAG || type==XT_LIST_NOTAG) rxt=(cont.asList()!=null && cont.asList().isNamed())?XT_LIST_TAG:XT_LIST_NOTAG; case XT_INT: l+=4; break; case XT_DOUBLE: l+=8; break; case XT_RAW: l+=4 + cont.asBytes().length; if ((l&3)>0) l=l-(l&3)+4; break; case XT_STR: case XT_SYMNAME: l+=(cont==null)?1:(cont.asString().length()+1); if ((l&3)>0) l=l-(l&3)+4; break; case XT_ARRAY_INT: l+=cont.asIntegers().length*4; break; case XT_ARRAY_DOUBLE: l+=cont.asDoubles().length*8; break; case XT_ARRAY_CPLX: l+=cont.asDoubles().length*8; break; case XT_ARRAY_BOOL: l += cont.asBytes().length + 4; if ((l & 3) > 0) l = l - (l & 3) + 4; break; case XT_LIST_TAG: case XT_LIST_NOTAG: case XT_VECTOR: final RList lst = cont.asList(); int i=0; while (i<lst.size()) { String sa[] = cont.asStrings(); int i=0; while (i < sa.length) {
REXP ca = getAttr().asList().at("class"); REXP ls = getAttr().asList().at("levels"); if (ca != null && ls != null && ca.asString().equals("factor")) { cont = new REXPFactor(d, ls.asStrings(), getAttr()); xt = XT_FACTOR; if (xt==XT_LIST_TAG || xt==XT_LANG_TAG) { o = nf.parseREXP(buf, o); if (nf.cont.isSymbol() || nf.cont.isString()) name = nf.cont.asString(); if (tagf.cont != null && (tagf.cont.isString() || tagf.cont.isSymbol())) rootList.setKeyAt(elIndex, tagf.cont.asString()); REXP nam = getAttr().asList().at("names"); String names[] = null; if (nam.isString()) names = nam.asStrings(); else if (nam.isVector()) { // names could be a vector if supplied by old Rserve RList l = nam.asList(); Object oa[] = l.toArray(); names = new String[oa.length]; for(int i = 0; i < oa.length; i++) names[i] = ((REXP)oa[i]).asString(); REXPFactory xx=new REXPFactory(); o = xx.parseREXP(buf,o); v.addElement(xx.cont.asString()); cont = new REXPSymbol(sym.getREXP().asString()); // content of a symbol is its printname string (so far) o=eox;
if (value.isLogical()) { if (((REXPLogical)value).length() == 1) { script = inputName + " <- " + value.asString() + ";\n" + script; } else { String[] strings = value.asStrings(); String completeString = "c(" + StringUtils.join(strings, ",") + ")"; script = inputName + " <- " + completeString + ";\n" + script; connection.assign(".tmp.", script); REXP r = connection.parseAndEval("try(eval(parse(text=.tmp.)),silent=TRUE)"); if (r.inherits("try-error")) { String errorString = r.asString(); if (errorString.contains(": ")) { errorString = errorString.substring(errorString.indexOf(": ") + 2); connection.assign(".tmp.", returnString.toString()); REXP results = connection.parseAndEval("try(eval(parse(text=.tmp.)),silent=TRUE)"); if (results.inherits("try-error")) { String errorString = results.asString(); if (errorString.contains(": ")) { errorString = errorString.substring(errorString.indexOf(": ") + 2); if (results.isList()) { RList resultList = results.asList(); else if (results.isVector()) { if (outputPorts.size() == 0) {
RList columnMeta = metaInfo.at(k).asList(); variableType = !columnMeta.at("type").isNull() ? columnMeta.at("type").asInteger() : null; variableTypeName = !columnMeta.at("type.string").isNull() ? columnMeta.at("type.string").asString() : null; variableLevels = !columnMeta.at("levels").isNull() ? columnMeta.at("levels").asStrings() : new String[0]; variableFormat = !columnMeta.at("format").isNull() ? columnMeta.at("format").asString() : null;
org.rosuda.REngine.RList dataList = rexp.asList(); temp = new RDataFrame(this, rexp.getAttribute( RInstructions.ATTRIBUTE_NAMES).asStrings(), rexp.getAttribute(RInstructions.ATTRIBUTE_ROWNAMES).asStrings(), data, ""); return temp;
RRequest request = mRequestBuilder.build(); request.script(fileInfoScript); RList fileInformation = request.eval().asList(); RList metaInfo = fileInformation.at("meta.info").asList(); variableNames = fileInformation.at("varNames").asStrings(); int caseQuantity = 0; try { caseQuantity = fileInformation.at("caseQnty").asInteger(); } catch (REXPMismatchException rexp) {
double[] b1 = xp.asDoubles(); outputData[n] = b1[b1.length - 1]; int[] b1 = xp.asIntegers(); outputData[n] = b1[b1.length - 1]; int[] b1 = xp.asIntegers(); outputData[n] = (long) b1[b1.length - 1]; outputData[len2] = xp.asBytes();
/** convenience method corresponding to <code>asDoubles()[0]</code> @return first entry returned by {@link #asDoubles} */ public double asDouble() throws REXPMismatchException { double[] d = asDoubles(); return d[0]; } /** convenience method corresponding to <code>asStrings()[0]</code>
/** convenience method corresponding to <code>asIntegers()[0]</code> @return first entry returned by {@link #asInteger} */ public int asInteger() throws REXPMismatchException { int[] i = asIntegers(); return i[0]; } /** convenience method corresponding to <code>asDoubles()[0]</code>
/** convenience method corresponding to <code>asStrings()[0]</code> @return first entry returned by {@link #asStrings} */ public String asString() throws REXPMismatchException { String[] s = asStrings(); return s[0]; }
public REXPFactory(REXP r) throws REXPMismatchException { if (r == null) r=new REXPNull(); REXPList a = r._attr(); cont = r; if (a != null) attr = new REXPFactory(a); type=XT_NULL; } else if (r instanceof REXPList) { RList l = r.asList(); type = l.isNamed()?XT_LIST_TAG:XT_LIST_NOTAG; if (r instanceof REXPLanguage)