您的位置:首页 > 编程语言

.NET调用R代码并显示图形

2015-03-02 13:16 603 查看
首先展示运行结果:



本人的运行环境:

R 3.1.2 64bits 版

Windows 7

Visual Studio 2013

步骤:

1. 在Visual Studio中安装NuGet,然后安装R.NET库。安装很容易,具体过程参考:
http://rdotnet.codeplex.com/documentation
2. 在RStudio中写出图程序,代码如下,保存在C:/data/CSharp.r

png('C:/CSharp.png')
plot(table(rpois(100, 5)), type = "h", col = "red", lwd = 10,main = "rpois(100, lambda = 5)")
dev.off()


3. 在Visual Studio中新建WinForm程序,并将代码编辑如下,最终R语言生成的图片显示在PictureBox中。

Form1.Desiner.cs

namespace WindowsFormsApplication1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(191, 369);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Show";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(12, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(413, 351);
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(437, 398);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pictureBox1;
}
}


Form1.cs

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;
using RDotNet;
using System.IO;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private REngine engine = null;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
engine.Evaluate("source('c:/data/CSharp.r')");

if(File.Exists("C:/CSharp.png"))
{
Image i = Image.FromFile("C:/CSharp.png");
Bitmap bmp = new Bitmap(i, pictureBox1.Size);
pictureBox1.Image = bmp;
}

Update();
}

private void Form1_Load(object sender, EventArgs e)
{
REngine.SetEnvironmentVariables();
engine = REngine.GetInstance();
// REngine requires explicit initialization.
// You can set some parameters.
engine.Initialize();
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
engine.Dispose();
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: