Click here to Skip to main content
14,429,934 members
Rate this:
Please Sign up or sign in to vote.
See more:
When I call R script method from Web API it is working for first request it returns the result, but for subsequent request getting Stack overflow exception.
The same code is working on another system, tried desposing all the objects in the R.

Please help me to sort out this issue.

R 3.4.2

What I have tried:

WebAPI Method calling R Script:
<pre>Public HttpResponseMessage CallTestMethod()
// SetupPath(); setting Rpackage and ODBC package paths in environment no issue with this method.
REngine engine;
engine = REngine.GetInstance();
//Mentioned R Script file path
var val = engine.Evaluate("source('C:/Folder/RProject1/Script.R')");
//Call FetchDatawithoutParam () and get data frame values in dsResult .
var dsResult = engine.Evaluate(@"TempDataResult <- FetchDatawithoutParam ()").AsDataFrame();

R Script function as below:
FetchDatawithoutParam <- function() {

    dbConnection <- odbcDriverConnect('driver={SQL Server};server=.;database=DBName;trusted_connection=true')
    DataResult <- sqlQuery(dbConnection, "SELECT * FROM TableName;")
 return(DataResult );
Updated 11-Mar-18 23:45pm
Jochen Arndt 12-Mar-18 4:31am
I don't know if this might be the reason for your problem but the
call is never executed because you are returning from the function in the previous line.
[no name] 12-Mar-18 4:44am
changed code still issue persist.
Jochen Arndt 12-Mar-18 6:25am
Therefore I write is as comment and not as solution.

How large is your returned data set?
It might be too large on specific systems initially and also on other systems when the previous instance has not been disposed when executing the query again.
[no name] 12-Mar-18 8:36am
It is not working even after I selected top 2 rows from the table.
Also tried by getting result from stored procedure with ODBC connection, No luck.
Jochen Arndt 12-Mar-18 8:40am
Then it is most probably a recursive call (a function calling itself) as mentioned in the comment by F-ES Sitecore.
[no name] 12-Mar-18 8:45am
But there is only one function mentioned in my R script, how it is calling itself?
Jochen Arndt 12-Mar-18 8:49am
I don't know. Are you sure that it occurs in the R Script and not somewhere else?
[no name] 13-Mar-18 7:28am
Yes, error occured at below line for second time execution.
var dsResult = engine.Evaluate(@"TempDataResult <- FetchDatawithoutParam ()").AsDataFrame();
F-ES Sitecore 12-Mar-18 5:07am
Stack overflow errors are almost always when your code gets into an infinite loop, normally a function that calls itself repeatedly. I can't really see anything immediate in your code that will do this, you'll need to do more investigation.
[no name] 12-Mar-18 8:48am
Do you have any idea that can help me to identify the recursion in my above mentioned code.
F-ES Sitecore 12-Mar-18 8:48am
Use the debugger to step through the code line by line.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100