This project is read-only.

How to execute a user defined function of R lang in R.Net

Apr 16, 2015 at 4:41 AM
I have using a user defined function in R for creating Graphs under Rgraphviz package and its working properly when executed in Rstudio

the code is as below
namedvector<- function(names, values) {names(values) = names;values}

render<- function(graph) {
nodes = union(graph$subject, graph$object)
edges = tapply(graph$object, graph$subject, function(x)
list(edges=as.character(x)), simplify=F)
# add edges for object-only nodes (leafs)
for (leaf in setdiff(graph$object, graph$subject))
{edges[[leaf]]= list(edges=NULL)}
# create graph
g<- graphNEL( nodes = nodes, edgeL = edges,edgemode="directed")
# add labels, width, color if given
edgeids = paste(graph$subject, graph$object, sep="~")
edgeAttrs = if (is.null(graph$label)) list() else
list(label=namedvector(edgeids, as.character(graph$label)) )
if (!is.null(graph$col))
edgeRenderInfo(g)<- list(col=namedvector(edgeids,as.character(graph$col)))
if (!is.null(graph$width))
edgeRenderInfo(g)<-  list(lwd=namedvector(edgeids,graph$width))
if (!is.null(graph$label))
edgeRenderInfo(g)<- list(label=namedvector(edgeids,as.character(graph$label)))
# layour and render
g = layoutGraph(g, recipEdges="distinct", edgeAttrs=edgeAttrs)


data<-read.table(file.choose(), header=TRUE, sep = ',')
graph = data.frame(subject=data$Source, object=data$Destination)
for (g in list(graph)) {
render(cbind(g, label=label))

which give correct output

then i used the same code In as follows

engine = REngine.GetInstanceFromID("RDotNet");
        engine.EagerEvaluate("namedvector<- function(names, values) {names(values) = names;values}");
       engine.EagerEvaluate("render<- function(graph)");
        engine.EagerEvaluate("{nodes = union(graph$subject, graph$object)");
        engine.EagerEvaluate(" edges = tapply(graph$object, graph$subject, function(x)");
        engine.EagerEvaluate("list(edges=as.character(x)), simplify=F)");
        engine.EagerEvaluate("for (leaf in setdiff(graph$object, graph$subject)){ ");
        engine.EagerEvaluate("edges[[leaf]]= list(edges=NULL)}");
        #engine.EagerEvaluate("g<- graphNEL( nodes = nodes, edgeL = edges)");
        engine.EagerEvaluate("edgeids = paste(graph$subject, graph$object, sep=\"~\")");
        engine.EagerEvaluate(" edgeAttrs = if (is.null(graph$label)) list() else");
        engine.EagerEvaluate("list(label=namedvector(edgeids, as.character(graph$label)) )");
        engine.EagerEvaluate(" if (!is.null(graph$col))");
        engine.EagerEvaluate("edgeRenderInfo(g)<- list(col=namedvector(edgeids,as.character(graph$col)))");
        engine.EagerEvaluate("if (!is.null(graph$width))");
        engine.EagerEvaluate("edgeRenderInfo(g)<-  list(lwd=namedvector(edgeids,graph$width))");
        engine.EagerEvaluate("  if (!is.null(graph$label))");
        engine.EagerEvaluate(" edgeRenderInfo(g)<- list(label=namedvector(edgeids,as.character(graph$label)))");
        engine.EagerEvaluate(" g = layoutGraph(g, recipEdges=\"distinct\", edgeAttrs=edgeAttrs)");

        engine.EagerEvaluate("data<-read.csv(file.choose(), header=TRUE, sep = ',')");

        engine.EagerEvaluate("for (g in list(graph)){");
errors are shown on the bold lines in the above code

i have used another method also

RF_RES_STATIC = @"render<- function(graph){
                                        nodes = union(graph$subject, graph$object);
                                        edges = tapply(graph$object, graph$subject, function(x);
                                        list(edges=as.character(x)), simplify=F);
                                        for (leaf in setdiff(graph$object, graph$subject)){
                                        edges[[leaf]]= list(edges=NULL)};
                                        g<- graphNEL( nodes = nodes, edgeL = edges,edgemode=""directed"");
                                        edgeids = paste(graph$subject, graph$object, sep=""~"");
                                        edgeAttrs = if (is.null(graph$label)) list() else;
                                        list(label=namedvector(edgeids, as.character(graph$label));
                                        if (!is.null(graph$col))
                                        edgeRenderInfo(g)<- list(col=namedvector(edgeids,as.character(graph$col)));
                                        if (!is.null(graph$width))
                                        edgeRenderInfo(g)<-  list(lwd=namedvector(edgeids,graph$width));
                                        if (!is.null(graph$label)
                                        edgeRenderInfo(g)<- list(label=namedvector(edgeids,as.character(graph$label)));
                                        g = layoutGraph(g, recipEdges=""distinct"", edgeAttrs=edgeAttrs);
                                        };".Replace(" ", "");
        engine = REngine.GetInstanceFromID("RDotNet");
engine.EagerEvaluate("namedvector<- function(names, values) {names(values) =names;values}");
engine.EagerEvaluate("data<-read.csv(file.choose(), header=TRUE, sep = ',')");
        engine.EagerEvaluate("graph = data.frame(subject=data$Source, object=data$Destination)");
        engine.EagerEvaluate("for (g in list(graph)){");
this is not showing any error but not shown any plots as out put

please help me to solve this problem i have tried a lot but is not working........please
Apr 16, 2015 at 6:26 PM

Write your function to a file and use the source() function to load it or put the entire function into a single String object and pass that to the engine.

There is no line continuation feature in R.NET.

engine.EagerEvaluate("{") is not a valid command
Apr 20, 2015 at 12:38 PM

could you give me an examble of how to use the source() function

i dont know how to do it
Apr 20, 2015 at 2:54 PM
You'll find samples including using R's source function at I just updated the existing code to use the latest R.NET packages.

You seem to be using a very outdated version of R.NET (EagerEvaluate is a function that has been deprecated months to years ago).
Apr 20, 2015 at 5:41 PM
Hello NikhilRajendran, source("filename") Please consult the R documentation and direct your R questions to forums about the R language. R.NET is only an interface between a .NET application and R.