REngine.CreateInstance

Aug 18, 2014 at 8:56 PM
I have R.NET version 1.5.15.33833, which I believe is the latest at the moment.

Like everybody else I am experiencing the Dispose bug (engine works only first time around).

I see different versions of a solution in the forum, but they all require the use of something like this:
engine = REngine.__GetInstanceFromID__("RDotNet");
if (engine == null)
    engine = REngine.__CreateInstance__("RDotNet", new[] { "-q" });
However my versions of REngine is missing both GetInstanceFromID and CreateInstance!

I searched high and low and nobody has this problem.

What am I doing wrong ?

Thank you,
SM
Developer
Aug 18, 2014 at 9:30 PM
GetInstanceFromID and CreateInstance are obsolete: you should use 'GetInstance` with the version you have, as indicated by the documentation and newer code samples. The example you show uses method calls from R.NET 1.5.5 or before.

The behavior after the call to the Dispose method is not a bug: this is intended to be so and the purpose of the 'Dispose' pattern.
Aug 18, 2014 at 9:47 PM
Edited Aug 18, 2014 at 9:58 PM
jperraud,

OK, so GetInstance() IS the bug fix.

My original problem I was trying to fix is that I am getting a collection of errors, but some come only the second time around. I can give you more details, but these are the errors I get at one point or another. Maybe you can see something obvious here:
  1. could not find function "memory.limit"
  2. package or namespace load failed for 'RODBC'
  3. Error in file(filename, "r", encoding = encoding) : cannot open the connection
  4. Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths())
Again, if you need more details, I can provide them.

Thanks so much!
SM

Edit:
Number 2 (package or namespace load failed for 'RODBC') is what I keep getting right now. I should mention that it only happens when running from an MVC app. When run from a console app, the same identical code works fine.
Aug 18, 2014 at 11:32 PM
Does package RODBC use a specific 32 or 62 bit library? Might be worth checking if the 32/64 bit settings are the same in the package and the MVC app.
Aug 19, 2014 at 3:55 PM
I tried all combinations. These are the details.

Installed R 64bit only
Requires Platform target "x64", which my MVC app does not support (because of 3rd party dependencies).

Installed R 32bit only
With Platform target "Any CPU", when I call REngine.GetInstance() I get:
Error: could not find function "memory.limit"
With Platform target "x86", when I call load my script I get:
Error: package or namespace load failed for 'RODBC'

Installed both 32bit and 64 bit
Same as just installing 32bit.

Console app always works provided that Platform target is compatible with R bitness, above results are for MVC app.

Not sure where to go from here.

Thank you,
SM
Aug 19, 2014 at 10:00 PM
Edited Aug 19, 2014 at 10:04 PM
jperraud wrote:
The behavior after the call to the Dispose method is not a bug: this is intended to be so and the purpose of the 'Dispose' pattern.
.

This is not working for me:

1) Create engine using GetInstance()
2) engine.Evaluate(myFunction); throws an exception
3) Dispose engine in Finally block
4) Rerun and I get:
The single REngine instance has already been disposed of (i.e. shut down). Multiple engine restart is not possible.

I can only retry after stopping/restarting IIS.
Developer
Aug 20, 2014 at 2:28 PM
Running applications that use R.NET from IIS is the topic of many discussion threads on this site already. The best description of the technical issue and a workaround is detailed in this issue. It appears this is not something that R.NET can take care of automagically.
Aug 20, 2014 at 8:02 PM
jperraud,

I had already seen that article, thank you.

I got around the problem above (The single REngine instance has...) by reusing the same engine instance. Basically it cannot be instantiated more than once.

Not sure why.
Aug 27, 2015 at 4:05 AM
So, in a static method in a SQL CLR library, should we NOT call Dispose()?
Aug 27, 2015 at 4:14 AM
Well, I tried that and still ended up with the same old error:

Msg 6522, Level 16, State 2, Line 32
A .NET Framework error occurred during execution of user-defined routine or aggregate "OneSidedHypothesis":
System.InvalidOperationException: The single REngine instance has already been disposed of (i.e. shut down). Multiple engine restart is not possible.
System.InvalidOperationException:
at RDotNet.REngine.GetInstance(String dll, Boolean initialize, StartupParameter parameter, ICharacterDevice device)
at ProviderAnalysisCLR.OneSidedHypothesis(SqlDecimal PriorAverage, SqlDecimal Tau, SqlDecimal ProvMean, SqlDecimal ProvStdDev, SqlDecimal N)

Anything that can be done here? I'm out of ideas....
Aug 27, 2015 at 2:41 PM
Update:

I got it to run properly in SQL Server by ommitting the call to Dispose(). However, now I'm running up against the library apparently crashing after running for several minutes. Any thoughts?

Thanks!
Sep 19, 2015 at 3:39 PM
This thread attracted my attention as I have a related problem with
REngine engine = REngine.GetInstance();

Using the NuGet package manager with Visual Studio, I have installed
R.Net.Community (v1.6.5) and its dependency DynamicInterop (v.0.7.4).

In my case, REngine.GetInstance fails with the exception:
Windows Registry key 'SOFTWARE\R-core' not found in HKEY_LOCAL_MACHINE nor HKEY_CURRENT_USER

I've installed the system on several machines and the same error appears.

I have also tried setting rHome and rPath explicitly as shown in the code snippet below, but another
exception appears:
An unhandled exception of type 'System.Exception' occurred in DynamicInterop.dll
Additional information: This 32-bit process failed to load the library R.dll.
Native error message is 'The specified module could not be found'
However, I have confirmed that R.dll does exist in the specified folder.

I am grateful for a solution or any helpful suggestions.

using System;
using System.IO;
using RDotNet;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {

        string rHome = @"C:\Program Files\R\R-3.1.3";
        string rPath = Path.Combine(rHome, @"bin\x64");
        REngine.SetEnvironmentVariables(rHome, rPath);

        // There are several options to initialize the engine, but by default the following suffice:
        REngine engine = REngine.GetInstance();

        engine.Dispose();
    }
}
}
Developer
Sep 22, 2015 at 12:52 AM
grc2001,

You use explicit rPath to set environment variables, but your process is 32 bits ("This 32-bit process failed to load the library R.dll" ) and you specify the path to 64 bits R ("string rPath = Path.Combine(rHome, @"bin\x64");"). It cannot work. You need to change your c# project settings to run in 64 bits, or specify the path to 32 bits R.

Regarding the first issue encountered, if the windows registry keys are indeed not set up, it behaves as it should, If you do have R reg keys set, report the overall content, as it is possibly a bug.

the page http://jmp75.github.io/rdotnet/ts_paths_on_windows/ provides some material to help diagnose such issues.