C# : can't execute code from R

0

I want to show ellipse in c#. My codes is fine when it running in R but i get message from c# like this : "Object is static; operation not allowed (Exception from HRESULT: 0x8004000B (OLE_E_STATIC))"

here this my codes :

df.rconn.Evaluate("library(cluster)")
df.rconn.Evaluate("library(rrcov)")
public void setScatter(int xAxis, int yAxis, int zAxis, List<string> variable) 
    {

        // plot from R
        //to show outlier with method : classic & robust Mve 
        this.comboBoxXAxis.SelectedIndex = xAxis;
        this.comboBoxYAxis.SelectedIndex = yAxis;
        dataform.rconn.EvaluateNoReturn("x<-X[," + xAxis + "] ");
        dataform.rconn.EvaluateNoReturn("y<-X[," + yAxis + "] ");
        dataform.rconn.EvaluateNoReturn("shape <- cov(X)");
        dataform.rconn.EvaluateNoReturn("center<- colMeans(X)");
        dataform.rconn.EvaluateNoReturn("d2.95 <- qchisq(0.95, df = 2)");
        //dataform.rconn.EvaluateNoReturn("gr<- grid(lty=3,col='lightgray', equilogs = 'TRUE')");
        //dataform.rconn.Evaluate("mtext('with classical (red) and robust (blue)')");
        dataform.rconn.EvaluateNoReturn("plot(x,y, main='Draw Ellipse ', pch=19,col='black', type='p')");
        dataform.rconn.EvaluateNoReturn("elp<- unname(ellipsoidPoints(shape, d2.95,center))");
        dataform.rconn.Evaluate(" lines(elp, col='red' , lty=7 , lwd=2)");
        //dataform.rconn.EvaluateNoReturn("lines(e)");
        //dataform.rconn.EvaluateNoReturn("lines(ellipsoidPoints(mve@cov, d2 = d2.95, loc=mve@center), col='blue', lty='7' , lwd='2') ");
        axGraphicsDevice2.RemoveFromConnector();
    }

in any code that I comment always got the same error. I don't know why this problem happen. Any idea how to show that ellipse ? Thank you very much because you have helped me in completing my thesis.

c#
r
outliers
drawellipse
asked on Stack Overflow Jun 14, 2015 by riu

1 Answer

0

Some missing contextual information, but going by what you provide a guess is that your "rconn" (statconnector?) is not happy when operations refresh a graphic device (lines, mtext, etc.).

As I needed to test some R graphic stuff from c# (with R.NET) for my own purposes, I used your code as a basis and propose it as a workaround for you to try. You'll find a sample console application that works, using R.NET, on GitHub under: https://github.com/jmp75/rdotnet-support/tree/master/samples/DrawEllipse

compiled/run from VS2013, Windows7 64 bits, .NET framework 4.5.

I noticed I needed optionally to use dev.hold and dev.flush to get the intuitive refreshes.

e.Evaluate("dev.hold()");
e.Evaluate("mtext('with classical (red) and robust (blue)')");
e.Evaluate("dev.flush()");

Hope this helps.

answered on Stack Overflow Jun 15, 2015 by j-m

User contributions licensed under CC BY-SA 3.0