5

Is it possible to call a user-defined (custom) R function from within C#?

For example a simple matrix multiplication function written in R:

matrix_mult = function(a, b) {
c = a %*% b;
return c;
}

How can I call this R function matrix_mult(a,b) from c#?

3
  • Here is a link to the R manual section on C intefacing with R. Using P/Invoke it should be reasonably easy to apply corresponding calls from C# instead. Commented Feb 9, 2013 at 15:18
  • Can you give us more information about the context and reasons on why you want to call R form c#? Commented Feb 9, 2013 at 16:10
  • @agstudy in order to use a matrix function written in R with the Function Router construct in .net finaquant.com/function-router/2802 Commented Feb 9, 2013 at 16:40

2 Answers 2

8

After some research I've found an answer myself.

1) Open an existing or new project in MS Visual Studio.

2) Install R.NET (NuGet) http://rdotnet.codeplex.com

Installation is easy: Menu: Visual Studio (2012) > Library Package Manager > Package Manager Console type "Install-Package R.NET"

3) Initialize a function in R and call it from C# See http://rdotnet.codeplex.com/documentation for data types in R

using RDotNet;

class Program
{
    static void Main(string[] args)
{
    // Set the folder in which R.dll locates.
    var envPath = Environment.GetEnvironmentVariable("PATH");

    // check the version and path on your computer
    var rBinPath = @"C:\Program Files\R\R-2.14.1\bin\x64";

    Environment.SetEnvironmentVariable("PATH", envPath + System.IO.Path.PathSeparator + rBinPath);

    using (REngine engine = REngine.CreateInstance("RDotNet"))
    {
        // Initializes settings.
        engine.Initialize();

        // create an R function
        // R style
        // See: http://rdotnet.codeplex.com/wikipage?title=Examples&referringTitle=Home

        Function matrix_mult = engine.Evaluate(@"matrix_mult <- function(a,b){ 
        c = a %*% b;
        return(c);
        }").AsFunction();

        NumericMatrix d = engine.Evaluate(@"d <- matrix_mult(a,b)").AsNumericMatrix();

        Console.WriteLine("Matrix d:");
        engine.Evaluate("print(d)");

        // convert NumericMatrix of R to double[,] of .net
        double[,] darr = new double[d.RowCount, d.ColumnCount];
        d.CopyTo(darr, d.RowCount, d.ColumnCount);

        Console.ReadKey();
    }
}
}
Sign up to request clarification or add additional context in comments.

3 Comments

How can the matrices a and b be passed to the R function?
If someone wants to define some input in c# and then pass it to R, how one can do that? I wanted to pass a vector which is stored in some variable, same variable I was passing to the R input vector in the double quotes but variable not found error appeared. Is there any way to do that?
In order to see how array parameters are passed from C# to R you may visit my article "Table data as input to estimation functions in R" at finaquant.com/table-data-as-input-to-estimation-functions-in-r/… see C# code examples under subtitle "Integration of R with table functions in C#"
1

Short answer: No.

Slightly longer answer: Wrong tool chain. R on Windows is built with the MinGW gcc port. Linking is somewhere between impossible to very fragile.

You can only do this with weak coupling using two machines, having Rserve on one and a .Net / C# connection to it. There are a few solution out there as eg RserveCLI.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.