* INITIAL POPULATIONS OF THIS REPO
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.fourelementscapital</groupId>
|
||||
<artifactId>lib-r</artifactId>
|
||||
<version>2.0</version>
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The software is only allowed to be used in Four Elements - no license is given to any other parties</name>
|
||||
</license>
|
||||
</licenses>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.lucarosellini.rJava</groupId>
|
||||
<artifactId>JRI</artifactId>
|
||||
<version>0.9-7</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.rosuda.REngine</groupId>
|
||||
<artifactId>Rserve</artifactId>
|
||||
<version>1.8.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.8.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
Executable
+27
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.fourelementscapital</groupId>
|
||||
<artifactId>lib</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<groupId>com.fourelementscapital</groupId>
|
||||
<artifactId>lib-r</artifactId>
|
||||
<name>lib-r</name>
|
||||
<description>a Java library used to send R code</description>
|
||||
<url>http://www.fourelementscapital.com</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.lucarosellini.rJava</groupId>
|
||||
<artifactId>JRI</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.rosuda.REngine</groupId>
|
||||
<artifactId>Rserve</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,66 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
|
||||
* All rights reserved.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
package com.fourelementscapital.r;
|
||||
|
||||
import java.awt.FileDialog;
|
||||
import java.awt.Frame;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import org.rosuda.JRI.RMainLoopCallbacks;
|
||||
import org.rosuda.JRI.Rengine;
|
||||
|
||||
/**
|
||||
* Text Console for Rengine
|
||||
*/
|
||||
public class TextConsole implements RMainLoopCallbacks
|
||||
{
|
||||
public void rWriteConsole(Rengine re, String text, int oType) {
|
||||
System.out.print(text);
|
||||
}
|
||||
|
||||
public void rBusy(Rengine re, int which) {
|
||||
System.out.println("rBusy("+which+")");
|
||||
}
|
||||
|
||||
public String rReadConsole(Rengine re, String prompt, int addToHistory) {
|
||||
System.out.print(prompt);
|
||||
try {
|
||||
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
|
||||
String s=br.readLine();
|
||||
return (s==null||s.length()==0)?s:s+"\n";
|
||||
} catch (Exception e) {
|
||||
System.out.println("jriReadConsole exception: "+e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void rShowMessage(Rengine re, String message) {
|
||||
System.out.println("rShowMessage \""+message+"\"");
|
||||
}
|
||||
|
||||
public String rChooseFile(Rengine re, int newFile) {
|
||||
FileDialog fd = new FileDialog(new Frame(), (newFile==0)?"Select a file":"Select a new file", (newFile==0)?FileDialog.LOAD:FileDialog.SAVE);
|
||||
fd.show();
|
||||
String res=null;
|
||||
if (fd.getDirectory()!=null) res=fd.getDirectory();
|
||||
if (fd.getFile()!=null) res=(res==null)?fd.getFile():(res+fd.getFile());
|
||||
return res;
|
||||
}
|
||||
|
||||
public void rFlushConsole (Rengine re) {
|
||||
}
|
||||
|
||||
public void rLoadHistory (Rengine re, String filename) {
|
||||
}
|
||||
|
||||
public void rSaveHistory (Rengine re, String filename) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,815 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
|
||||
* All rights reserved.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
package com.fourelementscapital.r;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.rosuda.REngine.REXP;
|
||||
import org.rosuda.REngine.REXPDouble;
|
||||
import org.rosuda.REngine.REXPFactor;
|
||||
import org.rosuda.REngine.REXPGenericVector;
|
||||
import org.rosuda.REngine.REXPInteger;
|
||||
import org.rosuda.REngine.REXPList;
|
||||
import org.rosuda.REngine.REXPLogical;
|
||||
import org.rosuda.REngine.REXPMismatchException;
|
||||
import org.rosuda.REngine.REXPRaw;
|
||||
import org.rosuda.REngine.REXPSymbol;
|
||||
import org.rosuda.REngine.REngine;
|
||||
import org.rosuda.REngine.RFactor;
|
||||
import org.rosuda.REngine.RList;
|
||||
import org.rosuda.REngine.Rserve.RConnection;
|
||||
import org.rosuda.REngine.Rserve.RserveException;
|
||||
|
||||
/**
|
||||
* Test Exception
|
||||
*/
|
||||
class TestException extends Exception {
|
||||
public TestException(String msg) { super(msg); }
|
||||
}
|
||||
|
||||
/**
|
||||
* RServe unit test
|
||||
*/
|
||||
public class RServeTest extends TestCase {
|
||||
|
||||
/**
|
||||
* Test very simple scenario
|
||||
*/
|
||||
public void testVerySimple() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
double d[] = c.eval("rnorm(10)").asDoubles();
|
||||
c.close();
|
||||
assertTrue(d != null );
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test string and list retrieval
|
||||
*/
|
||||
public void testString() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
System.out.println(">>" + c.eval("R.version$version.string").asString() + "<<");
|
||||
System.out.println("* Test string and list retrieval");
|
||||
RList l = c.eval("{d=data.frame(\"huhu\",c(11:20)); lapply(d,as.character)}").asList();
|
||||
int cols = l.size();
|
||||
int rows = l.at(0).length();
|
||||
String[][] s = new String[cols][];
|
||||
for (int i=0; i<cols; i++) s[i]=l.at(i).asStrings();
|
||||
System.out.println("PASSED");
|
||||
c.close();
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (RserveException rse) {
|
||||
System.out.println(rse);
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (REXPMismatchException mme) {
|
||||
System.out.println(mme);
|
||||
mme.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test NA/NaN support in double vectors
|
||||
*/
|
||||
public void testNA() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
System.out.println("* Test NA/NaN support in double vectors...");
|
||||
double R_NA = REXPDouble.NA;
|
||||
double x[] = { 1.0, 0.5, R_NA, Double.NaN, 3.5 };
|
||||
c.assign("x",x);
|
||||
String nas = c.eval("paste(capture.output(print(x)),collapse='\\n')").asString();
|
||||
System.out.println(nas);
|
||||
if (!nas.equals("[1] 1.0 0.5 NA NaN 3.5"))
|
||||
throw new TestException("NA/NaN assign+retrieve test failed");
|
||||
REXP rx = c.eval("c(2.2, NA_real_, NaN)");
|
||||
if (rx == null || !rx.isNumeric() || rx.isInteger())
|
||||
throw new TestException("NA/NaN pull test failed (invalid result)");
|
||||
boolean nal[] = rx.isNA();
|
||||
if (nal.length != 3 || nal[0] || !nal[1] || nal[2])
|
||||
throw new TestException("NS/NAN pull test: NA pull failed");
|
||||
x = rx.asDoubles();
|
||||
if (x.length != 3 || !Double.isNaN(x[2]) || REXPDouble.isNA(x[2]) || !REXPDouble.isNA(x[1]))
|
||||
throw new TestException("NS/NAN pull test: NA/NaN pull failed");
|
||||
System.out.println("PASSED");
|
||||
c.close();
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (RserveException rse) {
|
||||
System.out.println(rse);
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (REXPMismatchException mme) {
|
||||
System.out.println(mme);
|
||||
mme.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch(TestException te) {
|
||||
System.err.println("** Test failed: "+te.getMessage());
|
||||
te.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test assigning of lists and vectors
|
||||
*/
|
||||
public void testList() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
System.out.println("* Test assigning of lists and vectors ...");
|
||||
RList l = new RList();
|
||||
l.put("a",new REXPInteger(new int[] { 0,1,2,3}));
|
||||
l.put("b",new REXPDouble(new double[] { 0.5,1.2,2.3,3.0}));
|
||||
System.out.println(" assign x=pairlist");
|
||||
c.assign("x", new REXPList(l));
|
||||
System.out.println(" assign y=vector");
|
||||
c.assign("y", new REXPGenericVector(l));
|
||||
System.out.println(" assign z=data.frame");
|
||||
c.assign("z", REXP.createDataFrame(l));
|
||||
System.out.println(" pull all three back to Java");
|
||||
REXP x = c.parseAndEval("x");
|
||||
System.out.println(" x = "+x);
|
||||
x = c.eval("y");
|
||||
System.out.println(" y = "+x);
|
||||
x = c.eval("z");
|
||||
System.out.println(" z = "+x);
|
||||
System.out.println("PASSED");
|
||||
c.close();
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (RserveException rse) {
|
||||
System.out.println(rse);
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (REXPMismatchException mme) {
|
||||
System.out.println(mme);
|
||||
mme.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test support for logicals
|
||||
*/
|
||||
public void testLogical() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
|
||||
System.out.println("* Test support for logicals ... ");
|
||||
System.out.println(" assign b={true,false,true}");
|
||||
c.assign("b", new REXPLogical(new boolean[] { true, false, true }));
|
||||
REXP x = c.parseAndEval("b");
|
||||
System.out.println(" " + ((x != null) ? x.toDebugString() : "NULL"));
|
||||
if (!x.isLogical() || x.length() != 3)
|
||||
throw new TestException("boolean array assign+retrieve test failed");
|
||||
boolean q[] = ((REXPLogical)x).isTRUE();
|
||||
if (q[0] != true || q[1] != false || q[2] != true)
|
||||
throw new TestException("boolean array assign+retrieve test failed (value mismatch)");
|
||||
System.out.println(" get c(TRUE,FLASE,NA)");
|
||||
x = c.parseAndEval("c(TRUE,FALSE,NA)");
|
||||
System.out.println(" " + ((x != null) ? x.toDebugString() : "NULL"));
|
||||
if (!x.isLogical() || x.length() != 3)
|
||||
throw new TestException("boolean array NA test failed");
|
||||
boolean q1[] = ((REXPLogical)x).isTRUE();
|
||||
boolean q2[] = ((REXPLogical)x).isFALSE();
|
||||
boolean q3[] = ((REXPLogical)x).isNA();
|
||||
if (q1[0] != true || q1[1] != false || q1[2] != false ||
|
||||
q2[0] != false || q2[1] != true || q2[2] != false ||
|
||||
q3[0] != false || q3[1] != false || q3[2] != true)
|
||||
throw new TestException("boolean array NA test failed (value mismatch)");
|
||||
|
||||
c.close();
|
||||
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (RserveException rse) {
|
||||
System.out.println(rse);
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (REXPMismatchException mme) {
|
||||
System.out.println(mme);
|
||||
mme.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch(TestException te) {
|
||||
System.err.println("** Test failed: "+te.getMessage());
|
||||
te.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Testing functionality of assembled S3 objects
|
||||
*/
|
||||
public void testS3() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
|
||||
// regression: object bit was not set for Java-side generated objects before 0.5-3
|
||||
System.out.println("* Testing functionality of assembled S3 objects ...");
|
||||
|
||||
RList l = new RList();
|
||||
l.put("a",new REXPInteger(new int[] { 0,1,2,3}));
|
||||
l.put("b",new REXPDouble(new double[] { 0.5,1.2,2.3,3.0}));
|
||||
c.assign("z", REXP.createDataFrame(l));
|
||||
|
||||
REXP x = c.parseAndEval("z[2,2]");
|
||||
|
||||
System.out.println(" z[2,2] = " + x);
|
||||
if (x == null || x.length() != 1 || x.asDouble() != 1.2)
|
||||
throw new TestException("S3 object bit regression test failed");
|
||||
System.out.println("PASSED");
|
||||
|
||||
c.close();
|
||||
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (RserveException rse) {
|
||||
System.out.println(rse);
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (REXPMismatchException mme) {
|
||||
System.out.println(mme);
|
||||
mme.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch(TestException te) {
|
||||
System.err.println("** Test failed: "+te.getMessage());
|
||||
te.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Testing pass-though capability for data.frames
|
||||
*/
|
||||
public void testDataFrames() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
|
||||
// this test does a pull and push of a data frame. It will fail when the S3 test above failed.
|
||||
System.out.println("* Testing pass-though capability for data.frames ...");
|
||||
REXP df = c.parseAndEval("{data(iris); iris}");
|
||||
c.assign("df", df);
|
||||
REXP x = c.eval("identical(df, iris)");
|
||||
System.out.println(" identical(df, iris) = "+x);
|
||||
if (x == null || !x.isLogical() || x.length() != 1 || !((REXPLogical)x).isTrue()[0])
|
||||
throw new TestException("Pass-through test for a data.frame failed");
|
||||
System.out.println("PASSED");
|
||||
|
||||
c.close();
|
||||
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (RserveException rse) {
|
||||
System.out.println(rse);
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (REXPMismatchException mme) {
|
||||
System.out.println(mme);
|
||||
mme.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch(TestException te) {
|
||||
System.err.println("** Test failed: "+te.getMessage());
|
||||
te.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test support of factors
|
||||
*/
|
||||
public void testFactors() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
|
||||
System.out.println("* Test support of factors");
|
||||
REXP f = c.parseAndEval("factor(paste('F',as.integer(runif(20)*5),sep=''))");
|
||||
System.out.println(" f="+f);
|
||||
System.out.println(" isFactor: "+f.isFactor()+", asFactor: "+f.asFactor());
|
||||
if (!f.isFactor() || f.asFactor() == null) throw new TestException("factor test failed");
|
||||
System.out.println(" singe-level factor used to degenerate:");
|
||||
f = c.parseAndEval("factor('foo')");
|
||||
System.out.println(" isFactor: "+f.isFactor()+", asFactor: "+f.asFactor());
|
||||
if (!f.isFactor() || f.asFactor() == null) throw new TestException("single factor test failed (not a factor)");
|
||||
if (!f.asFactor().at(0).equals("foo")) throw new TestException("single factor test failed (wrong value)");
|
||||
System.out.println(" test factors with null elements contents:");
|
||||
c.assign("f", new REXPFactor(new RFactor(new String[] { "foo", "bar", "foo", "foo", null, "bar" })));
|
||||
f = c.parseAndEval("f");
|
||||
if (!f.isFactor() || f.asFactor() == null) throw new TestException("factor assign-eval test failed (not a factor)");
|
||||
System.out.println(" f = "+f.asFactor());
|
||||
f = c.parseAndEval("as.factor(c(1,'a','b',1,'b'))");
|
||||
System.out.println(" f = "+f);
|
||||
if (!f.isFactor() || f.asFactor() == null) throw new TestException("factor test failed (not a factor)");
|
||||
System.out.println("PASSED");
|
||||
|
||||
c.close();
|
||||
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (RserveException rse) {
|
||||
System.out.println(rse);
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (REXPMismatchException mme) {
|
||||
System.out.println(mme);
|
||||
mme.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch(TestException te) {
|
||||
System.err.println("** Test failed: "+te.getMessage());
|
||||
te.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test lowess
|
||||
*/
|
||||
public void testLowess() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
|
||||
System.out.println("* Lowess test");
|
||||
double x[] = c.eval("rnorm(100)").asDoubles();
|
||||
double y[] = c.eval("rnorm(100)").asDoubles();
|
||||
c.assign("x", x);
|
||||
c.assign("y", y);
|
||||
RList l = c.parseAndEval("lowess(x,y)").asList();
|
||||
System.out.println(" "+l);
|
||||
x = l.at("x").asDoubles();
|
||||
y = l.at("y").asDoubles();
|
||||
System.out.println("PASSED");
|
||||
|
||||
c.close();
|
||||
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (RserveException rse) {
|
||||
System.out.println(rse);
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (REXPMismatchException mme) {
|
||||
System.out.println(mme);
|
||||
mme.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test multi-line expressions
|
||||
*/
|
||||
public void testMultiLineExpressions() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
|
||||
System.out.println("* Test multi-line expressions");
|
||||
if (c.eval("{ a=1:10\nb=11:20\nmean(b-a) }\n").asInteger()!=10)
|
||||
throw new TestException("multi-line test failed.");
|
||||
System.out.println("PASSED");
|
||||
|
||||
c.close();
|
||||
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (RserveException rse) {
|
||||
System.out.println(rse);
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (REXPMismatchException mme) {
|
||||
System.out.println(mme);
|
||||
mme.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch(TestException te) {
|
||||
System.err.println("** Test failed: "+te.getMessage());
|
||||
te.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test matrix
|
||||
*/
|
||||
public void testMatrix() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
|
||||
System.out.println("* Matrix tests\n matrix: create a matrix");
|
||||
int m = 100, n = 100;
|
||||
double[] mat=new double[m * n];
|
||||
{ int i=0; while (i < m * n) mat[i++] = i / 100; }
|
||||
System.out.println(" matrix: assign/retrieve a matrix");
|
||||
c.assign("m", mat);
|
||||
c.voidEval("m<-matrix(m,"+m+","+n+")");
|
||||
System.out.println(" matrix: use createDoubleMatrix");
|
||||
double[][] A = new double[m][n];
|
||||
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) A[i][j] = mat[i + j * m];
|
||||
c.assign("m2", REXP.createDoubleMatrix(A));
|
||||
if (c.eval("identical(m ,m2)").asInteger() != 1)
|
||||
throw new TestException("matrix test failed: createDoubleMatrix result is not the same as direct assign");
|
||||
System.out.println("matrix: cross-product");
|
||||
double[][] mr = c.parseAndEval("crossprod(m,m)").asDoubleMatrix();
|
||||
System.out.println("PASSED");
|
||||
|
||||
c.close();
|
||||
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (RserveException rse) {
|
||||
System.out.println(rse);
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (REXPMismatchException mme) {
|
||||
System.out.println(mme);
|
||||
mme.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch(TestException te) {
|
||||
System.err.println("** Test failed: "+te.getMessage());
|
||||
te.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test serialization and raw vectors
|
||||
*/
|
||||
public void testSerialization() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
|
||||
System.out.println("* Test serialization and raw vectors");
|
||||
byte[] b = c.eval("serialize(ls, NULL, ascii=FALSE)").asBytes();
|
||||
System.out.println(" serialized ls is "+b.length+" bytes long");
|
||||
c.assign("r", new REXPRaw(b));
|
||||
String[] s = c.eval("unserialize(r)()").asStrings();
|
||||
System.out.println(" we have "+s.length+" items in the workspace");
|
||||
System.out.println("PASSED");
|
||||
|
||||
c.close();
|
||||
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (RserveException rse) {
|
||||
System.out.println(rse);
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (REXPMismatchException mme) {
|
||||
System.out.println(mme);
|
||||
mme.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test handling of NAs in character vectors ('foo', '', NA, 'NA')
|
||||
*/
|
||||
public void testNAInCharVectors() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
// REngine is the backend-agnostic API -- using eng instead of c makes sure that we don't use Rserve extensions inadvertently
|
||||
REngine eng = (REngine) c;
|
||||
|
||||
//NAs in character vectors are mapped to null references in String[] and vice versa. Only Rserve 0.6-2 and later support NAa in character vectors.
|
||||
//repression test: assigning empty string '' has failed in previous versions of RserveEngine
|
||||
System.out.println("* Test handling of NAs in character vectors ('foo', '', NA, 'NA')");
|
||||
System.out.print(" push String[] with NAs: ");
|
||||
eng.assign("s", new String[] { "foo", "", null, "NA" });
|
||||
int nas[] = eng.parseAndEval("is.na(s)").asIntegers();
|
||||
for (int i = 0; i < nas.length; i++) System.out.print(nas[i] + " ");
|
||||
if (nas.length != 4 || nas[0] != REXPLogical.FALSE || nas[1] != REXPLogical.FALSE || nas[2] != REXPLogical.TRUE || nas[3] != REXPLogical.FALSE)
|
||||
throw new TestException("assigning null Strings as NAs and '' has failed");
|
||||
System.out.println(" - OK");
|
||||
System.out.print(" pull String[] with NAs: ");
|
||||
String s[] = eng.parseAndEval("c('foo', '', NA, 'NA')").asStrings();
|
||||
for (int i = 0; i < s.length; i++) System.out.print("'" + s[i] + "' ");
|
||||
if (s.length != 4 || s[0] == null || s[1] == null || !s[1].equals("") || s[2] != null || s[3] == null)
|
||||
throw new TestException("pulling Strings containin NAs and '' has failed");
|
||||
System.out.println(" - OK");
|
||||
System.out.print(" compare pushed and constructed strings: ");
|
||||
if (eng.parseAndEval("identical(s, c('foo', '', NA, 'NA'))").asInteger() != REXPLogical.TRUE)
|
||||
throw new TestException("comparing Strings with NAs and '' has failed");
|
||||
System.out.println(" - OK");
|
||||
System.out.print(" check isNA() for REXPString:");
|
||||
boolean na[] = eng.parseAndEval("s").isNA();
|
||||
for (int i = 0; i < na.length; i++) System.out.print(" " + na[i]);
|
||||
if (na.length != 4 || na[0] || na[1] || !na[2] || na[3])
|
||||
throw new TestException("isNA() test failed");
|
||||
System.out.println(" - OK");
|
||||
System.out.println("PASSED");
|
||||
|
||||
c.close();
|
||||
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (RserveException rse) {
|
||||
System.out.println(rse);
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (REXPMismatchException mme) {
|
||||
System.out.println(mme);
|
||||
mme.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch(TestException te) {
|
||||
System.err.println("** Test failed: "+te.getMessage());
|
||||
te.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test string encoding support
|
||||
*/
|
||||
public void testStringEncoding() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
|
||||
// string encoding test (will work with Rserve 0.5-3 and higher only)
|
||||
System.out.println("* Test string encoding support ...");
|
||||
String t = "ひらがな"; // hiragana (literally, in hiragana ;))
|
||||
c.setStringEncoding("utf8");
|
||||
// -- Just in case the console is not UTF-8 don't display it
|
||||
//System.out.println(" unicode text: "+t);
|
||||
c.assign("s", t);
|
||||
REXP x = c.parseAndEval("nchar(s)");
|
||||
System.out.println(" nchar = " + x);
|
||||
if (x == null || !x.isInteger() || x.asInteger() != 4)
|
||||
throw new TestException("UTF-8 encoding string length test failed");
|
||||
// we cannot really test any other encoding ..
|
||||
System.out.println("PASSED");
|
||||
|
||||
c.close();
|
||||
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (RserveException rse) {
|
||||
System.out.println(rse);
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (REXPMismatchException mme) {
|
||||
System.out.println(mme);
|
||||
mme.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch(TestException te) {
|
||||
System.err.println("** Test failed: "+te.getMessage());
|
||||
te.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test eval without parse (direct calls)
|
||||
*/
|
||||
public void testEvalWithoutParse() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
|
||||
// test QAP evals
|
||||
System.out.println("* Test eval without parse (direct calls) ...");
|
||||
System.out.println(" call 1L + 2 as a language construct");
|
||||
REXP x = c.eval(REXP.asCall("+",
|
||||
new REXPInteger(1),
|
||||
new REXPDouble(2)), null, true);
|
||||
if (x == null || !x.isNumeric() || x.asDouble() != 3)
|
||||
throw new TestException("evaluating 1L + 2 as a call failed");
|
||||
System.out.println(" call a compound statement and exported symbol");
|
||||
x = c.eval(REXP.asCall("{", new REXP[] {
|
||||
// X <- local vector 1,2,3,4
|
||||
REXP.asCall("<-", new REXPSymbol("X"), new REXPInteger(new int[] { 1, 2, 3, 4 })),
|
||||
// base::length(X) # convoluted for the sake of testing exported symbols
|
||||
REXP.asCall(REXP.asCall("::", new REXPSymbol("base"), new REXPSymbol("length")),
|
||||
new REXPSymbol("X"))
|
||||
}), null, true);
|
||||
if (x == null || !x.isInteger() || x.asInteger() != 4)
|
||||
throw new TestException("calling a compound statement failed");
|
||||
// since we did an assignment we mauy as well test the get() method
|
||||
System.out.println(" get from global env");
|
||||
x = c.get("X", null, true);
|
||||
if (x == null || !x.isInteger() || x.length() != 4)
|
||||
throw new TestException("retrieving value created by assignment expression call failed");
|
||||
System.out.println("PASSED");
|
||||
|
||||
c.close();
|
||||
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (RserveException rse) {
|
||||
System.out.println(rse);
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (REXPMismatchException mme) {
|
||||
System.out.println(mme);
|
||||
mme.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch(TestException te) {
|
||||
System.err.println("** Test failed: "+te.getMessage());
|
||||
te.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test control commands (this will fail if control commands are disabled)
|
||||
*/
|
||||
public void testControlCommands() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) :
|
||||
/*
|
||||
try {
|
||||
RConnection c = new RConnection();
|
||||
|
||||
// test control commands (works only when enabled and in Rserve 0.6-0 and higher only) - must be the last test since it closes the connection and shuts down the server
|
||||
System.out.println("* Test control commands (this will fail if control commands are disabled) ...");
|
||||
System.out.println(" server eval");
|
||||
String key = "rn" + Math.random(); // generate a random number to prevent contamination from previous tests
|
||||
c.serverEval("xXx<-'" + key + "'");
|
||||
c.close();
|
||||
c = new RConnection();
|
||||
REXP x = c.eval("xXx");
|
||||
if (x == null || !x.isString() || x.length() != 1 || !x.asString().equals(key))
|
||||
throw new TestException("control eval test failed - assignment was not persistent");
|
||||
c.serverEval("rm(xXx)"); // remove the test variable to not pollute the global workspace
|
||||
System.out.println(" server shutdown");
|
||||
System.out.println("PASSED");
|
||||
|
||||
c.close();
|
||||
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (RserveException rse) {
|
||||
System.out.println(rse);
|
||||
// set assert to true. err : 'serverEval failed, request status: control pipe to master process is closed/broken'
|
||||
assertTrue(true);
|
||||
}
|
||||
catch (REXPMismatchException mme) {
|
||||
System.out.println(mme);
|
||||
mme.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch(TestException te) {
|
||||
System.err.println("** Test failed: "+te.getMessage());
|
||||
te.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
*/
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
|
||||
* All rights reserved.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
package com.fourelementscapital.r;
|
||||
|
||||
import java.util.Enumeration;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.rosuda.JRI.REXP;
|
||||
import org.rosuda.JRI.RList;
|
||||
import org.rosuda.JRI.RVector;
|
||||
import org.rosuda.JRI.Rengine;
|
||||
|
||||
/**
|
||||
* Rengine unit test
|
||||
*/
|
||||
public class RengineTest extends TestCase {
|
||||
|
||||
/**
|
||||
* Test Rengine
|
||||
*/
|
||||
public void testRengine() {
|
||||
// Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up JRI in java.library.path first) :
|
||||
/*
|
||||
String[] args = {"--no-save"};
|
||||
|
||||
// just making sure we have the right version of everything
|
||||
if (!Rengine.versionCheck()) {
|
||||
System.err.println("** Version mismatch - Java files don't match library version.");
|
||||
System.exit(1);
|
||||
assertTrue(false);
|
||||
}
|
||||
System.out.println("Creating Rengine (with arguments)");
|
||||
// 1) we pass the arguments from the command line
|
||||
// 2) we won't use the main loop at first, we'll start it later
|
||||
// (that's the "false" as second argument)
|
||||
// 3) the callbacks are implemented by the TextConsole class above
|
||||
Rengine re=new Rengine(args, false, new TextConsole());
|
||||
System.out.println("Rengine created, waiting for R");
|
||||
// the engine creates R is a new thread, so we should wait until it's ready
|
||||
if (!re.waitForR()) {
|
||||
System.out.println("Cannot load R");
|
||||
assertTrue(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// High-level API - do not use RNI methods unless there is no other way to accomplish what you want
|
||||
try {
|
||||
REXP x;
|
||||
re.eval("data(iris)",false);
|
||||
System.out.println(x=re.eval("iris"));
|
||||
// generic vectors are RVector to accomodate names
|
||||
RVector v = x.asVector();
|
||||
if (v.getNames()!=null) {
|
||||
System.out.println("has names:");
|
||||
for (Enumeration e = v.getNames().elements() ; e.hasMoreElements() ;) {
|
||||
System.out.println(e.nextElement());
|
||||
}
|
||||
}
|
||||
// for compatibility with Rserve we allow casting of vectors to lists
|
||||
RList vl = x.asList();
|
||||
String[] k = vl.keys();
|
||||
if (k!=null) {
|
||||
System.out.println("and once again from the list:");
|
||||
int i=0; while (i<k.length) System.out.println(k[i++]);
|
||||
}
|
||||
|
||||
// get boolean array
|
||||
System.out.println(x=re.eval("iris[[1]]>mean(iris[[1]])"));
|
||||
// R knows about TRUE/FALSE/NA, so we cannot use boolean[] this way
|
||||
// instead, we use int[] which is more convenient (and what R uses internally anyway)
|
||||
int[] bi = x.asIntArray();
|
||||
{
|
||||
int i = 0; while (i<bi.length) { System.out.print(bi[i]==0?"F ":(bi[i]==1?"T ":"NA ")); i++; }
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
// push a boolean array
|
||||
boolean by[] = { true, false, false };
|
||||
re.assign("bool", by);
|
||||
System.out.println(x=re.eval("bool"));
|
||||
// asBool returns the first element of the array as RBool
|
||||
// (mostly useful for boolean arrays of the length 1). is should return true
|
||||
System.out.println("isTRUE? "+x.asBool().isTRUE());
|
||||
|
||||
// now for a real dotted-pair list:
|
||||
System.out.println(x=re.eval("pairlist(a=1,b='foo',c=1:5)"));
|
||||
RList l = x.asList();
|
||||
if (l!=null) {
|
||||
int i=0;
|
||||
String [] a = l.keys();
|
||||
System.out.println("Keys:");
|
||||
while (i<a.length) System.out.println(a[i++]);
|
||||
System.out.println("Contents:");
|
||||
i=0;
|
||||
while (i<a.length) System.out.println(l.at(i++));
|
||||
}
|
||||
System.out.println(re.eval("sqrt(36)"));
|
||||
} catch (Exception e) {
|
||||
System.out.println("EX:"+e);
|
||||
e.printStackTrace();
|
||||
assertTrue(false);
|
||||
}
|
||||
|
||||
// Part 2 - low-level API - for illustration purposes only!
|
||||
//System.exit(0);
|
||||
|
||||
// simple assignment like a<-"hello" (env=0 means use R_GlobalEnv)
|
||||
long xp1 = re.rniPutString("hello");
|
||||
re.rniAssign("a", xp1, 0);
|
||||
|
||||
// Example: how to create a named list or data.frame
|
||||
double da[] = {1.2, 2.3, 4.5};
|
||||
double db[] = {1.4, 2.6, 4.2};
|
||||
long xp3 = re.rniPutDoubleArray(da);
|
||||
long xp4 = re.rniPutDoubleArray(db);
|
||||
|
||||
// now build a list (generic vector is how that's called in R)
|
||||
long la[] = {xp3, xp4};
|
||||
long xp5 = re.rniPutVector(la);
|
||||
|
||||
// now let's add names
|
||||
String sa[] = {"a","b"};
|
||||
long xp2 = re.rniPutStringArray(sa);
|
||||
re.rniSetAttr(xp5, "names", xp2);
|
||||
|
||||
// ok, we have a proper list now
|
||||
// we could use assign and then eval "b<-data.frame(b)", but for now let's build it by hand:
|
||||
String rn[] = {"1", "2", "3"};
|
||||
long xp7 = re.rniPutStringArray(rn);
|
||||
re.rniSetAttr(xp5, "row.names", xp7);
|
||||
|
||||
long xp6 = re.rniPutString("data.frame");
|
||||
re.rniSetAttr(xp5, "class", xp6);
|
||||
|
||||
// assign the whole thing to the "b" variable
|
||||
re.rniAssign("b", xp5, 0);
|
||||
|
||||
{
|
||||
System.out.println("Parsing");
|
||||
long e=re.rniParse("data(iris)", 1);
|
||||
System.out.println("Result = "+e+", running eval");
|
||||
long r=re.rniEval(e, 0);
|
||||
System.out.println("Result = "+r+", building REXP");
|
||||
REXP x=new REXP(re, r);
|
||||
System.out.println("REXP result = "+x);
|
||||
}
|
||||
{
|
||||
System.out.println("Parsing");
|
||||
long e=re.rniParse("iris", 1);
|
||||
System.out.println("Result = "+e+", running eval");
|
||||
long r=re.rniEval(e, 0);
|
||||
System.out.println("Result = "+r+", building REXP");
|
||||
REXP x=new REXP(re, r);
|
||||
System.out.println("REXP result = "+x);
|
||||
}
|
||||
{
|
||||
System.out.println("Parsing");
|
||||
long e=re.rniParse("names(iris)", 1);
|
||||
System.out.println("Result = "+e+", running eval");
|
||||
long r=re.rniEval(e, 0);
|
||||
System.out.println("Result = "+r+", building REXP");
|
||||
REXP x=new REXP(re, r);
|
||||
System.out.println("REXP result = "+x);
|
||||
String s[]=x.asStringArray();
|
||||
if (s!=null) {
|
||||
int i=0; while (i<s.length) { System.out.println("["+i+"] \""+s[i]+"\""); i++; }
|
||||
}
|
||||
}
|
||||
{
|
||||
System.out.println("Parsing");
|
||||
long e=re.rniParse("rnorm(10)", 1);
|
||||
System.out.println("Result = "+e+", running eval");
|
||||
long r=re.rniEval(e, 0);
|
||||
System.out.println("Result = "+r+", building REXP");
|
||||
REXP x=new REXP(re, r);
|
||||
System.out.println("REXP result = "+x);
|
||||
double d[]=x.asDoubleArray();
|
||||
if (d!=null) {
|
||||
int i=0; while (i<d.length) { System.out.print(((i==0)?"":", ")+d[i]); i++; }
|
||||
System.out.println("");
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
{
|
||||
REXP x=re.eval("1:10");
|
||||
System.out.println("REXP result = "+x);
|
||||
int d[]=x.asIntArray();
|
||||
if (d!=null) {
|
||||
int i=0; while (i<d.length) { System.out.print(((i==0)?"":", ")+d[i]); i++; }
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
re.eval("print(1:10/3)");
|
||||
*/
|
||||
assertTrue(true);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user