.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
3. 在Visual Studio中新建WinForm程序,并将代码编辑如下,最终R语言生成的图片显示在PictureBox中。
Form1.Desiner.cs
Form1.cs
本人的运行环境:
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(); } } }
相关文章推荐
- .NET中调用lisp代码的一个类
- 将代码逻辑转化为图形显示
- 基于.net技术的代码高亮显示组件
- 基于.net技术的代码高亮显示组件
- 基于.net技术的代码高亮显示组件
- .net中C#代码与javaScript函数的相互调用问题
- 基于.net技术的代码高亮显示组件
- .net中C#代码与javaScript函数的相互调用问题
- 一步一步学Silverlight 2系列(22):在Silverlight中如何用JavaScript调用.NET代码
- 不使用IA,在.NET代码中调用Office
- datawindow.net2.0在 .NET中调用选中行高亮显示
- 基于.net技术的代码高亮显示组件
- 在.Net中用C#调用Windows系统信息的代码
- 一个图形显示IP的PHP程序代码
- asp调用.Net 托管代码
- 『原创』DX图形显示基本流程(基于MESH静态模型绘制----托管代码)
- 我转载的文章《.NET中统一的存储过程调用方法 》具体实现代码
- 一步一步学Silverlight 2系列(22):在Silverlight中如何用JavaScript调用.NET代码
- 基于.net技术的代码高亮显示组件
- 在.net中如何把调用存储过程代码写入数据连接层中