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

}

Hi When I executed the above code it is crashing while initializing. OS is Windows XP sp3(32 bit) R version- R-3.0.2 using R.Net(1.5 version)

Please help me to connect to R from c#

2
  • What line it crashes on?CommentedNov 22, 2013 at 10:27
  • I need some help in this program crashing issue.
    – Rajesh P
    CommentedNov 23, 2013 at 5:25

2 Answers 2

3

I think that the engine crashes because you have some R-path error. better to read the path from windows registry.

Try something like this:

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 } 

Of course you should ad the right references:

using Microsoft.Win32; using RDotNet; using System.IO; 
4
  • Is there any thing to replace with "SOFTWARE\R-core\R" cause I'm getting ERROR System.NullReferenceException was unhandled
    – Rajesh P
    CommentedNov 22, 2013 at 12:31
  • No nothing. You can check you register , launch cmd --> regedit --> HKEY_LOCAL_MACHINE --> SOFTWARE --> R ... Then if you don't find any key entry , you should reinstall R and check the option to set up registry during install...
    – agstudy
    CommentedNov 22, 2013 at 12:44
  • 1
    Thanks very much agstudy for helping. in my case path is as follows. RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"software\R-core\R"); But still its crashing on same initializing line.
    – Rajesh P
    CommentedNov 22, 2013 at 13:15
  • I need some help in this program crashing issue.
    – Rajesh P
    CommentedNov 23, 2013 at 5:27
1

I had the same issue. Crash at engine.Initialize() call. I reinstalled R for windows as administrator and registered keys in registry. R.NET works fine now even in ASP.NET application.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.