private string convertDataToR(GraphROptions gro, Sides side)
{
string rD = ""; //rDataString
string bD = "data <- cbind("; //bindDataString
string colNamesD = "colnames(data) <- c("; //colNamesDataString
string sepSerie = "";
string xyFirstFound = "";
int count = 0; //this counts accepted series
int countCols=0;
int countRows=0; //on multisession, names of persons come in rows. Use this to discard some rows if unselected on treeview (! markedRows)
foreach(GraphSerie serie in GraphSeries) {
LogB.Information("serie:" + serie.Title);
if(
side == Sides.LEFT && ! serie.IsLeftAxis ||
side == Sides.RIGHT && serie.IsLeftAxis) {
continue;
}
//don't plot AVG row on multisession
if(sessions.Count > 1 && serie.Title == Catalog.GetString("AVG"))
continue;
//on multisession, names of persons come in rows. Use this to discard some rows if unselected on treeview (! markedRows)
if(sessions.Count > 1 && ! acceptCheckedData(countRows)) {
countRows ++;
continue;
}
//on XY only take two vars
if(gro.Type == Constants.GraphTypeXY) {
LogB.Information("groVarX: " + gro.VarX + " groVarY: " + gro.VarY + " tit: " + serie.Title);
if(gro.VarX != serie.Title && gro.VarY != serie.Title)
continue;
else if (xyFirstFound == "") {
if(gro.VarX == serie.Title)
xyFirstFound = "x";
else
xyFirstFound = "y";
}
}
//Histogram and Dotchart plot col 1
if( (gro.Type == Constants.GraphTypeHistogram || gro.Type == Constants.GraphTypeDotchart)
&& gro.VarX != serie.Title)
continue;
rD += "serie" + count.ToString() + " <- c(";
string sep = "";
countCols=0;
foreach(string val in serie.SerieData) {
LogB.Information(" val:" + val);
bool use = true;
//on simplesession, cols are persons. See if they are discarded on markedRows
if(sessions.Count == 1 && ! acceptCheckedData(countCols))
use = false;
//don't plot AVG col on multisession
if(sessions.Count > 1 && countCols == serie.SerieData.Count)
use = false;
//don't plot SD col on multisession
if(sessions.Count > 1 && countCols +1 == serie.SerieData.Count)
use = false;
countCols++;
if(! use)
continue;
if(val == "-")
rD += sep + "NA";
else
rD += sep + Util.ConvertToPoint(val);
sep = ", ";
}
rD += ")\n";
bD += sepSerie + "serie" + count.ToString();
colNamesD += sepSerie + "'" + Util.RemoveTilde(serie.Title) + "'";
sepSerie = ", ";
count ++;
countRows ++;
}
string rowNamesD = "rownames(data) <- c("; //rowNamesDataString
//create rows
string sep2 = "";
for(int i=0; i < CurrentGraphData.XAxisNames.Count; i++) {
//on simplesession, cols are persons. See if they are discarded on markedRows
if(sessions.Count == 1 && ! acceptCheckedData(i))
continue;
string name = Util.RemoveTilde(CurrentGraphData.XAxisNames[i].ToString());
//convert accents to Unicode in order to be plotted correctly on R windows
if(UtilAll.IsWindows())
name = Util.ConvertToUnicode(name);
rowNamesD += sep2 + "'" + name + "'";
//each four values add a \n to not have a "long line" problem in sending data to R
if((i+1) % 4 == 0)
rowNamesD += "\n";
sep2 = ", ";
}
bD += ")\n";
colNamesD += ")\n";
rowNamesD += ")\n";
if(gro.Type == Constants.GraphTypeXY) {
if(gro.VarX == gro.VarY) {
//if it's an XY with only one serie, (both selected vars are the same
rD += rD.Replace("serie0", "serie1"); //duplicate rD changing serie name
bD = "data <- cbind(serie0, serie1)\n";
//have two colNamesD equal to first
string [] cn = colNamesD.Split(new char[] {'\''});
colNamesD = "colnames(data) <- c('" + cn[1] + "', '" + cn[1] + "')\n";
} else if (xyFirstFound == "y") {
//if we first found the y value change serie0 to serie1
rD = rD.Replace("serie1", "serie2");
rD = rD.Replace("serie0", "serie1");
rD = rD.Replace("serie2", "serie0");
}
}
string allData = rD + bD + colNamesD + rowNamesD + "data\n";
if(gro.Transposed &&
gro.Type != Constants.GraphTypeXY &&
gro.Type != Constants.GraphTypeDotchart &&
gro.Type != Constants.GraphTypeBoxplot &&
gro.Type != Constants.GraphTypeHistogram &&
gro.Type != Constants.GraphTypeStripchart
)
allData += "data <- t(data)\n";
return allData;
}