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

Advertisements

One Response to RDotNet and F#: Example from the Code Project

  1. Hi James

    I hope you are having fun playing with R and F#. I am the main author of the RProvider.

    Your examples are using RDotNet directly. The good thing about the RProvider is that you get syntactic sugar for calling R functions, so you should for your t.test example you should be able to do the following (you don’t need to do the explicit initialization either if you use the RProvider):

    let group1 = [30.02;29.99;30.11;29.97;30.01;29.99]
    let group2 = [29.89,29.93,29.72,29.98,30.02,29.98]
    let testResult = R.t_test(group1, group2)

    You can see that it’s much simpler and cleaner, and you get intellisense for functions (though not their types, since R is dynamically typed). Also, the RProvider implicitly coerces things like lists or arrays into R vectors as needed.

    You may also notice that the dot in t.test has been transformed into an underscore. This is because dot is not a valid character in identifiers.

    Accessing results of functions is still harder than it should be, but the author of RDotNet and I are improving that somewhat.

    Hope that helps!

    Howard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: