我试图让RDotNet与C#一起工作,我遇到了问题

时间:2013-11-01 11:12:29

标签: c# r r.net

我在64位Windows机器上运行在Web环境中的Visual Studio 2012中。

我使用nuget安装最新版本的R.net(1.5.5),最新发布于2013年9月16日,

我尝试设置路径(用户和系统)以包含目录“E:\ Program Files \ R \ R-3.0.2 \ bin \ x64”

我还根据一些建议尝试了这段代码......

// 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);

在致电之前

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

Initialize();

但我得到这个错误DllNotFound .....

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

我已经研究过并发现其他人已经提出了如何解决这个问题的建议https://rdotnet.codeplex.com/discussions/353957我已经阅读了这个并尝试了各种各样的事情,包括“弃用的”SetDllDirectory(dllDirectory As String)并得到了消息它不赞成使用....

所以我有点难过RDotNet在64位工作吗?我已经读过可能的问题与RlaPack.dll引用我没有的另一个Dll

我还读过有关R_Home 可能的提示需要设置...但是其他人说它在Windows中工作,我不需要设置R_home。

所以我可以尝试一些来自社区的指导 感谢任何在c#环境中具有RDotNet / R经验的人

1 个答案:

答案 0 :(得分:8)

这对我来说很好。您应该在调用R引擎之前设置R路径。例如,您可以使用此功能设置它:

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);
}

然后你打电话给它,例如:

        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();
            }
        }

编辑更好的方法:读取注册表中的路径:

 RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\R-core\R");
 string rPath = (string)registryKey.GetValue("InstallPath");
 string rVersion = (string)registryKey.GetValue("Current Version");
 registryKey.Dispose();