从C#/ .NET返回数据表到R

时间:2014-04-06 23:23:20

标签: c# .net r datatable rclr

我试图从R调用.NET DLL文件并返回数据表。

这个Q&A显示了如何从R调用.NET,但是我可以找到有关如何返回数据的任何文档(除了像字符串这样的简单值):

.NET方法:

public DataTable GetDatatable(string parameters)
{
    var dt = new DataTable();
    dt.Columns.Add("Dates", typeof(DateTime));
    dt.Columns.Add("Strings", typeof(String));
    dt.Columns.Add("Number", typeof(Double));

    DataRow dr = dt.NewRow();
    dr[0] = new DateTime(2014, 1, 23);
    dr[1] = "test";
    dr[2] = 123.45;

    dt.Rows.Add(dr);
    return dt;
}

里面的R:

> library(rClr)
> clrLoadAssembly('C:/Dev/Sandbox/XYZ/bin/Debug/XYZ2R.dll')
> myObj <- clrNew('XYZ2R.DAL,XYZ2R')
> res = clrCall(myObj,'GetDatatable', "parameter")
> res

班级的对象&#34; cobjRef&#34;
Slot&#34; clrobj&#34;:
&lt; pointer:0x0027f218&gt;
 
Slot&#34; clrtype&#34;:
[1]&#34; System.Data.DataTable&#34;
 

  

res [0,1]
  res [0,1]中的错误:类型&#39; S4&#39;的对象不是子集的

From what I've read看来我需要传递参考资料。对参数的论证,例如:

public void Test(string parameters, params List<string>[] data)

虽然我不确定,但是没有任何从R调用.NET方法和返回列表的例子。有一些文章讨论了相反的情况。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

使用当前发布的软件包并使用您的测试方法,以下内容正确检索项目

library(rClr)
clrLoadAssembly('c:/tmp/DataTableTest/bin/Debug/DataTableTest.dll')
obj <- clrNew('DataTableTest.Class1')
dataTable <- clrCall(obj, 'GetDatatable', '')
clrReflect(dataTable)
dv <- clrGet(dataTable, 'DefaultView')
clrReflect(dv)
getTableItem <- function(view, rowInd, colInd) {
  dataRowView <- clrCall(view, 'get_Item', as.integer(rowInd))
  clrCall(dataRowView, 'get_Item', as.integer(colInd))
}
getTableItem(dv, 0L, 0L)
getTableItem(dv, 0L, 1L)
getTableItem(dv, 0L, 2L)

在接下来的几个月里,我将进行一些语法改进;我注意到DataTable和相关类是数据帧的自然兄弟,并将尝试为它提供更简洁的语法。请随时在 https://rclr.codeplex.com https://github.com/jmp75/rClr

中记录详细功能请求

答案 1 :(得分:1)

使用clrReflect方法从R检查.NET对象的方法和属性很简单:

