RDotNet and F#: Example from the Code Project
September 3, 2013 1 Comment
I added the R type provider via NuGet and it showed up in my references tab:
When I go to reference the library in code:
But if I fully-qualify the reference, it works:
Very Strange, but then I get yummy intellisense
I then wanted to add in the RDotNet assembly:
Filly qualified does not work
but if you add the “@” symbol, it does…
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:
- #r @"C:\TFS\Tff.RDotNetExample_Solution\packages\R.NET.1.5.3\lib\net40\RDotNet.dll"
- #r @"C:\TFS\Tff.RDotNetExample_Solution\packages\R.NET.1.5.3\lib\net40\RDotNet.NativeLibrary.dll"
- open System.IO
- open RDotNet
- let environmentPath = System.Environment.GetEnvironmentVariable("PATH")
- let binaryPath = @"C:\Program Files\R\R-3.0.1\bin\x64"
- System.Environment.SetEnvironmentVariable("PATH",environmentPath+System.IO.Path.PathSeparator.ToString()+binaryPath)
- let engine = RDotNet.REngine.CreateInstance("RDotNet")
- engine.Initialize()
- let group1 = engine.CreateNumericVector([30.02;29.99;30.11;29.97;30.01;29.99]);
- engine.SetSymbol("group1", group1)
- let expression = "group2 <- c(29.89,29.93,29.72,29.98,30.02,29.98)"
- let group2 = engine.Evaluate(expression).AsNumeric()
- let calcExpression = "t.test(group1, group2)"
- let testResult = engine.Evaluate(calcExpression).AsList()
- printfn "P-Value = %A" (testResult.Item("p.value").AsNumeric())
With the results coming out: