Windows Form calls R to draw and display, form

Source: Internet
Author: User

Windows Form calls R to draw and display, form

The R Software is very powerful and can perform various statistics well and output graphics. The following describes how to communicate with C # In R language and display the R drawing result on the WinForm UI.

1. Prerequisites

To install the R software, you must install the 32-bit R software. If the 64-bit call is performed, an error is returned. In addition, R is added to the computer environment variable.

Open the R Software and install the scatterplot3d package. This R package is required for demonstration.

2. Create the GraphGenerateByR project. The project structure is as follows:

Note that here you need to introduce the RDotNet class library, you can download: http://rdotnet.codeplex.com/

3 Main form code

1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. linq; 7 using System. text; 8 using System. windows. forms; 9 10 namespace GraphGenerateByR11 {12 using RDotNet; 13 public partial class Main: Form14 {15 public Main () 16 {17 InitializeComponent (); 18} 19 REngine engine = null; 20 21 string Rcode = ""; 22 private v Oid btnPlot_Click (object sender, EventArgs e) 23 {24 try25 {26 if(this.txt Rcode. text = "") 27 {28 Rcode = @ "library ('scatterplot3d ') 29 z <-seq (-10, 10, 0.01) 30 x <-cos (z) 31 y <-sin (z) 32 scatterplot3d (x, y, z, highlight.3d = TRUE, col. axis = 'blue', col. grid = 'lightblue ', main = '3d plotting', pch = 20) 33 "; 34} 35 else36 {37 Rcode = this.txt Rcode. text; 38} 39 40 // R.3.2.441 engine = REngine. getInstance (); 42 engine. Initialize (); 43 // Add the GUID to the image to prevent duplicate names (delete the image first and save it) 44 string rnd = System. guid. newGuid (). toString (). replace ("-", ""); 45 string filename = "I" + rnd + "__Rimage.png"; 46 engine. evaluate (string. format ("png (file = '{0}', bg = 'transparent', width = {1}, height = {2})", filename, this. ptbGraphic. width, this. ptbGraphic. height); 47 48 // engine. evaluate (@ "x <-() * pi/1249 // y <-cos (x) 50 // plot (x, y); 51 //"); 52 eng Ine. evaluate (Rcode); 53 engine. evaluate ("dev. off () "); 54 string path = System. IO. path. getFullPath (filename); 55 56 Bitmap image = new Bitmap (path); 57 ptbGraphic. image = image; 58} 59 catch (Exception ex) 60 {61 MessageBox. show (ex. message); 62} 63 64} 65 66 private void Main_FormClosing (object sender, FormClosingEventArgs e) 67 {68 if (engine! = Null) 69 {70 // clean up71 engine. Dispose (); 72} 73} 74} 75}

 

4 run:

Click plot and call the default R code. The structure is as follows:

Enter a valid R drawing statement and click Plot again. The result is as follows:

 

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.