What are the steps for calling a custom R routine (within a .R file) from a C# program?

0
using StatConnectorCommonLib;
using STATCONNECTORSRVLib;

StatConnector rConn = new StatConnector();
    rConn.Init("R");
    rConn.SetSymbol("n1", 5);
    rConn.Evaluate("x1<-rnorm(n1)");
    var o = rConn.GetSymbol("x1");
    foreach (double d in o)
                    {
                        Response.Write(d + "<br />");
                    }
                    rConn.Close();

The above code works fine because rnorm is an inbuilt R function.

I need to call other custom (user-defined) functions written in other .R files. For example samplefn(n)

Search as I might, I cannot find it on google....

As per suggestion from #hans-roggeman, I tried the following line

rConn.Evaluate("source('C:\\Program Files (x86)\\R\\RFunctions\\samplefnRfile.R')");

as well as this one.

rConn.Evaluate("source(\"C:\\Program Files (x86)\\R\\RFunctions\\samplefnRfile.R\")");

and they both give the same error. Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))

c#
r
asked on Stack Overflow Jan 16, 2014 by Deep Kumar • edited Jan 18, 2014 by Deep Kumar

2 Answers

0

You can source the R file from C#

rConn.Evaluate("source('other_r_code.R')");

This will execute the code in that file, so make sure it has function definitions and no code you actually want executed. You can specify the full path of the file or play with with working directories using setwd and getwd in R.

The best integration of R and an OO language I have seen so far is Rcpp and RInside with C++ by Dirk Eddelbuettel.

answered on Stack Overflow Jan 16, 2014 by Hans Roggeman
0

Ok, I now have the answer. I had to use StatConnectorClass.

To use this class, I had to open properties of StatConnectorsRVLib and set Embed Interop Types to False.

using StatConnectorCommonLib;
using STATCONNECTORSRVLib;
using STATCONNECTORCLNTLib;
            StatConnectorClass rConn = new StatConnectorClass();
            try
            {


                rConn.Init("R"); // here is where we initialize R 
                Response.Write("Initialized." + "<br />"); Response.Flush();
                Response.Write("1" + "<br />"); Response.Flush();
                string path = @"C:SOMEPATH\Black-Scholes.RData";
                rConn.SetSymbol("path", path); 
                Response.Write("2" + "<br />"); Response.Flush();
                rConn.Evaluate("load(path)");
                Response.Write("3" + "<br />"); Response.Flush();
                Int16 entry = 27; 
                rConn.SetSymbol("n1", entry);
                Response.Write("6" + "<br />"); Response.Flush();
                rConn.Evaluate("x1<-samplefn(n1)" ); 
                Response.Write("Entered : " + entry.ToString() + "<br/> "); 
                Object o = rConn.GetSymbol("x1");
                Response.Write("Ans:" + o.ToString() +  "<br />"); Response.Flush();
                rConn.Close();
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message );//+ " xx " +  rConn.GetErrorText());
                rConn.Close();
            }
answered on Stack Overflow Jan 20, 2014 by Deep Kumar

User contributions licensed under CC BY-SA 3.0