Quantcast
Channel: R.NET
Viewing all articles
Browse latest Browse all 1634

Created Unassigned: More R charts in a Windows form app [143]

$
0
0
Hi, I have a C# code for a simple windows form with 3 buttons. Button 1 calls R and plots a surface while button 2 plots a contour. If I launch the application and click on button 1, I correctly see the surface plot but then I would like to click on button 2 to open a new window with the counter plot. Unfortunately, if I try to do so the app freezes and I cannot go on. So I have added button 3 with the intention to close the R engine if it is running. The idea was to kill the R instance and reopen it when clicking on button 2. But this doesn't work either. Is there a way to fix my problem?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RDotNet;

namespace mySurface
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public void button1_Click(object sender, EventArgs e)
{
string dllPath = @"C:\Program Files\R\R-3.1.0\bin\i386\";
REngine.SetDllDirectory(dllPath);
REngine.CreateInstance("RDotNet");
REngine engine = REngine.GetInstanceFromID("RDotNet");

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

var x = engine.Evaluate("x <- 1:100").AsNumeric();
var y = engine.Evaluate("y <- 5:105").AsNumeric();
engine.Evaluate("model = function (a, b){23.86+5.525*b-2.5725*a-6.6413*b^2-5.1862*a^2}"); //evaluate function
engine.Evaluate("z = outer(x, y ,model)");
engine.Evaluate("persp(x,y,z)");
//Console.WriteLine(x[0]);
}

public void button2_Click(object sender, EventArgs e)
{
string dllPath = @"C:\Program Files\R\R-3.1.0\bin\i386\";
REngine.SetDllDirectory(dllPath);
REngine.CreateInstance("RDotNet");
REngine engine = REngine.GetInstanceFromID("RDotNet");

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

var x = engine.Evaluate("x <- 1:100").AsNumeric();
var y = engine.Evaluate("y <- 5:105").AsNumeric();
engine.Evaluate("model = function (a, b){23.86+5.525*b-2.5725*a-6.6413*b^2-5.1862*a^2}"); //evaluate function
engine.Evaluate("z = outer(x, y ,model)");
engine.Evaluate("contour(x,y,z, nlevels = 10)");

//Console.WriteLine(x[0]);
}

private void button3_Click(object sender, EventArgs e)
{
REngine engine = REngine.GetInstanceFromID("RDotNet");
if (engine.IsRunning == false)
{
engine.Close();
}
}
}
}

Viewing all articles
Browse latest Browse all 1634

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>