Calling .Net/C# from R

Calling .NET/C# from R

Another option that readers of this discussion might consider is the rClr package, which I have been working on for a couple of years to access arbitrary .NET code from R. It is a sibling of R.NET which, conversely, is a way to access R from .NET.

To give a flavour of the rClr package, the canonical "Hello World" looks like:

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

Feedback and suggestions welcome via the web site.

A simple Example for Calling a .NET object from R

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"

Had to do Little more research. Found a tutorial here.

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

Which helped me get my results:

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

Calling user defined functions inside a script in R.Net

What you describe is just fine and should work (good post by the way, thank you). I do not understand why userDefinedFunctionOne is not found, since it should have been loaded in the global R environment.

I've added a bit of code in one of the sample applications I use:
SourceRCode

The code derived from your post ran just fine, I could see the expected data frame from visual studio in debug mode. Can you give it a try and report if you have the issue or not with this sample application?

The sample application uses the latest NuGet package (1.6.5 as of writing), but
it looks like you are using a recent version of R.NET given the methods, and I to not expect version to the be cause of the issue.

is there a way to call R functions from C# and retrieve the result in C#

Given there is a COM interface to R, you can use C#'s COM interop to control it.

See this article for more details: http://www.codeproject.com/KB/cs/RtoCSharp.aspx



Related Topics



Leave a reply



Submit