Use R in C# : C#에서 R 사용

Use R in C#

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.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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.

[출처] https://coders-corner.net/2015/11/01/use-r-in-c/

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
공지 오라클 기본 샘플 데이터베이스 졸리운_곰 2014.01.02 86919
공지 [SQL컨셉] 서적 "SQL컨셉"의 샘플 데이타 베이스 SAMPLE DATABASE of ORACLE 가을의 곰을... 2013.02.10 79210
공지 [G_SQL] Sample Database 가을의 곰을... 2012.05.20 95941
78 [Kafka] Kafka 한번 살펴보자... Quickstart file 졸리운_곰 2021.06.18 1313
77 Java Kafka Producer, Consumer 예제 구현 Java를 이용하여 Kafka Producer와 Kakfa Consumer를 구현해보자. file 졸리운_곰 2021.06.18 1172
76 Beginner’s Guide to Understand Kafka file 졸리운_곰 2021.06.18 1575
75 [Kafka] Kafka 설치/실행 및 테스트 file 졸리운_곰 2021.06.18 1067
74 [java] [kafka] [Kafka] 개념 및 기본예제 file 졸리운_곰 2021.06.16 2170
73 Getting started with Apache Kafka in Python file 졸리운_곰 2020.09.10 2262
72 [Kafka] 다운로드 및 Quick Start file 졸리운_곰 2020.09.07 1900
71 [Kafka] 기본 개념잡기 file 졸리운_곰 2020.09.07 1717
70 Flume Integration with Kafka file 졸리운_곰 2019.04.16 2086
69 빅데이터: 플럼(Flume) 토폴로지 설계 file 졸리운_곰 2019.04.16 1604
68 실시간 처리를 위한 분산 메시징 시스템 카프카(Kafka) file 졸리운_곰 2018.05.12 1413
67 Flume과 Kafka를 사용한 초당 100만개 로그 수집 테스트 file 졸리운_곰 2018.05.12 1402
66 웹 크롤링 / web crwaling / web scraping / 웹 스크래핑 file 졸리운_곰 2017.07.09 1849
65 빅데이터 단지 몇퍼센트의 예측 정확성을 위하여 장애로 가득찬 빅데이터 시스템을 도입하여야 하는가에 대한 의문! file 졸리운_곰 2017.03.20 1611
64 빅데이터: 플럼(Flume) 토폴로지 설계 file 졸리운_곰 2017.03.20 1422
63 [실시간 분석 시스템] Apache Flume를 활용한 데이터 수집(1) file 졸리운_곰 2017.03.06 1324
62 [실시간 분석 시스템] 데이터 수집 #2 Apache Sqoop을 활용하여 RDBMS 데이터 수집(2) file 졸리운_곰 2017.03.06 1388
61 [실시간 분석 시스템] 데이터 수집 #2 Apache Sqoop을 활용하여 RDBMS 데이터 수집(1) file 졸리운_곰 2017.03.06 1109
60 [실시간 분석 시스템] 데이터 수집 #1 오픈 소스 수집기 비교 file 졸리운_곰 2017.03.06 1752
59 [실시간 분석 시스템] 일단 데이터 들여다 보기 file 졸리운_곰 2017.03.06 1948
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED