[R] Calling R functions into C# or C++

Fayssal El Moufatich moufatich at gmail.com
Tue Mar 30 11:02:43 CEST 2010


The zip file actually works fine for me. Anyhow, here is the code snippet
that you need:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interop.STATCONNECTORSRVLib;

namespace RFromCsharp
{
  class RConnector
  {

    private StatConnectorClass rdcom = null;
    private string rcmd;
    
    public StatConnectorClass RConnection { 
      get
      {
        return rdcom;
      }
      set
      {
        rdcom = value;
      } 
    }

    private bool initR()
    {
      try
      {
        rdcom = new StatConnectorClass();
        rdcom.Init("R");
        return true;
      }
      catch(Exception e)
      {
        string errmsg = "R Init failed: " + rdcom.GetErrorText() + " Other:
" +e.Message.ToString();
        Console.WriteLine(errmsg);
        return false;
      }
    }

    private bool loadR(string table, string filename, bool stripwhite, bool
header, string separator)
    {
      try
      {
        rcmd = table.ToString() + "<-read.delim('" + filename.ToString() +
"',strip.white=" + stripwhite.ToString().ToUpper() + ",header=" +
header.ToString().ToUpper() + ",sep='" + separator.ToString() + "')";
        rdcom.EvaluateNoReturn(rcmd);
        return true;
      }
      catch(Exception e)
      {
        string errmsg = rcmd.ToString() + " " + rdcom.GetErrorText() + "
Other:" + e.Message.ToString();
        Console.WriteLine(errmsg);
        return false;
      }
    }

    private bool closeR()
    {
      try
      {
        rcmd = "graphics.off()";
        rdcom.EvaluateNoReturn(rcmd);
        rcmd = "rm(list=ls(all=TRUE))";
        rdcom.EvaluateNoReturn(rcmd);
        rdcom.Close();
        return true;
      }
      catch(Exception e)
      {
        string errmsg = "R Close failed: " + rdcom.GetErrorText() + " Other:
" +
        e.Message.ToString();
        Console.WriteLine(errmsg);
        return false;
      }
    }

    static void Main(string[] args)
    {
      RConnector conn = new RConnector();

      // Initialize the instance to be used with R
      conn.initR();

      // create an R variable named abc and assign it the value of 5
      conn.RConnection.SetSymbol("abc", 5);

      // Retrieve the value of the R variable named abc and assign that
value to the F# value valueForabc
      var valueForabc = conn.RConnection.GetSymbol("abc");

      // Evaluate an expression in R and assign that value to an F# value
aTestEvaluation
      var aTestEvaluation = conn.RConnection.Evaluate("8 * sin(4)");

      // Close the R connection
      conn.closeR();

      Console.BackgroundColor = ConsoleColor.Gray;
      Console.ForegroundColor = ConsoleColor.Blue;
      Console.WriteLine("Value of abc:" + valueForabc);
      Console.WriteLine("Value of 8 * sin(4):" + aTestEvaluation);
      Console.WriteLine("Press any key to continue ...");
      Console.ReadKey();
      //-----------------------------
    }
  }
}

You would also need to reference the Interop.STATCONNECTORSRVLib.dll
assembly. Here is snapshot of my references list:
http://n4.nabble.com/file/n1744914/RFromCsharpReferences.png 

Best regards,
Fayssal El Moufatich
-- 
View this message in context: http://n4.nabble.com/Calling-R-functions-into-C-or-C-tp904267p1744914.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list