use of org.rosuda.REngine.Rserve.RserveException in project rsession by yannrichet.
the class RserveSession method set.
/**
* Set R data.frame in R env.
*
* @param varname R list name
* @param data numeric data in list
* @param names names of columns
* @return succeeded ?
*/
@Override
public boolean set(String varname, double[][] data, String... names) {
RList list = buildRList(data, names);
log(HEAD_SET + varname + " <- " + list, Level.INFO);
try {
synchronized (R) {
R.assign(varname, REXP.createDataFrame(list));
}
} catch (REXPMismatchException re) {
re.printStackTrace();
log(HEAD_ERROR + " RList " + list.toString() + " not convertible as dataframe.", Level.ERROR);
return false;
} catch (RserveException ex) {
log(HEAD_EXCEPTION + ex.getMessage() + "\n set(String varname=" + varname + ",double[][] data, String... names)", Level.ERROR);
return false;
}
return true;
}
use of org.rosuda.REngine.Rserve.RserveException in project dataverse by IQSS.
the class RRequest method eval.
/**
* Evaluate script
* @return R-Expression
*/
public REXP eval() {
REXP result = null;
try {
open();
result = mRC.eval(mScript);
} catch (RserveException e) {
LOG.warning(String.format("RRequest: %s", e.getMessage()));
} finally {
close();
}
return result;
}
use of org.rosuda.REngine.Rserve.RserveException in project rsession by yannrichet.
the class RserveSessionTest method testRFileIO.
@Test
public void testRFileIO() throws Exception {
// get file test...
String remoteFile1 = "get" + rand + ".csv";
File localfile1 = new File(tmpdir.getParent(), remoteFile1);
System.out.println("GET :" + localfile1.getAbsolutePath());
s.voidEval("aa<-data.frame(A=c(1,2,3),B=c(4,5,6))");
s.voidEval("write.csv(file='" + remoteFile1 + "',aa)");
InputStream is1 = null;
OutputStream os1 = null;
try {
// System.out.println("openFile");
is1 = s.R.openFile(remoteFile1);
// System.out.println("OK");
os1 = new FileOutputStream(localfile1.getAbsolutePath());
byte[] buf = new byte[65536];
try {
s.R.setSendBufferSize(buf.length);
} catch (RserveException ex) {
ex.printStackTrace();
}
int n = 0;
while ((n = is1.read(buf)) > 0) {
os1.write(buf, 0, n);
}
// os1.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os1 != null) {
os1.close();
}
if (is1 != null) {
is1.close();
}
} catch (IOException ee) {
ee.printStackTrace();
}
}
assert localfile1.exists();
// check csv file is written
StringBuffer b = new StringBuffer();
try {
FileInputStream fis = new FileInputStream(localfile1);
Reader r = new BufferedReader(new InputStreamReader(fis));
int n = 0;
while ((n = r.read()) > 0) {
b.append((char) n);
}
r.close();
} catch (IOException e) {
e.printStackTrace();
}
assert b.charAt(4) == 'A' : b.charAt(4);
s.rawEval("rm(aa)");
// localfile1.delete();
// put file test...
String remoteFile2 = "put" + rand + ".csv";
File localfile2 = new File(tmpdir.getParent(), remoteFile2);
System.out.println("PUT :" + localfile2.getAbsolutePath());
String content = "A,B,C\n1,2,3\n";
try {
FileOutputStream fos = new FileOutputStream(localfile2);
Writer w = new BufferedWriter(new OutputStreamWriter(fos));
w.write(content);
w.flush();
w.close();
} catch (IOException e) {
e.printStackTrace();
}
// check csv file is written
try {
FileInputStream fis = new FileInputStream(localfile2);
Reader r = new BufferedReader(new InputStreamReader(fis));
int n = 0;
while ((n = r.read()) > 0) {
System.out.print((char) n);
}
r.close();
} catch (IOException e) {
e.printStackTrace();
}
InputStream is2 = null;
OutputStream os2 = null;
try {
// System.out.println("createFile");
os2 = s.R.createFile(remoteFile2);
// System.out.println("OK");
is2 = new FileInputStream(localfile2);
byte[] buf = new byte[65536];
try {
s.R.setSendBufferSize(buf.length);
} catch (RserveException ex) {
ex.printStackTrace();
}
int n = 0;
while ((n = is2.read(buf)) > 0) {
System.out.print(buf);
os2.write(buf, 0, n);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os2 != null) {
os2.close();
}
if (is2 != null) {
is2.close();
}
} catch (IOException ee) {
ee.printStackTrace();
}
}
s.rawEval("ABC<-read.csv(file='" + remoteFile2 + "', header = TRUE,sep=',')");
System.out.println(s.toString(s.cast(s.rawEval("ABC"))));
assert ((REXP) s.rawEval("ABC$A")).isNumeric();
s.rawEval("rm(ABC)");
localfile2.delete();
}
Aggregations