Calling R with RDotNet from C# DLL in Tradelink

Aug 17, 2012 at 11:10 PM

I am using R.NET dll found here: http://rdotnet.codeplex.com/ I am trying to run the sample code from within a custom C# DLL specified for Tradelink (http://code.google.com/p/tradelink) Here is the code.

   using System;
   using
System.IO;
   using
System.Linq;
   using
RDotNet;
   using
TradeLink.Common;
   using
TradeLink.API;
   using
Microsoft.Win32;

namespace
Responses
{
   
class RTLTest : ResponseTemplate
   
{


       
////static void Main(string[] args)
       
public override void GotTick(TradeLink.API.Tick k)
       
{
             
//Set the folder in which R.dll locates.
           
REngine.SetDllDirectory(@"C:\Program Files\R\R-2.12.0\bin\i386");

           
REngine.SetDllDirectory(GetRPath());
           
REngine.SetDllDirectory(@"C:\Program Files\R\R-2.14.1\bin\i386");


            using
(REngine engine = REngine.CreateInstance("RDotNet", new[] { "-q" }))  // quiet mode
           
{
               
// .NET Framework array to R vector.
               
NumericVector group1 = engine.CreateNumericVector(new double[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99 });
                engine
.SetSymbol("group1", group1);
               
// Direct parsing from R script.
               
NumericVector group2 = engine.EagerEvaluate("group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)").AsNumeric();

               
// Test difference of mean and get the P-value.
               
GenericVector testResult = engine.EagerEvaluate("t.test(group1, group2)").AsList();
               
double p = testResult["p.value"].AsNumeric().First();

               
string s1 = String.Format("Group1: [{0}]", string.Join(", ", group1));
               
string s2 = String.Format("Group2: [{0}]", string.Join(", ", group2));
               
string s3 = String.Format("P-value = {0:0.000}", p);

                senddebug
(s1);
                senddebug
(s2);
                senddebug
(s3);
           
}
       
}
}

But I get an error of:

response threw exception: Could not load file or assembly 'R.NET, Version=1.4.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Can anyone help out to get around this?