<强>&GT; clrReflect(dataTable中)
$方法
  [1]&#34; AcceptChanges&#34; &#34; add_ColumnChanged&#34; &#34; add_ColumnChanging&#34; &#34; add_Disposed&#34; &#34; add_Initialized&#34; &#34; add_RowChanged&#34;
  [7]&#34; add_RowChanging&#34; &#34; add_RowDeleted&#34; &#34; add_RowDeleting&#34; &#34; add_TableCleared&#34; &#34; add_TableClearing&#34; &#34; add_TableNewRow&#34;
 [13]&#34; BeginInit&#34; &#34; BeginLoadData&#34; &#34;清除&#34; &#34;克隆&#34; &#34;计算&#34; &#34;复印&#34;
 [19]&#34; CreateDataReader&#34; &#34;处置&#34; &#34; EndInit&#34; &#34; EndLoadData&#34; &#34;的Equals&#34; &#34; get_CaseSensitive&#34;
 [25]&#34; get_ChildRelations&#34; &#34; get_Columns&#34; &#34; get_Constraints&#34; &#34; get_Container&#34; &#34; get_DataSet&#34; &#34; get_DefaultView&#34;
 [31]&#34; get_DesignMode&#34; &#34; get_DisplayExpression&#34; &#34; get_ExtendedProperties&#34; &#34; get_HasErrors&#34; &#34; get_IsInitialized&#34; &#34; get_Locale&#34;
 [37]&#34; get_MinimumCapacity&#34; &#34; get_Namespace&#34; &#34; get_ParentRelations&#34; &#34; get_Prefix&#34; &#34; get_PrimaryKey&#34; &#34; get_RemotingFormat&#34;
 [43]&#34; get_Rows&#34; &#34; get_Site&#34; &#34; get_TableName&#34; &#34; GetChanges&#34; &#34; GetChanges&#34; &#34; GetErrors&#34;
 [49]&#34; GetHashCode&#34; &#34; GetObjectData使用&#34; &#34; GetService的&#34; &#34;的GetType&#34; &#34; ImportRow&#34; &#34;装载&#34;
 [55]&#34;加载&#34; &#34;装载&#34; &#34; LoadDataRow&#34; &#34; LoadDataRow&#34; &#34;合并&#34; &#34;合并&#34;
 [61]&#34;合并&#34; &#34; NEWROW&#34; &#34;的ReadXml&#34; &#34;的ReadXml&#34; &#34;的ReadXml&#34; &#34;的ReadXml&#34;
 [67]&#34; ReadXmlSchema&#34; &#34; ReadXmlSchema&#34; &#34; ReadXmlSchema&#34; &#34; ReadXmlSchema&#34; &#34; RejectChanges&#34; &#34; remove_ColumnChanged&#34;
 [73]&#34; remove_ColumnChanging&#34; &#34; remove_Disposed&#34; &#34; remove_Initialized&#34; &#34; remove_RowChanged&#34; &#34; remove_RowChanging&#34; &#34; remove_RowDeleted&#34;
 [79]&#34; remove_RowDeleting&#34; &#34; remove_TableCleared&#34; &#34; remove_TableClearing&#34; &#34; remove_TableNewRow&#34; &#34;复位&#34; &#34;选择&#34;
 [85]&#34;选择&#34; &#34;选择&#34; &#34;选择&#34; &#34; set_CaseSensitive&#34; &#34; set_DisplayExpression&#34; &#34; set_Locale&#34;
 [91]&#34; set_MinimumCapacity&#34; &#34; set_Namespace&#34; &#34; set_Prefix&#34; &#34; set_PrimaryKey&#34; &#34; set_RemotingFormat&#34; &#34; set_Site&#34;
 [97]&#34; set_TableName&#34; &#34;的ToString&#34; &#34;&中WriteXML#34; &#34;&中WriteXML#34; &#34;&中WriteXML#34; &#34;&中WriteXML#34;
[103]&#34; WriteXml&#34; &#34;&中WriteXML#34; &#34;&中WriteXML#34; &#34;&中WriteXML#34; &#34;&中WriteXML#34; &#34;&中WriteXML#34;
[109]&#34; WriteXml&#34; &#34;&中WriteXML#34; &#34;&中WriteXML#34; &#34;&中WriteXML#34; &#34;&中WriteXML#34; &#34;&中WriteXML#34;
[115]&#34; WriteXmlSchema&#34; &#34; WriteXmlSchema&#34; &#34; WriteXmlSchema&#34; &#34; WriteXmlSchema&#34; &#34; WriteXmlSchema&#34; &#34; WriteXmlSchema&#34;
[121]&#34; WriteXmlSchema&#34; &#34; WriteXmlSchema&#34;

$字段
character(0)

$属性
 [1]&#34; CaseSensitive&#34; &#34; ChildRelations&#34; &#34;列&#34; &#34;约束&#34; &#34;集装箱&#34; &#34;数据集&#34; &#34;默认视图&#34; &#34;的designMode&#34;
 [9]&#34; DisplayExpression&#34; &#34; ExtendedProperties&#34; &#34; HasErrors&#34; &#34;将IsInitialized&#34; &#34;区域设置&#34; &#34; MinimumCapacity&#34; &#34;命名空间&#34; &#34; ParentRelations&#34;
[17]&#34;前缀&#34; &#34;的PrimaryKey&#34; &#34; RemotingFormat&#34; &#34;行&#34; &#34;网站&#34; &#34;表名&#34;

