Reputation: 73
I'm having trouble calling my own R functions using R.Net; I have several user defined functions inside some R scripts myScript.R, ..., and I want to call this functions using R.net
this is what I've got so far
one myScript.R file contains several functions like this
userDefinedFunctionOne <- function(parameter1, parameter2)
{
.
.
.
}
userDefinedFunctionTwo <- function(parameter1, parameter2, ...)
.
.
.
and with R.Net, in the constructor of my class I create an instance of the REngine class
private REngine engine;
public MyClass()
{
this.engine = REngine.GetInstance();
}
then I source the myScript.R file
this.engine.Evaluate(@"source('C:/RScripts/myScript.R')");
but when I call my functions like this
var dataframe = this.engine.Evaluate(string.Format("dataframe <- userDefinedFunctionOne(parameter1 = {0}, parameter2 = {1})",
value1,
value2)).AsDataFrame();
I'm getting this error
Error: could not find function "userDefinedFunctionOne"
I've found seen some examples when the user functions is defined like this
Function data = engine.Evaluate(@"data <- function(a,b){
c = a %*% b;
return(c);
}").AsFunction();
but I want to avoid that, because as I wrote, I have several functions inside my R script files
is there a way to do this without rewriting my R functions as strings???
I would apreciate any help, thanks
Upvotes: 2
Views: 1485
Reputation: 1554
What you describe is just fine and should work (good post by the way, thank you). I do not understand why userDefinedFunctionOne
is not found, since it should have been loaded in the global R environment.
I've added a bit of code in one of the sample applications I use: SourceRCode
The code derived from your post ran just fine, I could see the expected data frame from visual studio in debug mode. Can you give it a try and report if you have the issue or not with this sample application?
The sample application uses the latest NuGet package (1.6.5 as of writing), but it looks like you are using a recent version of R.NET given the methods, and I to not expect version to the be cause of the issue.
Upvotes: 1