Details how to use C # to implement Windows Form call R for drawing and display

Source: Internet
Author: User
Tags cos
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.