星空网 > 软件开发 > ASP.net

Windows Form调用R进行绘图并显示

  R软件功能非常强大,可以很好的进行各类统计,并能输出图形。下面介绍一种R语言和C#进行通信的方法,并将R绘图结果显示到WinForm UI界面上。

1 前提准备

   安装R软件,需要安装32位的R软件,64位的调用会报错。另外就是讲R添加到电脑环境变量中。

  打开R软件,安装包 scatterplot3d,演示需要用到此R包。

2 创建项目GraphGenerateByR,项目结构如下:

Windows Form调用R进行绘图并显示

注意这里需要引入RDotNet类库,可以自行下载:http://rdotnet.codeplex.com/

3 Main窗体代码

 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace GraphGenerateByR11 {12   using RDotNet;13   public partial class Main : Form14   {15     public Main()16     {17       InitializeComponent();18     }19     REngine engine = null;20 21     string Rcode = "";22     private void btnPlot_Click(object sender, EventArgs e)23     {24       try25       {26         if(this.txtRcode.Text=="")27         {28           Rcode = @"library('scatterplot3d')29               z <- seq(-10, 10, 0.01) 30               x <- cos(z)31               y <- sin(z) 32               scatterplot3d(x, y, z, highlight.3d=TRUE, col.axis='blue', col.grid='lightblue',main='3d绘图',pch=20)33              ";34         }35         else36         {37           Rcode = this.txtRcode.Text;38         }39 40         //R.3.2.441         engine = REngine.GetInstance();42         engine.Initialize();43         //图片加入GUID,防止重名(还有一种就是先删除后保存)44         string rnd = System.Guid.NewGuid().ToString().Replace("-", "");45         string filename ="i"+ rnd+ "__Rimage.png";46         engine.Evaluate(string.Format("png(file='{0}',bg ='transparent',width={1},height={2})", filename, this.ptbGraphic.Width, this.ptbGraphic.Height));47 48         //engine.Evaluate(@"x <- (0:12) * pi / 1249         //        y <- cos(x)50         //        plot(x,y);51         //        ");52         engine.Evaluate(Rcode);53         engine.Evaluate("dev.off()");54         string path = System.IO.Path.GetFullPath(filename);55 56         Bitmap image = new Bitmap(path);57         ptbGraphic.Image = image;58       }59       catch(Exception ex)60       {61         MessageBox.Show(ex.Message);62       }63     64     }65 66     private void Main_FormClosing(object sender, FormClosingEventArgs e)67     {68       if(engine!=null)69       {70         //clean up71         engine.Dispose();72       }73     }74   }75 }

 

4 运行:

单击plot后,调用默认R代码,结构如下:

Windows Form调用R进行绘图并显示

输入合法的R绘图语句,再次单击Plot,结果如下:

Windows Form调用R进行绘图并显示

 




原标题:Windows Form调用R进行绘图并显示

关键词:Windows

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

英国疫情封锁措施促使电子商务11月激增39%:https://www.ikjzd.com/articles/138418
英国首相振臂欢呼,近6万亿“离婚”协议,搞定!:https://www.ikjzd.com/articles/138419
全球重磅!英国脱欧协议正式达成!:https://www.ikjzd.com/articles/138420
亚马逊大卖轮番上市!跨境行业风头正紧:https://www.ikjzd.com/articles/138422
圣诞节怎么卖应季产品:https://www.ikjzd.com/articles/138423
个人做shopify-facebook广告常见的扩量方法看这篇就够了!:https://www.ikjzd.com/articles/138424
从园岭新村到大梅沙海滨总站坐什么车:https://www.vstour.cn/a/363191.html
七月份适合去日本旅游吗 7月份去日本哪里好玩:https://www.vstour.cn/a/363192.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流