Consuming RdotNET

In my explorations with R, Mathematica, FreeMat, MatLab, and RapidMiner (now with R support! Yay!), I’m seeing integration of R to be quite useful in building a trading app, as technical analysis is one of R’s fortés. For the sake of brevity, I’m including comments in the code instead of using paragraphs…use the source, Luke.
Note that I'm not using the R(D)COM package, but the RdotNET package found here. It's open source (thanks for the correction, Carlitos) closed source, unfortunately, and I've noted bugs when consuming with F# (which I may do a write-up, if I'm more successful with it - I could be Doing It Wrong).

The Source, Luke:

   1: using System;
   2: using System.Linq;
   3: using RDotNet;
   4:  
   5: namespace R.NET_Wrapper
   6: {
   7:     class Program
   8:     {
   9:         public static void Main(string[] args)
  10:         {
  11: //Console.WriteLine("Hello World!");
  12: // Point the R Engine to the dll dir
  13:             RDotNet
  14:             .REngine
  15:             .SetDllDirectory(
  16:                 @"C:\Program Files\R\R-2.12.1\bin\i386"
  17:             );
  18:             
  19: // make an instance, go ahead, do it
  20:             using
  21:                 (REngine engine =
  22:                     REngine
  23:                     .CreateInstance("RInstance")
  24:                 )
  25:             {
  26:                 // Let's see what it'll do.
  27:                 // Let's create a numeric vector with a double[]
  28:                 // .NET framework array to vector
  29:                 NumericVector group1 =
  30:                     engine.CreateNumericVector(
  31:                         new double[] {
  32:                             30.02,
  33:                             29.99,
  34:                             30.11,
  35:                             29.97,
  36:                             30.01,
  37:                             29.99
  38:                     });
  39:                 engine
  40:                 .SetSymbol("group1", group1);     // Dont forget this!
  41:  
  42:                 // Here's the sssllooww way
  43:                 NumericVector group2 =
  44:                     engine
  45:                     .EagerEvaluate(
  46:                     "group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)")
  47:                     .AsNumeric();
  48:                 // EagerEvaluate will also accept IO.Stream (R scripts, anyone?)
  49:  
  50:                 // Test difference of mean (student's t-test) and get P-value
  51:                 GenericVector testResult =
  52:                     engine
  53:                     .EagerEvaluate("t.test(group1, group2)")
  54:                     .AsList();
  55:                 double p =
  56:                     testResult["p.value"]
  57:                     .AsNumeric()
  58:                     .First();
  59:  
  60:                 Console.WriteLine(
  61:                     "Group 1 [{0}]",
  62:                     string.Join(
  63:                         ", ",
  64:                         group1.Select(i => i.ToString()))
  65:                 );
  66:                 Console.WriteLine(
  67:                     "Group 2 [{0}]",
  68:                     string.Join(
  69:                         ", ",
  70:                         group2.Select(i => i.ToString())
  71:                     )
  72:                 );
  73:                 Console.WriteLine("P-value = {0:0.000}", p);
  74:             }
  75:  
  76:              
  77:             //+ TODO: finish getting data into managed space
  78:             
  79:             Console.Write("Press any key to continue . . . ");
  80:             Console.ReadKey(true);
  81:         }
  82:     }
  83: }

Comments

Coleman said…
This is cool. I don't know any C# or R and I still think it's cool. Keep it up!
Baldrick said…
It seems to be open source:

http://code.google.com/p/rdotnet/source/browse/trunk/RdotNET/RWrapper.cs
Unknown said…
@carlitos - So it is. I must've misinterpreted the "...Our closed source project THREATS 2.0..." bit and got mixed up. Thanks for the correction.

@Coleman - Thanks for the encouragement. I'll be looking forwared to using this with Big Data Processing via CUDA or OpenCL.
ISDonline said…
@Jerold - I am curious. I tried to use RdotNet but, at that time, It seemed that the app was stucked using an old version of "R". For instance, it was using a Rproxy.dll that doesn't exist any longer (replaced by rscproxy package). Were you able to use RdotNet with new versions of R without having to change RdotNet code? Can you share anything about it? Thank you very much!
Unknown said…
@ISDonline - If you look at the example code, you'll see that I reference the R installation's DLL directory, and am using a current version of R (v2.12.1). I don't use the version included with the RDotNet library.

I've encountered some access violations since this posting from dealing with managed to unmanaged memory, and will be looking further into that as I get more free time to do so. I'm unsure yet whether it's compatibility issues or bugs.

Popular posts from this blog

RStudio 0.92.44 Release: Try It! You’ll Be Surprised!

Realtek HD Audio Conflicts with NVIDIA HD Audio Drivers