Author: jcouteau Date: 2010-04-21 11:59:36 +0200 (Wed, 21 Apr 2010) New Revision: 209 Log: Add toString method to RList Simplify set(x,y) methods for RDataFrame Modified: trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java trunk/src/main/java/org/nuiton/j2r/types/RList.java Modified: trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java 2010-04-21 07:56:37 UTC (rev 208) +++ trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java 2010-04-21 09:59:36 UTC (rev 209) @@ -417,20 +417,41 @@ * if no variable name has been given to the data.frame or the * data type is not allowed at this location. */ - public void set(int x, int y, Object data) throws RException { + public void set(int x, int y, Double data) throws RException { checkVariable(); checkX(x); checkY(y); try { - if (this.data.get(x).get(y) instanceof Double) { - ((ArrayList<Double>) this.data.get(x)).set(y, (Double) data); + if (this.data.get(x).get(y) instanceof Double){ + //set the value in the data.frame + ((ArrayList<Double>) this.data.get(x)).set(y, data); + + //send the value to REngine engine.voidEval(String.format(RInstructions.SET_DATAFRAME_ITEM, - this.variable, y + 1, - x + 1, - data)); - } else if (this.data.get(x).get(y) instanceof Boolean) { - ((ArrayList<Boolean>) this.data.get(x)).set(y, (Boolean) data); - if ((Boolean) data) { + this.variable, y + 1, x + 1, data)); + } else { + throw new RException( + "The data.frame does not accept this type on those coordinates : " + + data.getClass()); + } + } catch (ClassCastException eee) { + throw new RException( + "The data.frame does not accept this type on those coordinates : " + + data.getClass(), eee); + } + } + + public void set(int x, int y, Boolean data) throws RException { + checkVariable(); + checkX(x); + checkY(y); + try { + if (this.data.get(x).get(y) instanceof Boolean){ + //set the value in the data.frame + ((ArrayList<Boolean>) this.data.get(x)).set(y, data); + + //send the value to REngine + if (data) { engine.voidEval(String.format( RInstructions.SET_DATAFRAME_ITEM, this.variable, y + 1, x + 1, RInstructions.TRUE)); @@ -439,26 +460,65 @@ RInstructions.SET_DATAFRAME_ITEM, this.variable, y + 1, x + 1, RInstructions.FALSE)); } - } else if (this.data.get(x).get(y) instanceof String) { - ((ArrayList<String>) this.data.get(x)).set(y, (String) data); + } else { + throw new RException( + "The data.frame does not accept this type on those coordinates : " + + data.getClass()); + } + } catch (ClassCastException eee) { + throw new RException( + "The data.frame does not accept this type on those coordinates : " + + data.getClass(), eee); + } + } + + public void set(int x, int y, String data) throws RException { + checkVariable(); + checkX(x); + checkY(y); + try { + if (this.data.get(x).get(y) instanceof String){ + //set the value in the data.frame + ((ArrayList<String>) this.data.get(x)).set(y, data); + + //send the value to REngine engine.voidEval(String.format(RInstructions.SET_DATAFRAME_ITEM, this.variable, y + 1, x + 1, "\"" + data + "\"")); - } else if (this.data.get(x).get(y) instanceof Integer) { - ((ArrayList<Integer>) this.data.get(x)).set(y, (Integer) data); + } else { + throw new RException( + "The data.frame does not accept this type on those coordinates : " + + data.getClass()); + } + } catch (ClassCastException eee) { + throw new RException( + "The data.frame does not accept this type on those coordinates : " + + data.getClass(), eee); + } + } + + public void set(int x, int y, Integer data) throws RException { + checkVariable(); + checkX(x); + checkY(y); + try { + if (this.data.get(x).get(y) instanceof Integer){ + //set the value in the data.frame + ((ArrayList<Integer>) this.data.get(x)).set(y, data); + + //send the value to REngine engine.voidEval(String.format(RInstructions.SET_DATAFRAME_ITEM, this.variable, y + 1, x + 1, String.format( - RInstructions.AS_INTEGER, data))); + RInstructions.AS_INTEGER, data))); } else { - throw new ArrayStoreException( + throw new RException( "The data.frame does not accept this type on those coordinates : " + - data.getClass()); + data.getClass()); } - } catch (ClassCastException cce) { + } catch (ClassCastException eee) { throw new RException( "The data.frame does not accept this type on those coordinates : " + - data.getClass(), cce); + data.getClass(), eee); } - } /** @@ -899,10 +959,8 @@ Integer numberOfLines = 0; //Calculate the number of lines to display - if (!this.data.isEmpty()) { - if (!this.data.get(0).isEmpty()){ + if ((!this.data.isEmpty()) &&(!this.data.get(0).isEmpty())){ numberOfLines = this.data.get(0).size(); - } } if (displayNames){ numberOfLines += 1; Modified: trunk/src/main/java/org/nuiton/j2r/types/RList.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/types/RList.java 2010-04-21 07:56:37 UTC (rev 208) +++ trunk/src/main/java/org/nuiton/j2r/types/RList.java 2010-04-21 09:59:36 UTC (rev 209) @@ -141,25 +141,7 @@ String returnString = this.variable + "<-list("; if ((this.data != null) && (!(this.data.isEmpty()))) { for (int i = 0; i < data.size(); i++) { - if (!(this.names.isEmpty())) { - returnString += this.names.get(i) + "="; - } - if (data.get(i) instanceof String) { - returnString += "\"" + data.get(i) + "\","; - } else if ((data.get(i) instanceof Boolean) && - ((Boolean) data.get(i))) { - returnString += RInstructions.TRUE + ","; - } else if ((data.get(i) instanceof Boolean) && - (!(Boolean) data.get(i))) { - returnString += RInstructions.FALSE + ","; - } else if (data.get(i) instanceof Integer) { - returnString += String.format(RInstructions.AS_INTEGER, - data.get(i)) + ","; - } else if (data.get(i) instanceof REXP) { - returnString += ((REXP) (data.get(i))).toRString() + ","; - } else { - returnString += data.get(i) + ","; - } + returnString += toRString(i); } returnString = returnString.substring(0, returnString.length() - 1); } @@ -173,6 +155,38 @@ } /** + * Create the R instruction for element at index i + * @param i index + * @return the corresponding R instruction + * @throws RException if an error occur creating R instruction for REXPs + */ + private String toRString(int i) throws RException { + String returnString=""; + + Object obj = data.get(i); + + if (!(this.names.isEmpty())) { + returnString += this.names.get(i) + "="; + } + + if (obj instanceof String) { + returnString += "\"" + obj + "\","; + } else if ((obj instanceof Boolean) && ((Boolean) obj)) { + returnString += RInstructions.TRUE + ","; + } else if ((obj instanceof Boolean) && (!(Boolean)obj)) { + returnString += RInstructions.FALSE + ","; + } else if (obj instanceof Integer) { + returnString += String.format(RInstructions.AS_INTEGER, obj) + ","; + } else if (obj instanceof REXP) { + returnString += ((REXP)obj).toRString() + ","; + } else { + returnString += obj + ","; + } + + return returnString; + } + + /** * Method to set the xth object of the list to a value. * * @param x