How to call a user defined function (in a .rdata file) from c# R.net?

1.1k views Asked by At

I have, in one hand, a plateform I developped in R, composed of many user-defined functions used to produce a report, using as inputs matrices and arguments.

In an other I manage my data flow through a c# Platform.

What I want to do is calling my R Platform from c# : calling a function defined in an Rdata file into my c# code (which handle all my data matrices).

I am familiar with RdotNet and use it to call user defined functions, but these are functions defined in my c# code itself, while I need to call functions in a Rdata file.

Thanks in advance.

1

There are 1 answers

0
j-m On BEST ANSWER

R functions called from R.NET are not restricted to ones created from strings in .NET. If you define and save the function definition in R:

f <- function() {"Hello World"}
save(f, file='f.RData')

Then in C# you simply do something like

static void stackoverflow_27924923_2752565 (REngine engine)
{
    engine.Evaluate ("load('~/f.RData')");
    var s = engine.Evaluate ("f()").AsCharacter()[0];
    Console.WriteLine(s);
}