I am trying to run R scripts in C# with the below code snippet. But the output gets repeated -


      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;          }      } 

and try output using following event.

    public void cmdprocess_outputdatareceived(object sender,datareceivedeventargs e)     {         this.resulttext += e.data + "\n";     } 

i output repeated. input r script,

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

can me on issue?

it looks adding outputdatareceived event handler multiple times (resulting in multiple calls per event). might rewrite this, substitute r file io below instead of hard coded string:

    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");                 }             }         } 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -