C#實(shí)現(xiàn)Windows Form調(diào)用R進(jìn)行繪圖與顯示的方法
一、前提準(zhǔn)備
安裝R軟件,需要安裝32位的R軟件,64位的調(diào)用會(huì)報(bào)錯(cuò)。另外就是講R添加到電腦環(huán)境變量中。
打開R軟件,安裝包 scatterplot3d,演示需要用到此R包。
二、創(chuàng)建項(xiàng)目GraphGenerateByR,項(xiàng)目結(jié)構(gòu)如下:
注意:這里需要引入RDotNet類庫(kù),可以自行下載: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();
}
}
}
}
四、運(yùn)行:
單擊plot后,調(diào)用默認(rèn)R代碼,結(jié)構(gòu)如下:
輸入合法的R繪圖語(yǔ)句,再次單擊Plot,結(jié)果如下:
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)我們的支持。
欄 目:C#教程
本文標(biāo)題:C#實(shí)現(xiàn)Windows Form調(diào)用R進(jìn)行繪圖與顯示的方法
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5862.html
您可能感興趣的文章
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁(yè)頁(yè)面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置


