It is well known that R software is very powerful and can perform various kinds of statistics well, and can output graphics. The following describes an R language and C # communication methods, and the R drawing results displayed to the WinForm UI interface method, the article is described in detail, the need for friends can refer to.
First, the prerequisite preparation
Install r software, need to install 32-bit R software, 64-bit call will be error. The other is to say that R is added to the computer environment variable.
Open r software, install package Scatterplot3d, demo need to use this R package.
Second, create the project Graphgeneratebyr, the project structure is as follows:
Note: you need to introduce the Rdotnet class library, you can download it yourself: http://rdotnet.codeplex.com/
C. Main form code
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; 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 = @ "Libra Ry (' Scatterplot3d ') z <-seq ( -10, ten, 0.01) x <-cos (z) y <-sin (z) scatterplot3d (x, Y, Z, highlight.3d=true, col.axis= ' Blue ', col.grid= ' lightblue ', main= ' 3d drawing ', pch=20) "; } else {Rcode = This.txtRcode.Text; }//r.3.2.4 engine = Rengine.getinstance (); Engine. Initialize (); The image is added to the Guid to prevent duplicate names (there is also a deletion after saving) 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 en Gine. Dispose (); } } }}
Four, Operation:
When you click Plot, the default R code is called, with the following structure:
Enter a valid R drawing statement, click Plot again, and the result is as follows:
Summarize