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
May 2009
- 3 participants
- 21 discussions
[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