zoukankan      html  css  js  c++  java
  • Windows Form调用R进行绘图并显示

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

    1 前提准备

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

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

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

    注意这里需要引入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 GraphGenerateByR
    11 {
    12     using RDotNet;
    13     public partial class Main : Form
    14     {
    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             try
    25             {
    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                 else
    36                 {
    37                     Rcode = this.txtRcode.Text;
    38                 }
    39 
    40                 //R.3.2.4
    41                 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 / 12
    49                 //                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 up
    71                 engine.Dispose();
    72             }
    73         }
    74     }
    75 }

    4 运行:

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

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

  • 相关阅读:
    网络编程之Tcp,udp
    网络编程简介
    面向对象之高级篇 反射,元类
    面向对象 高级篇
    面向对象,继承
    初识面向对象
    包 hashlib,logging
    模块
    Dango生命周期回顾与forms组件
    Django中auth登录、注册、修改密码、退出、ORM自关联
  • 原文地址:https://www.cnblogs.com/isaboy/p/R_image_csharp_RDotNet.html
Copyright © 2011-2022 走看看