Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am trying to Make RDotNet work with C#, and I am encountering problems

Tags:

c#

r

r.net

I am on a 64 bit windows machine In Visual Studio 2012 working in a Web environment.

I have used nuget to install the latest version of R.net (1.5.5) last published 16/09/2013,

I have tried setting the Path (both user and system) to include the directrory "E:\Program Files\R\R-3.0.2\bin\x64"

I have also tried this code as per some suggestions...

// As per the example
var envPath = Environment.GetEnvironmentVariable("PATH");
const string rBinPath = @"E:\Program Files\R\R-3.0.2\bin\x64";
Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);

Prior to calling

// Create an instance of the engine
engine = REngine.CreateInstance("RDotNet");

Initialize();

But I get this Error DllNotFound.....

RDotNet.NativeLibrary.UnmanagedDll..ctor(String dllName) +267
RDotNet.REngine..ctor(String id, String dll) +55
RDotNet.REngine.CreateInstance(String id, String dll) +415

I have researched and found that others have made suggestions on how to fix this https://rdotnet.codeplex.com/discussions/353957 I have read through this and tried a variety of things including the "deprecated" SetDllDirectory(dllDirectory As String) and got the message that its deprecated and I shouldnt use....

So I am a little stumped Does RDotNet work in 64bit? I have read that the issue may be with the RlaPack.dll referencing another Dll that I don't have

I have also read that hints about R_Home might need to be set... but then others that say it works in windows and I dont need to set R_home.

So a little guidance from the community please for things I can try thanks anybody with RDotNet/R experience in a c# environment

like image 954
julian guppy Avatar asked Nov 01 '13 11:11

julian guppy


1 Answers

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();
like image 141
agstudy Avatar answered Nov 16 '22 04:11

agstudy