I need to interface R to some C# application. I installed rscproxy_1.3
and R_Scilab_DCOM3.0-1B5
added COM references to the STATCONNECTORCLNT
Here is how to call a custom R Function from an Rdata file ( not just a built-in R function). Actually this is what worked for me. To get StatConnectorClass working, I had to open up the properties for 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." + "
"); Response.Flush();
Response.Write("1" + "
"); Response.Flush();
string path = @"C:SOMEPATH\Black-Scholes.RData";
rConn.SetSymbol("path", path);
Response.Write("2" + "
"); Response.Flush();
rConn.Evaluate("load(path)");
Response.Write("3" + "
"); Response.Flush();
Int16 entry = 27;
rConn.SetSymbol("n1", entry);
Response.Write("6" + "
"); Response.Flush();
rConn.Evaluate("x1<-samplefn(n1)" );
Response.Write("Entered : " + entry.ToString() + "
");
Object o = rConn.GetSymbol("x1");
Response.Write("Ans:" + o.ToString() + "
"); Response.Flush();
rConn.Close();
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message );//+ " xx " + rConn.GetErrorText());
rConn.Close();
}
Hope this helps!