Passing DataTable over COM into R

懵懂的女人 提交于 2019-12-10 20:47:20

问题


I am trying to pass data from SQL, to C#, then to an R server for data analysis then back to my web application; however, the COM interface that I am using does not allow complex data types to be passed between C# and R (no data tables). I have gotten it to work in the past by using the following code:

    int count = dataTable.Rows.Count;
    object[] y = new object[count];
    object[] x = new object[count];

    //R does not accept DataTables, so here we extract the data from
    //the table and pass it into 2 double arrays.
    for (int i = 0; i < count; i++)
    {
        y[i] = Convert.ToDouble(dataTable.Rows[i][0]);
        x[i] = Convert.ToDouble(dataTable.Rows[i][1]);
    }
    //Instantiate R connection
    StatConnector r = new STATCONNECTORSRVLib.StatConnectorClass();
    r.Init("R");
    r.SetSymbol("y", y);  //Passes column y into R
    r.SetSymbol("x", x);  //Passes column x into R

My problem now arises because I am no longer limited to just doubles, anything coming out of the SQL Database is fair game (int, varchar, etc...), and that I am no longer calling just 2 columns of data (it can be however many the user specifies).

How can I convert a datatable of dynamic size and dynamic data types into an array that would be safe to pass over into rcom?


回答1:


In Professional Visual Basic 6.0 Business Objects by Rockford Lhotka (http://search.barnesandnoble.com/Professional-Visual-Basic-60-Business-Objects/Rockford-Lhotka/e/9781861001078), he makes various statements that the most effective data transfer structure between COM Interfaces across application boundaries is the String. I do not know if this is true, but I accept his qualifications. Therefore, I believe Biff MaGriff suggestion would be the a good simple implementation to your problem.




回答2:


I would use CSV. Of course, I know nothing about RCOM :s Good luck!

public static string DataTableToCSV(DataTable myTable)
{
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < myTable.Columns.Count; i++)
    {
        sb.Append("\"");
        sb.Append(myTable.Columns[i].ColumnName);
        sb.Append("\"");
        if (i < myTable.Columns.Count - 1)
            sb.Append(",");
    }
    sb.AppendLine();
    foreach (DataRow dr in myTable.Rows)
    {
        for (int i = 0; i < dr.ItemArray.Length; i++)
        {
            sb.Append("\"");
            sb.Append(dr.ItemArray[i].ToString());
            sb.Append("\"");
            if (i < dr.ItemArray.Length - 1)
                sb.Append(",");
        }
        sb.AppendLine();
    }
    return sb.ToString();
}


来源:https://stackoverflow.com/questions/3145302/passing-datatable-over-com-into-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!