private void RDotNetExecute(OutputWindow ow)
{
CommandOutput lst = new CommandOutput();
lst.IsFromSyntaxEditor = true;
engine.Evaluate("BSky_One_Way_Anova = as.data.frame (summary(dd <- aov(mpg ~ year,data=Dataset1))[[1]])");
engine.Evaluate("bskyfrmtobj <- BSkyFormat(BSky_One_Way_Anova)");
CharacterMatrix cmatrix = engine.Evaluate("bskyfrmtobj").AsCharacterMatrix();
string[,] mtx = new string[cmatrix.RowCount, cmatrix.ColumnCount];
for (int r = 0; r < cmatrix.RowCount; r++)
{
for (int c = 0; c < cmatrix.ColumnCount; c++)
{
mtx[r, c] = cmatrix[r, c];
}
}
string objectname = "bskyfrmtobj";
string headername = "This is generated in R.NET";
CommandRequest cmddf = new CommandRequest();
int dimrow = 1, dimcol = 1;
bool rowexists = false, colexists = false;
string dataclassname = string.Empty;
//Find class of data passed. data.frame, matrix, or array
cmddf.CommandSyntax = "class(" + objectname + ")"; // Row exists
object retres = engine.Evaluate(cmddf.CommandSyntax).AsCharacter()[0];
if (retres != null)
dataclassname = retres.ToString();
//find if dimension exists
cmddf.CommandSyntax = "!is.na(dim(" + objectname + ")[1])"; // Row exists
rowexists = engine.Evaluate(cmddf.CommandSyntax).AsLogical()[0];
cmddf.CommandSyntax = "!is.na(dim(" + objectname + ")[2])";// Col exists
colexists = engine.Evaluate(cmddf.CommandSyntax).AsLogical()[0];
/// Find size of matrix(objectname) & initialize data array ///
if (rowexists)
{
cmddf.CommandSyntax = "dim(" + objectname + ")[1]";
retres = engine.Evaluate(cmddf.CommandSyntax).AsInteger()[0];
if (retres != null)
dimrow = Int16.Parse(retres.ToString());
}
if (colexists)
{
cmddf.CommandSyntax = "dim(" + objectname + ")[2]";
retres = engine.Evaluate(cmddf.CommandSyntax).AsInteger()[0];
if (retres != null)
dimcol = Int16.Parse(retres.ToString());
}
string[,] data = new string[dimrow, dimcol];
//// now create FlexGrid and add to lst ///
/////////finding Col headers /////
cmddf.CommandSyntax = "colnames(" + objectname + ")";
CharacterVector colhdrobj = engine.Evaluate(cmddf.CommandSyntax).AsCharacter();
string[] colheaders;
if (colhdrobj != null && !colhdrobj.ToString().Contains("Error"))
{
if (true)//colhdrobj.GetType().IsArray)
{
int siz = colhdrobj.Count();
colheaders = new string[siz];
for (int ri = 0; ri < siz; ri++)
{
colheaders[ri] = colhdrobj[ri];
}
}
else
{
colheaders = new string[1];
colheaders[0] = colhdrobj.ToString();
}
}
else
{
colheaders = new string[dimcol];
for (int i = 0; i < dimcol; i++)
colheaders[i] = (i + 1).ToString();
}
/////////finding Row headers /////
//read configuration and then decide to pull row headers
bool shownumrowheaders = true; ///
cmddf.CommandSyntax = "rownames(" + objectname + ")";
CharacterVector rowhdrobj = engine.Evaluate(cmddf.CommandSyntax).AsCharacter();
string[] rowheaders;// = (string[])rowhdrobj;//{ "11", "22", "33" };
if (rowhdrobj != null && !rowhdrobj.ToString().Contains("Error"))
{
if (true)//rowhdrobj.GetType().IsArray)
{
int siz = rowhdrobj.Count();
rowheaders = new string[siz];
for (int ri = 0; ri < siz; ri++)
{
rowheaders[ri] = rowhdrobj[ri];
}
//rowheaders = (string[])rowhdrobj;//{ "Aa", "Bb", "Cc" };//
}
else
{
rowheaders = new string[1];
rowheaders[0] = rowhdrobj.ToString();
}
}
else
{
rowheaders = new string[dimrow];
//Type 1.//filling number for row header if rowheader is not present
for (int i = 0; i < dimrow; i++)
rowheaders[i] = (i + 1).ToString();
}
bool isnumericrowheaders = true; // assuming that row headers are numeric
short tnum;
for (int i = 0; i < dimrow; i++)
{
if (!Int16.TryParse(rowheaders[i], out tnum))
{
isnumericrowheaders = false; //row headers are non-numeric
break;
}
//if (i == 10)//just cheking first 10 numbers for being int. Not cheking all the row headers.
// break;
}
if (isnumericrowheaders && !shownumrowheaders)
{
//Type 2.//filling empty values for row header if rowheader is not present
for (int i = 0; i < dimrow; i++)
rowheaders[i] = "";
}
/// Populating array using data frame data
bool isRowEmpty = true;//for Virtual.
int emptyRowCount = 0;//for Virtual.
List<int> emptyRowIndexes = new List<int>(); //for Virtual.
string cellData = string.Empty;
for (int r = 1; r <= dimrow; r++)
{
isRowEmpty = true;//for Virtual.
for (int c = 1; c <= dimcol; c++)
{
if (dimcol == 1 && !dataclassname.ToLower().Equals("data.frame"))
cmddf.CommandSyntax = "as.character(" + objectname + "[" + r + "])";
else
cmddf.CommandSyntax = "as.character(" + objectname + "[" + r + "," + c + "])";
object v = engine.Evaluate(cmddf.CommandSyntax).AsCharacter()[0];
cellData = (v != null) ? v.ToString().Trim() : "";
data[r - 1, c - 1] = cellData;// v.ToString().Trim();
//for Virtual. // cell is non-empty in row, means row is non empty because of atleast 1 col
if (cellData.Length > 0)
isRowEmpty = false;
}
//for Virtual. // counting empty rows for virtual
if (isRowEmpty)
{
emptyRowCount++;
emptyRowIndexes.Add(r - 1);//making it zero based as in above nested 'for'
}
}
// whether you want C1Flexgrid to be generated by using XML DOM or by Virtual class(Dynamic)
bool DOMmethod = false;
if (DOMmethod)
{
//12Aug2014 Old way of creating grid using DOM and then creating and filling grid step by step
XmlDocument xdoc = createFlexGridXmlDoc(colheaders, rowheaders, data);
//string xdoc = "<html><body><table><thead><tr><th class=\"h\"></th><th class=\"c\">A</th><th class=\"c\">B</th></tr></thead><tbody><tr><td class=\"h\">X</td><td class=\"c\">5</td><td class=\"c\">6</td></tr><tr><td class=\"h\">Y</td><td class=\"c\">8</td><td class=\"c\">9</td></tr></tbody></table></body></html>";
createFlexGrid(xdoc, lst, headername);// headername = 'varname' else 'leftvarname' else 'objclass'
}
else//virutal list method
{
//There is no logic to remove empty rows in this vitual list method so
//here we try to send data with non-empty rows by dropping empty ones first.
if (emptyRowCount > 0)
{
int nonemptyrowcount = dimrow - emptyRowCount;
string[,] nonemptyrowsdata = new string[nonemptyrowcount, dimcol];
string[] nonemptyrowheaders = new string[nonemptyrowcount];
for (int rr = 0, rrr = 0; rr < data.GetLength(0); rr++)
{
if (emptyRowIndexes.Contains(rr))//skip empty rows.
continue;
for (int cc = 0; cc < data.GetLength(1); cc++)
{
nonemptyrowsdata[rrr, cc] = data[rr, cc];//only copy non-empty rows
}
nonemptyrowheaders[rrr] = rowheaders[rr];//copying row headers too.
rrr++;
}
//Using Dynamic Class creation and then populating the grid. //12Aug2014
CreateDynamicClassFlexGrid(headername, colheaders, nonemptyrowheaders, nonemptyrowsdata, lst);
}
else
{
//Using Dynamic Class creation and then populating the grid. //12Aug2014
CreateDynamicClassFlexGrid(headername, colheaders, rowheaders, data, lst);
}
}
if (ow != null)//22May2014
SendToOutput("", ref lst, ow);//send dataframe/matrix/array to output window or disk file
}