1

I'm looking to use R.NET to execute an existing R script but haven't had success. Is this actually possible? I've run the example code successfully, so my basic setup is ok.

My code looks like the following:

  static void RTest()
  {
     var envPath = Environment.GetEnvironmentVariable("PATH");
     var rBinPath = System.Environment.Is64BitProcess ? @"C:\Program Files\R\R-3.0.1\bin\x64" : @"C:\Program Files\R\R-3.0.1\bin\i386";
     Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);

     using( var engine = REngine.CreateInstance("RDotNet") )
     {
        engine.Initialize();

        using( var fs = File.OpenRead(@"C:\R-scripts\r-test.R") )
        {
           engine.Evaluate(fs);
        }
     }
  }

Which I'm running in a console app for testing (eventually I want to have it run server-side in a web app).

The r-test.R script works when run in RStudio so there's no problem there and should result in writing a csv file to disk. I do see some 'Loading required package' messages being output to the console so something is working but the resultant csv file does not appear.

L_C
  • 11
  • 1
  • 3

1 Answers1

5

As indicated in response to this post in the R.NET discussions, you can use engine.Evaluate(@"source('c:/path/to/r-test.R')"). Although a lot depends on the content of your script of course, it should work. That said your code looks like it should work as well, though I have not tried your approach.

It is possible that R.NET chokes on some particular R statement within your script. If you have visual studio it should be possible for you to attach to the process if you use R.NET compiled from source with debug symbols. If you have Visual Studio this is the easiest option; MonoDevelop / Xamarin studio is also an option, though a bit more involved. This should help you identify the troublesome line.

Hope this helps

j-m
  • 1,473
  • 1
  • 13
  • 17
  • I did see the reply to that post (which was also mine). That didn't work for me. While I did have my R script in a user folder, initially I experienced an `AccessViolationException`. So I moved the R script to a general location. However, I then get a different error along the lines of 'Error in file... cannot open connection'. It would appear that doing this operation is not so straight forward and I think I'll have to consider a different approach. – L_C Sep 06 '13 at 03:40
  • For a different approach check: [Run R Script with Start Process](http://stackoverflow.com/questions/18224439/run-r-script-with-start-process-in-net) this works with me. – ruedi Nov 12 '13 at 16:02