k ch
k ch

Reputation: 1

How to print on RDotNet console

ar envPath = Environment.GetEnvironmentVariable("PATH");
            var rBinPath = @"C:\R-4.0.2\bin\i386";
            Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);

            REngine r = REngine.GetInstance();

            r.Initialize();


         
            r.Evaluate("library('ggplot2')");
            r.Evaluate("set.seed(0)");
            r.Evaluate("n_samples <- 30");
            r.Evaluate("x <- runif(n_samples)");
            r.Evaluate("x <- x[order(x)]");
            r.Evaluate("y <- cos(1.5 * pi * x) + rnorm(n_samples) * 0.1");
            r.Evaluate("df <- data.frame(x, y)");
            r.Evaluate("model1 <- lm(y ~ x, data=df)");
            r.Evaluate("print(summary(model1))");

result:

enter image description here

Upvotes: 0

Views: 206

Answers (1)

Ali Safari
Ali Safari

Reputation: 1775

At the bottom of your aforementioned code, if you add the following piece of code then you get the result instead of the VS output, in a text file:

string [] data = r.Evaluate ("print(summary(model1))").AsCharacter().ToArray();
string filename ="R2Text.txt";//here you can use a fullpath, otherwise it will come out in your VS working directory
File.Create (filename).Dispose ();
for (int i=0; i<data.Length;i++)
{
File.AppendAllText (filename, data [i]+ Environment.NewLine);           
}

your output will be like this: enter image description here

However, if you change your VS language settings, you might get the same. But, changing the language settings, is not always practical.

Upvotes: 1

Related Questions