Displaying a Chart with WPF Dynamic Data Display.

screenshot.png

App.xaml.cs
private void Application_Startup(object sender, StartupEventArgs e)
{
   // Assumes that PATH is set correctly.
   REngine.CreateInstance("RDotNet");
}

private void Application_Exit(object sender, ExitEventArgs e)
{
   REngine engine = REngine.GetInstanceFromID("RDotNet");
   if (engine != null)
   {
      engine.Close();
   }
}

MainWindow.xaml.cs
private void Window_Loaded(object sender, RoutedEventArgs e)
{
   ObservableDataSource<Point> source = new ObservableDataSource<Point>();
   this.plotter.AddLineGraph(source, Colors.Black, 1.0, "RVector");

   REngine engine = REngine.GetInstanceFromID("RDotNet");
   NumericVector sequence = engine.Evaluate("x <- seq(-3, 3, 0.01)").AsNumeric();
   NumericVector dnorm = engine.Evaluate("dnorm(x, 0, 1)").AsNumeric();
   IEnumerable<Point> data = sequence.Zip(dnorm, (x, y) => new Point(x, y));
   source.AppendMany(data);
}

Seamlessness

R style
engine.Evaluate(@"x <- (0:12) * pi / 12
y <- cos(x)
print(y)");

Hybrid style
var x = engine.Evaluate("(0:12) * pi / 12").AsNumeric();
engine.SetSymbol("x", x);
var y = engine.Evaluate("cos(x)").AsNumeric();
Console.WriteLine(string.Join(" ", y));

C# style
var x = engine.CreateNumericVector(Enumerable.Range(0, 13).Select(i => i * Math.PI / 12).ToArray());
var cos = engine.GetSymbol("cos").AsFunction();
var y = cos.Invoke(new[] { x }).AsNumeric();
Console.WriteLine(string.Join(" ", y));

Last edited Aug 1, 2013 at 1:18 AM by kos59125, version 4

Comments

evolvedmicrobe Oct 31, 2013 at 10:38 PM 
any idea how to pass named arguments?

sanjayjadam Aug 7, 2012 at 9:57 AM 
I am trying to run first program, but i am getting an error "Dll was not found.".
After this line
REngine.SetDllDirectory(@"C:\Program Files\R\R-2.12.0\bin\i386");
using (REngine engine = REngine.CreateInstance("RDotNet"))
Please provide any suggestion...

daffodilnt Apr 8, 2012 at 9:54 PM 
I tried to run the first program. The dll path is correctly set to the R.dll location, and R.NET reference has been added properly, the program executes with no error, but the console doesn't show up. It seems that REngine.CreateInstance method doesn't work. any idea?

QuidProMS Mar 22, 2012 at 3:56 AM 
I don't understand how to C# Style something like the R-function, hist(x). Hybrid style is no problem.

var x = engine.CreateNumericVector(Enumerable.Range(0, 13).Select(i => i * Math.PI / 12).ToArray());
var hist = engine.GetSymbol("hist").AsFunction();
hist.Invoke(new[] { x });

Whereas in the example above, cos, is a built-in, hist is a closure. So it seems I am completely clueless on this one.