我正在尝试让RDotNet在C#中工作,但遇到了问题。

8

我正在一台64位的Windows机器上,在Visual Studio 2012中工作于Web环境下。

我已经使用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个回答

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

最好从注册表中读取路径,假设R安装将其写入注册表的某个位置。 - David Heffernan
@DavidHeffernan 不,R不会设置Windows注册表。它只是一些二进制文件的副本。但最好在这里读取R_HOME全局环境变量。这里提供的解决方案来自R.net文档网站。 - agstudy
好的。我的观点是,必须能够检查机器以找出R的位置,这可能比硬编码更好。 - David Heffernan
1
@DavidHeffernan 你是对的。R安装设置了Windows注册表中的一个键。我编辑了我的答案以反映这一点。 - agstudy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接