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

时间:2014-09-22 06:04:06

标签: 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 --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;

        }

    }

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

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

我重复了输出。 这是我输入的R脚本,

getwd()
data<-read.csv("Audit.csv")
str(data)

任何人都可以帮我解决这个问题吗? 谢谢

1 个答案:

答案 0 :(得分:1)

IT看起来像是多次添加outputdatareceived事件处理程序(导致每个事件多次调用)。我可能会把它改写成这样的东西,但是你可以用下面的R文件IO代替我的硬编码字符串:

    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");
                }
            }
        }
相关问题