<强>&GT; clrReflect(DV)
$方法
 [1]&#34; add_Disposed&#34; &#34; add_Initialized&#34; &#34; add_ListChanged&#34; &#34;的AddNew&#34; &#34; BeginInit在&#34; &#34; CopyTo从&#34; &#34;删除&#34; &#34;处置&#34;
 [9]&#34; EndInit&#34; &#34;的Equals&#34; &#34;的Equals&#34; &#34;查找与#34; &#34;查找与#34; &#34; FindRows&#34; &#34; FindRows&#34; &#34; get_AllowDelete&#34;
[17]&#34; get_AllowEdit&#34; &#34; get_AllowNew&#34; &#34; get_ApplyDefaultSort&#34; &#34; get_Container&#34; &#34; get_Count&#34; &#34; get_DataViewManager&#34; &#34; get_DesignMode&#34; &#34; get_IsInitialized&#34;
[25]&#34; get_Item&#34; &#34; get_RowFilter&#34; &#34; get_RowStateFilter&#34; &#34; get_Site&#34; &#34; get_Sort&#34; &#34; get_Table&#34; &#34;的GetEnumerator&#34; &#34;的GetHashCode&#34;
[33]&#34; GetService&#34; &#34;的GetType&#34; &#34; remove_Disposed&#34; &#34; remove_Initialized&#34; &#34; remove_ListChanged&#34; &#34; set_AllowDelete&#34; &#34; set_AllowEdit&#34; &#34; set_AllowNew&#34;
[41]&#34; set_ApplyDefaultSort&#34; &#34; set_RowFilter&#34; &#34; set_RowStateFilter&#34; &#34; set_Site&#34; &#34; set_Sort&#34; &#34; set_Table&#34; &#34;的ToString&#34; &#34; ToTable&#34;
[49]&#34; ToTable&#34; &#34; ToTable&#34; &#34; ToTable&#34;

$字段
character(0)

$属性
 [1]&#34; AllowDelete&#34; &#34; AllowEdit&#34; &#34; AllowNew&#34; &#34; ApplyDefaultSort&#34; &#34;集装箱&#34; &#34;计数&#34; &#34;的DataViewManager&#34; &#34;的designMode&#34; &#34;将IsInitialized&#34; &#34;项目&#34; &#34;的RowFilter&#34; &#34; RowStateFilter&#34; &#34;网站&#34; &#34;分类&#34; &#34;表&#34;

  

要调用方法,请使用clrCall(dotNetObject,&#39; MethodName&#39;)

     

要查看属性,请使用clrGet(dotNetObject,&#39; PropertyName&#39;)

     

例如,让我们算出返回的DataTable中的行数   来自GetDatatable方法:

<强>&GT; clrCall(DV,&#39; get_Count&#39)
[1] 2

  

现在让我们计算出列数:

<强>&GT; clrGet(dataTable中,&#39;列&#39)
上课的对象&#34; cobjRef&#34;
Slot&#34; clrobj&#34;:
  

Slot&#34; clrtype&#34;:
[1]&#34; System.Data.DataColumnCollection&#34;

<强>&GT; cols&lt; - clrGet(dataTable,&#39; Columns&#39;)
&GT; clrReflect(COLS)
$方法
 [1]&#34;添加&#34; &#34;添加&#34; &#34;添加&#34; &#34;添加&#34; &#34;添加&#34; &#34; add_CollectionChanged&#34;
 [7]&#34; AddRange&#34; &#34; CanRemove&#34; &#34;清除&#34; &#34;包含&#34; &#34; CopyTo从&#34; &#34; CopyTo从&#34;
[13]&#34;等于&#34; &#34; get_Count&#34; &#34; get_IsReadOnly&#34; &#34; get_IsSynchronized&#34; &#34; get_Item&#34; &#34; get_Item&#34;
[19]&#34; get_SyncRoot&#34; &#34;的GetEnumerator&#34; &#34;的GetHashCode&#34; &#34;的GetType&#34; &#34;的IndexOf&#34; &#34;的IndexOf&#34;
[25]&#34;删除&#34; &#34;删除&#34; &#34; remove_CollectionChanged&#34; &#34; RemoveAt移除&#34; &#34;的ToString&#34;

$字段
character(0)

$属性
[1]&#34;伯爵&#34; &#34; IsReadOnly&#34; &#34; IsSynchronized&#34; &#34;项目&#34; &#34;项目&#34; &#34; SyncRoot上&#34;

<强>&GT; clrGet(COLS,&#39;计数&#39)
[1] 26