php调用R进行图像绘制,详细介绍使用C#实现Windows Form调用R进行绘图与显示的方法(图)...

众所周知R软件功能非常强大,可以很好的进行各类统计,并能输出图形。下面介绍一种R语言和C#进行通信的方法,并将R绘图结果显示到WinForm UI界面上的方法,文中介绍的很详细,需要的朋友可以参考下。

一、前提准备

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

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

二、创建项目GraphGenerateByR,项目结构如下:

65f72c399fb1a1ed603cbfd6527ada79.png

注意:这里需要引入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

x

y

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

// y

// 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代码,结构如下:

d2542b268d9cd52b59322da57614c759.png

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

bc3e710951f3a753fc232d7085d174b5.png

总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值