Reputation: 11
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.
Upvotes: 1
Views: 9754
Reputation: 1539
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
Upvotes: 5