我正试图用下面的代码片段在C#中运行R脚本。但输出会重复

我正试图用下面的代码片段在C#中运行R脚本。但输出会重复,c#,r,process,C#,R,Process,我尝试使用以下事件获取输出 Process cmdProcess = new Process { StartInfo = { FileName = "cmd.exe", WorkingDirectory = "C:/Program Files/R/R-3.0.2/bin", Arguments = " /C R --slave

我尝试使用以下事件获取输出

      Process cmdProcess = new Process
        {
            StartInfo =
            {
                FileName = "cmd.exe",
                WorkingDirectory = "C:/Program Files/R/R-3.0.2/bin",
                Arguments = " /C R --slave --args $@ ",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                CreateNoWindow = true,
            }
        };            

        cmdProcess.Start();
        cmdProcess.BeginOutputReadLine();

        foreach (var item in File.ReadAllLines(“Audit.R”))
        {
            cmdProcess.StandardInput.WriteLine(item);
            cmdProcess.OutputDataReceived += cmdProcess_OutputDataReceived;

        }

    }
我得到了重复的输出。 这是我的输入R脚本

    public void cmdProcess_OutputDataReceived(object sender,DataReceivedEventArgs e)
    {
        this.ResultText += e.Data + "\n";
    }
getwd()

data看起来您多次添加outputdatareceived事件处理程序(导致每个事件有多个调用)。我可能会将其重写为类似的内容,但您可以用下面的R文件IO代替我的硬编码字符串:

getwd()
data<-read.csv("Audit.csv")
str(data)
公共类测试流程
{
公共StringBuilder输出=新建StringBuilder();
公共StringBuilder错误=新建StringBuilder();
字符串脚本=
@“getwd()
A.
    public class TestRProcess
    {
        public StringBuilder output = new StringBuilder();
        public StringBuilder error = new StringBuilder();

        string script =
@"getwd()
a<-1:3
b<-4:6
data<-data.frame(a,b)
str(data)
q()
";

        public void Process()
        {
            ProcessStartInfo ProcessParameters = new ProcessStartInfo(@"C:\Program Files\R\R-3.1.1\bin\R.exe")
            {
                Arguments = "--vanilla --slave",
                CreateNoWindow = true,
                UseShellExecute = false,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                RedirectStandardOutput = true
            };

            int Max_Time = 10000;


            Process p = new Process();
            p.StartInfo = ProcessParameters;

            //Borrowed: http://stackoverflow.com/questions/139593/processstartinfo-hanging-on-waitforexit-why
            output = new StringBuilder();
            error = new StringBuilder();

            using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
            using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
            {
                p.OutputDataReceived += (sender, e) =>
                {
                    if (e.Data == null)
                    {
                        outputWaitHandle.Set();
                    }
                    else
                    {
                        output.AppendLine(e.Data);
                    }
                };
                p.ErrorDataReceived += (sender, e) =>
                {
                    if (e.Data == null)
                    {
                        errorWaitHandle.Set();
                    }
                    else
                    {
                        error.AppendLine(e.Data);
                    }
                };

                p.Start();

                p.StandardInput.WriteLine(script);

                p.BeginOutputReadLine();
                p.BeginErrorReadLine();

                if (p.WaitForExit(Max_Time) &&
                    outputWaitHandle.WaitOne(Max_Time) &&
                    errorWaitHandle.WaitOne(Max_Time))
                {

                }
                else
                {
                    throw new Exception("Timed Out");
                }
            }
        }