5시간코딩

c#에서 R Code를 실행할 때 2가지 방법 본문

C#

c#에서 R Code를 실행할 때 2가지 방법

5시간코딩 2019. 12. 12. 14:42

1. Process로 직접 RTest.R 을 실행시키기

       string RScriptPath = @"C:\Program Files\R\R-3.4.2\bin\x64\Rscript.exe";
       string RCodeFilePath = @"C:\RTest.R";

 

       ProcessStartInfo pstInfo = new ProcessStartInfo(RScriptPath, RCodeFilePath);
       pstInfo.UseShellExecute = false;
       pstInfo.RedirectStandardOutput = true;
       Process.Start(pstInfo);
       Console.WriteLine("R Done!!");

 

2. R 연동시킨 후 RTest.R을 불러오는 RCode 작성하기

        string path = @"C:\RTest.R";

 

        REngine.SetEnvironmentVariables();
        REngine engine = REngine.GetInstance();
        engine.Initialize();

        var execution = "source('" + path + "', encoding='utf-8')";

        engine.Evaluate(execution);

 

# 1은 뭔가 파일을 읽어올 때 안전해보인다.

# 2의 장점은 내가 원하는 값을 R코드에 던질 수 있다. 예를들어 오늘 날짜를 넣고 싶으면 1번 방법은 안되는데, 2번방법은 된다.