C#实现实现Windows Form调用调用R进行绘图与显示的方法进行绘图与显示的方法
众所周知R软件功能非常强大,可以很好的进行各类统计,并能输出图形。下面介绍一种R语言和C#进行通信的
方法,并将R绘图结果显示到WinForm UI界面上的方法,文中介绍的很详细,需要的朋友可以参考下。
一、前提准备一、前提准备
安装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;