14

I'd like to use an API from R that is only available in .NET. Is there a standard method that can be used to call .NET C# code from R? If so, how can I do so?

0

2 Answers 2

14

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.

4
  • 2
    Wow - downloaded it and it worked absolutely perfectly!! This package is simply amazing, I look forward to using it a lot in the future. Keep up the good work!
    – Contango
    Oct 1, 2013 at 22:01
  • Does anyone have a simple specific example of above code?
    – Artiga
    Oct 26, 2015 at 6:24
  • library(rClr) clrLoadAssembly('C:\\__\\TstingRLib\\ClassLibrary1\\bin\\Release\\ClassLibrary1.dll') yObj <- 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\ClrFacade.cs:line 316
    – Artiga
    Oct 26, 2015 at 6:32
  • suggestion for R > 4.0 ?
    – Salix
    Dec 19, 2021 at 17:10
3

Exposing the .NET dll as COM dll and then calling a COM object in the dll from R seem to be the only way. And there is a package for it: http://cran.r-project.org/web/packages/rcom/rcom.pdf

If you cannot make a COM dll because it's third-party dll, you can always create a new interface-like .NET dll with COM interface where you can call actual dll.

1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.