2

I have the following code to plot my input series using R charts from my C# application:

public void plotGraphR_2D(List<double> x, double[,] y)
{
    string Rpath = @"C:\Program Files\R\R-3.1.0\bin\x64";

    REngine.SetEnvironmentVariables(Rpath);
    REngine engine = REngine.GetInstance();

    var v1 = engine.CreateNumericVector(x);
    var v2 = engine.CreateNumericMatrix(y);

    if (engine.IsRunning == false)
    {
        engine.Initialize();
    }

    engine.SetSymbol("v1", v1);
    engine.SetSymbol("v2", v2);

    engine.Evaluate("require('ggplot2')");
    engine.Evaluate("library('ggplot2')");
    engine.Evaluate("my_data <- data.frame(v2)");
    engine.Evaluate("colnames(my_data) <- c('Price', 'Quantity')");
    engine.Evaluate("myChart <- ggplot() + geom_line(data = my_data, my_data$Price)"); // THIS DOESN'T WORK
    engine.Evaluate("myChart");
    //engine.Evaluate("plot(my_data$Price)"); // THIS WORKS
}

My input x is a list while y is a 2 dimensional array. I first convert x to numeric vector and y to data frame, then I change column names to the data frame. I want to plot one of the column of my data frame (my_data$Price) but when using ggplot2 it doesn't work. I don't get any error but I don't see any chart popping up. If I try using the last line engine.Evaluate("plot(my_data$Price)") (so normal plot) it works fine. Is there any problem with the way I call ggplot2? I have installed the library with RStudio. Anything else I should do to fix the issue?

Thanks.

opt
  • 477
  • 1
  • 10
  • 25
  • 2
    Try `engine.Evaluate("print(myChart)");`. I'm not familiar with R.net, but that may force it to plot the `ggplot` plot – ialm Oct 21 '14 at 18:00
  • I suspect this might be relevant: http://stackoverflow.com/questions/5106782/use-of-ggplot-within-another-function-in-r. Maybe explicitly using aes_string would do the trick. Alternatively, you can always write the code into a script file and just source the script from R.NET... – etov Oct 21 '14 at 20:23
  • @ialm Thanks for your suggestion. Adding print() actually solve my issue and I'm now able to see the chart. Please add this as answer since it might be easier for future consultation about the same issue. – opt Oct 21 '14 at 22:14
  • No worries. It was a shot in the dark, as I could not test it myself. You can add your own answer documenting how you solved your problem and accept it as correct for future visitors with the same issue, or accept j-m answer if it is correct (though j-m seems to be having a different issue with his plot). – ialm Oct 21 '14 at 22:20

1 Answers1

3

The R code provided does not work as it is reported. The code below does create a ggplot successfully, however the print statement creates a graphic device window but shows an incorrect display (blank form). So yes there is an issue, but I do not know exactly why. The only workaround I can suggest to try is to output images to disk.

        engine.Evaluate("myChart <- ggplot(my_data, aes(x=Price, y=Quantity)) + geom_line()");
        engine.Evaluate("print(myChart)");
j-m
  • 1,473
  • 1
  • 13
  • 17
  • Adding the line engine.Evaluate("print(myChart)") solves my issue and I'm now able to see the chart correctly. Thanks to oth you and ialm for the help. – opt Oct 22 '14 at 12:32
  • 1
    Is there a way to achieve this in ASP.Net or some web application ? – Sandesh Daddi May 24 '16 at 09:44