Use R in C#: R.
NET data types
Veröffentlicht am Dezember 6, 2015von oliverfunke
If you want to use [Link] to access R via C# applications, you have to know the [Link] type
system. Following you will find a list with the R types, [Link] types and the equivalent in the
.NET framework.
R [Link] .NET Framework
character vector [Link] [Link][]
integer vector [Link] System.Int32[]
real vector [Link] [Link][]
complex vector [Link] [Link][]
raw vector [Link] [Link][]
logical vector [Link] [Link][]
character matrix [Link] [Link][, ]
integer matrix [Link] System.Int32[, ]
real matrix [Link] [Link][, ]
[Link][,
complex matrix [Link] ]
raw matrix [Link] [Link][, ]
logical matrix [Link] [Link][, ]
list [Link]
data frame [Link]
data frame [Link]
function [Link]
factor [Link] System.Int32[]
S4 RDotNet.S4Object [Link][]
Use R in C#: Execute Functions
Veröffentlicht am November 15, 2015von oliverfunke
As shown in a previous article it is easy to integrate R in a C# application by using [Link]. Within
this article I want to explain how to call R functions.
Use Case
The R function calls should be shown by calculating the correlation coefficient between two data
sets. In this example I want to analyze the correlation between the size and population of
countries. The data in this example is based on real data with size in thousand square kilometers
and population in thousand inhabitants.
Calculate correlation coefficient
The basic data type in R is a vector. Therefore we will start to create two vectors with the needed
data. The following source code contains the two data lists in C# and the code to create the
according R vectors. The function call “SetSymbol” introduces the vectors to the R engine and
give them an alias name. Later on we will access the data by using these alias names.
List<int> size = new List<int>() { 29, 33, 51, 110, 357, 45, 338,
543, 132, 70, 103, 301, 146, 10, 56, 243, 238 };
1
List<int> population = new List<int>() { 3162, 11142, 3834, 7305,
2 81890, 1339, 5414, 65697, 11280, 4589, 320, 60918, 480, 1806,
4267, 63228, 21327 };
3
4
IntegerVector sizeVector = [Link](size);
5
[Link]("size", sizeVector);
6
7
IntegerVector populationVector =
[Link](population);
8
[Link]("population", populationVector);
At next we can call the R function to calculate the correlation coefficient. This is done by calling
the according function “cor” and hand over the two vectors. The result itself is a vector too. We
could get the result by accessing the first element of this result vector.
NumericVector resultVector = [Link]("cor(size,
1 population)").AsNumeric();
2 result = resultVector[0].ToString();
Application
The following source code shows the complete example as console application.
1 using System;
using RDotNet;
2
using [Link];
3
4 namespace R_FunctionCall
{
5
class Program
6
{
7
static void Main(string[] args)
8
{
9
string result;
10
REngine engine;
11
12
//init the R engine
13
[Link]();
14 engine = [Link]();
15 [Link]();
16
17 //prepare data
18 List<int> size = new List<int>() { 29, 33, 51, 110,
357, 45, 338, 543, 132, 70, 103, 301, 146, 10, 56, 243, 238 };
19 List<int> population = new List<int>() { 3162, 11142,
3834, 7305, 81890, 1339, 5414, 65697, 11280, 4589, 320, 60918,
20 480, 1806, 4267, 63228, 21327 };
21
//calculate
22
IntegerVector sizeVector =
23 [Link](size);
24 [Link]("size", sizeVector);
25
IntegerVector populationVector =
26
[Link](population);
27
[Link]("population", populationVector);
28
29
NumericVector resultVector =
30 [Link]("cor(size, population)").AsNumeric();
31 result = resultVector[0].ToString();
32
//clean up
33
[Link]();
34
35
//output
36
[Link]("");
37
[Link]("Result: '{0}'", result);
38
[Link]("Press any key to exit");
39
[Link]();
40
}
41
}
42 }
43
Werbeanzeigen
DIESE ANZEIGE MELDEN
Use R in C#: Create a data plot and save it as png
Veröffentlicht am November 22, 2015von oliverfunke
As shown in a previously article it is easy to integrate R in a C# application by using
[Link]. Within this article I want to explain how to create a data visualization plot and
export it as png file.
Use Case
The R visualization functionality should be shown based on the correlation between the
size and population of countries. The data in this example is based on real data with size
in thousand square kilometers and population in thousand inhabitants.
Prepare vectors
At first the data vectors must be prepared. The following code shows the C# list with the
data and the according R vectors. Later on we will access the data by using the alias
names set by using “SetSymbol”.
List<int> size = new List<int>() { 29, 33, 51, 110, 357, 45, 338,
543, 132, 70, 103, 301, 146, 10, 56, 243, 238 };
1
List<int> population = new List<int>() { 3162, 11142, 3834, 7305,
2 81890, 1339, 5414, 65697, 11280, 4589, 320, 60918, 480, 1806,
4267, 63228, 21327 };
3
4
IntegerVector sizeVector = [Link](size);
5
[Link]("size", sizeVector);
6
7
IntegerVector populationVector =
[Link](population);
8
[Link]("population", populationVector);
To define the target file we will create a file name vector.
1 string fileName;
2
fileName = "C:\\[Link]";
3
4
CharacterVector fileNameVector = [Link](new
5 []{fileName});
6 [Link]("fileName", fileNameVector);
Create plot
The data visualization is done by using the “plot” function. We will put in the data
truncated with the “” sign. This will create a scatterplot of the data with population as the
target variable. Furthermore we calculate the linear regression between the two vectors
and add a regression line to the plot.
1 [Link]("reg <- lm(population~size)");
2 [Link]("plot(population~size)");
3 [Link]("abline(reg)");
Export plot
The plot data can be exported as file. R supports several file formats. Within this example
I want to create a png file. This file can be created by using the “png” command in front
of the “plot” call. In this case the plot data will be written to the file. The file itself is
locked by the R environment because you may add some more data, e.g. the regression
line. So if you want to access the file outside of the R engine you have to release it first.
This is done with the “[Link]()” function.
1 [Link]("png(filename=fileName, width=6, height=6,
units='in', res=100)");
2 [Link]("plot(population~size)");
3 [Link]("[Link]()");
If you want to change the size or the quality of the png file you may adapt the width,
height or resolution (“res”) parameter of the “png” function.
Application
The following source code shows the complete example as console application.
1 using System;
using RDotNet;
2
using [Link];
3
4
namespace R_Plot_LinearRegression
5
{
6
class Program
7
{
8
static void Main(string[] args)
9
{
10 REngine engine;
string fileName;
11
12
//init the R engine
13
[Link]();
14
engine = [Link]();
15
[Link]();
16
17
//prepare data
18
List<int> size = new List<int>() { 29, 33, 51, 110,
357, 45, 338, 543, 132, 70, 103, 301, 146, 10, 56, 243, 238 };
19
List<int> population = new List<int>() { 3162, 11142,
20 3834, 7305, 81890, 1339, 5414, 65697, 11280, 4589, 320, 60918,
480, 1806, 4267, 63228, 21327 };
21
22
fileName = "C:\\Users\\ofunke\\Desktop\\[Link]";
23
24
//calculate
25 IntegerVector sizeVector =
[Link](size);
26
[Link]("size", sizeVector);
27
28
IntegerVector populationVector =
[Link](population);
29
[Link]("population", populationVector);
30
31
CharacterVector fileNameVector =
[Link](new []{fileName});
32
[Link]("fileName", fileNameVector);
33
34
[Link]("reg <- lm(population~size)");
35
[Link]("png(filename=fileName, width=6,
36 height=6, units='in', res=100)");
37 [Link]("plot(population~size)");
38 [Link]("abline(reg)");
[Link]("[Link]()");
39
40
//clean up
41
[Link]();
42
43
//output
44
[Link]("");
45
[Link]("Press any key to exit");
46
[Link]();
47
}
48 }
49 }
50