REngine Initialize() failing in FSI window

Feb 25, 2014 at 10:58 PM
I send RDotNet/RProvider DLL references to FSI and then run the following code:
open RProvider
open RDotNet

// Initialize the R.NET Engine
let environmentPath = System.Environment.GetEnvironmentVariable("PATH")
let binaryPath = @"C:\Program Files\R\R-3.0.2\bin\x64"
System.Environment.SetEnvironmentVariable("PATH", environmentPath+System.IO.Path.PathSeparator.ToString() + binaryPath)

let r_engine = RDotNet.REngine.CreateInstance("RDotNet")

r_engine.Initialize()
the last line (r_engine.Initialize()) gives the following error:

r_engine.Initialize();;
Error in dll[["path"]] : subscript out of bounds
Error in get(name, envir = asNamespace(pkg), inherits = FALSE) :
object 'is' not found
Warning message:
package "methods" in options("defaultPackages") was not found
Error in get(name, envir = asNamespace(pkg), inherits = FALSE) :
object 'is' not found
Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
cyclic namespace dependency detected when loading 'utils', already loading 'methods', 'utils'
In addition: Warning message:
package 'datasets' in options("defaultPackages") was not found
Error in get(name, envir = asNamespace(pkg), inherits = FALSE) :
object 'is' not found
In addition: Warning message:
package 'utils' in options("defaultPackages") was not found
Error in get(name, envir = asNamespace(pkg), inherits = FALSE) :
object 'is' not found
In addition: Warning message:
package 'grDevices' in options("defaultPackages") was not found
Error in get(name, envir = asNamespace(pkg), inherits = FALSE) :
object 'is' not found
In addition: Warning message:
package 'graphics' in options("defaultPackages") was not found
Error in get(name, envir = asNamespace(pkg), inherits = FALSE) :
object 'is' not found
In addition: Warning message:
package 'stats' in options("defaultPackages") was not found
During startup - Warning message:
package 'methods' in options("defaultPackages") was not found

I know this error has been posted before, but none of the solutions proposed seem to work for me.
Feb 25, 2014 at 11:07 PM
That solution is a little brittle. Couple key things,

1 - Are you using FSI in 32 or 64 bit mode? You set the path to 64 bits so will need to match.

2 - Are you able to just set the PATH variable on the computer? In practice this is the most robust.

-N
Feb 26, 2014 at 12:14 AM
evolvedmicrobe wrote:
That solution is a little brittle. Couple key things,

1 - Are you using FSI in 32 or 64 bit mode? You set the path to 64 bits so will need to match.

2 - Are you able to just set the PATH variable on the computer? In practice this is the most robust.

-N
1- I've set the "64-bit F# Interactive" flag to True under Tools -> Options, and the Project has target x64

2- I tried setting the PATH variable in Windows, (so now I can open a Command Window, type "r" and it starts R), but still I have the same issue.
Developer
Feb 26, 2014 at 12:24 AM
Yes there is an issue, at least with the latest release, possibly with the latest development code (yet to test)
These symptoms are known to show up if reinitializing an R engine several times in a process lifetime. However there is no reason this is the case here.

Background information possibly also at:
I'll coordinate with the authors/contributors to RProvider to diagnose and fix this
Feb 26, 2014 at 12:26 AM
jperraud wrote:
Yes there is an issue, at least with the latest release, possibly with the latest development code (yet to test)
These symptoms are known to show up if reinitializing an R engine several times in a process lifetime. However there is no reason this is the case here.

Background information possibly also at:
I'll coordinate with the authors/contributors to RProvider to diagnose and fix this
That's what I don't understand. I'm aware of the issue with re-instantiating/re-initializing, but I'm resetting the FSI session between trials. I doesn't seem like I'm creating more than one instance or initializing multiple times.
Developer
Feb 26, 2014 at 2:42 AM
Actually, the code you uses does recreate/reinitializes the R engine. RProvider does this already I now realise; you do not need to do it in your F# code. The code sample below taken from the R type provider quickstart works fine.

Well, actually this was time I dived into F# and its R provider. Besides, this makes me think R.NET should change its API to prevent new users from falling into this multiple engine initalisations trap.
#I @"../path/to/RProvider.1.0.6/lib"
#r "RDotNet.dll"
#r "RDotNet.FSharp.dll"
#r "RDotNet.NativeLibrary.dll"
#r "RProvider.dll"

open System
open RDotNet
open RProvider
open RProvider.``base``
open RProvider.graphics
open RProvider.stats

// Random number generator
let rng = Random()
let rand () = rng.NextDouble()

// Generate fake X1 and X2 
let X1s = [ for i in 0 .. 9 -> 10. * rand () ]
let X2s = [ for i in 0 .. 9 -> 5. * rand () ]

// Build Ys, following the "true" model
let Ys = [ for i in 0 .. 9 -> 5. + 3. * X1s.[i] - 2. * X2s.[i] + rand () ]

let dataset =
    namedParams [
        "Y", box Ys;
        "X1", box X1s;
        "X2", box X2s; ]
    |> R.data_frame

Feb 26, 2014 at 2:55 PM
jperraud wrote:
Actually, the code you uses does recreate/reinitializes the R engine. RProvider does this already I now realise; you do not need to do it in your F# code. The code sample below taken from the R type provider quickstart works fine.

Well, actually this was time I dived into F# and its R provider. Besides, this makes me think R.NET should change its API to prevent new users from falling into this multiple engine initalisations trap.
#I @"../path/to/RProvider.1.0.6/lib"
#r "RDotNet.dll"
#r "RDotNet.FSharp.dll"
#r "RDotNet.NativeLibrary.dll"
#r "RProvider.dll"

open System
open RDotNet
open RProvider
open RProvider.``base``
open RProvider.graphics
open RProvider.stats

// Random number generator
let rng = Random()
let rand () = rng.NextDouble()

// Generate fake X1 and X2 
let X1s = [ for i in 0 .. 9 -> 10. * rand () ]
let X2s = [ for i in 0 .. 9 -> 5. * rand () ]

// Build Ys, following the "true" model
let Ys = [ for i in 0 .. 9 -> 5. + 3. * X1s.[i] - 2. * X2s.[i] + rand () ]

let dataset =
    namedParams [
        "Y", box Ys;
        "X1", box X1s;
        "X2", box X2s; ]
    |> R.data_frame

Thanks! That explains everything...