R 설치의 \ bin \ 디렉토리에있는 RScript.exe를 사용하여 R 스크립트를 실행하려고합니다. 창 명령 행에서 RScript를 실행하면 화면에 결과가 기록됩니다. System.Diagnostics.Process()를 사용하여 C#에서 복제하려고하면 출력이 누락 된 것 같습니다.RScript에서 C#을 통해 stdout 캡처
예를 들어, 실제 r 코드를 무시하고 RScript.exe에서 버전 정보를 읽으려고합니다. cmd를 프롬프트에서 실행하는 경우 :
rscript --version
나는 내가이 C# 코드에서 동일한 작업을 수행 화면
R scripting front-end version 3.2.3 (2015-12-10)
에 표시 아무것도 캡처되지 않습니다.
StringBuilder outputBuilder = new StringBuilder();
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "rscript";
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.ErrorDialog = false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Arguments = "--version";
proc.EnableRaisingEvents = true;
proc.OutputDataReceived += new DataReceivedEventHandler
(
delegate(object sender, DataReceivedEventArgs e)
{
outputBuilder.Append(e.Data);
}
);
proc.ErrorDataReceived += new DataReceivedEventHandler
(
delegate(object sender, DataReceivedEventArgs e)
{
outputBuilder.Append(e.Data);
}
);
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
string allOutput = outputBuilder.ToString();
이것은 매우 단순해야하지만 C#에서 stdout을 캡처하는 많은 예제를 보았음에도 불구하고 난 여전히 혼란 스럽습니다.
최근의 추측은'System.Diagnostics.Process()'를 통해 시작될 때 RScript가 stdout에 쓰지 않는다는 것입니다. 나는 그것이 왜 그런 경우인지 전혀 모른다. –