一、条件预备
装置R软件,须要装置32位的R软件,64位的挪用会报错。别的就是讲R添加到电脑环境变量中。
翻开R软件,装置包 scatterplot3d,演示须要用到此R包。
二、建立项目GraphGenerateByR,项目构造以下:
注重:这里须要引入RDotNet类库,能够自行下载:http://rdotnet.codeplex.com/
三、Main窗体代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace GraphGenerateByR { using RDotNet; public partial class Main : Form { public Main() { InitializeComponent(); } REngine engine = null; string Rcode = ""; private void btnPlot_Click(object sender, EventArgs e) { try { if(this.txtRcode.Text=="") { Rcode = @"library('scatterplot3d') z <- seq(-10, 10, 0.01) x <- cos(z) y <- sin(z) scatterplot3d(x, y, z, highlight.3d=TRUE, col.axis='blue', col.grid='lightblue',main='3d画图',pch=20) "; } else { Rcode = this.txtRcode.Text; } //R.3.2.4 engine = REngine.GetInstance(); engine.Initialize(); //图片到场GUID,防备重名(另有一种就是先删除后保留) string rnd = System.Guid.NewGuid().ToString().Replace("-", ""); string filename ="i"+ rnd+ "Rimage.png"; engine.Evaluate(string.Format("png(file='{0}',bg ='transparent',width={1},height={2})", filename, this.ptbGraphic.Width, this.ptbGraphic.Height)); //engine.Evaluate(@"x <- (0:12) * pi / 12 // y <- cos(x) // plot(x,y); // "); engine.Evaluate(Rcode); engine.Evaluate("dev.off()"); string path = System.IO.Path.GetFullPath(filename); Bitmap image = new Bitmap(path); ptbGraphic.Image = image; } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void Main_FormClosing(object sender, FormClosingEventArgs e) { if(engine!=null) { //clean up engine.Dispose(); } } } }
四、运转:
单击plot后,挪用默许R代码,构造以下:
输入正当的R画图语句,再次单击Plot,效果以下:
总结
以上就是细致引见运用C#完成Windows Form挪用R举行画图与显现的要领(图)的细致内容,更多请关注ki4网别的相干文章!