Nuiton-j2r-commits
Threads by month
- ----- 2026 -----
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- 316 discussions
[LutinJ2R-commits] r76 - in lutinj2r/tags: . lutinj2r-0.4
by chatellier@users.labs.libre-entreprise.org 04 May '09
by chatellier@users.labs.libre-entreprise.org 04 May '09
04 May '09
Author: chatellier
Date: 2009-05-04 15:58:09 +0000 (Mon, 04 May 2009)
New Revision: 76
Added:
lutinj2r/tags/lutinj2r-0.4/
lutinj2r/tags/lutinj2r-0.4/pom.xml
Removed:
lutinj2r/tags/lutinj2r-0.4/pom.xml
Log:
[maven-release-plugin] copy for tag lutinj2r-0.4
Copied: lutinj2r/tags/lutinj2r-0.4 (from rev 74, lutinj2r/trunk)
Deleted: lutinj2r/tags/lutinj2r-0.4/pom.xml
===================================================================
--- lutinj2r/trunk/pom.xml 2009-05-03 09:18:40 UTC (rev 74)
+++ lutinj2r/tags/lutinj2r-0.4/pom.xml 2009-05-04 15:58:09 UTC (rev 76)
@@ -1,190 +0,0 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-
- <modelVersion>4.0.0</modelVersion>
-
- <!-- ************************************************************* -->
- <!-- *** POM Relationships *************************************** -->
- <!-- ************************************************************* -->
-
- <parent>
- <groupId>org.codelutin</groupId>
- <artifactId>lutinproject</artifactId>
- <version>3.4</version>
- </parent>
-
- <artifactId>lutinj2r</artifactId>
- <version>0.4-SNAPSHOT</version>
-
- <dependencies>
- <!-- dependency>
- <groupId>externallib</groupId>
- <artifactId>JRclient</artifactId>
- <version>RF503</version>
- <scope>compile</scope>
- </dependency-->
- <dependency>
- <groupId>externallib</groupId>
- <artifactId>JRI</artifactId>
- <version>0.7-0</version>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>externallib</groupId>
- <artifactId>REngine</artifactId>
- <version>0.6-0</version>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>externallib</groupId>
- <artifactId>Rserve</artifactId>
- <version>0.6-0</version>
- <scope>compile</scope>
- </dependency>
- </dependencies>
-
- <!-- ************************************************************* -->
- <!-- *** Project Information ************************************* -->
- <!-- ************************************************************* -->
-
- <name>Lutin Java-2-R library</name>
- <description>Librairie permettant d'utiliser R en Java, que ce soit par le
- Network ou en JNI.
- </description>
- <inceptionYear>2006</inceptionYear>
-
- <!-- ************************************************************* -->
- <!-- *** Build Settings ****************************************** -->
- <!-- ************************************************************* -->
-
- <packaging>jar</packaging>
-
- <properties>
- <!-- id du projet du labs -->
- <labs.id>109</labs.id>
-
- <labs.project>lutinj2r</labs.project>
-
- <!-- FIXME -->
- <maven.test.skip>true</maven.test.skip>
-
- <!-- Jar main class -->
- <!--maven.jar.main.class>org.codelutin.j2r.RProxy</maven.jar.main.class-->
- </properties>
-
- <build>
- <defaultGoal>install</defaultGoal>
-
- <plugins>
-
- <!-- Always process jrst files, but only called on pre-site phase -->
- <plugin>
- <groupId>org.codelutin</groupId>
- <artifactId>maven-jrst-plugin</artifactId>
- <version>0.8.4</version>
- <configuration>
- <defaultLocale>fr</defaultLocale>
- </configuration>
- <executions>
- <execution>
- <goals>
- <goal>jrst</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
-
- <pluginManagement>
- <plugins>
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <configuration>
- <descriptors>
- <descriptor>src/main/assembly/deps.xml</descriptor>
- <descriptor>src/main/assembly/full.xml</descriptor>
- </descriptors>
- <attach>false</attach>
- </configuration>
- <executions>
- <execution>
- <id>create-assembly</id>
- <goals>
- <goal>single</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </pluginManagement>
-
- </build>
-
- <scm>
- <connection>${maven.scm.connection}</connection>
- <developerConnection>${maven.scm.developerConnection}</developerConnection>
- <url>${maven.scm.url}</url>
- </scm>
-
- <!--Code Lutin Repository-->
- <repositories>
- <repository>
- <id>codelutin-repository</id>
- <name>CodeLutinRepository</name>
- <url>http://lutinbuilder.labs.libre-entreprise.org/maven2</url>
- <snapshots>
- <enabled>true</enabled>
- <checksumPolicy>warn</checksumPolicy>
- </snapshots>
- <releases>
- <enabled>true</enabled>
- <checksumPolicy>warn</checksumPolicy>
- </releases>
- </repository>
- </repositories>
-
- <profiles>
- <profile>
- <id>release-profile</id>
- <activation>
- <property>
- <name>performRelease</name>
- <value>true</value>
- </property>
- </activation>
- <build>
- <plugins>
-
- <!-- launch in a release the assembly automaticly -->
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <executions>
- <execution>
- <id>create-assembly</id>
- <phase>package</phase>
- </execution>
- </executions>
- </plugin>
-
- <!-- always add license and third-party files to classpath -->
- <plugin>
- <groupId>org.codelutin</groupId>
- <artifactId>maven-license-switcher-plugin</artifactId>
- <version>0.6</version>
- <executions>
- <execution>
- <id>attach-licenses</id>
- <goals>
- <goal>license</goal>
- <goal>third-party</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
-
- </plugins>
-
- </build>
- </profile>
- </profiles>
-
-</project>
\ No newline at end of file
Copied: lutinj2r/tags/lutinj2r-0.4/pom.xml (from rev 75, lutinj2r/trunk/pom.xml)
===================================================================
--- lutinj2r/tags/lutinj2r-0.4/pom.xml (rev 0)
+++ lutinj2r/tags/lutinj2r-0.4/pom.xml 2009-05-04 15:58:09 UTC (rev 76)
@@ -0,0 +1,190 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <!-- ************************************************************* -->
+ <!-- *** POM Relationships *************************************** -->
+ <!-- ************************************************************* -->
+
+ <parent>
+ <groupId>org.codelutin</groupId>
+ <artifactId>lutinproject</artifactId>
+ <version>3.4</version>
+ </parent>
+
+ <artifactId>lutinj2r</artifactId>
+ <version>0.4</version>
+
+ <dependencies>
+ <!-- dependency>
+ <groupId>externallib</groupId>
+ <artifactId>JRclient</artifactId>
+ <version>RF503</version>
+ <scope>compile</scope>
+ </dependency-->
+ <dependency>
+ <groupId>externallib</groupId>
+ <artifactId>JRI</artifactId>
+ <version>0.7-0</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>externallib</groupId>
+ <artifactId>REngine</artifactId>
+ <version>0.6-0</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>externallib</groupId>
+ <artifactId>Rserve</artifactId>
+ <version>0.6-0</version>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+
+ <!-- ************************************************************* -->
+ <!-- *** Project Information ************************************* -->
+ <!-- ************************************************************* -->
+
+ <name>Lutin Java-2-R library</name>
+ <description>Librairie permettant d'utiliser R en Java, que ce soit par le
+ Network ou en JNI.
+ </description>
+ <inceptionYear>2006</inceptionYear>
+
+ <!-- ************************************************************* -->
+ <!-- *** Build Settings ****************************************** -->
+ <!-- ************************************************************* -->
+
+ <packaging>jar</packaging>
+
+ <properties>
+ <!-- id du projet du labs -->
+ <labs.id>109</labs.id>
+
+ <labs.project>lutinj2r</labs.project>
+
+ <!-- FIXME -->
+ <maven.test.skip>true</maven.test.skip>
+
+ <!-- Jar main class -->
+ <!--maven.jar.main.class>org.codelutin.j2r.RProxy</maven.jar.main.class-->
+ </properties>
+
+ <build>
+ <defaultGoal>install</defaultGoal>
+
+ <plugins>
+
+ <!-- Always process jrst files, but only called on pre-site phase -->
+ <plugin>
+ <groupId>org.codelutin</groupId>
+ <artifactId>maven-jrst-plugin</artifactId>
+ <version>0.8.4</version>
+ <configuration>
+ <defaultLocale>fr</defaultLocale>
+ </configuration>
+ <executions>
+ <execution>
+ <goals>
+ <goal>jrst</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <artifactId>maven-assembly-plugin</artifactId>
+ <configuration>
+ <descriptors>
+ <descriptor>src/main/assembly/deps.xml</descriptor>
+ <descriptor>src/main/assembly/full.xml</descriptor>
+ </descriptors>
+ <attach>false</attach>
+ </configuration>
+ <executions>
+ <execution>
+ <id>create-assembly</id>
+ <goals>
+ <goal>single</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+
+ </build>
+
+ <scm>
+ <connection>scm:svn:svn://anonymous@labs.libre-entreprise.org/svnroot/lutinj2r/lutinj2r/tags/lutinj2r-0.4</connection>
+ <developerConnection>scm:svn:svn+ssh://chatellier@labs.libre-entreprise.org/svnroot/lutinj2r/lutinj2r/tags/lutinj2r-0.4</developerConnection>
+ <url>http://labs.libre-entreprise.org/plugins/scmsvn/viewcvs.php/lutinj2r/tags/l…</url>
+ </scm>
+
+ <!--Code Lutin Repository-->
+ <repositories>
+ <repository>
+ <id>codelutin-repository</id>
+ <name>CodeLutinRepository</name>
+ <url>http://lutinbuilder.labs.libre-entreprise.org/maven2</url>
+ <snapshots>
+ <enabled>true</enabled>
+ <checksumPolicy>warn</checksumPolicy>
+ </snapshots>
+ <releases>
+ <enabled>true</enabled>
+ <checksumPolicy>warn</checksumPolicy>
+ </releases>
+ </repository>
+ </repositories>
+
+ <profiles>
+ <profile>
+ <id>release-profile</id>
+ <activation>
+ <property>
+ <name>performRelease</name>
+ <value>true</value>
+ </property>
+ </activation>
+ <build>
+ <plugins>
+
+ <!-- launch in a release the assembly automaticly -->
+ <plugin>
+ <artifactId>maven-assembly-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>create-assembly</id>
+ <phase>package</phase>
+ </execution>
+ </executions>
+ </plugin>
+
+ <!-- always add license and third-party files to classpath -->
+ <plugin>
+ <groupId>org.codelutin</groupId>
+ <artifactId>maven-license-switcher-plugin</artifactId>
+ <version>0.6</version>
+ <executions>
+ <execution>
+ <id>attach-licenses</id>
+ <goals>
+ <goal>license</goal>
+ <goal>third-party</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ </plugins>
+
+ </build>
+ </profile>
+ </profiles>
+
+</project>
\ No newline at end of file
1
0
04 May '09
Author: chatellier
Date: 2009-05-04 15:58:05 +0000 (Mon, 04 May 2009)
New Revision: 75
Modified:
lutinj2r/trunk/pom.xml
Log:
[maven-release-plugin] prepare release lutinj2r-0.4
Modified: lutinj2r/trunk/pom.xml
===================================================================
--- lutinj2r/trunk/pom.xml 2009-05-03 09:18:40 UTC (rev 74)
+++ lutinj2r/trunk/pom.xml 2009-05-04 15:58:05 UTC (rev 75)
@@ -13,7 +13,7 @@
</parent>
<artifactId>lutinj2r</artifactId>
- <version>0.4-SNAPSHOT</version>
+ <version>0.4</version>
<dependencies>
<!-- dependency>
@@ -120,9 +120,9 @@
</build>
<scm>
- <connection>${maven.scm.connection}</connection>
- <developerConnection>${maven.scm.developerConnection}</developerConnection>
- <url>${maven.scm.url}</url>
+ <connection>scm:svn:svn://anonymous@labs.libre-entreprise.org/svnroot/lutinj2r/lutinj2r/tags/lutinj2r-0.4</connection>
+ <developerConnection>scm:svn:svn+ssh://chatellier@labs.libre-entreprise.org/svnroot/lutinj2r/lutinj2r/tags/lutinj2r-0.4</developerConnection>
+ <url>http://labs.libre-entreprise.org/plugins/scmsvn/viewcvs.php/lutinj2r/tags/l…</url>
</scm>
<!--Code Lutin Repository-->
1
0
[LutinJ2R-commits] r74 - in lutinj2r/trunk/src: main/java/org/codelutin/j2r/types test/java/org/codelutin/j2r
by jcouteau@users.labs.libre-entreprise.org 03 May '09
by jcouteau@users.labs.libre-entreprise.org 03 May '09
03 May '09
Author: jcouteau
Date: 2009-05-03 09:18:40 +0000 (Sun, 03 May 2009)
New Revision: 74
Modified:
lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java
lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java
Log:
Add csv import/export to dataframes
Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java
===================================================================
--- lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java 2009-05-01 09:47:16 UTC (rev 73)
+++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java 2009-05-03 09:18:40 UTC (rev 74)
@@ -1,5 +1,27 @@
+/* *##% Lutin Java-2-R library
+ * Copyright (C) 2006 - 2008 CodeLutin
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%*/
+
package org.codelutin.j2r.types;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Set;
@@ -128,7 +150,7 @@
public void setData(Vector<Vector<? extends Serializable>> data,
REngine engine) throws RException {
this.data = data;
- if(checkConsistency()==false){
+ if (checkConsistency() == false) {
throw new RException("A dimension is wrong on the dataframe");
}
}
@@ -219,7 +241,7 @@
Vector<Serializable> thisColumn = new Vector<Serializable>();
for (int j = 0; j < vectorlength; j++) {
thisColumn.add((Serializable) engine.eval(this.variable + "["
- + (i + 1) + "," + (j + 1) + "]"));
+ + (j + 1) + "," + (i + 1) + "]"));
}
data.add(thisColumn);
}
@@ -236,8 +258,8 @@
+ this.variable + ")$" + key + ")");
attributes.put(key, attribute);
}
-
- if(checkConsistency()==false){
+
+ if (checkConsistency() == false) {
throw new RException("A dimension is wrong on the dataframe");
}
@@ -428,4 +450,276 @@
return true;
}
+ public void exportCsv(File outputFile, boolean rowNames, boolean names) {
+ try {
+ BufferedWriter file = new BufferedWriter(new FileWriter(outputFile));
+
+ if (names) {
+ if (rowNames) {
+ file.write(";");
+ }
+ for (int i = 0; i < this.names.size(); i++) {
+ file.write(this.names.get(i) + ";");
+ }
+ file.newLine();
+ }
+
+ for (int i = 0; i < this.data.get(0).size(); i++) {
+ if (rowNames) {
+ file.write(this.rowNames.get(i) + ";");
+ }
+ for (int j = 0; j < this.data.size(); j++) {
+ file.write(this.data.get(j).get(i) + ";");
+ }
+ file.newLine();
+ }
+ file.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ /**
+ * Import a dataframe form a csv file. The dataframe will contain Strings.
+ * Use this method if you don't know the type of data that is in the csv
+ * file.
+ *
+ * @param inputFile
+ * Csv file to import.
+ * @param rowNames
+ * Does the csv file contains names of the rows.
+ * @param names
+ * Does the csv file contain names of the columns.
+ */
+ public void importCsv(File inputFile, boolean rowNames, boolean names) {
+ String tmp;
+ Integer dataSize = 0;
+ try {
+
+ BufferedReader first = new BufferedReader(new FileReader(inputFile));
+ tmp = first.readLine();
+ String[] splitted = tmp.split("\\;");
+ if (rowNames) {
+ dataSize = splitted.length - 1;
+ } else {
+ dataSize = splitted.length;
+ }
+
+ BufferedReader br = new BufferedReader(new FileReader(inputFile));
+ if (this.data != null) {
+ this.data.clear();
+ }
+ if (rowNames) {
+ if (this.rowNames != null) {
+ this.rowNames.clear();
+ }
+ }
+ if (names) {
+ if (this.names != null) {
+ this.names.clear();
+ }
+ tmp = br.readLine();
+ splitted = tmp.split("\\;");
+ for (int i = 1; i < splitted.length; i++) {
+ this.names.add(splitted[i]);
+ }
+ }
+
+ Vector<Vector<? extends Serializable>> data = new Vector<Vector<? extends Serializable>>();
+
+ for (int i = 0; i < dataSize; i++) {
+ Vector<Serializable> vector = new Vector<Serializable>();
+ data.add(vector);
+ }
+
+ while ((tmp = br.readLine()) != null) {
+ splitted = tmp.split("\\;");
+ int index = 0;
+ if (rowNames) {
+ this.rowNames.add(splitted[0]);
+ index = 1;
+ }
+ for (int i = 0 + index; i < splitted.length; i++) {
+ ((Vector<Serializable>) data.get(i - index))
+ .add((Serializable) splitted[i]);
+ }
+ }
+ br.close();
+ this.data = data;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Import a dataframe form a csv file. The dataframe will contain Objects of
+ * the same class than importType. Use this method if you know the type of
+ * data that is in the csv file.
+ *
+ * @param inputFile
+ * Csv file to import.
+ * @param rowNames
+ * Does the csv file contains names of the rows.
+ * @param names
+ * Does the csv file contain names of the columns.
+ * @param importType
+ * Object of the class of the data imported (all the data have
+ * the same type). (Supported types : String, Double and Integer)
+ */
+ public void importCsv(File inputFile, boolean rowNames, boolean names,
+ Object importType) {
+ String tmp;
+ Integer dataSize = 0;
+ try {
+
+ BufferedReader first = new BufferedReader(new FileReader(inputFile));
+ tmp = first.readLine();
+ String[] splitted = tmp.split("\\;");
+ if (rowNames) {
+ dataSize = splitted.length - 1;
+ } else {
+ dataSize = splitted.length;
+ }
+
+ BufferedReader br = new BufferedReader(new FileReader(inputFile));
+ if (this.data != null) {
+ this.data.clear();
+ }
+ if (rowNames) {
+ if (this.rowNames != null) {
+ this.rowNames.clear();
+ }
+ }
+ if (names) {
+ if (this.names != null) {
+ this.names.clear();
+ }
+ tmp = br.readLine();
+ splitted = tmp.split("\\;");
+ for (int i = 1; i < splitted.length; i++) {
+ this.names.add(splitted[i]);
+ }
+ }
+
+ Vector<Vector<? extends Serializable>> data = new Vector<Vector<? extends Serializable>>();
+
+ for (int i = 0; i < dataSize; i++) {
+ Vector<Serializable> vector = new Vector<Serializable>();
+ data.add(vector);
+ }
+
+ while ((tmp = br.readLine()) != null) {
+ splitted = tmp.split("\\;");
+ int index = 0;
+ if (rowNames) {
+ this.rowNames.add(splitted[0]);
+ index = 1;
+ }
+ for (int i = 0 + index; i < splitted.length; i++) {
+ if (importType instanceof String) {
+ ((Vector<Serializable>) data.get(i - index))
+ .add((Serializable) splitted[i]);
+ } else if (importType instanceof Double) {
+ ((Vector<Serializable>) data.get(i - index)).add(Double
+ .valueOf(splitted[i]));
+ } else if (importType instanceof Integer) {
+ ((Vector<Serializable>) data.get(i - index))
+ .add(Integer.valueOf(splitted[i]));
+ }
+ }
+ }
+ br.close();
+ this.data = data;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Import a dataframe form a csv file. The dataframe will contain Objects of
+ * the same class than each element of importTypes (one element = one
+ * column). Use this method if you know the type of data that is in the csv
+ * file.
+ *
+ * @param inputFile
+ * Csv file to import.
+ * @param rowNames
+ * Does the csv file contains names of the rows.
+ * @param names
+ * Does the csv file contain names of the columns.
+ * @param importTypes
+ * Vector of Object of the class of the data imported (the Vector
+ * match the data type of the columns). (Supported types :
+ * String, Double and Integer)
+ */
+ public void importCsv(File inputFile, boolean rowNames, boolean names,
+ Vector<Object> importTypes) {
+ String tmp;
+ Integer dataSize = 0;
+ try {
+
+ BufferedReader first = new BufferedReader(new FileReader(inputFile));
+ tmp = first.readLine();
+ String[] splitted = tmp.split("\\;");
+ if (rowNames) {
+ dataSize = splitted.length - 1;
+ } else {
+ dataSize = splitted.length;
+ }
+
+ BufferedReader br = new BufferedReader(new FileReader(inputFile));
+ if (this.data != null) {
+ this.data.clear();
+ }
+ if (rowNames) {
+ if (this.rowNames != null) {
+ this.rowNames.clear();
+ }
+ }
+ if (names) {
+ if (this.names != null) {
+ this.names.clear();
+ }
+ tmp = br.readLine();
+ splitted = tmp.split("\\;");
+ for (int i = 1; i < splitted.length; i++) {
+ this.names.add(splitted[i]);
+ }
+ }
+
+ Vector<Vector<? extends Serializable>> data = new Vector<Vector<? extends Serializable>>();
+
+ for (int i = 0; i < dataSize; i++) {
+ Vector<Serializable> vector = new Vector<Serializable>();
+ data.add(vector);
+ }
+
+ while ((tmp = br.readLine()) != null) {
+ splitted = tmp.split("\\;");
+ int index = 0;
+ if (rowNames) {
+ this.rowNames.add(splitted[0]);
+ index = 1;
+ }
+ for (int i = 0 + index; i < splitted.length; i++) {
+ if (importTypes.get(i - index) instanceof String) {
+ ((Vector<Serializable>) data.get(i - index))
+ .add((Serializable) splitted[i]);
+ } else if (importTypes.get(i - index) instanceof Double) {
+ ((Vector<Serializable>) data.get(i - index)).add(Double
+ .valueOf(splitted[i]));
+ } else if (importTypes.get(i - index) instanceof Integer) {
+ ((Vector<Serializable>) data.get(i - index))
+ .add(Integer.valueOf(splitted[i]));
+ }
+ }
+ }
+ br.close();
+ this.data = data;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
}
Modified: lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java
===================================================================
--- lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java 2009-05-01 09:47:16 UTC (rev 73)
+++ lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java 2009-05-03 09:18:40 UTC (rev 74)
@@ -17,6 +17,7 @@
package org.codelutin.j2r;
+import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Vector;
@@ -166,15 +167,22 @@
dataframe2.getFrom("test", engine);
//Test data
- Assert.assertEquals(new Double(3.0), (Double) engine.eval("test[1,1]"));
- Assert.assertEquals(new Double(4.5), (Double) engine.eval("test[2,1]"));
Assert
- .assertEquals(new Double(0.01), (Double) engine
- .eval("test[3,1]"));
- Assert.assertEquals(new Double(1.0), (Double) engine.eval("test[1,2]"));
- Assert.assertEquals(new Double(5555555555555555555555.0),
- (Double) engine.eval("test[2,2]"));
- Assert.assertEquals(new Double(3.0), (Double) engine.eval("test[3,2]"));
+ .assertEquals(new Double(3.0), dataframe2.getData().get(0).get(
+ 0));
+ Assert
+ .assertEquals(new Double(4.5), dataframe2.getData().get(0).get(
+ 1));
+ Assert.assertEquals(new Double(0.01), dataframe2.getData().get(0)
+ .get(2));
+ Assert
+ .assertEquals(new Double(1.0), dataframe2.getData().get(1).get(
+ 0));
+ Assert.assertEquals(new Double(5555555555555555555555.0), dataframe2
+ .getData().get(1).get(1));
+ Assert
+ .assertEquals(new Double(3.0), dataframe2.getData().get(1).get(
+ 2));
//Test names
Assert.assertEquals("column1", dataframe2.getNames().get(0));
Assert.assertEquals("column2", dataframe2.getNames().get(1));
@@ -285,4 +293,168 @@
}
}
+ @Test
+ public void testImportExportCsv() throws Exception {
+
+ //Test import with same type for each column.
+
+ Vector<String> names = new Vector<String>();
+ names.add("column1");
+ names.add("column2");
+
+ Vector<String> rowNames = new Vector<String>();
+ rowNames.add("row 1");
+ rowNames.add("row 2");
+ rowNames.add("row 3");
+
+ Vector<Double> column1 = new Vector<Double>();
+ column1.add(3.0);
+ column1.add(4.5);
+ column1.add(0.01);
+
+ Vector<Double> column2 = new Vector<Double>();
+ column2.add(1.0);
+ column2.add(5555555555555555555555.0);
+ column2.add(3.0);
+
+ Vector<Vector<? extends Serializable>> data = new Vector<Vector<? extends Serializable>>();
+ data.add(column1);
+ data.add(column2);
+
+ RDataFrame testDataFrame = new RDataFrame();
+ try {
+ testDataFrame = new RDataFrame(names, rowNames, data, engine,
+ "test");
+ } catch (RException eee) {
+ Assert.fail();
+ }
+ testDataFrame.exportCsv(new File("/tmp/test.csv"), true, true);
+
+ RDataFrame dataframe2 = new RDataFrame();
+ dataframe2.importCsv(new File("/tmp/test.csv"), true, true, new Double(
+ 3.0));
+
+ //Test data
+ Assert
+ .assertEquals(new Double(3.0), dataframe2.getData().get(0).get(
+ 0));
+ Assert
+ .assertEquals(new Double(4.5), dataframe2.getData().get(0).get(
+ 1));
+ Assert.assertEquals(new Double(0.01), dataframe2.getData().get(0)
+ .get(2));
+ Assert
+ .assertEquals(new Double(1.0), dataframe2.getData().get(1).get(
+ 0));
+ Assert.assertEquals(new Double(5555555555555555555555.0), dataframe2
+ .getData().get(1).get(1));
+ Assert
+ .assertEquals(new Double(3.0), dataframe2.getData().get(1).get(
+ 2));
+ //Test names
+ Assert.assertEquals("column1", dataframe2.getNames().get(0));
+ Assert.assertEquals("column2", dataframe2.getNames().get(1));
+ //Test row names
+ Assert.assertEquals("row 1", dataframe2.getRowNames().get(0));
+ Assert.assertEquals("row 2", dataframe2.getRowNames().get(1));
+ Assert.assertEquals("row 3", dataframe2.getRowNames().get(2));
+
+ //Test import with different types for each column
+
+ names = new Vector<String>();
+ names.add("column1");
+ names.add("column3");
+ names.add("column4");
+
+ rowNames = new Vector<String>();
+ rowNames.add("row 1");
+ rowNames.add("row 2");
+ rowNames.add("row 3");
+
+ column1 = new Vector<Double>();
+ column1.add(3.0);
+ column1.add(4.5);
+ column1.add(0.01);
+
+ Vector<Integer> column3 = new Vector<Integer>();
+ column3.add(1);
+ column3.add(5);
+ column3.add(3);
+
+ Vector<String> column4 = new Vector<String>();
+ column4.add("bla");
+ column4.add("blabla");
+ column4.add("blablabla");
+
+ data = new Vector<Vector<? extends Serializable>>();
+ data.add(column1);
+ data.add(column3);
+ data.add(column4);
+
+ Vector<Object> types = new Vector<Object>();
+ types.add(new Double(4.0));
+ types.add(new Integer(4));
+ types.add(new String());
+
+ testDataFrame = new RDataFrame();
+ try {
+ testDataFrame = new RDataFrame(names, rowNames, data, engine,
+ "test");
+ } catch (RException eee) {
+ Assert.fail();
+ }
+ testDataFrame.exportCsv(new File("/tmp/test.csv"), true, true);
+
+ RDataFrame dataframe3 = new RDataFrame();
+ dataframe3.importCsv(new File("/tmp/test.csv"), true, true, types);
+
+ //Test data
+ Assert
+ .assertEquals(new Double(3.0), dataframe3.getData().get(0).get(
+ 0));
+ Assert
+ .assertEquals(new Double(4.5), dataframe3.getData().get(0).get(
+ 1));
+ Assert.assertEquals(new Double(0.01), dataframe3.getData().get(0)
+ .get(2));
+ Assert.assertEquals(new Integer(1), dataframe3.getData().get(1).get(0));
+ Assert.assertEquals(new Integer(5), dataframe3.getData().get(1).get(1));
+ Assert.assertEquals(new Integer(3), dataframe3.getData().get(1).get(2));
+ Assert.assertEquals("bla", dataframe3.getData().get(2).get(0));
+ Assert.assertEquals("blabla", dataframe3.getData().get(2).get(1));
+ Assert.assertEquals("blablabla", dataframe3.getData().get(2).get(2));
+ //Test names
+ Assert.assertEquals("column1", dataframe3.getNames().get(0));
+ Assert.assertEquals("column3", dataframe3.getNames().get(1));
+ Assert.assertEquals("column4", dataframe3.getNames().get(2));
+ //Test row names
+ Assert.assertEquals("row 1", dataframe3.getRowNames().get(0));
+ Assert.assertEquals("row 2", dataframe3.getRowNames().get(1));
+ Assert.assertEquals("row 3", dataframe3.getRowNames().get(2));
+
+ //Test import without precision on type
+
+ RDataFrame dataframe4 = new RDataFrame();
+ dataframe4.importCsv(new File("/tmp/test.csv"), true, true);
+
+ //Test data
+ Assert.assertEquals("3.0", dataframe4.getData().get(0).get(0));
+ Assert.assertEquals("4.5", dataframe4.getData().get(0).get(1));
+ Assert.assertEquals("0.01", dataframe4.getData().get(0).get(2));
+ Assert.assertEquals("1", dataframe4.getData().get(1).get(0));
+ Assert.assertEquals("5", dataframe4.getData().get(1).get(1));
+ Assert.assertEquals("3", dataframe4.getData().get(1).get(2));
+ Assert.assertEquals("bla", dataframe4.getData().get(2).get(0));
+ Assert.assertEquals("blabla", dataframe4.getData().get(2).get(1));
+ Assert.assertEquals("blablabla", dataframe4.getData().get(2).get(2));
+ //Test names
+ Assert.assertEquals("column1", dataframe4.getNames().get(0));
+ Assert.assertEquals("column3", dataframe4.getNames().get(1));
+ Assert.assertEquals("column4", dataframe4.getNames().get(2));
+ //Test row names
+ Assert.assertEquals("row 1", dataframe4.getRowNames().get(0));
+ Assert.assertEquals("row 2", dataframe4.getRowNames().get(1));
+ Assert.assertEquals("row 3", dataframe4.getRowNames().get(2));
+ }
+
}
1
0
[LutinJ2R-commits] r73 - lutinj2r/trunk/src/site/rst
by jcouteau@users.labs.libre-entreprise.org 01 May '09
by jcouteau@users.labs.libre-entreprise.org 01 May '09
01 May '09
Author: jcouteau
Date: 2009-05-01 09:47:16 +0000 (Fri, 01 May 2009)
New Revision: 73
Modified:
lutinj2r/trunk/src/site/rst/installation.rst
Log:
Adding installation precisions for Windows on website.
Modified: lutinj2r/trunk/src/site/rst/installation.rst
===================================================================
--- lutinj2r/trunk/src/site/rst/installation.rst 2009-05-01 09:43:50 UTC (rev 72)
+++ lutinj2r/trunk/src/site/rst/installation.rst 2009-05-01 09:47:16 UTC (rev 73)
@@ -76,7 +76,11 @@
La compilation effectuée, un fichier a été généré, dont le nom est propre à la plateforme (libjri.so, jri.dll, ...). Il ne reste plus qu'à configurer le système comme indiqué à l'étape suivante.
+Des versions précompilées des librairies sont disponibles (http://lutinj2r.labs.libre-entreprise.org/libsys/)
+Site web de JRI : http://www.rforge.net/JRI/
+
+
Configuration
=============
@@ -120,9 +124,9 @@
Quelque soit la plateforme, il faut positionner des variables d'environnement.
-- R_HOME : doit pointer sur le dossier d'installation de R (soit /usr/lib/R sous Linux ou C:\Program Files\R\R-2.3.1 sous Windows ou ...)
+- R_HOME : doit pointer sur le dossier d'installation de R (soit /usr/lib/R sous Linux ou C:\Program Files\R\R-(version) sous Windows ou ...)
- LD_LIBRARY_PATH : doit pointer sur le dossier lib contenu dans R_HOME (soit R_HOME/lib/ sous Linux ou R_HOME\lib sous Windows ou ...)
-
+- PATH (pour Windows) : doit pointer sur le dossier d'installation de R (C:\Program Files\R\R-(version) ou ...) (en fait sur le dossier contenant la librairie R.dll)
Il faut ensuite copier la librairie (libjri.so ou jri.dll ou ...) dans le dossier LD_LIBRARY_PATH
@@ -151,3 +155,10 @@
Rserve.exe dans le dossier d'installation de R (par défaut : C:\Program Files\R\R-2.3.1\bin)
+
+A noter que vous pouvez aussi lancer Rserve à partir de R :
+
+::
+
+ library(Rserve)
+ Rserve()
\ No newline at end of file
1
0
[LutinJ2R-commits] r72 - in lutinj2r/trunk/src: main/java/org/codelutin/j2r/types test/java/org/codelutin/j2r
by jcouteau@users.labs.libre-entreprise.org 01 May '09
by jcouteau@users.labs.libre-entreprise.org 01 May '09
01 May '09
Author: jcouteau
Date: 2009-05-01 09:43:50 +0000 (Fri, 01 May 2009)
New Revision: 72
Modified:
lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java
lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java
Log:
Adding data checking in data.frames
Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java
===================================================================
--- lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java 2009-05-01 08:14:19 UTC (rev 71)
+++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java 2009-05-01 09:43:50 UTC (rev 72)
@@ -2,7 +2,6 @@
import java.io.Serializable;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
@@ -38,6 +37,9 @@
this.rowNames = rowNames;
this.data = (Vector<Vector<? extends Serializable>>) data;
this.variable = variable;
+ if (checkConsistency() == false) {
+ throw new RException("A dimension is wrong on the dataframe");
+ }
}
/**
@@ -66,6 +68,9 @@
public void setNames(Vector<String> names, REngine engine)
throws RException {
this.names = names;
+ if (checkConsistency() == false) {
+ throw new RException("A dimension is wrong on the dataframe");
+ }
}
/**
@@ -93,6 +98,9 @@
public void setRowNames(Vector<String> rowNames, REngine engine)
throws RException {
this.rowNames = rowNames;
+ if (checkConsistency() == false) {
+ throw new RException("A dimension is wrong on the dataframe");
+ }
}
/**
@@ -120,6 +128,9 @@
public void setData(Vector<Vector<? extends Serializable>> data,
REngine engine) throws RException {
this.data = data;
+ if(checkConsistency()==false){
+ throw new RException("A dimension is wrong on the dataframe");
+ }
}
/**
@@ -225,6 +236,10 @@
+ this.variable + ")$" + key + ")");
attributes.put(key, attribute);
}
+
+ if(checkConsistency()==false){
+ throw new RException("A dimension is wrong on the dataframe");
+ }
}
@@ -388,6 +403,29 @@
}
}
- //TODO check data consistency.
+ /**
+ * Method to check data consistency
+ *
+ * @return true if data is consistent (no dimensions error), false if there
+ * is a dimension error.
+ */
+ public boolean checkConsistency() {
+ Integer rowNameslength = rowNames.size();
+ Integer namesLength = names.size();
+ Integer dataLength = data.size();
+ Integer datavectorLength = data.get(0).size();
+ if (rowNameslength != datavectorLength) {
+ return false;
+ }
+ if (namesLength != dataLength) {
+ return false;
+ }
+ for (int i = 0; i < dataLength; i++) {
+ if (data.get(i).size() != datavectorLength) {
+ return false;
+ }
+ }
+ return true;
+ }
}
Modified: lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java
===================================================================
--- lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java 2009-05-01 08:14:19 UTC (rev 71)
+++ lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java 2009-05-01 09:43:50 UTC (rev 72)
@@ -80,8 +80,13 @@
attributes.put("attr1", "\"this is an attribute\"");
attributes.put("attr2", "\"this is another attribute\"");
- RDataFrame testDataFrame = new RDataFrame(names, rowNames, data,
- engine, "test");
+ RDataFrame testDataFrame = new RDataFrame();
+ try {
+ testDataFrame = new RDataFrame(names, rowNames, data, engine,
+ "test");
+ } catch (RException eee) {
+ Assert.fail();
+ }
testDataFrame.setAttributes(attributes);
testDataFrame.commit(engine);
@@ -147,8 +152,13 @@
attributes.put("attr1", "\"this is an attribute\"");
attributes.put("attr2", "\"this is another attribute\"");
- RDataFrame testDataFrame = new RDataFrame(names, rowNames, data,
- engine, "test");
+ RDataFrame testDataFrame = new RDataFrame();
+ try {
+ testDataFrame = new RDataFrame(names, rowNames, data, engine,
+ "test");
+ } catch (RException eee) {
+ Assert.fail();
+ }
testDataFrame.setAttributes(attributes);
testDataFrame.commit(engine);
@@ -179,4 +189,100 @@
.getAttribute("attr2"));
}
+ @Test
+ public void testDataFrameWrongDimensions() throws Exception {
+ //Test wrong names size
+ Vector<String> names = new Vector<String>();
+ names.add("column1");
+
+ Vector<String> rowNames = new Vector<String>();
+ rowNames.add("row 1");
+ rowNames.add("row 2");
+ rowNames.add("row 3");
+
+ Vector<Double> column1 = new Vector<Double>();
+ column1.add(3.0);
+ column1.add(4.5);
+ column1.add(0.01);
+
+ Vector<Double> column2 = new Vector<Double>();
+ column2.add(1.0);
+ column2.add(5555555555555555555555.0);
+ column2.add(3.0);
+
+ Vector<Vector<? extends Serializable>> data = new Vector<Vector<? extends Serializable>>();
+ data.add(column1);
+ data.add(column2);
+
+ try {
+ RDataFrame testDataFrame = new RDataFrame(names, rowNames, data,
+ engine, "test");
+ } catch (RException eee) {
+ Assert.assertEquals("A dimension is wrong on the dataframe", eee
+ .getMessage());
+ }
+
+ //Test wrong row names size
+ names = new Vector<String>();
+ names.add("column1");
+ names.add("column2");
+
+ rowNames = new Vector<String>();
+ rowNames.add("row 1");
+ rowNames.add("row 2");
+
+ column1 = new Vector<Double>();
+ column1.add(3.0);
+ column1.add(4.5);
+ column1.add(0.01);
+
+ column2 = new Vector<Double>();
+ column2.add(1.0);
+ column2.add(5555555555555555555555.0);
+ column2.add(3.0);
+
+ data = new Vector<Vector<? extends Serializable>>();
+ data.add(column1);
+ data.add(column2);
+
+ try {
+ RDataFrame testDataFrame = new RDataFrame(names, rowNames, data,
+ engine, "test");
+ } catch (RException eee) {
+ Assert.assertEquals("A dimension is wrong on the dataframe", eee
+ .getMessage());
+ }
+
+ //Test wrong data size
+ names = new Vector<String>();
+ names.add("column1");
+ names.add("column2");
+
+ rowNames = new Vector<String>();
+ rowNames.add("row 1");
+ rowNames.add("row 2");
+ rowNames.add("row 3");
+
+ column1 = new Vector<Double>();
+ column1.add(3.0);
+ column1.add(4.5);
+ column1.add(0.01);
+
+ column2 = new Vector<Double>();
+ column2.add(1.0);
+ column2.add(5555555555555555555555.0);
+
+ data = new Vector<Vector<? extends Serializable>>();
+ data.add(column1);
+ data.add(column2);
+
+ try {
+ RDataFrame testDataFrame = new RDataFrame(names, rowNames, data,
+ engine, "test");
+ } catch (RException eee) {
+ Assert.assertEquals("A dimension is wrong on the dataframe", eee
+ .getMessage());
+ }
+ }
+
}
1
0
[LutinJ2R-commits] r71 - in lutinj2r/trunk/src: main/java/org/codelutin/j2r/types test/java/org/codelutin/j2r
by jcouteau@users.labs.libre-entreprise.org 01 May '09
by jcouteau@users.labs.libre-entreprise.org 01 May '09
01 May '09
Author: jcouteau
Date: 2009-05-01 08:14:19 +0000 (Fri, 01 May 2009)
New Revision: 71
Modified:
lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java
lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java
Log:
Adding attributes management to data.frames
Changing the R/Java update process. Now need to be expressed.
Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java
===================================================================
--- lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java 2009-04-30 12:10:02 UTC (rev 70)
+++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java 2009-05-01 08:14:19 UTC (rev 71)
@@ -1,6 +1,9 @@
package org.codelutin.j2r.types;
import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Set;
import java.util.Vector;
import org.codelutin.j2r.REngine;
@@ -8,10 +11,16 @@
public class RDataFrame {
+ //Vector containing the names of the data.frame vectors
private Vector<String> names;
+ //Vector containing the names of the rows of the data.frame vectors
private Vector<String> rowNames;
+ //Vector containing the vectors of the data.frame
private Vector<Vector<? extends Serializable>> data;
+ //Name of the data.frame in R
private String variable;
+ //Attributes of the data.frame under a map -> name : R expression
+ private HashMap<String, String> attributes;
public RDataFrame() {
super();
@@ -29,12 +38,11 @@
this.rowNames = rowNames;
this.data = (Vector<Vector<? extends Serializable>>) data;
this.variable = variable;
- assignTo(variable, engine);
}
/**
* Method to get the names of the vectors of the R data.frame (there is no
- * synchronizing with R, use the synchro() method to synchronize data with R
+ * synchronizing with R, use the update() method to synchronize data with R
* before using this method if you think data may have changed.
*
* @return a vector of strings containing the names of each vector of the R
@@ -45,8 +53,8 @@
}
/**
- * Method to assign names of the vectors of the R data.frame (the names will
- * then be synchronized with R)
+ * Method to assign names of the vectors of the R data.frame (there is no
+ * synchronizing with R, use the commit() method to send data to R.
*
* @param names
* a vector containing the names of the vector of the R
@@ -58,12 +66,11 @@
public void setNames(Vector<String> names, REngine engine)
throws RException {
this.names = names;
- assignNames(engine);
}
/**
* Method to get the names of the rows of the R data.frame (there is no
- * synchronizing with R, use the synchro() method to synchronize data with R
+ * synchronizing with R, use the update() method to synchronize data with R
* before using this method if you think data may have changed.
*
* @return a vector of strings containing the names of each row of the R
@@ -74,8 +81,8 @@
}
/**
- * Method to assign names of the rows of the R data.frame (the names will
- * then be synchronized with R)
+ * Method to assign names of the rows of the R data.frame (there is no
+ * synchronizing with R, use the commit() method to send data to R.
*
* @param rowNames
* a vector containing the names of the rows of the R data.frame
@@ -86,12 +93,11 @@
public void setRowNames(Vector<String> rowNames, REngine engine)
throws RException {
this.rowNames = rowNames;
- assignRowNames(engine);
}
/**
* Method to get the vectors of the R data.frame (there is no synchronizing
- * with R, use the synchro() method to synchronize data with R before using
+ * with R, use the update() method to synchronize data with R before using
* this method if you think data may have changed.
*
* @return a Vector containing the vectors of the R data.frame
@@ -101,8 +107,8 @@
}
/**
- * Method to assign the data of the R data.frame (the data will then be
- * synchronized with R)
+ * Method to assign the data of the R data.frame (there is no synchronizing
+ * with R, use the commit() method to send data to R.
*
* @param data
* a Vector of Vectors, containing each vector of the R
@@ -114,7 +120,6 @@
public void setData(Vector<Vector<? extends Serializable>> data,
REngine engine) throws RException {
this.data = data;
- assignData(engine);
}
/**
@@ -127,12 +132,24 @@
* @throws RException
*/
public void assignTo(String variable, REngine engine) throws RException {
+ this.variable = variable;
+ commit(engine);
- this.variable = variable;
+ }
+
+ /**
+ * Method to commit changes made to the data.frame in java and send them to
+ * R.
+ *
+ * @param engine
+ * a REngine where the R session is located.
+ * @throws RException
+ */
+ public void commit(REngine engine) throws RException {
assignData(engine);
assignRowNames(engine);
assignNames(engine);
-
+ assignAttributes(engine);
}
/**
@@ -146,20 +163,42 @@
*/
public void getFrom(String variable, REngine engine) throws RException {
this.variable = variable;
- rowNames.clear();
- names.clear();
- data.clear();
+ if (rowNames != null) {
+ rowNames.clear();
+ } else {
+ rowNames = new Vector<String>();
+ }
+ if (names != null) {
+ names.clear();
+ } else {
+ names = new Vector<String>();
+ }
+ if (data != null) {
+ data.clear();
+ } else {
+ data = new Vector<Vector<? extends Serializable>>();
+ }
+ if (attributes != null) {
+ attributes.clear();
+ } else {
+ attributes = new HashMap<String, String>();
+ }
+
+ //update row names
String[] rowNamesArray = (String[]) engine.eval("row.names("
+ this.variable + ")");
for (int i = 0; i < rowNamesArray.length; i++) {
rowNames.add(rowNamesArray[i]);
}
+
+ //update names
String[] namesArray = (String[]) engine.eval("names(" + this.variable
+ ")");
for (int i = 0; i < namesArray.length; i++) {
names.add(namesArray[i]);
}
+ //update data
Integer dataframelength = (Integer) engine.eval("length("
+ this.variable + ")");
@@ -174,6 +213,19 @@
data.add(thisColumn);
}
+ //update attributes
+ Integer attributeslength = (Integer) engine.eval("length(attributes("
+ + this.variable + "))");
+
+ for (int i = 0; i < attributeslength; i++) {
+ String key = (String) engine.eval("names(attributes("
+ + this.variable + "))[" + (i + 1) + "]");
+
+ String attribute = (String) engine.eval("toString(attributes("
+ + this.variable + ")$" + key + ")");
+ attributes.put(key, attribute);
+ }
+
}
/**
@@ -184,7 +236,7 @@
* a REngine where the R session is located.
* @throws RException
*/
- public void check(REngine engine) throws RException {
+ public void update(REngine engine) throws RException {
getFrom(variable, engine);
}
@@ -245,10 +297,97 @@
engine.voidEval(dataframe);
}
- //TODO method set attributes, get attributes
+ /**
+ * Assign a value to an attribute in R.
+ *
+ * @param engine
+ * a REngine where the R session is located.
+ * @param attribute
+ * the attribute name
+ * @param value
+ * the value to be assigned (this is a R expression, String may
+ * be rounded with escaped quote like : \"this is a R string\" ).
+ * @throws RException
+ */
+ private void assignAttribute(REngine engine, String attribute, String value)
+ throws RException {
+ engine.voidEval("attr(" + this.variable + ",\"" + attribute + "\")<-"
+ + value);
+ }
- //TODO method add attribute, remove attribute
+ /**
+ * Assign all the attributes in R.
+ *
+ * @param engine
+ * a REngine where the R session is located.
+ * @throws RException
+ */
+ private void assignAttributes(REngine engine) throws RException {
+ if (attributes != null) {
+ Set<String> keyset = attributes.keySet();
+ String[] keys = keyset.toArray(new String[0]);
+ for (int i = 0; i < keys.length; i++) {
+ assignAttribute(engine, keys[i], attributes.get(keys[i]));
+ }
+ }
+ }
+ /**
+ * Method to set all the attributes of the data.frame (there is no
+ * synchronizing with R, use the commit() method to send data to R.
+ *
+ * @param attributes
+ * a Map containing the attributes (key) and values (value)
+ * (values are a R expression, String may be rounded with escaped
+ * quote like : \"this is a R string\" ).
+ */
+ public void setAttributes(HashMap<String, String> attributes) {
+ this.attributes = attributes;
+ }
+
+ /**
+ * Method to get all the attributes of the data.frame (there is no
+ * synchronizing with R, use the update() method to synchronize data with R
+ * before using this method if you think data may have changed.
+ *
+ * @return a Map containing the attributes (key) and values (value)
+ */
+ public HashMap<String, String> getAttributes() {
+ return this.attributes;
+ }
+
+ /**
+ * Method to get the value of an attribute (there is no synchronizing with
+ * R, use the update() method to synchronize data with R before using this
+ * method if you think data may have changed.
+ *
+ * @param attribute
+ * name of the attribute
+ * @return
+ */
+ public String getAttribute(String attribute) {
+ return attributes.get(attribute);
+ }
+
+ /**
+ * Method to set the value of an attribute (there is no synchronizing with
+ * R, use the commit() method to send data to R.
+ *
+ * @param attribute
+ * name of the attribute
+ * @param value
+ * the value to be set (this is a R expression, String may be
+ * rounded with escaped quote like : \"this is a R string\" ).
+ */
+ public void setAttribute(String attribute, String value) {
+ if (attributes.containsKey(attribute)) {
+ attributes.remove(attribute);
+ attributes.put(attribute, value);
+ } else {
+ attributes.put(attribute, value);
+ }
+ }
+
//TODO check data consistency.
}
Modified: lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java
===================================================================
--- lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java 2009-04-30 12:10:02 UTC (rev 70)
+++ lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java 2009-05-01 08:14:19 UTC (rev 71)
@@ -18,6 +18,7 @@
package org.codelutin.j2r;
import java.io.Serializable;
+import java.util.HashMap;
import java.util.Vector;
import org.apache.commons.logging.Log;
@@ -75,8 +76,15 @@
data.add(column1);
data.add(column2);
+ HashMap<String, String> attributes = new HashMap<String, String>();
+ attributes.put("attr1", "\"this is an attribute\"");
+ attributes.put("attr2", "\"this is another attribute\"");
+
RDataFrame testDataFrame = new RDataFrame(names, rowNames, data,
engine, "test");
+ testDataFrame.setAttributes(attributes);
+ testDataFrame.commit(engine);
+
//Test data
Assert.assertEquals(new Double(3.0), (Double) engine.eval("test[1,1]"));
Assert.assertEquals(new Double(4.5), (Double) engine.eval("test[2,1]"));
@@ -100,8 +108,14 @@
Assert
.assertEquals("row 3", (String) engine
.eval("row.names(test)[3]"));
+
+ //Test attributes
+ Assert.assertEquals("this is an attribute", (String) engine
+ .eval("attr(test,\"attr1\")"));
+ Assert.assertEquals("this is another attribute", (String) engine
+ .eval("attr(test,\"attr2\")"));
}
-
+
@Test
public void testGetDataFrame() throws Exception {
//This test is also a test for all the setters...
@@ -129,10 +143,18 @@
data.add(column1);
data.add(column2);
+ HashMap<String, String> attributes = new HashMap<String, String>();
+ attributes.put("attr1", "\"this is an attribute\"");
+ attributes.put("attr2", "\"this is another attribute\"");
+
RDataFrame testDataFrame = new RDataFrame(names, rowNames, data,
engine, "test");
+ testDataFrame.setAttributes(attributes);
+ testDataFrame.commit(engine);
+
RDataFrame dataframe2 = new RDataFrame();
dataframe2.getFrom("test", engine);
+
//Test data
Assert.assertEquals(new Double(3.0), (Double) engine.eval("test[1,1]"));
Assert.assertEquals(new Double(4.5), (Double) engine.eval("test[2,1]"));
@@ -147,12 +169,14 @@
Assert.assertEquals("column1", dataframe2.getNames().get(0));
Assert.assertEquals("column2", dataframe2.getNames().get(1));
//Test row names
- Assert
- .assertEquals("row 1", dataframe2.getRowNames().get(0));
- Assert
- .assertEquals("row 2", dataframe2.getRowNames().get(1));
- Assert
- .assertEquals("row 3", dataframe2.getRowNames().get(2));
+ Assert.assertEquals("row 1", dataframe2.getRowNames().get(0));
+ Assert.assertEquals("row 2", dataframe2.getRowNames().get(1));
+ Assert.assertEquals("row 3", dataframe2.getRowNames().get(2));
+ //test attributes
+ Assert.assertEquals("this is an attribute", dataframe2
+ .getAttribute("attr1"));
+ Assert.assertEquals("this is another attribute", dataframe2
+ .getAttribute("attr2"));
}
}
1
0
[LutinJ2R-commits] r70 - in lutinj2r/trunk/src: main/java/org/codelutin/j2r main/java/org/codelutin/j2r/types test/java/org/codelutin/j2r
by jcouteau@users.labs.libre-entreprise.org 30 Apr '09
by jcouteau@users.labs.libre-entreprise.org 30 Apr '09
30 Apr '09
Author: jcouteau
Date: 2009-04-30 12:10:02 +0000 (Thu, 30 Apr 2009)
New Revision: 70
Added:
lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/
lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java
lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java
Log:
Adding data.frame data type + tests.
Added: lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java
===================================================================
--- lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java (rev 0)
+++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/types/RDataFrame.java 2009-04-30 12:10:02 UTC (rev 70)
@@ -0,0 +1,254 @@
+package org.codelutin.j2r.types;
+
+import java.io.Serializable;
+import java.util.Vector;
+
+import org.codelutin.j2r.REngine;
+import org.codelutin.j2r.RException;
+
+public class RDataFrame {
+
+ private Vector<String> names;
+ private Vector<String> rowNames;
+ private Vector<Vector<? extends Serializable>> data;
+ private String variable;
+
+ public RDataFrame() {
+ super();
+ this.names = new Vector<String>();
+ this.rowNames = new Vector<String>();
+ this.data = new Vector<Vector<? extends Serializable>>();
+ this.variable = "";
+ }
+
+ public RDataFrame(Vector<String> names, Vector<String> rowNames,
+ Vector<Vector<? extends Serializable>> data, REngine engine,
+ String variable) throws RException {
+ super();
+ this.names = names;
+ this.rowNames = rowNames;
+ this.data = (Vector<Vector<? extends Serializable>>) data;
+ this.variable = variable;
+ assignTo(variable, engine);
+ }
+
+ /**
+ * Method to get the names of the vectors of the R data.frame (there is no
+ * synchronizing with R, use the synchro() method to synchronize data with R
+ * before using this method if you think data may have changed.
+ *
+ * @return a vector of strings containing the names of each vector of the R
+ * data.frame
+ */
+ public Vector<String> getNames() {
+ return names;
+ }
+
+ /**
+ * Method to assign names of the vectors of the R data.frame (the names will
+ * then be synchronized with R)
+ *
+ * @param names
+ * a vector containing the names of the vector of the R
+ * data.frame
+ * @param engine
+ * a REngine where the R session is located.
+ * @throws RException
+ */
+ public void setNames(Vector<String> names, REngine engine)
+ throws RException {
+ this.names = names;
+ assignNames(engine);
+ }
+
+ /**
+ * Method to get the names of the rows of the R data.frame (there is no
+ * synchronizing with R, use the synchro() method to synchronize data with R
+ * before using this method if you think data may have changed.
+ *
+ * @return a vector of strings containing the names of each row of the R
+ * data.frame
+ */
+ public Vector<String> getRowNames() {
+ return rowNames;
+ }
+
+ /**
+ * Method to assign names of the rows of the R data.frame (the names will
+ * then be synchronized with R)
+ *
+ * @param rowNames
+ * a vector containing the names of the rows of the R data.frame
+ * @param engine
+ * a REngine where the R session is located.
+ * @throws RException
+ */
+ public void setRowNames(Vector<String> rowNames, REngine engine)
+ throws RException {
+ this.rowNames = rowNames;
+ assignRowNames(engine);
+ }
+
+ /**
+ * Method to get the vectors of the R data.frame (there is no synchronizing
+ * with R, use the synchro() method to synchronize data with R before using
+ * this method if you think data may have changed.
+ *
+ * @return a Vector containing the vectors of the R data.frame
+ */
+ public Vector<Vector<? extends Serializable>> getData() {
+ return data;
+ }
+
+ /**
+ * Method to assign the data of the R data.frame (the data will then be
+ * synchronized with R)
+ *
+ * @param data
+ * a Vector of Vectors, containing each vector of the R
+ * data.frame
+ * @param engine
+ * a REngine where the R session is located.
+ * @throws RException
+ */
+ public void setData(Vector<Vector<? extends Serializable>> data,
+ REngine engine) throws RException {
+ this.data = data;
+ assignData(engine);
+ }
+
+ /**
+ * Method to assign this data.frame to a variable in R.
+ *
+ * @param variable
+ * name of the variable
+ * @param engine
+ * a REngine where the R session is located.
+ * @throws RException
+ */
+ public void assignTo(String variable, REngine engine) throws RException {
+
+ this.variable = variable;
+ assignData(engine);
+ assignRowNames(engine);
+ assignNames(engine);
+
+ }
+
+ /**
+ * Method to get a data.frame from a variable in R.
+ *
+ * @param variable
+ * name of the data.frame in R
+ * @param engine
+ * a REngine where the R session is located.
+ * @throws RException
+ */
+ public void getFrom(String variable, REngine engine) throws RException {
+ this.variable = variable;
+ rowNames.clear();
+ names.clear();
+ data.clear();
+ String[] rowNamesArray = (String[]) engine.eval("row.names("
+ + this.variable + ")");
+ for (int i = 0; i < rowNamesArray.length; i++) {
+ rowNames.add(rowNamesArray[i]);
+ }
+ String[] namesArray = (String[]) engine.eval("names(" + this.variable
+ + ")");
+ for (int i = 0; i < namesArray.length; i++) {
+ names.add(namesArray[i]);
+ }
+
+ Integer dataframelength = (Integer) engine.eval("length("
+ + this.variable + ")");
+
+ for (int i = 0; i < dataframelength; i++) {
+ Integer vectorlength = (Integer) engine.eval("length("
+ + this.variable + "[," + (i + 1) + "])");
+ Vector<Serializable> thisColumn = new Vector<Serializable>();
+ for (int j = 0; j < vectorlength; j++) {
+ thisColumn.add((Serializable) engine.eval(this.variable + "["
+ + (i + 1) + "," + (j + 1) + "]"));
+ }
+ data.add(thisColumn);
+ }
+
+ }
+
+ /**
+ * Synchronization with R method. This method updates the content of the
+ * data.frame from R. All local changes are lost.
+ *
+ * @param engine
+ * a REngine where the R session is located.
+ * @throws RException
+ */
+ public void check(REngine engine) throws RException {
+ getFrom(variable, engine);
+ }
+
+ /**
+ * Send names changes to R
+ *
+ * @param engine
+ * @throws RException
+ */
+ private void assignNames(REngine engine) throws RException {
+ String vectorNames = "names(" + this.variable + ")<-c(";
+ for (int i = 0; i < names.size(); i++) {
+ vectorNames = vectorNames + "\"" + names.get(i) + "\"" + ",";
+ }
+ vectorNames = vectorNames.substring(0, vectorNames.length() - 1) + ")";
+ engine.voidEval(vectorNames);
+ }
+
+ /**
+ * Send row names changes to R
+ *
+ * @param engine
+ * a REngine where the R session is located.
+ * @throws RException
+ */
+ private void assignRowNames(REngine engine) throws RException {
+ String vectorRowNames = "row.names(" + this.variable + ")<-c(";
+ for (int i = 0; i < rowNames.size(); i++) {
+ vectorRowNames = vectorRowNames + "\"" + rowNames.get(i) + "\""
+ + ",";
+ }
+ vectorRowNames = vectorRowNames.substring(0,
+ vectorRowNames.length() - 1)
+ + ")";
+ engine.voidEval(vectorRowNames);
+ }
+
+ /**
+ * Send data changes to R
+ *
+ * @param engine
+ * a REngine where the R session is located.
+ * @throws RException
+ */
+ private void assignData(REngine engine) throws RException {
+ String dataframe = variable + "<-data.frame(";
+ for (int i = 0; i < data.size(); i++) {
+ dataframe = dataframe + "data" + i + ",";
+ String datai = "data" + i + "<-c(";
+ for (int j = 0; j < data.get(i).size(); j++) {
+ datai = datai + data.get(i).get(j) + ",";
+ }
+ datai = datai.substring(0, datai.length() - 1) + ")";
+ engine.voidEval(datai);
+
+ }
+ dataframe = dataframe.substring(0, dataframe.length() - 1) + ")";
+ engine.voidEval(dataframe);
+ }
+
+ //TODO method set attributes, get attributes
+
+ //TODO method add attribute, remove attribute
+
+ //TODO check data consistency.
+
+}
Added: lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java
===================================================================
--- lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java (rev 0)
+++ lutinj2r/trunk/src/test/java/org/codelutin/j2r/DataframeTest.java 2009-04-30 12:10:02 UTC (rev 70)
@@ -0,0 +1,158 @@
+/* *##% Lutin Java-2-R library
+ * Copyright (C) 2006 - 2008 CodeLutin
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%*/
+
+package org.codelutin.j2r;
+
+import java.io.Serializable;
+import java.util.Vector;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.codelutin.j2r.types.RDataFrame;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class DataframeTest {
+
+ private static Log log = LogFactory.getLog(DataframeTest.class);
+
+ private REngine engine;
+
+ @Before
+ public void setUp() throws Exception {
+ LutinTimer init = new LutinTimer();
+ init.startTiming();
+ if (engine == null) {
+ engine = new RProxy();
+ }
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ engine.terminate();
+ }
+
+ @Test
+ public void testDataFrameCreation() throws Exception {
+ //This test is also a test for all the setters...
+ //they work the same way and use the same internal methods.
+ Vector<String> names = new Vector<String>();
+ names.add("column1");
+ names.add("column2");
+
+ Vector<String> rowNames = new Vector<String>();
+ rowNames.add("row 1");
+ rowNames.add("row 2");
+ rowNames.add("row 3");
+
+ Vector<Double> column1 = new Vector<Double>();
+ column1.add(3.0);
+ column1.add(4.5);
+ column1.add(0.01);
+
+ Vector<Double> column2 = new Vector<Double>();
+ column2.add(1.0);
+ column2.add(5555555555555555555555.0);
+ column2.add(3.0);
+
+ Vector<Vector<? extends Serializable>> data = new Vector<Vector<? extends Serializable>>();
+ data.add(column1);
+ data.add(column2);
+
+ RDataFrame testDataFrame = new RDataFrame(names, rowNames, data,
+ engine, "test");
+ //Test data
+ Assert.assertEquals(new Double(3.0), (Double) engine.eval("test[1,1]"));
+ Assert.assertEquals(new Double(4.5), (Double) engine.eval("test[2,1]"));
+ Assert
+ .assertEquals(new Double(0.01), (Double) engine
+ .eval("test[3,1]"));
+ Assert.assertEquals(new Double(1.0), (Double) engine.eval("test[1,2]"));
+ Assert.assertEquals(new Double(5555555555555555555555.0),
+ (Double) engine.eval("test[2,2]"));
+ Assert.assertEquals(new Double(3.0), (Double) engine.eval("test[3,2]"));
+ //Test names
+ Assert.assertEquals("column1", (String) engine.eval("names(test)[1]"));
+ Assert.assertEquals("column2", (String) engine.eval("names(test)[2]"));
+ //Test row names
+ Assert
+ .assertEquals("row 1", (String) engine
+ .eval("row.names(test)[1]"));
+ Assert
+ .assertEquals("row 2", (String) engine
+ .eval("row.names(test)[2]"));
+ Assert
+ .assertEquals("row 3", (String) engine
+ .eval("row.names(test)[3]"));
+ }
+
+ @Test
+ public void testGetDataFrame() throws Exception {
+ //This test is also a test for all the setters...
+ //they work the same way and use the same internal methods.
+ Vector<String> names = new Vector<String>();
+ names.add("column1");
+ names.add("column2");
+
+ Vector<String> rowNames = new Vector<String>();
+ rowNames.add("row 1");
+ rowNames.add("row 2");
+ rowNames.add("row 3");
+
+ Vector<Double> column1 = new Vector<Double>();
+ column1.add(3.0);
+ column1.add(4.5);
+ column1.add(0.01);
+
+ Vector<Double> column2 = new Vector<Double>();
+ column2.add(1.0);
+ column2.add(5555555555555555555555.0);
+ column2.add(3.0);
+
+ Vector<Vector<? extends Serializable>> data = new Vector<Vector<? extends Serializable>>();
+ data.add(column1);
+ data.add(column2);
+
+ RDataFrame testDataFrame = new RDataFrame(names, rowNames, data,
+ engine, "test");
+ RDataFrame dataframe2 = new RDataFrame();
+ dataframe2.getFrom("test", engine);
+ //Test data
+ Assert.assertEquals(new Double(3.0), (Double) engine.eval("test[1,1]"));
+ Assert.assertEquals(new Double(4.5), (Double) engine.eval("test[2,1]"));
+ Assert
+ .assertEquals(new Double(0.01), (Double) engine
+ .eval("test[3,1]"));
+ Assert.assertEquals(new Double(1.0), (Double) engine.eval("test[1,2]"));
+ Assert.assertEquals(new Double(5555555555555555555555.0),
+ (Double) engine.eval("test[2,2]"));
+ Assert.assertEquals(new Double(3.0), (Double) engine.eval("test[3,2]"));
+ //Test names
+ Assert.assertEquals("column1", dataframe2.getNames().get(0));
+ Assert.assertEquals("column2", dataframe2.getNames().get(1));
+ //Test row names
+ Assert
+ .assertEquals("row 1", dataframe2.getRowNames().get(0));
+ Assert
+ .assertEquals("row 2", dataframe2.getRowNames().get(1));
+ Assert
+ .assertEquals("row 3", dataframe2.getRowNames().get(2));
+ }
+
+}
1
0
[LutinJ2R-commits] r69 - in lutinj2r/trunk/src: main/java/org/codelutin/j2r main/java/org/codelutin/j2r/jni main/java/org/codelutin/j2r/net test/java/org/codelutin/j2r
by jcouteau@users.labs.libre-entreprise.org 29 Apr '09
by jcouteau@users.labs.libre-entreprise.org 29 Apr '09
29 Apr '09
Author: jcouteau
Date: 2009-04-29 14:43:55 +0000 (Wed, 29 Apr 2009)
New Revision: 69
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 copy, move, list all objects and clear session capability
Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/REngine.java
===================================================================
--- lutinj2r/trunk/src/main/java/org/codelutin/j2r/REngine.java 2009-04-28 18:42:55 UTC (rev 68)
+++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/REngine.java 2009-04-29 14:43:55 UTC (rev 69)
@@ -30,6 +30,7 @@
package org.codelutin.j2r;
import java.io.File;
+import java.util.List;
/**
* Cette interface est le point commun entre les differentes technologies
@@ -37,114 +38,171 @@
*/
public interface REngine {
- /**
+ /**
* Effectue l'initialisation du moteur. Les parametres sont passes par les
* options de JVM, -D...
- * @return true/false pour indiquer si l'initialisation a pu se faire sans
- * encombre
- */
- public boolean init();
+ *
+ * @return true/false pour indiquer si l'initialisation a pu se faire sans
+ * encombre
+ */
+ public boolean init();
- /**
- * Delegue a R le traitement passe en parametre.
- * @param expr l'expression a evaluer
- * @return La valeur renvoyee par la commande
- * @throws RException
- */
- public Object eval(String expr) throws RException;
+ /**
+ * Delegue a R le traitement passe en parametre.
+ *
+ * @param expr
+ * l'expression a evaluer
+ * @return La valeur renvoyee par la commande
+ * @throws RException
+ */
+ public Object eval(String expr) throws RException;
/**
* Effectue un eval mais sans retour de resultat. Quand c'est possible cela
* permet donc de na pas consommer du temps de transfert et conversion de R
* a Java.
- * @param expr l'expression a evaluer
- * @throws RException
+ *
+ * @param expr
+ * l'expression a evaluer
+ * @throws RException
*/
public void voidEval(String expr) throws RException;
- /**
+ /**
* Met fin a l'utilisation de l'engine
- * @throws RException
- */
- public void terminate() throws RException;
-
- /**
+ *
+ * @throws RException
+ */
+ public void terminate() throws RException;
+
+ /**
* Load .RData file located in directory
*
- * @param directory directory where the .RData file is located
+ * @param directory
+ * directory where the .RData file is located
* @throws RException
*/
- public void loadRData(File directory) 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
+ * @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
+ *
+ * @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
+ * @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
+ * 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
+ * @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
+ * @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.
+ * 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
+ * @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
+ *
+ * @param rObject
+ * to be removed from the session
* @throws RException
*/
- public void remove (String rObject) throws RException;
+ public void remove(String rObject) throws RException;
+ /**
+ * Make a copy of an object and remove the old one.
+ *
+ * @param inputObject
+ * the object to be copied and deleted
+ * @param outputObject
+ * the object to be created
+ * @throws RException
+ */
+ public void mv(String inputObject, String outputObject) throws RException;
+
+ /**
+ * Copy an object.
+ *
+ * @param inputObject
+ * the object to be copied
+ * @param outputObject
+ * the object to be created
+ * @throws RException
+ */
+ public void cp(String inputObject, String outputObject) throws RException;
+
+ /**
+ * List all R object present in the actual session
+ *
+ * @return a list containing the name of all the R objects in the R session.
+ * @throws RException
+ */
+ public String[] ls() throws RException;
+
+ /**
+ * Remove all the objects present in the actual R session.
+ *
+ * @throws RException
+ */
+ public void clearSession() 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-28 18:42:55 UTC (rev 68)
+++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/RProxy.java 2009-04-29 14:43:55 UTC (rev 69)
@@ -41,21 +41,21 @@
* l'implatation choisie. La detection est basee sur l'option de demarrage de la
* JVM :
* <ul>
- * <li><code>-DR.type=net</code></li>
- * <li><code>-DR.type=jni</code></li>
- * <li><code>-DR.type=...</code></li>
+ * <li><code>-DR.type=net</code></li>
+ * <li><code>-DR.type=jni</code></li>
+ * <li><code>-DR.type=...</code></li>
* </ul>
* Il est possible de parametrer la solution network, pour cela, voir la
* documentation de {@link org.codelutin.j2r.net.RNetEngine}
*/
public class RProxy implements REngine {
- private Log log = LogFactory.getLog(RProxy.class);
+ private Log log = LogFactory.getLog(RProxy.class);
- private REngine engine;
+ private REngine engine;
public RProxy() {
- init();
+ init();
}
private boolean init(String RType) {
@@ -64,255 +64,312 @@
RType = "net";
}
if (RType.startsWith("net")) {
- initSucceded = initOnNet();
- if (!initSucceded) {
- if (log.isErrorEnabled()) {
- log.error("Initialization of R with Network failed, trying JNI");
- }
- initSucceded = initOnJNI();
- }
+ initSucceded = initOnNet();
+ if (!initSucceded) {
+ if (log.isErrorEnabled()) {
+ log
+ .error("Initialization of R with Network failed, trying JNI");
+ }
+ initSucceded = initOnJNI();
+ }
} else if ("jni".equalsIgnoreCase(RType)) {
- initSucceded = initOnJNI();
- if (!initSucceded) {
- if (log.isErrorEnabled()) {
- log.error("Initialization of R over JNI failed, trying with Network");
- }
- initSucceded = initOnNet();
- }
+ initSucceded = initOnJNI();
+ if (!initSucceded) {
+ if (log.isErrorEnabled()) {
+ log
+ .error("Initialization of R over JNI failed, trying with Network");
+ }
+ initSucceded = initOnNet();
+ }
} else if (log.isErrorEnabled()) {
- log.error("Invalid type specified : " + RType);
+ log.error("Invalid type specified : " + RType);
}
return initSucceded;
}
private boolean initOnJNI() {
- if (log.isInfoEnabled()) {
- log.info("Trying to initialize the R Proxy over JNI");
- }
- RJniEngine newEngine = new RJniEngine();
- if (newEngine.init()) {
- engine = newEngine;
- return true;
- }
- return false;
+ if (log.isInfoEnabled()) {
+ log.info("Trying to initialize the R Proxy over JNI");
+ }
+ RJniEngine newEngine = new RJniEngine();
+ if (newEngine.init()) {
+ engine = newEngine;
+ return true;
+ }
+ return false;
}
private boolean initOnNet() {
- if (log.isInfoEnabled()) {
- log.info("Trying to initialize the R Proxy with Network");
- }
- RNetEngine newEngine = new RNetEngine();
- if (newEngine.init()) {
- engine = newEngine;
- return true;
- }
- return false;
+ if (log.isInfoEnabled()) {
+ log.info("Trying to initialize the R Proxy with Network");
+ }
+ RNetEngine newEngine = new RNetEngine();
+ if (newEngine.init()) {
+ engine = newEngine;
+ return true;
+ }
+ return false;
}
- /* (non-Javadoc)
- * @see org.codelutin.R.REngine#eval(java.lang.String)
- */
- public Object eval(String expr) throws RException {
- if (engine == null) {
- log.fatal("The R Proxy is not initialized. An error probably " +
- "occured during the initialization.");
- return null;
- }
- return engine.eval(expr);
- }
+ /* (non-Javadoc)
+ * @see org.codelutin.R.REngine#eval(java.lang.String)
+ */
+ public Object eval(String expr) throws RException {
+ if (engine == null) {
+ log.fatal("The R Proxy is not initialized. An error probably "
+ + "occured during the initialization.");
+ return null;
+ }
+ return engine.eval(expr);
+ }
- /* (non-Javadoc)
- * @see org.codelutin.j2r.REngine#init()
- */
- public boolean init() {
+ /* (non-Javadoc)
+ * @see org.codelutin.j2r.REngine#init()
+ */
+ public boolean init() {
String RType = System.getProperty("R.type");
if (RType == null || "".equals(RType)) {
- log.warn("No R type specified.\nusage:\n\tjava -DR.type=net ...\n" +
- "or\n\tjava -DR.type=jni ...\n\nConsidering network type");
- RType = "net";
+ log
+ .warn("No R type specified.\nusage:\n\tjava -DR.type=net ...\n"
+ + "or\n\tjava -DR.type=jni ...\n\nConsidering network type");
+ RType = "net";
}
if (!init(RType)) {
- if (log.isFatalEnabled()) {
- log.fatal("The initializing of R failed.");
- }
- return false;
+ if (log.isFatalEnabled()) {
+ log.fatal("The initializing of R failed.");
+ }
+ return false;
}
- return true;
- }
+ return true;
+ }
- /* (non-Javadoc)
- * @see org.codelutin.j2r.REngine#terminate()
- */
- public void terminate() throws RException {
- if (engine == null) {
- log.fatal("The R Proxy is not initialized. An error probably " +
- "occured during the initialization.");
- } else {
- engine.terminate();
- }
- }
+ /* (non-Javadoc)
+ * @see org.codelutin.j2r.REngine#terminate()
+ */
+ public void terminate() throws RException {
+ if (engine == null) {
+ log.fatal("The R Proxy is not initialized. An error probably "
+ + "occured during the initialization.");
+ } else {
+ engine.terminate();
+ }
+ }
/* (non-Javadoc)
* @see org.codelutin.j2r.REngine#voidEval(java.lang.String)
*/
public void voidEval(String expr) throws RException {
if (engine == null) {
- log.fatal("The R Proxy is not initialized. An error probably " +
- "occured during the initialization.");
+ log.fatal("The R Proxy is not initialized. An error probably "
+ + "occured during the initialization.");
return;
}
engine.voidEval(expr);
}
-
+
/**
* Load .RData file located in directory
*
- * @param directory directory where the .RData file is located
+ * @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.");
+ 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
+ * @param directory
+ * where the .RData file will be saved
* @throws RException
*/
- public void saveRData(File directory) 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.");
+ 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
+ *
+ * @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.");
+ log.fatal("The R Proxy is not initialized. An error probably "
+ + "occured during the initialization.");
return;
}
- engine.voidEval("setwd(\""+directory.getAbsolutePath() + "\")");
+ 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
+ * @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.");
+ log.fatal("The R Proxy is not initialized. An error probably "
+ + "occured during the initialization.");
return null;
}
- String directory = (String)engine.eval("getwd()");
+ 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
+ * @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.");
+ 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
+ * 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
+ * @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.");
+ 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
+ * @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.");
+ 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()));
+ 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.
+ * 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
+ * @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.");
+ 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);
+ 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.");
+ log.fatal("The R Proxy is not initialized. An error probably "
+ + "occured during the initialization.");
return;
}
engine.remove(rObject);
}
+ public void mv(String inputObject, String outputObject) throws RException {
+ if (engine == null) {
+ log.fatal("The R Proxy is not initialized. An error probably "
+ + "occured during the initialization.");
+ return;
+ }
+ engine.mv(inputObject, outputObject);
+
+ }
+
+ public void cp(String inputObject, String outputObject) throws RException {
+ if (engine == null) {
+ log.fatal("The R Proxy is not initialized. An error probably "
+ + "occured during the initialization.");
+ return;
+ }
+ engine.cp(inputObject, outputObject);
+
+ }
+
+ public String[] ls() throws RException {
+ if (engine == null) {
+ log.fatal("The R Proxy is not initialized. An error probably "
+ + "occured during the initialization.");
+ return null;
+ }
+ return engine.ls();
+ }
+
+ public void clearSession() throws RException {
+ if (engine == null) {
+ log.fatal("The R Proxy is not initialized. An error probably "
+ + "occured during the initialization.");
+ return;
+ }
+ engine.clearSession();
+
+ }
+
} //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-28 18:42:55 UTC (rev 68)
+++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java 2009-04-29 14:43:55 UTC (rev 69)
@@ -78,7 +78,7 @@
public Object eval(String expr) throws RException {
REXP result = null;
try {
- log.info(expr);
+ log.debug(expr);
result = engine.eval(expr);
} catch (Exception eee) {
throw new RException("Unable to evaluate the R expression "
@@ -102,7 +102,7 @@
result = rexp.asString();
break;
case REXP.XT_INT:
- result = (Integer)rexp.asInt();
+ result = (Integer) rexp.asInt();
break;
case REXP.XT_ARRAY_INT:
result = rexp.asIntArray();
@@ -120,7 +120,6 @@
break;
case REXP.XT_BOOL:
result = rexp.asBool().isTRUE();
- log.info(result);
break;
case REXP.XT_DOUBLE:
result = rexp.asDoubleArray();
@@ -151,6 +150,9 @@
case REXP.XT_VECTOR:
result = rexp.asVector();
break;
+ case 34:
+ result = rexp.asStringArray();
+ break;
default:
log.error("Unknown return type [" + type + "] " + "on : "
+ rexp.toString());
@@ -179,7 +181,6 @@
// 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);
@@ -304,4 +305,28 @@
voidEval("remove(" + rObject + ")");
}
+ public void mv(String inputObject, String outputObject) throws RException {
+ cp(inputObject, outputObject);
+ remove(inputObject);
+
+ }
+
+ public void cp(String inputObject, String outputObject) throws RException {
+ voidEval(outputObject + "<-" + inputObject);
+
+ }
+
+ public String[] ls() throws RException {
+ String[] Robjects = (String[]) eval("ls()");
+ return Robjects;
+ }
+
+ public void clearSession() throws RException {
+ String[] Robjects = ls();
+ for (int i = 0; i < Robjects.length; i++) {
+ remove(Robjects[i]);
+ }
+
+ }
+
} // 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-28 18:42:55 UTC (rev 68)
+++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/net/RNetEngine.java 2009-04-29 14:43:55 UTC (rev 69)
@@ -30,6 +30,7 @@
package org.codelutin.j2r.net;
import java.io.File;
+import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -40,7 +41,6 @@
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
-
/**
* Cette classe represente le moteur reseau pour acceder a R. Par defaut, il
* essaye de se connecter a l'adresse 127.0.0.1 sur le port 6311. Cependant, il
@@ -53,252 +53,262 @@
*/
public class RNetEngine implements REngine {
- public static final int DEFAULT_PORT = 6311;
- public static final String DEFAULT_HOST = "127.0.0.1";
+ public static final int DEFAULT_PORT = 6311;
+ public static final String DEFAULT_HOST = "127.0.0.1";
- private Log log = LogFactory.getLog(RNetEngine.class);
+ private Log log = LogFactory.getLog(RNetEngine.class);
- private RConnection conn;
+ private RConnection conn;
- /*
- * (non-Javadoc)
- *
- * @see org.codelutin.j2r.REngine#init()
- */
- public boolean init() {
- String typeProp = System.getProperty("R.type", "net");
- int urlPos = typeProp.indexOf("net://");
- String host = null;
- String portAsString = null;
- if (urlPos != -1) {
- String url = typeProp.substring(urlPos + 6);
- int commaPos = url.indexOf(":");
- if (commaPos != -1) {
- host = url.substring(0, commaPos);
- portAsString = url.substring(commaPos + 1);
- } else {
- host = url;
- }
- }
- if (host == null || "".equals(host)) {
- host = DEFAULT_HOST;
- }
- int port = DEFAULT_PORT;
- if (portAsString != null) {
- try {
- port = Integer.parseInt(portAsString);
- } catch (NumberFormatException nfe) {
- if (log.isWarnEnabled()) {
- log.warn("Bad port format " + portAsString
- + ", using default" + " port : " + port);
- }
- }
- }
- return init(host, port);
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.codelutin.j2r.REngine#init()
+ */
+ public boolean init() {
+ String typeProp = System.getProperty("R.type", "net");
+ int urlPos = typeProp.indexOf("net://");
+ String host = null;
+ String portAsString = null;
+ if (urlPos != -1) {
+ String url = typeProp.substring(urlPos + 6);
+ int commaPos = url.indexOf(":");
+ if (commaPos != -1) {
+ host = url.substring(0, commaPos);
+ portAsString = url.substring(commaPos + 1);
+ } else {
+ host = url;
+ }
+ }
+ if (host == null || "".equals(host)) {
+ host = DEFAULT_HOST;
+ }
+ int port = DEFAULT_PORT;
+ if (portAsString != null) {
+ try {
+ port = Integer.parseInt(portAsString);
+ } catch (NumberFormatException nfe) {
+ if (log.isWarnEnabled()) {
+ log.warn("Bad port format " + portAsString
+ + ", using default" + " port : " + port);
+ }
+ }
+ }
+ return init(host, port);
+ }
- public boolean init(String host, int port) {
- if (log.isInfoEnabled()) {
- log.info("Trying to connect to the Rserve on '" + host + ":" + port
- + "'");
- }
- try {
- conn = new RConnection(host, port);
- } catch (RserveException e) {
- log.error("Unable to establish a connection to the R server. "
- + "Maybe you forgot to start it. "
- + "Try using the command \"R CMD Rserve\".");
- e.printStackTrace();
- return false;
- }
- return conn.isConnected();
- }
+ public boolean init(String host, int port) {
+ if (log.isInfoEnabled()) {
+ log.info("Trying to connect to the Rserve on '" + host + ":" + port
+ + "'");
+ }
+ try {
+ conn = new RConnection(host, port);
+ } catch (RserveException e) {
+ log.error("Unable to establish a connection to the R server. "
+ + "Maybe you forgot to start it. "
+ + "Try using the command \"R CMD Rserve\".");
+ e.printStackTrace();
+ return false;
+ }
+ return conn.isConnected();
+ }
- /*
- * (non-Javadoc)
- *
- * @see org.codelutin.R.REngine#eval(java.lang.String)
- */
- 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);
- }
- return convertResult(result);
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.codelutin.R.REngine#eval(java.lang.String)
+ */
+ public Object eval(String expr) throws RException {
+ REXP result = null;
+ try {
+ log.debug(expr);
+ result = conn.eval(expr);
+ } catch (RserveException e) {
+ throw new RException("An error occured during the eval method", e);
+ }
+ return convertResult(result);
+ }
- private Object convertResult(REXP rexp) {
- if (rexp == null) {
- log.debug("Null returned");
- return null;
- }
- if (log.isDebugEnabled()) {
- log.debug("Converting : " + rexp.toString());
- }
+ private Object convertResult(REXP rexp) {
+ if (rexp == null) {
+ log.debug("Null returned");
+ return null;
+ }
+ if (log.isDebugEnabled()) {
+ log.debug("Converting : " + rexp.toString());
+ }
- Object result = null;
+ Object result = null;
- try {
+ try {
- if (rexp.isInteger()) {
- result = rexp.asIntegers();
- int[] intarray = (int[]) result;
- if (intarray.length == 1) {
- result = intarray[0];
- }
- }
+ if (rexp.isInteger()) {
+ result = rexp.asIntegers();
+ int[] intarray = (int[]) result;
+ if (intarray.length == 1) {
+ result = intarray[0];
+ }
+ }
- else if (rexp.isFactor()) {
- result = rexp.asFactor();
- }
+ else if (rexp.isFactor()) {
+ result = rexp.asFactor();
+ }
- else if (rexp.isNumeric()) {
- result = rexp.asDoubles();
- double[] doublearray = (double[]) result;
- if (doublearray.length == 1) {
- result = doublearray[0];
- }
- }
-
- else if (rexp.isString()) {
- result = rexp.asStrings();
- String[] stringArray = (String[]) result;
- if (stringArray.length == 1) {
- result = stringArray[0];
- }
-
- }
-
- else if (rexp.isLogical()) {
- result = rexp.asStrings();
- 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[])stringArray;}
- }
-
- else if (rexp.isNull()) {
- return null;
- }
+ else if (rexp.isNumeric()) {
+ result = rexp.asDoubles();
+ double[] doublearray = (double[]) result;
+ if (doublearray.length == 1) {
+ result = doublearray[0];
+ }
+ }
- else {
- log.error("Unknown return type on : " + rexp.toString());
- }
- } catch (REXPMismatchException e) {
- e.printStackTrace();
- }
- return result;
- }
+ else if (rexp.isString()) {
+ result = rexp.asStrings();
+ String[] stringArray = (String[]) result;
+ if (stringArray.length == 1) {
+ result = stringArray[0];
+ }
- /*
- * (non-Javadoc)
- *
- * @see org.codelutin.j2r.REngine#terminate()
- */
- public void terminate() throws RException {
- if (conn != null && conn.isConnected()) {
- conn.close();
- }
- }
+ }
- /*
- * (non-Javadoc)
- *
- * @see org.codelutin.j2r.REngine#voidEval(java.lang.String)
- */
- 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);
- }
- }
-
- /**
+ else if (rexp.isLogical()) {
+ result = rexp.asStrings();
+ 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[]) stringArray;
+ }
+ }
+
+ else if (rexp.isNull()) {
+ return null;
+ }
+
+ else {
+ log.error("Unknown return type on : " + rexp.toString());
+ }
+ } catch (REXPMismatchException e) {
+ e.printStackTrace();
+ }
+ return result;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.codelutin.j2r.REngine#terminate()
+ */
+ public void terminate() throws RException {
+ if (conn != null && conn.isConnected()) {
+ conn.close();
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.codelutin.j2r.REngine#voidEval(java.lang.String)
+ */
+ public void voidEval(String expr) throws RException {
+ try {
+ 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
+ * @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
+ * @param directory
+ * where the .RData file will be saved
* @throws RException
*/
- public void saveRData(File directory) throws RException{
+ public void saveRData(File directory) throws RException {
setwd(directory);
voidEval("save.image()");
}
-
+
/**
* Set the R working directory
- * @param directory to set
+ *
+ * @param directory
+ * to set
* @throws RException
*/
public void setwd(File directory) throws RException {
- voidEval("setwd(\""+directory.getAbsolutePath() + "\")");
+ 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()");
+ 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
+ * @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
+ * 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
+ * @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
+ * @param rObject
+ * R object to save
+ * @param outputFileName
+ * the file to save
* @throws RException
*/
public void dput(String rObject, File outputFile) throws RException {
@@ -308,13 +318,15 @@
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.
+ * 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
+ * @param rObject
+ * name of the R object created
+ * @param inputFile
+ * file to load
* @throws RException
*/
public void dget(String rObject, File inputFile) throws RException {
@@ -322,11 +334,35 @@
setwd(inputFile.getParentFile());
String rInstruction = "%s <- dget(\"%s\")";
voidEval(String.format(rInstruction, rObject, inputFile.getName()));
- setwd(workingdir);
+ setwd(workingdir);
}
-
- public void remove (String rObject) throws RException {
- voidEval("remove("+rObject+")");
+
+ public void remove(String rObject) throws RException {
+ voidEval("remove(" + rObject + ")");
}
+ public void mv(String inputObject, String outputObject) throws RException {
+ cp(inputObject, outputObject);
+ remove(inputObject);
+
+ }
+
+ public void cp(String inputObject, String outputObject) throws RException {
+ voidEval(outputObject + "<-" + inputObject);
+
+ }
+
+ public String[] ls() throws RException {
+ String[] Robjects = (String[]) eval("ls()");
+ return Robjects;
+ }
+
+ public void clearSession() throws RException {
+ String[] Robjects = ls();
+ for (int i = 0; i < Robjects.length; i++) {
+ remove(Robjects[i]);
+ }
+
+ }
+
} // RNetEngine
Modified: lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java
===================================================================
--- lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java 2009-04-28 18:42:55 UTC (rev 68)
+++ lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java 2009-04-29 14:43:55 UTC (rev 69)
@@ -255,5 +255,31 @@
engine.remove("a");
Assert.assertNull(engine.eval("a"));
}
+
+ @Test
+ public void testMvCp() throws Exception {
+ //Test only mv as mv uses cp.
+ engine.voidEval("a<-5.0");
+ engine.remove("b");
+ engine.mv("a", "b");
+ Assert.assertEquals(new Double(5.0), engine.eval("b"));
+ Assert.assertFalse((Boolean) engine.eval("exists(\"a\")"));
+ }
+
+ @Test
+ public void testLsClearSession() throws Exception {
+ //Test only ClearSession as it uses ls
+ engine.voidEval("a<-5.0");
+ engine.voidEval("b<-5.0");
+ engine.voidEval("d<-5.0");
+ engine.voidEval("e<-5.0");
+ engine.voidEval("f<-5.0");
+ engine.clearSession();
+ Assert.assertFalse((Boolean) engine.eval("exists(\"a\")"));
+ Assert.assertFalse((Boolean) engine.eval("exists(\"b\")"));
+ Assert.assertFalse((Boolean) engine.eval("exists(\"d\")"));
+ Assert.assertFalse((Boolean) engine.eval("exists(\"e\")"));
+ Assert.assertFalse((Boolean) engine.eval("exists(\"f\")"));
+ }
} // JNITest
Modified: lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java
===================================================================
--- lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java 2009-04-28 18:42:55 UTC (rev 68)
+++ lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java 2009-04-29 14:43:55 UTC (rev 69)
@@ -49,129 +49,130 @@
public class NetTest {
- private static Log log = LogFactory.getLog(NetTest.class);
+ private static Log log = LogFactory.getLog(NetTest.class);
- private REngine engine;
- private String savedRType;
+ private REngine engine;
+ private String savedRType;
- @Before
- public void setUp() throws Exception {
- LutinTimer init = new LutinTimer();
- init.startTiming();
- savedRType = System.getProperty("R.type", "");
- System.setProperty("R.type", "net://:6311");
- if (engine == null) {
- engine = new RProxy();
- }
- if (log.isInfoEnabled()) {
- log.info("net init: " + init.endTiming() + "ms");
- }
- }
+ @Before
+ public void setUp() throws Exception {
+ LutinTimer init = new LutinTimer();
+ init.startTiming();
+ savedRType = System.getProperty("R.type", "");
+ System.setProperty("R.type", "net://:6311");
+ if (engine == null) {
+ engine = new RProxy();
+ }
+ if (log.isInfoEnabled()) {
+ log.info("net init: " + init.endTiming() + "ms");
+ }
+ }
- @After
- public void tearDown() throws Exception {
- engine.terminate();
- System.setProperty("R.type", savedRType);
- }
+ @After
+ public void tearDown() throws Exception {
+ engine.terminate();
+ System.setProperty("R.type", savedRType);
+ }
- @Test
- public void testDouble() throws Exception {
- Assert.assertEquals(5.0, engine.eval("5.0"));
- engine.voidEval("t<-sin(0)");
- double d = (Double) engine.eval("t");
- Assert.assertEquals(0.0, d, 0);
- }
+ @Test
+ public void testDouble() throws Exception {
+ Assert.assertEquals(5.0, engine.eval("5.0"));
+ engine.voidEval("t<-sin(0)");
+ double d = (Double) engine.eval("t");
+ Assert.assertEquals(0.0, d, 0);
+ }
- @Test
- public void testIntArray() throws Exception {
- Object result = engine.eval("5:10");
- Assert.assertNotNull(result);
- Assert.assertEquals(int[].class, result.getClass());
- int[] intArray = (int[]) result;
- Assert.assertEquals(6, intArray.length);
- for (int i = 5; i < 11; i++) {
- Assert.assertEquals(i, intArray[i - 5]);
- }
- }
+ @Test
+ public void testIntArray() throws Exception {
+ Object result = engine.eval("5:10");
+ Assert.assertNotNull(result);
+ Assert.assertEquals(int[].class, result.getClass());
+ int[] intArray = (int[]) result;
+ Assert.assertEquals(6, intArray.length);
+ for (int i = 5; i < 11; i++) {
+ Assert.assertEquals(i, intArray[i - 5]);
+ }
+ }
- @Test
- public void testSimpleOp() throws Exception {
- LutinTimer t = new LutinTimer();
- for (int loop = 0; loop < S_NB_LOOPS; loop++) {
- engine.voidEval("t<-0");
- t.startTiming();
- engine.voidEval(S_OP);
- t.endTiming();
- double d = (Double) engine.eval("t");
- Assert.assertEquals((double) S_T_MAX, d, 0);
- }
- double[] results = t.computeResults();
- System.err.println("[SO]min: " + results[0]);
- System.err.println("[SO]avg: " + results[1]);
- System.err.println("[SO]max: " + results[2]);
- System.err.println("[SO]etype: " + results[3]);
- }
+ @Test
+ public void testSimpleOp() throws Exception {
+ LutinTimer t = new LutinTimer();
+ for (int loop = 0; loop < S_NB_LOOPS; loop++) {
+ engine.voidEval("t<-0");
+ t.startTiming();
+ engine.voidEval(S_OP);
+ t.endTiming();
+ double d = (Double) engine.eval("t");
+ Assert.assertEquals((double) S_T_MAX, d, 0);
+ }
+ double[] results = t.computeResults();
+ System.err.println("[SO]min: " + results[0]);
+ System.err.println("[SO]avg: " + results[1]);
+ System.err.println("[SO]max: " + results[2]);
+ System.err.println("[SO]etype: " + results[3]);
+ }
- @Test
- public void testVector() throws Exception {
- LutinTimer t = new LutinTimer();
- for (int loop = 0; loop < V_NB_LOOPS; loop++) {
- t.startTiming();
- engine.voidEval(V_OP_A);
- engine.voidEval(V_OP_B);
- double[] r = (double[]) engine.eval(V_OP_AB);
- t.endTiming();
- Assert.assertEquals(V_MAX, r.length);
- }
- double[] results = t.computeResults();
- System.err.println("[V]min: " + results[0]);
- System.err.println("[V]avg: " + results[1]);
- System.err.println("[V]max: " + results[2]);
- System.err.println("[V]etype: " + results[3]);
- }
+ @Test
+ public void testVector() throws Exception {
+ LutinTimer t = new LutinTimer();
+ for (int loop = 0; loop < V_NB_LOOPS; loop++) {
+ t.startTiming();
+ engine.voidEval(V_OP_A);
+ engine.voidEval(V_OP_B);
+ double[] r = (double[]) engine.eval(V_OP_AB);
+ t.endTiming();
+ Assert.assertEquals(V_MAX, r.length);
+ }
+ double[] results = t.computeResults();
+ System.err.println("[V]min: " + results[0]);
+ System.err.println("[V]avg: " + results[1]);
+ System.err.println("[V]max: " + results[2]);
+ System.err.println("[V]etype: " + results[3]);
+ }
- @Test
- public void testString() throws Exception {
- engine.voidEval("a<-\"testing string\"");
- String testString = (String) engine.eval("a");
- Assert.assertEquals("testing string", testString);
- }
+ @Test
+ public void testString() throws Exception {
+ engine.voidEval("a<-\"testing string\"");
+ String testString = (String) engine.eval("a");
+ Assert.assertEquals("testing string", testString);
+ }
- @Test
- public void testInt() throws Exception {
- engine.voidEval("a<-as.integer(5)");
- Integer testInteger = (Integer) engine.eval("a");
- Integer toCompare = 5;
- Assert.assertEquals(toCompare, testInteger);
- }
+ @Test
+ public void testInt() throws Exception {
+ engine.voidEval("a<-as.integer(5)");
+ Integer testInteger = (Integer) engine.eval("a");
+ Integer toCompare = 5;
+ Assert.assertEquals(toCompare, testInteger);
+ }
- @Test
- public void testBool() throws Exception {
- engine.voidEval("a<-TRUE");
- engine.voidEval("b<-FALSE");
- Boolean testA = (Boolean) engine.eval("a");
- Boolean testB = (Boolean) engine.eval("b");
- Assert.assertTrue(testA);
- Assert.assertFalse(testB);
- }
+ @Test
+ public void testBool() throws Exception {
+ engine.voidEval("a<-TRUE");
+ engine.voidEval("b<-FALSE");
+ Boolean testA = (Boolean) engine.eval("a");
+ Boolean testB = (Boolean) engine.eval("b");
+ Assert.assertTrue(testA);
+ Assert.assertFalse(testB);
+ }
- @Test
- public void testArrayBool() throws Exception {
- engine.voidEval("a<-c(TRUE,FALSE,TRUE)");
- Boolean[] testBoolArray = (Boolean[]) engine.eval("a");
- Assert.assertTrue(testBoolArray[0]);
- Assert.assertFalse(testBoolArray[1]);
- Assert.assertTrue(testBoolArray[2]);
- }
-
- @Test
+ @Test
+ public void testArrayBool() throws Exception {
+ engine.voidEval("a<-c(TRUE,FALSE,TRUE)");
+ Boolean[] testBoolArray = (Boolean[]) engine.eval("a");
+ 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());
+ Assert.assertEquals(workingDirectory.getAbsolutePath(),
+ testWorkingDirectory.getAbsolutePath());
}
-
+
@Test
public void testRData() throws Exception {
File workingdir = new File("/tmp");
@@ -179,46 +180,73 @@
engine.saveRData(workingdir);
engine.remove("a");
engine.loadRData(workingdir);
- Double testDouble = (Double)engine.eval("a");
- Double compareTo = new Double (5.0);
+ 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);
+ Double testDouble = (Double) engine.eval("a");
+ Double compareTo = new Double(5.0);
Assert.assertEquals(compareTo, testDouble);
-
- workingdir= new File("/");
+
+ workingdir = new File("/");
//test method using absolute path
engine.setwd(workingdir);
engine.voidEval("a<-6.0");
- engine.dput("a",testingFile);
+ engine.dput("a", testingFile);
engine.remove("a");
engine.dget("a", testingFile);
- Double testDouble2 = (Double)engine.eval("a");
- Double compareTo2 = new Double (6.0);
+ 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());
+ 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"));
+ Assert.assertEquals(new Double(6.0), engine.eval("a"));
engine.remove("a");
- Assert.assertFalse((Boolean)engine.eval("exists(\"a\")"));
+ Assert.assertFalse((Boolean) engine.eval("exists(\"a\")"));
}
+
+ @Test
+ public void testMvCp() throws Exception {
+ //Test only mv as mv uses cp.
+ engine.voidEval("a<-5.0");
+ engine.remove("b");
+ engine.mv("a", "b");
+ Assert.assertEquals(new Double(5.0), engine.eval("b"));
+ Assert.assertFalse((Boolean) engine.eval("exists(\"a\")"));
+ }
+
+ @Test
+ public void testLsClearSession() throws Exception {
+ //Test only ClearSession as it uses ls
+ engine.voidEval("a<-5.0");
+ engine.voidEval("b<-5.0");
+ engine.voidEval("d<-5.0");
+ engine.voidEval("e<-5.0");
+ engine.voidEval("f<-5.0");
+ engine.clearSession();
+ Assert.assertFalse((Boolean) engine.eval("exists(\"a\")"));
+ Assert.assertFalse((Boolean) engine.eval("exists(\"b\")"));
+ Assert.assertFalse((Boolean) engine.eval("exists(\"d\")"));
+ Assert.assertFalse((Boolean) engine.eval("exists(\"e\")"));
+ Assert.assertFalse((Boolean) engine.eval("exists(\"f\")"));
+ }
} // NetTest
1
0
[LutinJ2R-commits] r68 - lutinj2r/trunk/src/test/java/org/codelutin/j2r
by jcouteau@users.labs.libre-entreprise.org 28 Apr '09
by jcouteau@users.labs.libre-entreprise.org 28 Apr '09
28 Apr '09
Author: jcouteau
Date: 2009-04-28 18:42:55 +0000 (Tue, 28 Apr 2009)
New Revision: 68
Modified:
lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java
Log:
Fixed integer test on Net engine
Modified: lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java
===================================================================
--- lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java 2009-04-28 16:25:41 UTC (rev 67)
+++ lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java 2009-04-28 18:42:55 UTC (rev 68)
@@ -137,13 +137,13 @@
Assert.assertEquals("testing string", testString);
}
- /*@Test
+ @Test
public void testInt() throws Exception {
- engine.voidEval("a<-5");
+ engine.voidEval("a<-as.integer(5)");
Integer testInteger = (Integer) engine.eval("a");
Integer toCompare = 5;
Assert.assertEquals(toCompare, testInteger);
- }*/
+ }
@Test
public void testBool() throws Exception {
1
0
[LutinJ2R-commits] r67 - in lutinj2r/trunk/src: main/java/org/codelutin/j2r/jni test/java/org/codelutin/j2r
by jcouteau@users.labs.libre-entreprise.org 28 Apr '09
by jcouteau@users.labs.libre-entreprise.org 28 Apr '09
28 Apr '09
Author: jcouteau
Date: 2009-04-28 16:25:41 +0000 (Tue, 28 Apr 2009)
New Revision: 67
Modified:
lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java
lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java
Log:
Adding Boolean management to JRIEngine
Fixed Integer tests
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-28 15:42:01 UTC (rev 66)
+++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java 2009-04-28 16:25:41 UTC (rev 67)
@@ -26,7 +26,6 @@
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;
-
/**
* RJniEngine.java
*
@@ -39,212 +38,240 @@
*/
public class RJniEngine implements REngine {
- private Log log = LogFactory.getLog(RJniEngine.class);
+ private Log log = LogFactory.getLog(RJniEngine.class);
- /**
- * Le Rengine est fait pour tourner en static
- */
- private static Rengine engine;
+ /**
+ * Le Rengine est fait pour tourner en static
+ */
+ private static Rengine engine;
- /*
- * (non-Javadoc)
- *
- * @see org.codelutin.j2r.REngine#init()
- */
- public boolean init() {
- if (engine == null) {
- try {
- String[] args = { "--no-save" };
- engine = new Rengine(args, false, null);
- if (!engine.waitForR()) {
- if (log.isErrorEnabled()) {
- log.error("Cannot load the R engine");
- }
- return false;
- }
- } catch (Throwable twable) {
- log.error("An error occured during R/JNI initialization.",
- twable);
- return false;
- }
- }
- return true;
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.codelutin.j2r.REngine#init()
+ */
+ public boolean init() {
+ if (engine == null) {
+ try {
+ String[] args = { "--no-save" };
+ engine = new Rengine(args, false, null);
+ if (!engine.waitForR()) {
+ if (log.isErrorEnabled()) {
+ log.error("Cannot load the R engine");
+ }
+ return false;
+ }
+ } catch (Throwable twable) {
+ log.error("An error occured during R/JNI initialization.",
+ twable);
+ return false;
+ }
+ }
+ return true;
+ }
- /*
- * (non-Javadoc)
- *
- * @see org.codelutin.R.REngine#eval(java.lang.String)
- */
- 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 "
- + "over JNI", eee);
- }
- return convertResult(result);
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.codelutin.R.REngine#eval(java.lang.String)
+ */
+ 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 "
+ + "over JNI", eee);
+ }
+ return convertResult(result);
+ }
- private Object convertResult(REXP rexp) {
- if (rexp == null) {
- log.debug("Null returned");
- return null;
- }
- if (log.isDebugEnabled()) {
- log.debug("Converting : " + rexp.toString());
- }
- int type = rexp.getType();
- Object result = null;
- switch (type) {
- case REXP.XT_STR:
- result = rexp.asString();
- break;
- /*case REXP.XT_INT:
+ private Object convertResult(REXP rexp) {
+ if (rexp == null) {
+ log.debug("Null returned");
+ return null;
+ }
+ if (log.isDebugEnabled()) {
+ log.debug("Converting : " + rexp.toString());
+ }
+ int type = rexp.getType();
+ Object result = null;
+ switch (type) {
+ case REXP.XT_STR:
+ result = rexp.asString();
+ break;
+ case REXP.XT_INT:
result = (Integer)rexp.asInt();
- break;*/
- case REXP.XT_ARRAY_INT:
+ break;
+ case REXP.XT_ARRAY_INT:
result = rexp.asIntArray();
int[] intarray = (int[]) result;
if (intarray.length == 1) {
- result = (Integer)intarray[0];
+ result = (Integer) intarray[0];
}
break;
- case REXP.XT_ARRAY_DOUBLE:
- result = rexp.asDoubleArray();
- double[] doublearray = (double[]) result;
- if (doublearray.length == 1) {
- result = doublearray[0];
- }
- break;
- case REXP.XT_BOOL:
- result = rexp.asBool().isTRUE();
- break;
- case REXP.XT_DOUBLE:
- result = rexp.asDoubleArray();
- double[] doublearray2 = (double[]) result;
- result = doublearray2[0];
- /*case REXP.XT_NULL:
- result = null;*/
- /*case REXP.XT_ARRAY_BOOL:
- result = rexp.asIntArray();
- boolean[] booleanarray = (boolean[]) result;
- result = booleanarray;*/
- case REXP.XT_VECTOR:
- result = rexp.asVector();
- default:
- log.error("Unknown return type [" + type + "] " + "on : "
- + rexp.toString());
- }
- return result;
- }
+ case REXP.XT_ARRAY_DOUBLE:
+ result = rexp.asDoubleArray();
+ double[] doublearray = (double[]) result;
+ if (doublearray.length == 1) {
+ result = doublearray[0];
+ }
+ break;
+ case REXP.XT_BOOL:
+ result = rexp.asBool().isTRUE();
+ log.info(result);
+ break;
+ case REXP.XT_DOUBLE:
+ result = rexp.asDoubleArray();
+ double[] doublearray2 = (double[]) result;
+ result = doublearray2[0];
+ break;
+ case REXP.XT_NULL:
+ result = null;
+ break;
+ case 37:
+ result = rexp.asIntArray();
+ int[] integers = ((int[]) result);
+ Boolean[] booleanArray = new Boolean[integers.length];
+ for (int i = 0; i < integers.length; i++) {
+ if (integers[i] == 1) {
+ booleanArray[i] = Boolean.TRUE;
+ } else {
+ booleanArray[i] = Boolean.FALSE;
+ }
+ }
+ if (booleanArray.length == 1) {
+ result = booleanArray[0];
+ } else {
+ result = booleanArray;
+ }
- /*
- * (non-Javadoc)
- *
- * @see org.codelutin.j2r.REngine#terminate()
- */
- public void terminate() {
- if (engine.isAlive()) {
- engine.end();
- }
- }
+ break;
+ case REXP.XT_VECTOR:
+ result = rexp.asVector();
+ break;
+ default:
+ log.error("Unknown return type [" + type + "] " + "on : "
+ + rexp.toString());
+ break;
+ }
+ return result;
+ }
- /*
- * (non-Javadoc)
- *
- * @see org.codelutin.j2r.REngine#voidEval(java.lang.String)
- */
- public void voidEval(String expr) throws RException {
- // 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);
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.codelutin.j2r.REngine#terminate()
+ */
+ public void terminate() {
+ if (engine.isAlive()) {
+ engine.end();
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.codelutin.j2r.REngine#voidEval(java.lang.String)
+ */
+ public void voidEval(String expr) throws RException {
+ // 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);
+ }
+
+ /**
+ * 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
- */
-
+ }
+
+ /**
+ * 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()");
+ 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
+ * @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
+ * 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
+ * @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
+ * @param rObject
+ * R object to save
+ * @param outputFileName
+ * the file to save
* @throws RException
*/
public void dput(String rObject, File outputFile) throws RException {
@@ -254,13 +281,15 @@
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.
+ * 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
+ * @param rObject
+ * name of the R object created
+ * @param inputFile
+ * file to load
* @throws RException
*/
public void dget(String rObject, File inputFile) throws RException {
@@ -268,11 +297,11 @@
setwd(inputFile.getParentFile());
String rInstruction = "%s <- dget(\"%s\")";
voidEval(String.format(rInstruction, rObject, inputFile.getName()));
- setwd(workingdir);
+ setwd(workingdir);
}
-
- public void remove (String rObject) throws RException {
- voidEval("remove("+rObject+")");
+
+ public void remove(String rObject) throws RException {
+ voidEval("remove(" + rObject + ")");
}
} // RJniEngine
Modified: lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java
===================================================================
--- lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java 2009-04-28 15:42:01 UTC (rev 66)
+++ lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java 2009-04-28 16:25:41 UTC (rev 67)
@@ -84,17 +84,6 @@
}
@Test
- public void testIntArray() throws Exception {
- Object result = engine.eval("5:10");
- Assert.assertEquals(int[].class, result.getClass());
- int[] intArray = (int[]) result;
- Assert.assertEquals(6, intArray.length);
- for (int i = 5; i < 11; i++) {
- Assert.assertEquals(i, intArray[i - 5]);
- }
- }
-
- @Test
public void testDoubleArray() throws Exception {
Object result = engine.eval("5.5:10.5");
Assert.assertEquals(double[].class, result.getClass());
@@ -150,15 +139,27 @@
Assert.assertEquals("testing string", testString);
}
- /*@Test
+ @Test
public void testInt() throws Exception {
- engine.voidEval("a<-5");
+ engine.voidEval("a<-as.integer(5)");
Integer testInteger = (Integer) engine.eval("a");
Integer toCompare = 5;
Assert.assertEquals(toCompare, testInteger);
- }*/
+ }
+
+ @Test
+ public void testIntArray() throws Exception {
+ engine.voidEval("a<-as.integer(5)");
+ engine.voidEval("b<-as.integer(6)");
+ engine.voidEval("z<-c(a,b)");
+ int[] testIntArray = (int[]) engine.eval("z");
+ int toCompare1 = 5;
+ Assert.assertEquals(toCompare1, testIntArray[0]);
+ int toCompare2 = 6;
+ Assert.assertEquals(toCompare2, testIntArray[1]);
+ }
- /* @Test
+ @Test
public void testBool() throws Exception {
engine.voidEval("a<-TRUE");
engine.voidEval("b<-FALSE");
@@ -166,16 +167,16 @@
Boolean testB = (Boolean) engine.eval("b");
Assert.assertTrue(testA);
Assert.assertFalse(testB);
- }*/
+ }
- /* @Test
+ @Test
public void testArrayBool() throws Exception {
engine.voidEval("a<-c(TRUE,FALSE,TRUE)");
Boolean[] testBoolArray = (Boolean[]) engine.eval("a");
Assert.assertTrue(testBoolArray[0]);
Assert.assertFalse(testBoolArray[1]);
Assert.assertTrue(testBoolArray[2]);
- }*/
+ }
/*@Test
public void testDataFrame() throws Exception {
1
0