Calling external LiB Function

Feb 14, 2013 at 10:12 AM
i want to use randIndex function from flexclust library but i failed to use this..
this function working fine in R environment. how i can use this function in C# code
Developer
Feb 18, 2013 at 10:23 AM
Hi,

Can you provide precise information as to what fails, preferably as a stand-alone small C# program.

Regards
Feb 20, 2013 at 9:47 AM
        REngine engine = REngine.GetInstanceFromID("RDotNet");

        if (engine == null)
            engine = REngine.CreateInstance("RDotNet", new[] { "-q" });

        // .NET Framework array to R vector.
        IntegerVector group1 = engine.CreateIntegerVector(classes);
        engine.SetSymbol("classes", group1);

        IntegerVector group2 = engine.CreateIntegerVector(clusters);
        engine.SetSymbol("clusters", group2);

        engine.EagerEvaluate("tab=table(classes,clusters)");
        double result = engine.EagerEvaluate("randIndex(tab,correct=FALSE)").AsNumeric().First();

        MessageBox.Show(result.ToString());

where randIndex is function of external Library named "flexclust"
Developer
Feb 28, 2013 at 12:36 AM
Your program is not stand-alone, and classes and clusters are undefined. Also unknown is where things fail.
All I can say is that an example from the randIndex documentation works as expected. This is running on a recent code checkout of R.NET and highly likely to run on R.NET 1.5 as well.
            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"))
            {
                engine.Initialize();
                engine.Evaluate("library(flexclust)");
                engine.Evaluate("set.seed(42)");
                engine.Evaluate("g1 <- sample(1:5, size=1000, replace=TRUE)");
                engine.Evaluate("g2 <- sample(1:5, size=1000, replace=TRUE)");
                engine.Evaluate("tab <- table(g1, g2)");
                double result = engine.Evaluate("randIndex(tab,correct=FALSE)").AsNumeric().First();
            }