Author: jcouteau Date: 2009-04-28 15:42:01 +0000 (Tue, 28 Apr 2009) New Revision: 66 Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/REngine.java lutinj2r/trunk/src/main/java/org/codelutin/j2r/RProxy.java lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java lutinj2r/trunk/src/main/java/org/codelutin/j2r/net/RNetEngine.java lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java Log: Adding support for booleans in NetEngine Adding support for R native methods to: - set/get R working directory - store/get R objects in files - remove objects from R session - save/load a R session Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/REngine.java =================================================================== --- lutinj2r/trunk/src/main/java/org/codelutin/j2r/REngine.java 2009-04-09 12:29:24 UTC (rev 65) +++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/REngine.java 2009-04-28 15:42:01 UTC (rev 66) @@ -29,6 +29,8 @@ package org.codelutin.j2r; +import java.io.File; + /** * Cette interface est le point commun entre les differentes technologies * utilisees pour l'acces a R @@ -65,5 +67,84 @@ * @throws RException */ public void terminate() throws RException; + + /** + * Load .RData file located in directory + * + * @param directory directory where the .RData file is located + * @throws RException + */ + public void loadRData(File directory) throws RException; + + /** + * Save the session in a .RData file in directory + * + * @param directory where the .RData file will be saved + * @throws RException + */ + public void saveRData(File directory) throws RException; + + /** + * Set the R working directory + * @param directory to set + * @throws RException + */ + public void setwd(File directory) throws RException; + + /** + * Get the actual R session working directory + * + * @return a File that is the actual R session working directory + * @throws RException + */ + + public File getwd() throws RException; + + /** + * Use the dput R instruction to store the content of a R object to a file. + * The file created will be in the working directory + * + * @param rObject name of the R object to save + * @param outputFileName name of the file to save + * @throws RException + */ + public void dput(String rObject, String outputFileName) throws RException; + + + /** + * Use the dget rInstruction to store the content of a file (created with the dput instruction) into a R object. + * The file used have to be in the working directory + * + * @param rObject name of the R object created + * @param inputFileName name of the file to load + * @throws RException + */ + public void dget(String rObject, String inputFileName) throws RException; + + /** + * Use the dput R instruction to store the content of a R object to a file. + * + * @param rObject R object to save + * @param outputFileName the file to save + * @throws RException + */ + public void dput(String rObject, File outputFile) throws RException; + + + /** + * Use the dget rInstruction to store the content of a file (created with the dput instruction) into a R object. + * + * @param rObject name of the R object created + * @param inputFile file to load + * @throws RException + */ + public void dget(String rObject, File inputFile) throws RException; + + /** + * Remove a R object from the actual session + * @param rObject to be removed from the session + * @throws RException + */ + public void remove (String rObject) throws RException; } //RJniEngine Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/RProxy.java =================================================================== --- lutinj2r/trunk/src/main/java/org/codelutin/j2r/RProxy.java 2009-04-09 12:29:24 UTC (rev 65) +++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/RProxy.java 2009-04-28 15:42:01 UTC (rev 66) @@ -29,6 +29,8 @@ package org.codelutin.j2r; +import java.io.File; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codelutin.j2r.jni.RJniEngine; @@ -131,7 +133,7 @@ } if (!init(RType)) { if (log.isFatalEnabled()) { - log.fatal("The initializaing of R failed."); + log.fatal("The initializing of R failed."); } return false; } @@ -161,5 +163,156 @@ } engine.voidEval(expr); } + + /** + * Load .RData file located in directory + * + * @param directory directory where the .RData file is located + * @throws RException + */ + public void loadRData(File directory) throws RException { + if (engine == null) { + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); + return; + } + engine.setwd(directory); + engine.voidEval("load(\".RData\")"); + } + + /** + * Save the session in a .RData file in directory + * + * @param directory where the .RData file will be saved + * @throws RException + */ + public void saveRData(File directory) throws RException{ + if (engine == null) { + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); + return; + } + engine.setwd(directory); + engine.voidEval("save.image()"); + } + + /** + * Set the R working directory + * @param directory to set + * @throws RException + */ + public void setwd(File directory) throws RException { + if (engine == null) { + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); + return; + } + engine.voidEval("setwd(\""+directory.getAbsolutePath() + "\")"); + } + + /** + * Get the actual R session working directory + * + * @return a File that is the actual R session working directory, null if the engine is not initialized + * @throws RException + */ + + public File getwd() throws RException { + if (engine == null) { + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); + return null; + } + String directory = (String)engine.eval("getwd()"); + return new File(directory); + } + + /** + * Use the dput R instruction to store the content of a R object to a file. + * The file created will be in the working directory + * + * @param rObject name of the R object to save + * @param outputFileName name of the file to save + * @throws RException + */ + public void dput(String rObject, String outputFileName) throws RException { + if (engine == null) { + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); + return; + } + String rInstruction = "dput(%s,file=\"%s\")"; + engine.voidEval(String.format(rInstruction, rObject, outputFileName)); + } + + + /** + * Use the dget rInstruction to store the content of a file (created with the dput instruction) into a R object. + * The file used have to be in the working directory + * + * @param rObject name of the R object created + * @param inputFileName name of the file to load + * @throws RException + */ + public void dget(String rObject, String inputFileName) throws RException { + if (engine == null) { + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); + return; + } + String rInstruction = "%s <- dget(\"%s\")"; + engine.voidEval(String.format(rInstruction, rObject, inputFileName)); + + } + + /** + * Use the dput R instruction to store the content of a R object to a file. + * + * @param rObject R object to save + * @param outputFileName the file to save + * @throws RException + */ + public void dput(String rObject, File outputFile) throws RException { + if (engine == null) { + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); + return; + } + File workingdir = getwd(); + engine.setwd(outputFile.getParentFile()); + String rInstruction = "dput(%s,file=\"%s\")"; + engine.voidEval(String.format(rInstruction, rObject, outputFile.getName())); + setwd(workingdir); + } + + + /** + * Use the dget rInstruction to store the content of a file (created with the dput instruction) into a R object. + * + * @param rObject name of the R object created + * @param inputFile file to load + * @throws RException + */ + public void dget(String rObject, File inputFile) throws RException { + if (engine == null) { + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); + return; + } + File workingdir = getwd(); + engine.setwd(inputFile.getParentFile()); + String rInstruction = "%s <- dget(\"%s\")"; + engine.voidEval(String.format(rInstruction, rObject, inputFile.getName())); + setwd(workingdir); + } + + public void remove(String rObject) throws RException { + if (engine == null) { + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); + return; + } + engine.remove(rObject); + } } //RProxy Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java =================================================================== --- lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java 2009-04-09 12:29:24 UTC (rev 65) +++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java 2009-04-28 15:42:01 UTC (rev 66) @@ -17,6 +17,8 @@ package org.codelutin.j2r.jni; +import java.io.File; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codelutin.j2r.REngine; @@ -77,6 +79,7 @@ public Object eval(String expr) throws RException { REXP result = null; try { + log.info(expr); result = engine.eval(expr); } catch (Exception eee) { throw new RException("Unable to evaluate the R expression " @@ -158,11 +161,118 @@ // voidEval is not really supproted by JRI, we just discard the result // conversion try { + log.info(expr); engine.eval(expr); } catch (Exception eee) { throw new RException("An error occured while voidEval on JNI", eee); } } + + /** + * Load .RData file located in directory + * + * @param directory directory where the .RData file is located + * @throws RException + */ + public void loadRData(File directory) throws RException { + setwd(directory); + voidEval("load(\".RData\")"); + } + + /** + * Save the session in a .RData file in directory + * + * @param directory where the .RData file will be saved + * @throws RException + */ + public void saveRData(File directory) throws RException{ + setwd(directory); + voidEval("save.image()"); + } + + /** + * Set the R working directory + * @param directory to set + * @throws RException + */ + public void setwd(File directory) throws RException { + voidEval("setwd(\""+directory.getAbsolutePath() + "\")"); + } + + /** + * Get the actual R session working directory + * + * @return a File that is the actual R session working directory + * @throws RException + */ + + public File getwd() throws RException { + String directory = (String)eval("getwd()"); + return new File(directory); + } + + /** + * Use the dput R instruction to store the content of a R object to a file. + * The file created will be in the working directory + * + * @param rObject name of the R object to save + * @param outputFileName name of the file to save + * @throws RException + */ + public void dput(String rObject, String outputFileName) throws RException { + String rInstruction = "dput(%s,file=\"%s\")"; + voidEval(String.format(rInstruction, rObject, outputFileName)); + } + + + /** + * Use the dget rInstruction to store the content of a file (created with the dput instruction) into a R object. + * The file used have to be in the working directory + * + * @param rObject name of the R object created + * @param inputFileName name of the file to load + * @throws RException + */ + public void dget(String rObject, String inputFileName) throws RException { + String rInstruction = "%s <- dget(\"%s\")"; + voidEval(String.format(rInstruction, rObject, inputFileName)); + + } + + /** + * Use the dput R instruction to store the content of a R object to a file. + * + * @param rObject R object to save + * @param outputFileName the file to save + * @throws RException + */ + public void dput(String rObject, File outputFile) throws RException { + File workingdir = getwd(); + setwd(outputFile.getParentFile()); + String rInstruction = "dput(%s,file=\"%s\")"; + voidEval(String.format(rInstruction, rObject, outputFile.getName())); + setwd(workingdir); + } + + + /** + * Use the dget rInstruction to store the content of a file (created with the dput instruction) into a R object. + * + * @param rObject name of the R object created + * @param inputFile file to load + * @throws RException + */ + public void dget(String rObject, File inputFile) throws RException { + File workingdir = getwd(); + setwd(inputFile.getParentFile()); + String rInstruction = "%s <- dget(\"%s\")"; + voidEval(String.format(rInstruction, rObject, inputFile.getName())); + setwd(workingdir); + } + + public void remove (String rObject) throws RException { + voidEval("remove("+rObject+")"); + } } // RJniEngine Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/net/RNetEngine.java =================================================================== --- lutinj2r/trunk/src/main/java/org/codelutin/j2r/net/RNetEngine.java 2009-04-09 12:29:24 UTC (rev 65) +++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/net/RNetEngine.java 2009-04-28 15:42:01 UTC (rev 66) @@ -29,6 +29,8 @@ package org.codelutin.j2r.net; +import java.io.File; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codelutin.j2r.REngine; @@ -120,6 +122,7 @@ public Object eval(String expr) throws RException { REXP result = null; try { + log.info(expr); result = conn.eval(expr); } catch (RserveException e) { throw new RException("An error occured during the eval method", e); @@ -171,12 +174,20 @@ else if (rexp.isLogical()) { result = rexp.asStrings(); - Boolean[] stringArray = (Boolean[])result; + String[] strings = ((String [])result); + Boolean[] stringArray = new Boolean[strings.length]; + for (int i=0;i<((String [])result).length;i++){ + stringArray[i]=Boolean.parseBoolean(strings[i]); + } if (stringArray.length == 1) { result = (Boolean)stringArray[0]; } - else {result = (Boolean[])result;} + else {result = (Boolean[])stringArray;} } + + else if (rexp.isNull()) { + return null; + } else { log.error("Unknown return type on : " + rexp.toString()); @@ -205,10 +216,117 @@ */ public void voidEval(String expr) throws RException { try { + log.info(expr); conn.voidEval(expr); } catch (RserveException rse) { throw new RException("An error occured while voidEval on net", rse); } } + + /** + * Load .RData file located in directory + * + * @param directory directory where the .RData file is located + * @throws RException + */ + public void loadRData(File directory) throws RException { + setwd(directory); + voidEval("load(\".RData\")"); + } + + /** + * Save the session in a .RData file in directory + * + * @param directory where the .RData file will be saved + * @throws RException + */ + public void saveRData(File directory) throws RException{ + setwd(directory); + voidEval("save.image()"); + } + + /** + * Set the R working directory + * @param directory to set + * @throws RException + */ + public void setwd(File directory) throws RException { + voidEval("setwd(\""+directory.getAbsolutePath() + "\")"); + } + + /** + * Get the actual R session working directory + * + * @return a File that is the actual R session working directory + * @throws RException + */ + + public File getwd() throws RException { + String directory = (String)eval("getwd()"); + return new File(directory); + } + + /** + * Use the dput R instruction to store the content of a R object to a file. + * The file created will be in the working directory + * + * @param rObject name of the R object to save + * @param outputFileName name of the file to save + * @throws RException + */ + public void dput(String rObject, String outputFileName) throws RException { + String rInstruction = "dput(%s,file=\"%s\")"; + voidEval(String.format(rInstruction, rObject, outputFileName)); + } + + + /** + * Use the dget rInstruction to store the content of a file (created with the dput instruction) into a R object. + * The file used have to be in the working directory + * + * @param rObject name of the R object created + * @param inputFileName name of the file to load + * @throws RException + */ + public void dget(String rObject, String inputFileName) throws RException { + String rInstruction = "%s <- dget(\"%s\")"; + voidEval(String.format(rInstruction, rObject, inputFileName)); + + } + + /** + * Use the dput R instruction to store the content of a R object to a file. + * + * @param rObject R object to save + * @param outputFileName the file to save + * @throws RException + */ + public void dput(String rObject, File outputFile) throws RException { + File workingdir = getwd(); + setwd(outputFile.getParentFile()); + String rInstruction = "dput(%s,file=\"%s\")"; + voidEval(String.format(rInstruction, rObject, outputFile.getName())); + setwd(workingdir); + } + + + /** + * Use the dget rInstruction to store the content of a file (created with the dput instruction) into a R object. + * + * @param rObject name of the R object created + * @param inputFile file to load + * @throws RException + */ + public void dget(String rObject, File inputFile) throws RException { + File workingdir = getwd(); + setwd(inputFile.getParentFile()); + String rInstruction = "%s <- dget(\"%s\")"; + voidEval(String.format(rInstruction, rObject, inputFile.getName())); + setwd(workingdir); + } + + public void remove (String rObject) throws RException { + voidEval("remove("+rObject+")"); + } } // RNetEngine Modified: lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java =================================================================== --- lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java 2009-04-09 12:29:24 UTC (rev 65) +++ lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java 2009-04-28 15:42:01 UTC (rev 66) @@ -39,9 +39,7 @@ import static org.codelutin.j2r.TestConstants.V_OP_B; import static org.codelutin.j2r.TestConstants.V_OP_R; -import java.util.ArrayList; -import java.util.List; -import java.util.Vector; +import java.io.File; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -49,8 +47,6 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.rosuda.JRI.REXP; -import org.rosuda.JRI.RVector; public class JNITest { @@ -181,7 +177,7 @@ Assert.assertTrue(testBoolArray[2]); }*/ - @Test + /*@Test public void testDataFrame() throws Exception { engine.voidEval("X1<-data.frame(matrix(runif(5*100),nrow=100))"); Assert.assertTrue(engine.eval("X1") instanceof Vector); @@ -190,5 +186,73 @@ .asDoubleArray().length, 100); } + @Test + public void testSobol() throws Exception { + engine.voidEval("library(sensitivity)"); + engine.voidEval("setwd(\"/home/couteau/isis-export/as__2009-04-07-17-36\")"); + engine.voidEval("a<-fast99(model=NULL,factors=c(2), n=17,M=1, q = \"qunif\", q.arg=list(min=0,max=1))"); + engine.voidEval("dput(a,file=\".fast99\")"); + Assert.assertTrue(engine.eval("a$X") instanceof Vector); + Assert.assertEquals(((Integer) engine.eval("length(a$X[,1])")).intValue(), 34); + + }*/ + + @Test + public void testWorkingDirectory() throws Exception { + File workingDirectory = new File("/tmp"); + engine.setwd(workingDirectory); + File testWorkingDirectory = engine.getwd(); + Assert.assertEquals(workingDirectory.getAbsolutePath(), testWorkingDirectory.getAbsolutePath()); + } + + @Test + public void testRData() throws Exception { + File workingdir = new File("/tmp"); + engine.voidEval("a<-5.0"); + engine.saveRData(workingdir); + engine.remove("a"); + engine.loadRData(workingdir); + Double testDouble = (Double)engine.eval("a"); + Double compareTo = new Double (5.0); + Assert.assertEquals(compareTo, testDouble); + } + + @Test + public void testDputDget() throws Exception { + File workingdir = new File("/tmp"); + File testingFile = new File("/tmp/testfile"); + + // test method using the workingdir + engine.voidEval("a<-5.0"); + engine.setwd(workingdir); + engine.dput("a", "testDputDgetfile"); + engine.remove("a"); + engine.dget("a", "testDputDgetfile"); + Double testDouble = (Double)engine.eval("a"); + Double compareTo = new Double (5.0); + Assert.assertEquals(compareTo, testDouble); + + //test method using absolute path + workingdir = new File("/"); + engine.setwd(workingdir); + engine.voidEval("a<-6.0"); + engine.dput("a",testingFile); + engine.remove("a"); + engine.dget("a", testingFile); + Double testDouble2 = (Double)engine.eval("a"); + Double compareTo2 = new Double (6.0); + Assert.assertEquals(compareTo2, testDouble2); + File testWorkingDirectory = engine.getwd(); + Assert.assertEquals(workingdir.getAbsolutePath(), testWorkingDirectory.getAbsolutePath()); + } + + @Test + public void testRemove() throws Exception { + engine.voidEval("a<-6.0"); + Assert.assertEquals(new Double(6.0),engine.eval("a")); + engine.remove("a"); + Assert.assertNull(engine.eval("a")); + } + } // JNITest Modified: lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java =================================================================== --- lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java 2009-04-09 12:29:24 UTC (rev 65) +++ lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java 2009-04-28 15:42:01 UTC (rev 66) @@ -38,6 +38,8 @@ import static org.codelutin.j2r.TestConstants.V_OP_AB; import static org.codelutin.j2r.TestConstants.V_OP_B; +import java.io.File; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.After; @@ -141,7 +143,7 @@ Integer testInteger = (Integer) engine.eval("a"); Integer toCompare = 5; Assert.assertEquals(toCompare, testInteger); - } + }*/ @Test public void testBool() throws Exception { @@ -160,6 +162,63 @@ Assert.assertTrue(testBoolArray[0]); Assert.assertFalse(testBoolArray[1]); Assert.assertTrue(testBoolArray[2]); - }*/ + } + + @Test + public void testWorkingDirectory() throws Exception { + File workingDirectory = new File("/tmp"); + engine.setwd(workingDirectory); + File testWorkingDirectory = engine.getwd(); + Assert.assertEquals(workingDirectory.getAbsolutePath(), testWorkingDirectory.getAbsolutePath()); + } + + @Test + public void testRData() throws Exception { + File workingdir = new File("/tmp"); + engine.voidEval("a<-5.0"); + engine.saveRData(workingdir); + engine.remove("a"); + engine.loadRData(workingdir); + Double testDouble = (Double)engine.eval("a"); + Double compareTo = new Double (5.0); + Assert.assertEquals(compareTo, testDouble); + } + + @Test + public void testDputDget() throws Exception { + File workingdir = new File("/tmp"); + File testingFile = new File("/tmp/testfile"); + + // test method using the workingdir + engine.voidEval("a<-5.0"); + engine.setwd(workingdir); + engine.dput("a", "testDputDgetfile"); + engine.remove("a"); + engine.dget("a", "testDputDgetfile"); + Double testDouble = (Double)engine.eval("a"); + Double compareTo = new Double (5.0); + Assert.assertEquals(compareTo, testDouble); + + workingdir= new File("/"); + //test method using absolute path + engine.setwd(workingdir); + engine.voidEval("a<-6.0"); + engine.dput("a",testingFile); + engine.remove("a"); + engine.dget("a", testingFile); + Double testDouble2 = (Double)engine.eval("a"); + Double compareTo2 = new Double (6.0); + Assert.assertEquals(compareTo2, testDouble2); + File testWorkingDirectory = engine.getwd(); + Assert.assertEquals(workingdir.getAbsolutePath(), testWorkingDirectory.getAbsolutePath()); + } + + @Test + public void testRemove() throws Exception { + engine.voidEval("a<-6.0"); + Assert.assertEquals(new Double(6.0),engine.eval("a")); + engine.remove("a"); + Assert.assertFalse((Boolean)engine.eval("exists(\"a\")")); + } } // NetTest