R library not getting loaded when I try this using statConnector in R

0

I can access the basic function of stat library in R when I connect C# and R through statconnector. But if I load a library(), I am not able to call any of its function.

The code I am trying is:

rConn.SetSymbol("n1", 20);
rConn.Evaluate("library(dtw)");
rConn.Evaluate("x1<-rnorm(n1)");
rConn.Evaluate("x2<-rnorm(n1)");
rConn.Evaluate("Score<-dtw(x1,x2,keep.internals=TRUE)");

The error I get is when I run the last line i.e., rConn.Evaluate("Score<-dtw(x1,x2,keep.internals=TRUE)");

The error i get is -

There is no connection for this connection ID (Exception from HRESULT: 0x80040004 (OLE_E_NOCONNECTION))
r
asked on Stack Overflow Apr 1, 2013 by Gaurav Sharma • edited Dec 13, 2013 by BenMorel

2 Answers

0

You will find that with some functions called via StatConnector that you MUST use .EvaluateNoReturn instead of .Evaluate. It makes sense if you recognize that if you examine the class of "Score" (within R) via:

class(Score)
[1] "dtw"

You see that "Score" contains a dtw object. Implicitly whenever you use .Evaluate, you are calling a function with a return value. However, C# doesn't know what to do with a dtw object, so you get a funky error message that make you go hmmm. Try the equivalent of a void call instead:

rConn.EvaluateNoReturn("Score<-dtw(x1,x2,keep.internals=TRUE)");

And you should have no error executing dtw. To get a return value back into C#, while in R, take a look at names(Score) to see which variables are available, then something like

class(Score$normalizedDistance)
"numeric"

to get an idea how to cast the values for C#, as in:

double myDist = (double)rConn.Evaluate("Score$normalizedDistance");
answered on Stack Overflow Apr 16, 2013 by Patrick White • edited Apr 16, 2013 by Patrick White
0

Additional information I found that I received a similar error when calling 10+ concurrent statconnector instances.

The .Evaluate function when returning values would fail on 10% of calculations with the following error

There is no connection for this connection ID (Exception from HRESULT: 0x80040004 (OLE_E_NOCONNECTION))   
   at StatConnectorCommonLib.IStatConnector.Evaluate(String bstrExpression)

The fix I found to work was to store the result in a variable in R, and use the .EvaluateNoReturn and .GetSymbol to return the results.

StatConnector rConn = new StatConnector();

rConn.Init("R");
rConn.SetSymbol("n1", 20);
rConn.Evaluate ("x1<-rnorm(n1)");
var o = rConn.GetSymbol ("x1");

foreach (double d in o)
    Console.WriteLine(d);

rConn.Close();

Check out the following article for my source and examples, http://www.billbak.com/2010/11/accessing-r-from-clessons-learned/

answered on Stack Overflow Sep 30, 2013 by David Pitchford

User contributions licensed under CC BY-SA 3.0