Returning results to C#

Oct 4, 2011 at 7:37 PM
Edited Oct 5, 2011 at 2:25 PM


TO R.Net

I am really excited about this project and realize it is in early development. Thanks for all of your efforts.

I am using VS 2010 and C#.

I have the project running and printing a list of numbers to the C# console from R to confirm it is running. My goal is to run a manova test and print the returned results to the Console.  The R dataframe (Myvar) consists of 4 numeric and one categorical column named comments.  I have it all working natively in R. Manova is in the stats package. .A second manova function is in the Mass package. I am using the function from Stats The running R script is as follows:

Myvar <- read.csv("C:/RManovaTestData/ManovaTestData.csv", header = TRUE)

bf.manova<-manova(cbind(EDG,HR,BVP,HRV)~Comments, data=Myvar)



I have written the following C# code and it all compiles. I do not understand how to get the information returned by the R summary statements back into C# (after many guesses). I do not know what type is returned by the summary statements and I do not know how to find out. Can someone help? I want to get this code working and return the data to the console for now. Eventually I will plug it into WPF. I also need to understand generically how to determine what is being returned to C#.  Some things seem obvious like a numeric vector but others are not. I did look long and hard at the list of types and how to map in the documentation but did not follow some/many of them. Does more sample code exist than is shown on the web page?

Thanks in advance for any help



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using RDotNet;

using System.Numerics; 

namespace Rdotnettest


    class Program 



        static void Main(string[] args)

        {   //setting C# directory 

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


            using (REngine engine = REngine.CreateInstance("RDotNet"))


               //this is to test that the engine is running will print in C# console. It works.... 

                engine.EagerEvaluate("y <- 1:10");

                NumericVector y = engine.GetSymbol("y").AsNumeric();

                foreach (double el in y)

                { Console.WriteLine(el.ToString());           

              // this is a break out of each R statement 

               // the follow four statements will compile.

               engine.EagerEvaluate("Myvar <-read.csv(\"C:/RManovaTestData/ManovaTestData.csv\")");

                engine.EagerEvaluate("bf.manova <- manova(cbind(EDG,HR,BVP,HRV)~as.factor(Comments), data=Myvar");




            // Here is where I need the help. Get and Set Symbols goes here


             // This is just to get into  break mode to inspect variables in Locals

              Console.WriteLine("this is a test");












Oct 14, 2011 at 5:28 PM

Instead of doing this:




Do this:

GenericVector summary1 = engine.EagerEvaluate("summary('bf.manova')").AsList();
GenericVector summary2 = engine.EagerEvaluate("summary.aov('bf.manova')").AsList();

You can then manipulate summary1 and summary2 in C#.  You may want to play around with the other "As" methods to see if something other than a generic list works better for your purposes.

Oct 14, 2011 at 8:53 PM

Thank you for your help.

I did as you suggested and it gets me on step closer than I was before.  I tired all of the Asxxx() and AsList() was the only one it would allow.  It is returning a GenericVector of SymbolicExpressions. I tired to dig into this vector with a foreach loop

foreach (SymbolicExpression s in summary1)
at the s.     point I tried just about all combinations to get the expected return from a Manova shown below
> summary(bf.manova)
           Df Pillai approx F num Df den Df    Pr(>F)   
Comments    3 1.3436   104.23     12   1542 < 2.2e-16 ***
Residuals 515                                           
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
I could not get any return except "SymbolicExpression"
Can you show me how to manipulate the C# variable to get to the values shown above
Thanks In advance
Dec 2, 2011 at 8:39 PM

not sure .AsList() is what you are looking for?  I use this to return a list of the packages available:

script = "ip<-installed.packages(lib.loc = .Library)";


script = "df<-data.frame(ip)";           


DataFrame f = engine.EagerEvaluate(script).AsDataFrame();


Then I just access the data frame with f[row, column]


I also think you could use this:

DynamicVector summary = engine.EagerEvaluate("summary(\"bf.manova\")").AsVector();

to get what you are looking for....

Jun 29, 2012 at 6:12 PM
Edited Jun 30, 2012 at 12:18 PM