Author: jcouteau Date: 2010-04-19 11:28:53 +0200 (Mon, 19 Apr 2010) New Revision: 201 Log: Add a toString method to RDataFrame to represent it as in R. (under a table form with column and row names). Modified: trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java Modified: trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java 2010-04-16 18:12:42 UTC (rev 200) +++ trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java 2010-04-19 09:28:53 UTC (rev 201) @@ -57,8 +57,6 @@ //Vector containing the vectors of the data.frame private List<List<?>> data; - //TODO JC 20100415 Create a toString method that displays the DataFrame - /** * Constructor * @@ -720,7 +718,7 @@ //get the first line of the file BufferedReader br = new BufferedReader(new FileReader(inputFile)); tmp = br.readLine(); - String[] splitted = tmp.split("\\;"); + String[] splitted = tmp.split(";"); //get the data size (number of columns) if (rowNames) { @@ -762,7 +760,7 @@ while ((tmp = br.readLine()) != null) { //parse each line - splitted = tmp.split("\\;"); + splitted = tmp.split(";"); //to determine the data index on the line. int index = 0; @@ -814,20 +812,10 @@ br.close(); this.data = tempData; - if (log.isDebugEnabled()){ //Display the imported data.frame in debug mode - - log.debug("Imported DataFrame : "); - - for(List<?> column:this.data){ - String debugLine="Column " + this.data.indexOf(column)+ " : "; - for(Object obj:column){ - debugLine += obj + " ,"; - } - log.debug(debugLine); - } + log.debug("Imported DataFrame : \n"+toString()); } } @@ -866,4 +854,124 @@ return (new int[]{x,y}); } + + /** + * Method that returns a string representing the DataFrame. The output + * String should looks like the dataframe in R. + * @return a string representing the dataframe as in R. + */ + @Override + public String toString(){ + + List<String> linesToDisplay = new ArrayList<String>(); + + Boolean displayRowNames = !rowNames.isEmpty(); + + Boolean displayNames = !names.isEmpty(); + + Integer numberOfLines = 0; + + //Calculate the number of lines to display + if (!this.data.isEmpty()) { + if (!this.data.get(0).isEmpty()){ + numberOfLines = this.data.get(0).size(); + } + } + if (displayNames){ + numberOfLines += 1; + } + + // fill the lines with empty strings + for (int i=0;i<numberOfLines;i++){ + linesToDisplay.add(i,""); + } + + // Add the row names if necessary + if (displayRowNames){ + + // calculate the column size + int columnSize = 0; + for (String name:rowNames){ + if (name.length()>columnSize){ + columnSize = name.length(); + } + } + columnSize +=2; + + // add the row name to each line, complete with spaces to have next + // column aligned + if (displayNames) { + String line = linesToDisplay.get(0); + String str = String.format("%-"+columnSize+"s", ""); + line +=str ; + linesToDisplay.set(0, line); + } + for (int i=0;i<rowNames.size();i++){ + String str = String.format("%-"+columnSize+"s", rowNames.get(i)); + + if(displayNames) { + String line = linesToDisplay.get(i+1); + line += str; + linesToDisplay.set(i+1, line); + } else { + String line = linesToDisplay.get(i); + line += str; + linesToDisplay.set(i, line); + } + + } + + } + + // for each column, calculate the column size, add the value to each + // line, complete with spaces to have next column aligned + for (List<?> column:this.data){ + + String columnName=""; + + // calculate the column size + int columnSize = 0; + if(displayNames){ + columnName = names.get(this.data.indexOf(column)); + columnSize = columnName.length(); + } + for (Object obj:column){ + String toString = obj.toString(); + if (toString.length()>columnSize){ + columnSize = toString.length(); + } + } + columnSize +=2; + + if(displayNames){ + String str = String.format("%-"+columnSize+"s", columnName); + String line = linesToDisplay.get(0); + line += str; + linesToDisplay.set(0, line); + } + + for (int i=0;i<column.size();i++){ + + String str = String.format("%-"+columnSize+"s", column.get(i)); + + if(displayNames) { + String line = linesToDisplay.get(i+1); + line += str; + linesToDisplay.set(i+1, line); + } else { + String line = linesToDisplay.get(i); + line += str; + linesToDisplay.set(i, line); + } + } + } + + String returnString = ""; + + for (String str:linesToDisplay){ + returnString+= str +"\n"; + } + + return returnString; + } }
participants (1)
-
jcouteau@users.nuiton.org