Evaluating user-defined functions; including libraries; displaying plots...

Mar 4, 2013 at 2:27 PM
Hello,

I started to play with both C# and R.NET a few days ago and got stuck with the following problem:

Given a dataset ('dataset'), I am trying to make a plot by calling a function (makeProfilePlot).
It seems that the function is not called using the code below.

I would really appreciate any suggestion.

Thanks in advance!


public Form1()
    {
        InitializeComponent();

        REngine.SetDllDirectory(@"C:\Program Files\R\R-2.13.0\bin\i386");

        REngine.CreateInstance("RDotNet");
    }
(...)

private void button3_Click(object sender, EventArgs e)
    {
        REngine engine = REngine.GetInstanceFromID("RDotNet");

        string fplot = @"makeProfilePlot <- function(mylist,names){
                        require(RColorBrewer);
                        numvariables <- length(mylist);     
                        colours <- brewer.pal(numvariables,'Set1');                
                        mymin <- 1e+20;
                        mymax <- 1e-20;
                        for (i in 1:numvariables)
                        {
                            vectori <- mylist[[i]];
                            mini <- min(vectori);
                            maxi <- max(vectori);
                            mymin <- mini;              
                            mymax <- maxi;
                            if (mini < mymin) { mymin <- mini};
                            if (maxi > mymax) { mymax <- maxi};
                        };
                        png('test_plot.png');
                        for (i in 1:numvariables)
                        {
                            vectori <- mylist[[i]];
                            namei <- names[i];
                            colouri <- colours[i];
                            if (i == 1) { plot(vectori,col=colouri,type='l',ylim=c(mymin,mymax)) }
                            else   { points(vectori, col=colouri,type='l') };
                            lastxval <- length(vectori);
                            lastyval <- vectori[length(vectori)];
                            text((lastxval-10),(lastyval),namei,col='black',cex=0.6);       
                        };
                        dev.off();
                        }";

        try
            {
                Function makeProfilePlot = engine.EagerEvaluate(fplot).AsFunction();
                engine.Evaluate("C:/Program Files/R/R-2.13.0/library");
                engine.Evaluate("library(RColorBrewer)");
                NumericVector names = engine.EagerEvaluate(@"names <- c('V2','V3','V4','V5','V6')").AsNumeric();
                DataFrame mylist = engine.EagerEvaluate(@"mylist <- list(dataset$V2,dataset$V3,dataset$V4,dataset$V5,dataset$V6)").AsDataFrame();
                engine.Evaluate("makeProfilePlot(mylist,names)");
                Image image = Image.FromFile("test_plot.png");
                pictureBox1.Image = image;
                pictureBox1.Height = image.Height;
                pictureBox1.Width = image.Width;

            }

            catch
            {
                textBox4.Text = "Error";
            }

        } 
Developer
Mar 9, 2013 at 10:32 PM
Works for me, using a version nearly identical to release 1.5.

// setup with
            //var envPath = Environment.GetEnvironmentVariable("PATH");
            //var rBinPath = @"F:\bin\R\bin\x64";
            //Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);
            //using (REngine engine = REngine.CreateInstance("RDotNet"))


        private const string fplot = @"makeProfilePlot <- function(mylist,names){
  require(RColorBrewer);
  numvariables <- length(mylist);     
  colours <- brewer.pal(numvariables,'Set1');                
  mymin <- 1e+20;
  mymax <- 1e-20;
  for (i in 1:numvariables)
  {
    vectori <- mylist[[i]];
    mini <- min(vectori);
    maxi <- max(vectori);
    mymin <- mini;              
    mymax <- maxi;
    if (mini < mymin) { mymin <- mini};
    if (maxi > mymax) { mymax <- maxi};
  };
  png('f:/tmp/test_plot.png');
  for (i in 1:numvariables)
  {
    vectori <- mylist[[i]];
    namei <- names[i];
    colouri <- colours[i];
    if (i == 1) { plot(vectori,col=colouri,type='l',ylim=c(mymin,mymax)) }
    else   { points(vectori, col=colouri,type='l') };
    lastxval <- length(vectori);
    lastyval <- vectori[length(vectori)];
    text((lastxval-10),(lastyval),namei,col='black',cex=0.6);       
  };
  dev.off();
}";
        private static void TestMailingList(REngine engine)
        {
            Function makeProfilePlot = engine.Evaluate(fplot).AsFunction();
            engine.Evaluate("library(RColorBrewer)");
            engine.Evaluate("d <- as.data.frame(matrix(rnorm(600), ncol=6))");
            engine.Evaluate("dnames <- c('V2','V3','V4','V5','V6')         ");
            engine.Evaluate("names(d) <- dnames                            ");
            engine.Evaluate("makeProfilePlot(d,dnames)                     ");
        }