This project is read-only.

R Engine Reuse (re-initialize)

Aug 13, 2013 at 2:01 PM
Hi,
I want to run this example code in a button, and copied to all of them and paste into buttonClick function. When I clicked button first time codes work perfectly , but when click to button secondly , it throws an parse error at this line : GenericVector testResult = engine.Evaluate("t.test(group1, group2)").AsList();

I tried a lot of code piece in a buttonclick function. All of them work perfect in first time but doesn't work second time. I think it ise about reusing engine with initialize funciton!?
How can I solve this problem?
THX.


example code is :

// Set the folder in which R.dll locates.
  // See Documentation for automatic search of installation path.
  // This example set PATH environment variable for 64-bit process and R x64 2.15.1 on Windows.
  // You should appropriately set the correct location for other cases.
  // Refer to the Documentation page.
  var envPath = Environment.GetEnvironmentVariable("PATH");
  var rBinPath = @"C:\Program Files\R\R-2.15.1\bin\x64"; 
  Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);

  // For Linux or Mac OS, R_HOME environment variable may be needed.
  //Environment.SetEnvironmentVariable("R_HOME", "/usr/lib/R")

  using (REngine engine = REngine.CreateInstance("RDotNet"))
  {
     // From v1.5, REngine requires explicit initialization.
     // You can set some parameters.
     engine.Initialize();

     // .NET Framework array to R vector.
     NumericVector group1 = engine.CreateNumericVector(new double[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99 });
     engine.SetSymbol("group1", group1);
     // Direct parsing from R script.
     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);
Aug 14, 2013 at 8:32 AM
I cannot the quote, but I think Kosei mentions somewhere as a known limitation that the R engine cannot be initialised twice in the same process. In real applications, i.e. beyond a bit of code called within a "using ( Rengine engine = etc." C# statement, one needs to initialise the R engine once during the main process lifetime. The reference to the REngine may be static of an object property, but there needs to be only one REngine instance, and concurrent access should be carefully controlled. R is largely not thread safe.
Aug 14, 2013 at 8:38 AM
I used already the reference to the REngine static object property. but it didn't work and one initialize too didn't work. another idea?
Aug 15, 2013 at 8:36 AM
up up
Aug 16, 2013 at 2:56 AM
up up ???
Aug 16, 2013 at 7:06 AM
it supply my questions up to top of discussion page. I need solve this problem immidiately
Aug 22, 2013 at 2:37 AM
I met the same problem, any ideas for that?
Sep 11, 2013 at 7:43 AM
jperraud,

Is it possible to run the REngine instance in an isolated process, close that process, close the REngine - and then create a new process with a new (fresh) REngine?

Any work around is good!!

Donald
Sep 11, 2013 at 8:32 AM
Hi,

then you need inter-process communication between your C# program and another C# program. Not an advisable thing in my opinion, and if you need it a lot of work I'd bet. I do not understand why a singleton pattern or more simply a static property for the REngine would not work. You did not mention what did not work when you tried that mid-August.
If you really want concurrent access to multiple instances of the engine (multiple users accessing a web service or the like) they yes you will need inter-process communication as far as I know. If you have a simple, single user, non concurrent access, stick to a "singleton" approach.

Hope this helps.
Apr 7, 2014 at 6:58 PM
Hi jperraud,

I am also facing the same issue. I am trying to use in a Window service. How do I do with inter-process communication. can you send me a sample code.

Thanks