How to Call R Functions from C# and Retrieve The Result in C#

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

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.

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

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);
}

Using R function in C#

You'd need to install R.Net library to run R in .NET Framework.

How to run R script from C# code?

There are multiple R executables. For running a R script in batch mode you will want to use Rscript.exe. It is located in the bin/ subfolder of your R installation directory.

The first argument is the .R file to execute, additional arguments can be supplied. All arguments are available to your R-Script via calling the commandArgs() function.

Note that there exists R.NET which is also available as NuGet-package. This library allows you to directly interact with the R intepreter from C#. You can also exchange data diretcly.



Related Topics



Leave a reply



Submit