If you want to develop a desktop application on a windows platform then you will most likely use C# as programming language. Actually C# and the .Net Framework offer an efficient way to implement such applications and therefore many developers choose C#. Few of these desktop applications have to analyze data, create statistics and display nice diagrams or charts to show the analysis results. Of course, such use cases can be solved by using C#. But there may be more efficient tools. In case of data analysis and presentation there is an interesting tool, the programming language “R”.
R is a programming language specialized in statistical computing and graphics. As C# developer you can access R directly within your C# source code by using the R.Net library. R.Net is an interoperability bridge to R from the .NET Framework.
R itself isn’t difficult to learn. In short time you will be familiar with the basic principles and syntax. So you can write you applications with C# and use R in well-chosen scenarios only.
Within this article I want to show how R.Net can be used to integrate R in C#. You don’t need any knowledge about the R syntax to understand the example code. Within further articles I will introduce some more examples with practical relevance.
Install R.Net
To use R, you have to download and install the R package. For this article I have used “R-3.2.2 for Windows”. Furthermore you have to download the R.Net assemblies from the codeplex page and copy them to a place of your choice. I have used “R.Net 1.3.13”.
Hello World
If you have installed R and R.Net you are ready to write you first R application. I want to show some kind of a “hello world” example, but it should do more than just print text.
R is an interpreted language and users typically access it through a command-line interpreter. If a user types a calculation, e.g. “2+5*3” in the command prompt and presses enter, the computer replies the result, in this case “17”. I want to use this feature for our hello world example. So we will implement a simple calculator as console application.
Within this console application the user should input the calculation, e.g. “2+5*3” and we want to use R to calculate the result.
The following source code shows the full implementation of this application. Below the source code you will find some more details about the used R.Net commands.
using System; using RDotNet; namespace RCalculator { class Program { static void Main(string[] args) { string result; string input; REngine engine; //init the R engine REngine.SetEnvironmentVariables(); engine = REngine.GetInstance(); engine.Initialize(); //input Console.WriteLine("Please enter the calculation"); input = Console.ReadLine(); //calculate CharacterVector vector = engine.Evaluate(input).AsCharacter(); result = vector[0]; //clean up engine.Dispose(); //output Console.WriteLine(""); Console.WriteLine("Result: '{0}'", result); Console.WriteLine("Press any key to exit"); Console.ReadKey(); } } }
First you have to add the needed assemblies to you application. So add a reference to “RDotNet.dll” and “RDotNet.NativeLibrary.dll” which can be found in your R.Net directory. At next add the using command for “RDotNet”. To access the R features you have to set up an according engine. This is shown in lines 15 to 17. At the end of you application you should dispose this engine, like in line 28.
With the R engine you can access all R features. In out example we want to use the R interpreter to do a calculation. So we only need the “Evaluate” function with our user input as parameter. This function will return the result of the interpreter. We can convert this result in a format of our choice. Please see the R.Net documentation for the possible types of data. In our example we want to get the result as a string to show it in the console, so we use the R.Net type character vector which represents a vector of single characters. This vector can be stored within a string variable. The lines 24 and 25 show the according source code.
Summary
This little demo application demonstrates the possibility of using R in your C# application. By using R.Net you get an easy to use interface to access R.
Pingback: Use R in C#: Execute Functions | coders corner
Is there a way to show an R plot from C#?
A simple R plot example accessed from C# will be magnificient.
Is R.net works well with R downloaded packages (such R package alabama, etc…) ? or only with R basic packages ?
Thank u for your responses.
is there any way to integrate visual studio r project(RTVS project) into IIS.
Very nice and thank you for taking the trouble to put this up. Now I have made a successful start I feel like the woRld is my oysteRRRR! Cheers from Australia – Dave
Is there a way running two different instances of REngine at the same time?
Hello, could u pls elaborate on lines 15-27. how to add dll paths and where?
Hello, I have problem with engine = REngine.GetInstance(). Error is An unhandled exception of type ‚System.NullReferenceException‘ occurred in RDotNet.dll. Please tell me Where I made
a mistake.
Pingback: Интеграция R с C# и WPF - c# wpf r - Вопросы и ответы по программированию
Hi it’s me, I am also visiting this website daily, this web page is
truly good and the viewers are in fact sharing good thoughts.
– Calator prin Romania.
Pingback: Integrating R with C # and WPF - YeahEXP
That seems to make sense. R doesn’t seem to make much sense to me (yet?), but since the customer wants it running, what can I do about it…