最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C#实现Windows Form调用R进行绘图与显示的方法
时间:2022-06-25 07:51:37 编辑:袖梨 来源:一聚教程网
一、前提准备
安装R软件,需要安装32位的R软件,64位的调用会报错。另外就是讲R添加到电脑环境变量中。
打开R软件,安装包 scatterplot3d,演示需要用到此R包。
二、创建项目GraphGenerateByR,项目结构如下:
注意:这里需要引入RDotNet类库,可以自行下载:http://rdotnet.c**odepl*ex.com/
三、Main窗体代码
代码如下 | 复制代码 |
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms;
namespaceGraphGenerateByR { usingRDotNet; publicpartialclassMain : Form { publicMain() { InitializeComponent(); } REngine engine =null;
stringRcode =""; privatevoidbtnPlot_Click(objectsender, EventArgs e) { try { if(this.txtRcode.Text=="") { Rcode =@"library('scatterplot3d') z x y scatterplot3d(x, y, z, highlight.3d=TRUE, col.axis='blue', col.grid='lightblue',main=Ɖd绘图',pch=20) "; } else { Rcode =this.txtRcode.Text; }
//R.3.2.4 engine = REngine.GetInstance(); engine.Initialize(); //图片加入GUID,防止重名(还有一种就是先删除后保存) stringrnd = System.Guid.NewGuid().ToString().Replace("-",""); stringfilename ="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 // y // plot(x,y); // "); engine.Evaluate(Rcode); engine.Evaluate("dev.off()"); stringpath = System.IO.Path.GetFullPath(filename);
Bitmap image =newBitmap(path); ptbGraphic.Image = image; } catch(Exception ex) { MessageBox.Show(ex.Message); }
}
privatevoidMain_FormClosing(objectsender, FormClosingEventArgs e) { if(engine!=null) { //clean up engine.Dispose(); } } } } |
四、运行:
单击plot后,调用默认R代码,结构如下:
输入合法的R绘图语句,再次单击Plot,结果如下:
相关文章
- 三国群英传策定九州兑换码是什么 三国群英传策定九州2025最新兑换码大全 10-14
- 寻道大千兑换码大全 2025最新可用兑换码一览 10-14
- 绝区零真斗技能详解 10-14
- 原神千星奇域装扮获取途径 10-14
- 火影忍者手游侠隐佐助有哪些玩法技巧 10-14
- 燕云十六声曲岸妙妙喵在哪-曲岸妙妙喵位置分享 10-14