国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現Windows Form調用R進行繪圖與顯示的方法

2020-01-24 00:44:31
字體:
來源:轉載
供稿:網友

一、前提準備

安裝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;    }    //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 <- (0:12) * pi / 12    //    y <- cos(x)    //    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代碼,結構如下:

輸入合法的R繪圖語句,再次單擊Plot,結果如下:

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 瓮安县| 重庆市| 进贤县| 博客| 保康县| 晋州市| 都兰县| 沙湾县| 淳化县| 满洲里市| 穆棱市| 江源县| 攀枝花市| 新宾| 邵东县| 信阳市| 万年县| 永安市| 新田县| 什邡市| 香港 | 寿宁县| 信阳市| 武威市| 祁连县| 丰原市| 新昌县| 西平县| 右玉县| 兴国县| 鄂温| 弥勒县| 德昌县| 崇州市| 张家港市| 环江| 夹江县| 彭阳县| 礼泉县| 海南省| 元阳县|