Tuesday, August 14, 2012

Calling R From C# .NET (CSharp DotNet)

As mentioned in a previous post about Calling R From VB6 VBA, I've been using 7Bit's very useful utility to call R from VB6. It is so useful that I needed to have the same capability in C#. So I took my VB6 interface and translated it to C#.

Originally the mt4R.dll tool was created to interface to R from Metatrader. Knowing that this is a windows DLL, therefore mt4R can be accessed from a number of languages, including VB6 and now C#, if a suitable interface is used.

A simple class interface was born in C# that allows .NET users to access R via calls to the mt4R dll. I'm about to embark on a project that will use this newly minted interface in C#.

I've been using this for some time in VB6 but as I'm moving more of my development to C#, and I've decided it was time to share with everyone else. So I put together a simple demo application that shows how to access mt4R.dll in a simple C# Windows form application.

You can download Calling R From CSharp DotNet. (Please follow the instructions on that page) for use in your own projects.

C# / .NET developers don't need to worry about transposing matrices to get them to work in R. R and C# arrays are set up the same way:

A[row, column]

Please let me know if you have any comments, questions or problems getting this code to work.

Calling R From VB6 VBA

I've been using 7Bit's very useful utility to call R from VB6. Originally the mt4R.dll tool was created to interface to R from Metatrader. Knowing that this is a windows DLL, therefore mt4R can be accessed from a number of languages, including VB6, and C# if a suitable interface is used.

To that end I translated the existing Metatrader interface to a simple drop-in VB6 class that may be accessed in Visual Studio, or from VBA from Excel. This handy interface in conjunction with 7Bit's dll allows a VB6 / VBA developer to access the power of the R project directly from code.

I've been using this for some time but decided it was time to share with everyone else. So I put together a simple demo application that shows how to access mt4R.dll in a simple VB6 form application.

You can download Calling R From VB6 VBA. (Please follow the instructions on that page) for use in your own projects.

A note for VB6 / VBA developers:

The array structure of R and VB is not the same. Whereas in R the arrays are arranged as:

A[row, column]

In VB6 / VBA the arrays are declared as follows

Dim A(column, row) as double

When transferring this matrix via Rm (the matrix operation) it will send the data in the correct format if the array in VB is declared as above.

Please let me know if you have any comments, questions or problems getting this code to work.