2013-11-22 42 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也能正常工作。