从R调用.NET对象的简单示例

时间:2015-10-26 07:07:14

标签: .net r rclr

我是从

尝试这个例子的

Calling .NET/C# from R

但是当我使用以下代码时会抛出错误

library(rClr)
clrLoadAssembly('c:/path/to/myassembly.dll')
myObj <- clrNew('MyNamespace.MyClass,MyAssemblyName')
clrCall(myObj, 'SayHelloWorld')

以下列方式:

library(rClr) 
clrLoadAssembly('C:\\__\\TstingRLib\\ClassLibrary1\\bin\\Release\\ClassLibrary.dll') 

myObj <- clrNew('System.System.Text,System')
 Type not found: System.System.Text,System Error in clrNew("System.System.Text,System") : Type: System.ArgumentException Message: Could not determine Type from string 'System.System.Text,System' Method: System.Object CreateInstance(System.String, System.Object[]) Stack trace: at Rclr.ClrFacade.CreateInstance(String typename, Object[] arguments) in ___\AppData\Local\Temp\Rtmp2D63Nz\R.INSTALL1f3c2a50350\rClr\src\ClrFacade\ClrFacad‌​e.cs:line 316

如何摆脱这个错误?

1 个答案:

答案 0 :(得分:2)

clrLoadAssembly('-----\\Documents\\visual studio 2013\\Projects\\DataConversionSample\\DataConversionSample\\bin\\Release\\DataConversionSample.dll')
> typename = clrGetTypesInAssembly('DataConversionSample')
> typename = "rClr.Samples.DataConversionSample,DataConversionSample"
> obj = clrNew(typename)
> obj
An object of class "cobjRef"
Slot "clrobj":
<pointer: 0x00000000181cb140>

Slot "clrtype":
[1] "rClr.Samples.DataConversionSample,DataConversionSample"

不得不做更多的研究。在这里找到了一个教程。

https://searchcode.com/codesearch/view/28480847/

这帮助我获得了结果:

str(clrCallStatic(typename, "GetOneDimStringArray", as.integer(5)))
 chr [1:5] "0" "1" "2" "3" "4"


DateTime to string


clrCallStatic(typename, "DateTimeToString", as.Date('2015-10-26')
+ )
[1] "26/10/15 12:00:00 AM"


Numeric Vector


> str(clrCallStatic(typename, "GetOneDimArray", as.integer(5)))
 num [1:5] 0 1.1 2.2 3.3 4.4



Rectangular Array

clrCallStatic(typename, "GetRectArray", as.integer(3), as.integer(5))
     [,1] [,2] [,3] [,4] [,5]
[1,]  0.0 0.22 0.44 0.66 0.88
[2,]  1.1 1.32 1.54 1.76 1.98
[3,]  2.2 2.42 2.64 2.86 3.08