RDotNet and F#: Example from the Code Project

I added the R type provider via NuGet and it showed up in my references tab:

image

When I go to reference the library in code:

image

But if I fully-qualify the reference, it works:

image

Very Strange, but then I get yummy intellisense

image

 

I then wanted to add in the RDotNet assembly:

Filly qualified does not work

image

but if you add the “@” symbol, it does…

image

I then added the test case that is on codeplex(https://rdotnet.codeplex.com/).

After figuring out that your can only have 1 instance of the engine going at any point in time and the FSI holds a reference (my issue logged here), I wrote this very much procedural code:

  1. #r @"C:\TFS\Tff.RDotNetExample_Solution\packages\R.NET.1.5.3\lib\net40\RDotNet.dll"
  2. #r @"C:\TFS\Tff.RDotNetExample_Solution\packages\R.NET.1.5.3\lib\net40\RDotNet.NativeLibrary.dll"
  3.  
  4. open System.IO
  5. open RDotNet
  6.  
  7. let environmentPath = System.Environment.GetEnvironmentVariable("PATH")
  8. let binaryPath = @"C:\Program Files\R\R-3.0.1\bin\x64"
  9. System.Environment.SetEnvironmentVariable("PATH",environmentPath+System.IO.Path.PathSeparator.ToString()+binaryPath)
  10.  
  11. let engine = RDotNet.REngine.CreateInstance("RDotNet")
  12. engine.Initialize()
  13.  
  14. let group1 = engine.CreateNumericVector([30.02;29.99;30.11;29.97;30.01;29.99]);
  15. engine.SetSymbol("group1", group1)
  16. let expression = "group2 <- c(29.89,29.93,29.72,29.98,30.02,29.98)"
  17. let group2 = engine.Evaluate(expression).AsNumeric()
  18.  
  19. let calcExpression = "t.test(group1, group2)"
  20. let testResult = engine.Evaluate(calcExpression).AsList()
  21.  
  22. printfn "P-Value = %A" (testResult.Item("p.value").AsNumeric())

With the results coming out:

image

Calling R from F#

So I thought I would check out R.NET so I downloaded R from its home site.  I then created a basic F# library and installed R.DotNet.  I found it amusing that it installed from the recycle bin:

image

I then fired up a F# project to test it out.  I decided to mimic the C# example on the Codeplex site in F#.  I gopt to the 4th line where I actually tried to create an instance of the REngine:

image

So I typed into Google R and F# and what do you know,  some folks created a type provider for R  How cool is that?  I installed the RProvider from NuGet and then typed in the basic code sample but it was not getting recognized.

image

I then read in the documentation that you need to restart Visual Studio (good to know)  and make sure that the .NET version is 4.5.  Still no luck.  I then downloaded the example script that is on Github and I was getting the same problem.  Out of desperation, I hit F6 and I got this:

image

 

Then all of the R type provider references resolved.  I then changed the R.Net to point at the bin and all of those references resolved.  I then commented out

  1. //open RProvider.tseries
  2. //open RProvider.zoo

and

  1. //let adf = R.adf_test(msft)

and no more red squiqqley!  When I hit F6 though, I get this:

 

image

Undaunted, I ran the script anyway and sure enough, I got some output:

image