.NET調用R代碼並顯示圖形

首先展示運行結果:



本人的運行環境:

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();
        }

    }
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章