Saturday, May 3, 2014

Run R code from C#.

We have tryied to use R.Net to run R code in our project last week. Single runs were successfull.

But then problems started.

1. Initialize only one instance per process.

Current version 1.5.5 in nuget can be created only one time. Second call to Initialize just hangs.

2. Crashes on 4th- 5th try

Last available version 1.5.11 throws exceptions on 4th or 5 th run. And almost never returns error message.

Our solution:

Our solution based on this answer on stackoverflow

With monor change, we send R code from string and save it to temp file, since user run custom R code when needed.

  Here is gist of test:

 Result, run over R scipt over 2000 times and no error.

Hope, saved your time.


  1. Hi,

    Could you please post a detailed reproducible bug report at, as an issue or discussion? This is a beta release, and it is really important to report issues such as this; I can run many unit tests repeatedly without any serious issue, so I do not know of any serious stability issue with 1.5.11. It is really important to report these. It is only by luck that I came across your post, and I cannot see a reproducible bug.

    Thank you

    1. Posted issue

  2. I had to include all these statements:

    using System.Diagnostics;
    using System.Linq;
    using Microsoft.Win32;

  3. Hi, I'm new to programming and working on a project where i have to call R code from C#.
    Would you please explain the logic of the method RunFromCmd. I don't really get the code.

    1. Its a workaround. Batch is your R code, arguments will be passed as an array items.

    2. so basically i have to pass my whole R script as a string (batch) and the input to my R code in (args)? and how do i retrieve the output?

    3. E.g. Create temp file for output and pass path to it as a parameter. When script will finish read file in C#.