opt
opt

Reputation: 477

C# and R.Net not displaying any graph with ggplot2

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.

Upvotes: 2

Views: 5040

Answers (1)

j-m
j-m

Reputation: 1554

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)");

Upvotes: 3

Related Questions