2013-11-22 41 views
1
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(); 
    } 

}R发动机未初始化

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

请帮我从C#

+0

它崩溃的线是什么? –

+1

engine.Initialize(); –

+0

我在这个程序崩溃问题中需要一些帮助。 –

回答

3

连接到R I认为发动机因为你有一些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; 
+0

是否有任何东西要替换为“SOFTWARE \ R-core \ R” 因为我收到错误 System.NullReferenceException是未处理的 –

+0

没有任何东西。你可以检查你的注册,启动cmd - > regedit - > HKEY_LOCAL_MACHINE - > SOFTWARE - > R ...然后如果你没有找到任何键入口,你应该重新安装R并检查选项以设置注册表在安装... – agstudy

+1

非常感谢帮助。 在我的情况下路径如下。 RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@“software \ R-core \ R”); 但它仍然在相同的初始化线上崩溃。 –

1

我有同样的问题。 engine.Initialize()调用时发生崩溃。 我以管理员身份重新安装了R for Windows,并在注册表中注册了注册表项。即使在ASP.NET应用程序中,R.NET也能正常工作。