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

jul*_*ppy 8 c# r r.net

我在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);
Run Code Online (Sandbox Code Playgroud)

在打电话之前

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

Initialize();
Run Code Online (Sandbox Code Playgroud)

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

RDotNet.NativeLibrary.UnmanagedDll..ctor(String dllName) +267
RDotNet.REngine..ctor(String id, String dll) +55
RDotNet.REngine.CreateInstance(String id, String dll) +415
Run Code Online (Sandbox Code Playgroud)

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

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

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

所以请社区的一点指导,我可以尝试感谢任何有关ac#环境的RDotNet/R经验的人

ags*_*udy 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);
}
Run Code Online (Sandbox Code Playgroud)

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

        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();
            }
        }
Run Code Online (Sandbox Code Playgroud)

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

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