/** * assign a string value to a symbol in R. The symbol is created if it doesn't exist already. * * @param sym symbol name. Currently assign uses CMD_setSEXP command of Rserve, i.e. the symbol * value is NOT parsed. It is the responsibility of the user to make sure that the symbol name * is valid in R (recall the difference between a symbol and an expression!). In fact R will * always create the symbol, but it may not be accessible (examples: "bar\nfoo" or "bar$foo"). * @param ct contents * @return <code>true</code> on success, otherwise <code>false</code> */ public void assign(String sym, String ct) throws RSrvException { if (!connected || rt == null) throw new RSrvException(this, "Not connected"); byte[] symn = sym.getBytes(); byte[] ctn = ct.getBytes(); int sl = symn.length + 1; int cl = ctn.length + 1; if ((sl & 3) > 0) sl = (sl & 0xfffffc) + 4; // make sure the symbol length is divisible by 4 if ((cl & 3) > 0) cl = (cl & 0xfffffc) + 4; // make sure the content length is divisible by 4 byte[] rq = new byte[sl + 4 + cl + 4]; int ic; for (ic = 0; ic < symn.length; ic++) rq[ic + 4] = symn[ic]; while (ic < sl) { rq[ic + 4] = 0; ic++; } for (ic = 0; ic < ctn.length; ic++) rq[ic + sl + 8] = ctn[ic]; while (ic < cl) { rq[ic + sl + 8] = 0; ic++; } Rtalk.setHdr(Rtalk.DT_STRING, sl, rq, 0); Rtalk.setHdr(Rtalk.DT_STRING, cl, rq, sl + 4); Rpacket rp = rt.request(Rtalk.CMD_setSEXP, rq); if (rp != null && rp.isOk()) return; throw new RSrvException(this, "assign failed", rp); }
/** * assign a content of a REXP to a symbol in R. The symbol is created if it doesn't exist already. * * @param sym symbol name. Currently assign uses CMD_setSEXP command of Rserve, i.e. the symbol * value is NOT parsed. It is the responsibility of the user to make sure that the symbol name * is valid in R (recall the difference between a symbol and an expression!). In fact R will * always create the symbol, but it may not be accessible (examples: "bar\nfoo" or "bar$foo"). * @param ct contents. currently only basic types (int, double, int[], double[]) are supported. * @return <code>true</code> on success, otherwise <code>false</code> */ public void assign(String sym, REXP r) throws RSrvException { if (!connected || rt == null) throw new RSrvException(this, "Not connected"); int rl = r.getBinaryLength(); byte[] symn = sym.getBytes(); int sl = symn.length + 1; if ((sl & 3) > 0) sl = (sl & 0xfffffc) + 4; // make sure the symbol length is divisible by 4 byte[] rq = new byte[sl + rl + ((rl > 0xfffff0) ? 12 : 8)]; int ic; for (ic = 0; ic < symn.length; ic++) rq[ic + 4] = symn[ic]; while (ic < sl) { rq[ic + 4] = 0; ic++; } ; // pad with 0 Rtalk.setHdr(Rtalk.DT_STRING, sl, rq, 0); Rtalk.setHdr(Rtalk.DT_SEXP, rl, rq, sl + 4); r.getBinaryRepresentation(rq, sl + ((rl > 0xfffff0) ? 12 : 8)); Rpacket rp = rt.request(Rtalk.CMD_setSEXP, rq); if (rp != null && rp.isOk()) return; throw new RSrvException(this, "assign failed", rp); }