internal List <object> runScript(string path, List <object> inputs) { var lines = File.ReadAllLines(path); int length = lines.Length; int i = -1; List <object> result = new List <object>(); IStatConnector con = new StatConnectorClass(); con.Init("R"); try { for (i = 0; i < length; i++) { if (lines[i].StartsWith("SetSymbol(") && lines[i].EndsWith(")")) { try { var parts = lines[i].Split('(', ',', ')'); con.SetSymbol(parts[1], inputs[int.Parse(parts[2])]); } catch (Exception ex) { throw new FormatException(ex.Message); } } else if (lines[i].StartsWith("GetSymbol(") && lines[i].EndsWith(")")) { try { var parts = lines[i].Split('(', ')'); result.Add(con.GetSymbol(parts[1])); } catch (Exception ex) { throw new FormatException(ex.Message); } } else { con.EvaluateNoReturn(lines[i]); } } } catch (FormatException ex) { con.Close(); throw new Exception("Line:" + (i + 1) + " " + ex.Message); } catch { con.Close(); throw new Exception("Line:" + (i + 1) + " " + con.GetErrorText()); } return(result); }
internal List<object> runScript(string path, List<object> inputs) { var lines = File.ReadAllLines(path); int length = lines.Length; int i =-1 ; List<object> result = new List<object>(); IStatConnector con = new StatConnectorClass(); con.Init("R"); try { for (i = 0; i < length; i++) { if(lines[i].StartsWith("SetSymbol(")&&lines[i].EndsWith(")")) { try { var parts = lines[i].Split('(', ',', ')'); con.SetSymbol(parts[1], inputs[int.Parse(parts[2])]); } catch (Exception ex) { throw new FormatException(ex.Message); } } else if (lines[i].StartsWith("GetSymbol(") && lines[i].EndsWith(")")) { try { var parts = lines[i].Split('(',')'); result.Add(con.GetSymbol(parts[1])); } catch (Exception ex) { throw new FormatException(ex.Message); } } else con.EvaluateNoReturn(lines[i]); } } catch(FormatException ex) { con.Close(); throw new Exception("Line:"+(i+1)+" "+ex.Message); } catch { con.Close(); throw new Exception("Line:" + (i + 1) + " " + con.GetErrorText()); } return result; }
internal void DrawContourPlot(string path, string name, double[] x, double[] y, double[] z, int width, int height, double[] levels, bool drawline) { var con = new StatConnectorClass(); try { con.Init("R"); con.EvaluateNoReturn("setwd(\"" + path.Replace("\\", "/") + "/\")"); con.EvaluateNoReturn("png(file=\"" + name + ".png\", bg=\"transparent\",width=" + width + ",height=" + height + ")"); con.EvaluateNoReturn("library(akima)"); con.SetSymbol("x", x); con.SetSymbol("y", y); con.SetSymbol("z", z); con.EvaluateNoReturn("r<-interp(x,y,z,seq(min(x),max(x),length = 100),seq(min(y),max(y),length = 100),duplicate = \"mean\")"); con.EvaluateNoReturn("zz<-matrix(r$z,nrow = 100,ncol = 100,byrow = TRUE)"); con.EvaluateNoReturn("zl<-range(zz,finite=TRUE)"); if (levels.Length == 1) { if (levels[0] == 0) { con.EvaluateNoReturn("lvs <- pretty(zl,20)"); } else { con.EvaluateNoReturn("lvs <- pretty(zl," + levels[0] + ")"); } } else { con.SetSymbol("lvs", levels); } con.EvaluateNoReturn("leg <- length(lvs)-1"); if (drawline) { con.EvaluateNoReturn("filled.contour(r$x,r$y,zz,zlim=zl,levels=lvs,color.palette=heat.colors,col=heat.colors(leg)[leg:1],plot.axes = {axis(1);axis(2);contour(r$x,r$y,zz,drawlabels = F,levels = lvs,col=\"black\",add=T)})"); } else { con.EvaluateNoReturn("filled.contour(r$x,r$y,zz,zlim=zl,levels=lvs,color.palette=heat.colors,col=heat.colors(leg)[leg:1])"); } con.EvaluateNoReturn("dev.off()"); } catch { throw new Exception(con.GetErrorText()); } finally { con.Close(); } }
internal double[,] BaseStart(string path, string name, int width, int height, string targetcolumn, string[] sourcecolumns, Dictionary <string, double[]> doubledata, Dictionary <string, string[]> stringdata, string method, double cp) { var con = new StatConnectorClass(); double[,] result; try { con.Init("R"); con.EvaluateNoReturn("setwd(\"" + path.Replace("\\", "/") + "/\")"); con.EvaluateNoReturn("png(file=\"" + name + ".png\", bg=\"transparent\",width=" + width + ",height=" + height + ")"); string function; int i, length = sourcecolumns.Length; foreach (var item in doubledata) { con.SetSymbol(item.Key, item.Value); con.EvaluateNoReturn(item.Key + "<-as.vector(" + item.Key + ")"); } foreach (var item in stringdata) { con.SetSymbol(item.Key, item.Value); con.EvaluateNoReturn(item.Key + "<-as.vector(" + item.Key + ")"); } function = targetcolumn + " ~ "; for (i = 0; i < length - 1; i++) { function += sourcecolumns[i] + " + "; } function += sourcecolumns[length - 1]; con.EvaluateNoReturn("library(rpart)"); con.EvaluateNoReturn("library(rpart.plot)"); con.EvaluateNoReturn("result<-rpart(" + function + ",method=\"" + method + "\")"); if (cp > 0) { con.EvaluateNoReturn("result<-prune(result,cp=" + cp + ")"); } con.EvaluateNoReturn("rpart.plot(result,type=0,nn=T,yesno=F,cex=0.0000001,nn.cex=1,split.col=0)"); con.EvaluateNoReturn(string.Format("sink(\"{0}.txt\")", name)); con.EvaluateNoReturn("print(result)"); con.EvaluateNoReturn("sink()"); result = con.Evaluate("result$cptable") as double[, ]; } catch (Exception ex) { throw new Exception(ex.Message + " RException:" + con.GetErrorText()); } finally { con.Close(); } return(result); }
internal double[,] BaseStart(string path,string name,int width,int height,string targetcolumn, string[] sourcecolumns,Dictionary<string,double[]> doubledata,Dictionary<string,string[]> stringdata,string method,double cp) { var con = new StatConnectorClass(); double[,] result; try { con.Init("R"); con.EvaluateNoReturn("setwd(\"" + path.Replace("\\", "/") + "/\")"); con.EvaluateNoReturn("png(file=\"" + name + ".png\", bg=\"transparent\",width=" + width + ",height=" + height + ")"); string function; int i,length = sourcecolumns.Length; foreach(var item in doubledata) { con.SetSymbol(item.Key, item.Value); con.EvaluateNoReturn(item.Key + "<-as.vector(" + item.Key + ")"); } foreach (var item in stringdata) { con.SetSymbol(item.Key, item.Value); con.EvaluateNoReturn(item.Key + "<-as.vector(" + item.Key + ")"); } function = targetcolumn + " ~ "; for (i = 0; i < length-1;i++ ) { function += sourcecolumns[i] + " + "; } function += sourcecolumns[length - 1]; con.EvaluateNoReturn("library(rpart)"); con.EvaluateNoReturn("library(rpart.plot)"); con.EvaluateNoReturn("result<-rpart("+function+",method=\""+method+"\")"); if(cp>0) { con.EvaluateNoReturn("result<-prune(result,cp="+cp+")"); } con.EvaluateNoReturn("rpart.plot(result,type=0,nn=T,yesno=F,cex=0.0000001,nn.cex=1,split.col=0)"); con.EvaluateNoReturn(string.Format("sink(\"{0}.txt\")",name)); con.EvaluateNoReturn("print(result)"); con.EvaluateNoReturn("sink()"); result = con.Evaluate("result$cptable") as double[,]; } catch(Exception ex) { throw new Exception(ex.Message+" RException:"+con.GetErrorText()); } finally { con.Close(); } return result; }
internal void DrawContourPlot(string path, string name, double[] x, double[] y, double[] z, int width, int height,double[] levels,bool drawline) { var con = new StatConnectorClass(); try { con.Init("R"); con.EvaluateNoReturn("setwd(\"" + path.Replace("\\", "/") + "/\")"); con.EvaluateNoReturn("png(file=\"" + name + ".png\", bg=\"transparent\",width=" + width + ",height=" + height + ")"); con.EvaluateNoReturn("library(akima)"); con.SetSymbol("x", x); con.SetSymbol("y", y); con.SetSymbol("z", z); con.EvaluateNoReturn("r<-interp(x,y,z,seq(min(x),max(x),length = 100),seq(min(y),max(y),length = 100),duplicate = \"mean\")"); con.EvaluateNoReturn("zz<-matrix(r$z,nrow = 100,ncol = 100,byrow = TRUE)"); con.EvaluateNoReturn("zl<-range(zz,finite=TRUE)"); if (levels.Length==1) { if (levels[0]==0) con.EvaluateNoReturn("lvs <- pretty(zl,20)"); else con.EvaluateNoReturn("lvs <- pretty(zl," + levels[0] + ")"); } else { con.SetSymbol("lvs", levels); } con.EvaluateNoReturn("leg <- length(lvs)-1"); if (drawline) { con.EvaluateNoReturn("filled.contour(r$x,r$y,zz,zlim=zl,levels=lvs,color.palette=heat.colors,col=heat.colors(leg)[leg:1],plot.axes = {axis(1);axis(2);contour(r$x,r$y,zz,drawlabels = F,levels = lvs,col=\"black\",add=T)})"); } else { con.EvaluateNoReturn("filled.contour(r$x,r$y,zz,zlim=zl,levels=lvs,color.palette=heat.colors,col=heat.colors(leg)[leg:1])"); } con.EvaluateNoReturn("dev.off()"); } catch { throw new Exception(con.GetErrorText()); } finally { con.Close(); } }
internal double[] BaseStart(string path, string name, int width, int height, string targetcolumn, string[] sourcecolumns, Dictionary <string, double[]> doubledata) { var con = new StatConnectorClass(); double[] coefficients = null; try { con.Init("R"); con.EvaluateNoReturn("setwd(\"" + path.Replace("\\", "/") + "/\")"); con.EvaluateNoReturn("png(file=\"" + name + ".png\", bg=\"transparent\",width=" + width + ",height=" + height + ")"); string function; int i, length = sourcecolumns.Length; foreach (var item in doubledata) { con.SetSymbol(item.Key, item.Value); con.EvaluateNoReturn(item.Key + "<-as.vector(" + item.Key + ")"); } function = targetcolumn + " ~ "; for (i = 0; i < length - 1; i++) { function += sourcecolumns[i] + " + "; } function += sourcecolumns[length - 1]; con.EvaluateNoReturn("result<-lm(" + function + ")"); con.EvaluateNoReturn("par(mfrow=c(2,2))"); con.EvaluateNoReturn("plot(result,which=1:4)"); con.EvaluateNoReturn(string.Format("sink(\"{0}.txt\")", name)); con.EvaluateNoReturn("print(summary(result))"); con.EvaluateNoReturn("sink()"); coefficients = con.Evaluate("as.array(result$coefficient)") as double[]; } catch (Exception ex) { throw new Exception(ex.Message + " RException:" + con.GetErrorText()); } finally { con.Close(); } return(coefficients); }
internal double[] BaseStart(string path, string name, int width, int height, string targetcolumn, string[] sourcecolumns, Dictionary<string, double[]> doubledata) { var con = new StatConnectorClass(); double[] coefficients = null; try { con.Init("R"); con.EvaluateNoReturn("setwd(\"" + path.Replace("\\", "/") + "/\")"); con.EvaluateNoReturn("png(file=\"" + name + ".png\", bg=\"transparent\",width=" + width + ",height=" + height + ")"); string function; int i, length = sourcecolumns.Length; foreach (var item in doubledata) { con.SetSymbol(item.Key, item.Value); con.EvaluateNoReturn(item.Key + "<-as.vector(" + item.Key + ")"); } function = targetcolumn + " ~ "; for (i = 0; i < length - 1; i++) { function += sourcecolumns[i] + " + "; } function += sourcecolumns[length - 1]; con.EvaluateNoReturn("result<-lm(" + function + ")"); con.EvaluateNoReturn("par(mfrow=c(2,2))"); con.EvaluateNoReturn("plot(result,which=1:4)"); con.EvaluateNoReturn(string.Format("sink(\"{0}.txt\")", name)); con.EvaluateNoReturn("print(summary(result))"); con.EvaluateNoReturn("sink()"); coefficients = con.Evaluate("as.array(result$coefficient)") as double[]; } catch (Exception ex) { throw new Exception(ex.Message + " RException:" + con.GetErrorText()); } finally { con.Close(); } return coefficients; }