R引擎未初始化

时间:2013-11-22 10:26:26

标签: c# r r.net

static void Main(string[] args)
{
    // Set the folder in which R.dll locates.
    var envPath = Environment.GetEnvironmentVariable("PATH");
    var rBinPath = @"C:\R-3.0.2\bin\i386\";
    Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);
    using (REngine engine = REngine.CreateInstance("RDotNet"))
    {
        // Initializes settings.
        engine.Initialize(); // After Executing this line its crashing.

        NumericVector group1 = engine.CreateNumericVector(new double[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99 });
        engine.SetSymbol("group1", group1);
        NumericVector group2 = engine.Evaluate("group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)").AsNumeric();

        // Test difference of mean and get the P-value.
        GenericVector testResult = engine.Evaluate("t.test(group1, group2)").AsList();
        double p = testResult["p.value"].AsNumeric().First();

        Console.WriteLine("Group1: [{0}]", string.Join(", ", group1));
        Console.WriteLine("Group2: [{0}]", string.Join(", ", group2));
        Console.WriteLine("P-value = {0:0.000}", p);
        Console.ReadLine();
    }

}

您好 当我执行上面的代码时,它在初始化时崩溃。 操作系统是Windows XP sp3(32位) R版本-R-3.0.2 使用R.Net(1.5版)

请帮我从c#

连接到R.

2 个答案:

答案 0 :(得分:3)

我认为发动机崩溃是因为你有一些R路径错误。最好从Windows注册表中读取路径。

尝试这样的事情:

using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\R-core\R")){
    var envPath = Environment.GetEnvironmentVariable("PATH");
    string rBinPath = (string)registryKey.GetValue("InstallPath");
    string rVersion = (string)registryKey.GetValue("Current Version");
    rBinPath = System.Environment.Is64BitProcess ? rBinPath + "\\bin\\x64" : 
                                                     rBinPath + "\\bin\\i386";
    Environment.SetEnvironmentVariable("PATH", 
                          envPath + Path.PathSeparator + rBinPath);
}
using (REngine engine = REngine.CreateInstance("RDotNet")){
              // same code here 
}

当然,您应该使用正确的参考资料:

using Microsoft.Win32;
using RDotNet;
using System.IO;

答案 1 :(得分:1)

我有同样的问题。在engine.Initialize()调用崩溃。 我重新安装了R for Windows作为管理员和注册表中的注册密钥。即使在ASP.NET应用程序中,R.NET也能正常运行。