This works fine for me. You should setup the R path before calling the R engine. For example you can set it using this function:
public static void SetupPath(string Rversion = "R-3.0.0" ){
var oldPath = System.Environment.GetEnvironmentVariable("PATH");
var rPath = System.Environment.Is64BitProcess ?
string.Format(@"C:\Program Files\R\{0}\bin\x64", Rversion) :
string.Format(@"C:\Program Files\R\{0}\bin\i386",Rversion);
if (!Directory.Exists(rPath))
throw new DirectoryNotFoundException(
string.Format(" R.dll not found in : {0}", rPath));
var newPath = string.Format("{0}{1}{2}", rPath,
System.IO.Path.PathSeparator, oldPath);
System.Environment.SetEnvironmentVariable("PATH", newPath);
}
Then you call it for example:
static void Main(string[] args)
{
SetupPath(); // current process, soon to be deprecated
using (REngine engine = REngine.CreateInstance("RDotNet"))
{
engine.Initialize(); // required since v1.5
CharacterVector charVec = engine.CreateCharacterVector(new[] {
"Hello, R world!, .NET speaking" });
engine.SetSymbol("greetings", charVec);
engine.Evaluate("str(greetings)"); // print out in the console
string[] a = engine.Evaluate("'Hi there .NET, from the R
engine'").AsCharacter().ToArray();
Console.WriteLine("R answered: '{0}'", a[0]);
Console.WriteLine("Press any key to exit the program");
Console.ReadKey();
}
}
EDIT better method : read the path out of the registry:
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\R-core\R");
string rPath = (string)registryKey.GetValue("InstallPath");
string rVersion = (string)registryKey.GetValue("Current Version");
registryKey.Dispose();