35

I'm working on an application that requires a great deal of stastical processing and output as images in a .net desktop application. The problems, including generating the output images, seem like a natural fit for R http://www.r-project.org/

Is there a wrapper, API, SDK, or port that will allow me to call R from .net?

detroitpro
  • 3,853
  • 4
  • 35
  • 64
  • For complete example C# source code for R.NET, see http://stackoverflow.com/questions/5377070/c-sharp-r-interface/14177183#14177183 – Contango Jan 06 '13 at 01:34
  • Use the R(D)-COM server interface as explained here: http://www.codeproject.com/KB/cs/RtoCSharp.aspx – Michael Dillon Feb 17 '11 at 05:23
  • 1
    There is another alternative: [RServeCLI](http://rservecli.codeplex.com/). It allows to connect to the R through TCP/IP, supports multiple sessions and works under Mono (tested with R 2.15 on Debian). It seems to be less buggy than R.NET. Worth to give it a try. – devstat Nov 06 '12 at 11:52

5 Answers5

15

R.NET is pretty buggy with the newer version of R. And if it doesn't work right, it works terribly (and will continue to do so unless you know exactly how to fix it).

Personally, I'd recommend using R script files and executing them. What you should do is start your R script with

> sink()
> #set your working directory here with setwd()
> #your code comes in here
> sink(#name your output file here - could label it with a .txt if you please
+ )

And from .NET, you have to include the System.Diagnostics namespace by typing using System.Diagnostics and then write this code:

string strCmdLine;
strCmdLine = "R CMD BATCH" + /* the path to your R script goes here */;
System.Diagnostics.Process.Start("CMD.exe",strCmdLine);
process1.Close();

You can then use a StreamReader like this:

StreamReader ROutput = new StreamReader(/* your R output file's path should go here */)

And then parse it as you please (see RegEx and a string's split method if you need help with that too).

Hope this helps!

Moriarty Snarly
  • 506
  • 5
  • 9
  • Could you expand on the kinds of problems you're seeing with R.Net? And does R 2.15.1 trigger these problems too? – Christian Hudon Jul 24 '12 at 14:22
  • 6
    @ChristianHudon: Main problem is memory management. I tried some graphics stuff (you will find it on the R.NET page) and reported the bugs, but these were never seriously addressed. This so called "stable" library has not been updated since 1.5 years in spite of the many bugs and some available patches. Too bad, it was a great start. – Dieter Menne Jul 26 '12 at 13:53
  • 1
    @ChristianHudon: When I run the example on the R.NET home page, I get an unhandled exception inside RDotNet.REngine.Parse(). "Error in matrix(NA_character_, 0L, 3L) : 5 arguments passed to .Internal(matrix) which requires 7" – John D. Cook Dec 18 '12 at 16:47
  • 2
    The latest version of R.NET has fixed the memory management issues. – Contango Jan 06 '13 at 01:33
11

I found this library easier to use:

http://rdotnet.codeplex.com/

Some reasons why:

  • Only a single .NET assembly is required
  • The DCOM Server actually requires several components from different places
  • One of the components has a very restrictive license. Only direct downloads from the website are allowed - no other form of distribution is permitted, by default, which is going to make deployment interesting
Thomas Bratt
  • 48,038
  • 36
  • 121
  • 139
4

For the record, if you want to call C# from R, check out rClr at https://rclr.codeplex.com/.

Contango
  • 76,540
  • 58
  • 260
  • 305
1

As other people said, R.NET is pretty buggy. To shield yourselfs from effects of new R versions, you can use R-server (rserve) here.

You can then use a rserve-client to execute your R scripts.
On sourceforge, you will find the C# version.

You will need R 1.5+ installed on the server.

Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
0

Shiny is an option.

You can run a shiny app and open your report both in your browser and your wpf browser control.

Here is a more detailed explanation. It is in other language, but I tried to insert English keywords. All italic text is English.

Example in other language but with English keywords

Andrey K.
  • 665
  • 8
  • 29